Skip to content

E0502 diagnostic missing help text when using IndexMut instead of .get_mut. #95574

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
BGR360 opened this issue Apr 1, 2022 · 0 comments · Fixed by #95807
Closed

E0502 diagnostic missing help text when using IndexMut instead of .get_mut. #95574

BGR360 opened this issue Apr 1, 2022 · 0 comments · Fixed by #95807
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@BGR360
Copy link
Contributor

BGR360 commented Apr 1, 2022

Given the following code (playground):

fn main() {
    let mut vec = vec![0u32; 420];

    vec[vec.len() - 1] = 123;
}

The current output is:

error[E0502]: cannot borrow `vec` as immutable because it is also borrowed as mutable
 --> src/main.rs:4:9
  |
4 |     vec[vec.len() - 1] = 123;
  |     ----^^^^^^^^^-----
  |     |   |
  |     |   immutable borrow occurs here
  |     mutable borrow occurs here
  |     mutable borrow later used here

When you use .get_mut instead, you get a helpful hint advising you to extract the immutable borrow to a local variable (playground):

fn main() {
    let mut vec = vec![0u32; 420];

    *vec.get_mut(vec.len() - 1).unwrap() = 123;
}
error[E0502]: cannot borrow `vec` as immutable because it is also borrowed as mutable
 --> src/main.rs:4:18
  |
4 |     *vec.get_mut(vec.len() - 1).unwrap() = 123;
  |      ------------^^^^^^^^^-----
  |      |   |       |
  |      |   |       immutable borrow occurs here
  |      |   mutable borrow later used by call
  |      mutable borrow occurs here
  |
help: try adding a local storing this argument...
 --> src/main.rs:4:18
  |
4 |     *vec.get_mut(vec.len() - 1).unwrap() = 123;
  |                  ^^^^^^^^^
help: ...and then using that local as the argument to this call
 --> src/main.rs:4:6
  |
4 |     *vec.get_mut(vec.len() - 1).unwrap() = 123;
  |      ^^^^^^^^^^^^^^^^^^^^^^^^^^

Ideally the output for the first code snippet should contain the same help message.

@rustbot label +D-terse +D-confusing +D-newcomer-roadblock

@BGR360 BGR360 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 1, 2022
@rustbot rustbot added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Apr 1, 2022
@TaKO8Ki TaKO8Ki self-assigned this Apr 4, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 10, 2022
…r, r=fee1-dead

Suggest adding a local for vector to fix borrowck errors

closes rust-lang#95574
@bors bors closed this as completed in c172544 Apr 10, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. 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