Skip to content

assigning to immutable argument produces a confusing error #46659

Closed
@nikomatsakis

Description

@nikomatsakis

This program:

fn foo(_x: u32) {
    _x = 4;
}

fn main() {}

produces this error:

error[E0384]: re-assignment of immutable variable `_x`
 --> src/main.rs:2:5
  |
1 | fn foo(_x: u32) {
  |        -- first assignment to `_x`
2 |     _x = 4;
  |     ^^^^^^ re-assignment of immutable variable

I consider this a somewhat confusing error. I think it should rather be:

error[E0384]: assignment of immutable argument `_x`
 --> src/main.rs:2:5
  |
1 | fn foo(_x: u32) {
  |        -- `_x` not declared as `mut`
2 |     _x = 4;
  |     ^^^^^^ assignment of immutable argument `_x`

perhaps with a suggestion or something.

This error is issued by the borrow checker, so to do this right we should also modify the MIR-based borrow checker.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions