Skip to content

Give better suggestion for reference of Type with trait #106496

Closed
@chenyukang

Description

@chenyukang

Given the following code

#[derive(Clone)]
struct S;

trait X {}

impl X for S {}

fn foo<T: X>(_: &T) {}

fn main() {
    let s = &S;
    foo(*s);
}

The current output is:

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
  --> src/main.rs:12:9
   |
12 |     foo(*s);
   |     --- ^^
   |     |   |
   |     |   expected reference, found struct `S`
   |     |   help: consider borrowing here: `&*s`
   |     arguments to this function are incorrect
   |
   = note: expected reference `&_`
                 found struct `S`
note: function defined here
  --> src/main.rs:8:4
   |
8  | fn foo<T: X>(_: &T) {}
   |    ^^^       -----

For more information about this error, try `rustc --explain E0308`.

Ideally the output should suggest remove the * to this:

   foo(s);

Metadata

Metadata

Assignees

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