Skip to content

Give better suggestion for reference of Type with trait #106496

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

Closed
chenyukang opened this issue Jan 5, 2023 · 1 comment · Fixed by #107203
Closed

Give better suggestion for reference of Type with trait #106496

chenyukang opened this issue Jan 5, 2023 · 1 comment · Fixed by #107203
Assignees
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

@chenyukang
Copy link
Member

chenyukang commented Jan 5, 2023

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);
@chenyukang chenyukang 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 Jan 5, 2023
@chenyukang chenyukang changed the title Give better suggestion for reference with trait Give better suggestion for reference of Type with trait Jan 5, 2023
@chenyukang
Copy link
Member Author

@rustbot claim

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jan 23, 2023
…e-deref, r=compiler-errors

Suggest remove deref for type mismatch

Fixes rust-lang#106496
@bors bors closed this as completed in 2aa5555 Jan 23, 2023
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.

1 participant