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

Const reference help message does not use type annotations #84210

Closed
SparkyPotato opened this issue Apr 15, 2021 · 1 comment · Fixed by #86815
Closed

Const reference help message does not use type annotations #84210

SparkyPotato opened this issue Apr 15, 2021 · 1 comment · Fixed by #86815
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@SparkyPotato
Copy link
Contributor

Given the following code:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=ea6b44e39b5b86350d6426f355610990

trait Set
{
  fn set(&mut self, value: i8);
}

impl Set for i8
{
  fn set(&mut self, value: i8)
  {
    *self = value
  }
}

fn main()
{
  let mut integer = 0i8;
  let set: &dyn Set = &mut integer;
  set.set(10)
}

The current output is:

error[E0596]: cannot borrow `*set` as mutable, as it is behind a `&` reference
  --> src/main.rs:18:3
   |
17 |   let set: &dyn Set = &mut integer;
   |                       ------------ help: consider changing this to be a mutable reference: `&mut mut integer`
18 |   set.set(10)
   |   ^^^ `set` is a `&` reference, so the data it refers to cannot be borrowed as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

Ideally the output should look like:

error[E0596]: cannot borrow `*set` as mutable, as it is behind a `&` reference
  --> src/main.rs:18:3
   |
17 |   let set: &dyn Set = &mut integer;
   |            -------- help: consider changing this to be a mutable reference: `&mut dyn Set`
18 |   set.set(10)
   |   ^^^ `set` is a `&` reference, so the data it refers to cannot be borrowed as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

The compiler currently does not use type annotations for the help message, and instead asks for a double-mutable reference(?).
This happens on both stable and nightly.

@SparkyPotato SparkyPotato 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 15, 2021
@JohnTitor JohnTitor added C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Apr 28, 2021
@JohnTitor
Copy link
Member

The current nightly output:

error[E0596]: cannot borrow `*set` as mutable, as it is behind a `&` reference
  --> src/main.rs:18:3
   |
17 |   let set: &dyn Set = &mut integer;
   |       --- help: consider changing this to be a mutable reference: `&mut dyn Set`
18 |   set.set(10)
   |   ^^^ `set` is a `&` reference, so the data it refers to cannot be borrowed as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

The suggestion itself is now correct but the span position is wrong.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jul 11, 2021
Improve error reporting for modifications behind `&` references

I had a look at rust-lang#84210 and noticed that rust-lang#85823 has effectively already fixed rust-lang#84210.

However, the string matching in rust-lang#85823 is _very_ crude and already breaks down when a variable name starts with `mut`. I have made this a bit more robust; further improvements could definitely be made but are complicated by the lack of information provided by an earlier pass:
https://github.com/rust-lang/rust/blob/ce331ee6ee010438d1a58c7da8ced4f26d69a20e/compiler/rustc_mir_build/src/build/matches/mod.rs#L2103-L2107

I have also fixed a missing comma in the error message.
@bors bors closed this as completed in e97c29b Jul 12, 2021
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-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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.

2 participants