Skip to content

Give better suggestion for reference of Type with trait #106496

Closed
@chenyukang

Description

@chenyukang
Member

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);

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Jan 5, 2023
changed the title [-]Give better suggestion for reference with trait[/-] [+]Give better suggestion for reference of Type with trait[/+] on Jan 5, 2023
chenyukang

chenyukang commented on Jan 22, 2023

@chenyukang
MemberAuthor

@rustbot claim

added a commit that references this issue on Jan 22, 2023

Fix rust-lang#106496, suggest remove deref for type mismatch

2aa5555
added a commit that references this issue on Jan 23, 2023

Rollup merge of rust-lang#107203 - chenyukang:yukang/fix-106496-remov…

66d6a0b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Participants

    @chenyukang

    Issue actions

      Give better suggestion for reference of Type with trait · Issue #106496 · rust-lang/rust