Skip to content

Const reference help message does not use type annotations #84210

Closed
@SparkyPotato

Description

@SparkyPotato

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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