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

Cannot assign twice to immutable variable with 'l: while break 'l {} as initializer #62115

Closed
Centril opened this issue Jun 25, 2019 · 3 comments · Fixed by #61872
Closed

Cannot assign twice to immutable variable with 'l: while break 'l {} as initializer #62115

Centril opened this issue Jun 25, 2019 · 3 comments · Fixed by #61872
Labels
A-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Centril
Copy link
Contributor

Centril commented Jun 25, 2019

With:

fn _f() {
    let _x = 'label: while break 'label {};
}

we get:

warning[E0384]: cannot assign twice to immutable variable `_x`
 --> src/lib.rs:2:14
  |
2 |     let _x = 'label: while break 'label {};
  |         --   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
  |         |
  |         first assignment to `_x`
  |         help: make this binding mutable: `mut _x`
  |
  = warning: this error has been downgraded to a warning for backwards compatibility with previous releases
  = warning: this represents potential undefined behavior in your code and this warning will become a hard error in the future
  = note: for more information, try `rustc --explain E0729`

This seems wrong. At least from a source view, _x is assigned to once.

Current MIR dump:

fn  _f() -> () {
    let mut _0: ();                      // return place in scope 0 at src/lib.rs:1:9: 1:9
    let _1: ();                          // "_x" in scope 0 at src/lib.rs:2:9: 2:11
    scope 1 {
    }

    bb0: {
        StorageLive(_1);                 // bb0[0]: scope 0 at src/lib.rs:2:9: 2:11
        StorageDead(_1);                 // bb0[1]: scope 0 at src/lib.rs:3:1: 3:2
        return;                          // bb0[2]: scope 0 at src/lib.rs:3:2: 3:2
    }
}

This might possibly be fixed by #61988.

cc @eddyb @pnkfelix @matthewjasper

@Centril Centril added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. labels Jun 25, 2019
@pnkfelix
Copy link
Member

@eddyb points out that the first bullet item of PR #61872 says that it fixes this.

@Centril Centril added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. labels Jun 25, 2019
@Centril
Copy link
Contributor Author

Centril commented Jun 25, 2019

Great. Then we'll just need to add a regression test to https://github.com/rust-lang/rust/blob/07e5297faf2966a77c466b0b5ad936c0deb828ac/src/test/ui/nll/assign-while-to-immutable.rs once the PR is merged.

@eddyb
Copy link
Member

eddyb commented Jun 25, 2019

@Centril once it's merged? @matthewjasper could add this test and close this issue as part of the PR.

bors added a commit that referenced this issue Jun 26, 2019
…sakis

Clean up MIR drop generation

* Don't assign twice to the destination of a `while` loop containing a `break` expression
* Use `as_temp` to evaluate statement expression
* Avoid consecutive `StorageLive`s for the condition of a `while` loop
* Unify `return`, `break` and `continue` handling, and move it to `scopes.rs`
* Make some of the `scopes.rs` internals private
* Don't use `Place`s that are always `Local`s in MIR drop generation

Closes #42371
Closes #61579
Closes #61731
Closes #61834
Closes #61910
Closes #62115
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants