Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For loops should not be desugared #15392

Closed
LemmingAvalanche opened this issue Jul 4, 2014 · 5 comments · Fixed by #15809
Closed

For loops should not be desugared #15392

LemmingAvalanche opened this issue Jul 4, 2014 · 5 comments · Fixed by #15809
Milestone

Comments

@LemmingAvalanche
Copy link
Contributor

Using a constant in an enum with the same name as another constant from another enum in the std module - namely Option in this case - causes an error in a (seemingly) unrelated for-loop. "Mangling" the name of this constant - say from None to NoneN - makes the code compile.

Compiler used:

rustc 0.11.0-pre-nightly (eda75bcf42ad99e3e4c99585d39705fccac606ee 2014-07-03 16:56:51 +0000)

Code:

fn main() {
    enum Test { None }
    for i in range(0, 10u) {
        println!("{}", i)
    }
}

Output:

$ rustc debug_2.rs
debug_2.rs:3:5: 6:2 error: mismatched types: expected `core::option::Option<uint>` but found `main::Test` (expected enum core::option::Option but found enum main::Test)
debug_2.rs:3     for i in range(0, 10u) {
debug_2.rs:4         println!("{}", i)
debug_2.rs:5     }
debug_2.rs:6 }
error: aborting due to previous error
@pcwalton
Copy link
Contributor

pcwalton commented Jul 4, 2014

Yeah, we have to fix the for iteration for 1.0. Nominating.

@emberian
Copy link
Member

A simple fix is expanding to ::std::option::None, but a more comprehensive fix would involve "import capturing" that I've discussed with @jbclements, @pcwalton and @brson

@pnkfelix
Copy link
Member

Assigning P-backcompat-lang, 1.0 milestone, under the assumption that we will at least make the situation much much less fragile by adopting some solution, perhaps along the lines that @cmr outlined in the previous comment.

@pnkfelix pnkfelix added this to the 1.0 milestone Jul 10, 2014
@jbclements
Copy link
Contributor

Just to be clear, it also requires hygiene for these names. Actually, this probably wouldn't be too hard--though you'd almost certainly wind up merging resolution and expansion. :)

@pcwalton pcwalton changed the title Shadowing enum constant creates error in (seemingly) unrelated for-loop For loops should not be desugared Jul 19, 2014
@pcwalton
Copy link
Contributor

Updated title with what is (IMO) the best near-term solution. This solution may improve compile times as well by adding less macro expansion and making the trans code simpler.

pcwalton added a commit to pcwalton/rust that referenced this issue Jul 25, 2014
This makes edge cases in which the `Iterator` trait was not in scope
and/or `Option` or its variants were not in scope work properly.

This breaks code that looks like:

    struct MyStruct { ... }

    impl MyStruct {
        fn next(&mut self) -> Option<int> { ... }
    }

    for x in MyStruct { ... } { ... }

Change ad-hoc `next` methods like the above to implementations of the
`Iterator` trait. For example:

    impl Iterator<int> for MyStruct {
        fn next(&mut self) -> Option<int> { ... }
    }

Closes rust-lang#15392.

[breaking-change]
bors added a commit that referenced this issue Jul 25, 2014
librustc: Stop desugaring `for` expressions and translate them directly.

This makes edge cases in which the `Iterator` trait was not in scope
and/or `Option` or its variants were not in scope work properly.

This breaks code that looks like:

    struct MyStruct { ... }

    impl MyStruct {
        fn next(&mut self) -> Option<int> { ... }
    }

    for x in MyStruct { ... } { ... }

Change ad-hoc `next` methods like the above to implementations of the
`Iterator` trait. For example:

    impl Iterator<int> for MyStruct {
        fn next(&mut self) -> Option<int> { ... }
    }

Closes #15392.

[breaking-change]
bors added a commit to rust-lang-ci/rust that referenced this issue Aug 7, 2023
Use the warning color when rust-analyzer is stopped

If the rust-analyzer server isn't running, we can't do much. Treat this state as a warning color, so it's more obvious.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants