- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
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
estebank
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.