Skip to content

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

Closed
@Centril

Description

@Centril

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)C-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions