Skip to content

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

Closed
@brandondong

Description

@brandondong

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions