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

Multiple errors for the same missing mut #53466

Closed
RalfJung opened this issue Aug 18, 2018 · 1 comment · Fixed by #106284
Closed

Multiple errors for the same missing mut #53466

RalfJung opened this issue Aug 18, 2018 · 1 comment · Fixed by #106284
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@RalfJung
Copy link
Member

For the following program

fn main() {
    let v = Vec::new();
    v.push(0);
    v.push(0);
}

rustc shows two errors:

error[E0596]: cannot borrow immutable local variable `v` as mutable
 --> src/main.rs:3:5
  |
2 |     let v = Vec::new();
  |         - consider changing this to `mut v`
3 |     v.push(0);
  |     ^ cannot borrow mutably

error[E0596]: cannot borrow immutable local variable `v` as mutable
 --> src/main.rs:4:5
  |
2 |     let v = Vec::new();
  |         - consider changing this to `mut v`
3 |     v.push(0);
4 |     v.push(0);
  |     ^ cannot borrow mutably

error: aborting due to 2 previous errors

These are not exactly the same, but still pretty much a duplicate.

it would be nice if rustc could avoid complaining about the same thing twice here.

@csmoe csmoe added the A-diagnostics Area: Messages for errors, warnings, and lints label Aug 20, 2018
@estebank
Copy link
Contributor

We should collect borrow errors by definition span and emit a single diagnostic:

error[E0596]: cannot borrow immutable local variable `v` as mutable
 --> src/main.rs:3:5
  |
2 |     let v = Vec::new();
  |         - consider changing this to `mut v`
3 |     v.push(0);
  |     ^ cannot borrow mutably
4 |     v.push(0);
  |     ^ cannot borrow mutably

error: aborting due to previous error

Also, we probably should bound how many lines of incorrect mutable borrow attempts are displayed, show only up to three(?) primary spans, and if more, display:

error[E0596]: cannot borrow immutable local variable `v` as mutable
 --> src/main.rs:3:5
  |
2 |     let v = Vec::new();
  |         - consider changing this to `mut v`
3 |     v.push(0);
  |     ^ cannot borrow mutably
4 |     v.push(0);
  |     ^ cannot borrow mutably
5 |     v.push(0);
  |     ^ cannot borrow mutably
  = note: ...and more

error: aborting due to previous error

After all, once the binding is fixed all of those errors would go away, so we're not hiding useful information from the user.

@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 18, 2019
estebank added a commit to estebank/rust that referenced this issue Dec 30, 2022
jyn514 added a commit to jyn514/rust that referenced this issue Dec 31, 2022
Merge multiple mutable borrows of immutable binding errors

Fix rust-lang#53466.
Noratrieb added a commit to Noratrieb/rust that referenced this issue Dec 31, 2022
Merge multiple mutable borrows of immutable binding errors

Fix rust-lang#53466.
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 2, 2023
Merge multiple mutable borrows of immutable binding errors

Fix rust-lang#53466.
@bors bors closed this as completed in 5bfcfee Jan 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. 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