Elided lifetimes in trait impl error messages are confusing
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
trait Trait<T> {
fn method(self) -> T;
}
impl Trait<&i32> for &i32 {
fn method(self) -> Self {self}
}
Current output
error[E0308]: method not compatible with trait
--> src/lib.rs:7:5
|
7 | fn method(self) -> Self {self}
| ^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected signature `fn(&_) -> &_`
found signature `fn(&_) -> &_`
note: the anonymous lifetime as defined here...
--> src/lib.rs:6:22
|
6 | impl Trait<&i32> for &i32 {
| ^
note: ...does not necessarily outlive the anonymous lifetime as defined here
--> src/lib.rs:6:12
|
6 | impl Trait<&i32> for &i32 {
| ^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (lib) due to 1 previous error
Desired output
Compiling playground v0.0.1 (/playground)
error: `impl` item signature doesn't match `trait` item signature
--> src/lib.rs:10:5
|
4 | fn method(x: T) -> Self;
| ------------------------ expected `fn(&'1 S) -> &'2 S`
...
7 | fn method(self) -> Self {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 S) -> &'1 S`
|
= note: expected signature `fn(&'1 S) -> &'2 S`
found signature `fn(&'1 S) -> &'1 S`
help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
error: could not compile `playground` (lib) due to 1 previous error
Rationale and extra context
I first reported this in #87763, and the example there actually now has good error messages. This is not a regression of that issue, but something is still wrong in the general case.
I'm not sure what the right fix is (and not entirely sure I remember what the right types are), but the above "desired output" is similar to what you get in the case of #87763, so I'd expect something sort of similar here.
Other cases
If instead, you use the form from #87763, you get the following:
trait Trait<T> {
fn method(_: T) -> Self;
}
impl Trait<&i32> for &i32 {
fn method(x: &i32) -> &i32 {x}
}
->
Compiling playground v0.0.1 (/playground)
error: `impl` item signature doesn't match `trait` item signature
--> src/lib.rs:7:5
|
4 | fn method(_: T) -> Self;
| ------------------------ expected `fn(&'1 i32) -> &'2 i32`
...
7 | fn method(x: &i32) -> &i32 {x}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 i32) -> &'1 i32`
|
= note: expected signature `fn(&'1 i32) -> &'2 i32`
found signature `fn(&'1 i32) -> &'1 i32`
help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
--> src/lib.rs:4:18
|
4 | fn method(_: T) -> Self;
| ^ ^^^^ consider borrowing this type parameter in the trait
| |
| consider borrowing this type parameter in the trait
error: could not compile `playground` (lib) due to 1 previous error
I bet there are many variations along a theme one could try here.
Rust Version
Rust playground 1.93.0
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
Reproduce the two trait and impl examples in src/lib.rs with Rust Playground 1.93.0, comparing the current diagnostics with the desired output and the working form from #87763. Investigate the compiler diagnostic path for trait-impl signature mismatches, including how elided lifetimes are rendered. Done means the general case reports distinguishable lifetime signatures and explains the mismatch without misleading anonymous lifetimes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100