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

Confusing error message suggestion when invoking mutable trait method implemented by a mutable reference #83241

Closed
brandondong opened this issue Mar 17, 2021 · 0 comments · Fixed by #83251
Labels
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.

Comments

@brandondong
Copy link

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

trait Foo {
    fn bar(&mut self);
}

impl Foo for &mut String {
    fn bar(&mut self) {
    }
}

pub fn baz(f: &mut String) {
    f.bar();
}

The current output is:

error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable
  --> src/lib.rs:11:5
   |
11 |     f.bar();
   |     ^
   |     |
   |     cannot borrow as mutable
   |     try removing `&mut` here

error: aborting due to previous error

The suggestion to remove &mut is quite confusing and does not appear actionable unless it means removing &mut from the declaration of fn bar(&mut self);. I believe the correct fix is to add a mut before the argument in baz:

pub fn baz(mut f: &mut String) {
    f.bar();
}

Ideally the output should be similar to the case where the trait is implemented by something that is not a mutable reference:

error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable
  --> src/lib.rs:11:5
   |
10 | pub fn baz(f: String) {
   |            - help: consider changing this to be mutable: `mut f`
11 |     f.bar();
   |     ^ cannot borrow as mutable

error: aborting due to previous error
@brandondong brandondong 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 Mar 17, 2021
@estebank estebank self-assigned this Mar 18, 2021
@estebank estebank removed their assignment Mar 22, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Aug 29, 2021
Suggestion for call on immutable binding of mutable type

When calling a method requiring a mutable self borrow on an inmutable
to a mutable borrow of the type, suggest making the binding mutable.

Fix rust-lang#83241.
@bors bors closed this as completed in e4368de Aug 29, 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 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