Confusing error when implementing a trait over a reference and incorrectly using &self as parameter
Open
Nobody has claimed this yet.
A-diagnostics
A-trait-system
D-incorrect
D-papercut
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
struct S {}
trait Trait { fn f(self); }
impl Trait for &S {
fn f(&self) { }
}
Current output
error[E0053]: method `f` has an incompatible type for trait
--> src/lib.rs:4:10
|
4 | fn f(&self) { }
| ^^^^^
| |
| expected `S`, found `&S`
| help: change the self-receiver type to match the trait: `self`
|
note: type in trait
--> src/lib.rs:2:20
|
2 | trait Trait { fn f(self); }
| ^^^^
= note: expected signature `fn(&S)`
found signature `fn(&&S)`
For more information about this error, try `rustc --explain E0053`.
Desired output
error[E0053]: method `f` has an incompatible type for trait
--> src/lib.rs:4:10
|
4 | fn f(&self) { }
| ^^^^^
| |
| expected `&S`, found `&&S`
| help: change the self-receiver type to match the trait: `self`
|
note: type in trait
--> src/lib.rs:2:20
|
2 | trait Trait { fn f(self); }
| ^^^^
= note: expected signature `fn(&S)`
found signature `fn(&&S)`
For more information about this error, try `rustc --explain E0053`.
Rationale and extra context
I found this specific part of the message confusing:
8 | fn f(&self) {
| ^^^^^
| |
| expected `S`, found `&S`
I would expect it to say either:
8 | fn f(&self) {
| ^^^^^
| |
| expected `&S`, found `&&S`
or:
8 | fn f(&self) {
| ^^^^
| |
| expected `S`, found `&S`
Note that the error message is the exact same (but not the note) with this other example:
struct S {}
trait Trait { fn f(self); }
impl Trait for S {
fn f(&self) { }
}
Of course, I might misinterpret how this message should be read. In any case, the attached note is perfectly clear, so this is a minor issue.
Other cases
No response
Anything else?
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reproducer in src/lib.rs and run it to inspect E0053. Compare the diagnostic for impl Trait for &S with the second S example; done means the reported expected and found types match the relevant receiver and regression coverage preserves both cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100