Skip to content

MIR-borrowck: add "consider changing this to mut x" note, as in AST borrowck #46020

Closed
@arielb1

Description

@arielb1

Currently, when you try to access an immutable local, AST borrowck suggests that you should change it to mut x, for example:

fn main() {
    let x = 0;
    x = 1;
    std::mem::replace(&mut x, 2);
}
warning: value assigned to `x` is never read
 --> src/main.rs:2:9
  |
2 |     let x = 0;
  |         ^
  |
  = note: #[warn(unused_assignments)] on by default

error[E0596]: cannot borrow immutable local variable `x` as mutable
 --> src/main.rs:4:28
  |
2 |     let x = 0;
  |         - consider changing this to `mut x` <---- THIS NOTE
3 |     x = 1;
4 |     std::mem::replace(&mut x, 2);
  |                            ^ cannot borrow mutably

error[E0384]: cannot assign twice to immutable variable `x`
 --> src/main.rs:3:5
  |
2 |     let x = 0;
  |         - first assignment to `x`
3 |     x = 1;
  |     ^^^^^ cannot assign twice to immutable variable

error: aborting due to 2 previous errors

MIR borrowck should be doing this too.

FIXME: I should write mentor notes

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.NLL-diagnosticsWorking towards the "diagnostic parity" goalT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions