Indicate the user when lifetime requirements from a method diverge from trait
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
trait Trait {
fn foo(&'static self) {}
}
impl Trait for bool {
fn foo(&self) {}
}
fn bar() {
let b = false;
b.foo();
}
Current output
error[E0597]: `b` does not live long enough
--> src/lib.rs:10:5
|
9 | let b = false;
| - binding `b` declared here
10 | b.foo();
| ^------
| |
| borrowed value does not live long enough
| argument requires that `b` is borrowed for `'static`
11 | }
| - `b` dropped here while still borrowed
Desired output
error[E0597]: `b` does not live long enough
--> src/lib.rs:10:5
|
9 | let b = false;
| - binding `b` declared here
10 | b.foo();
| ^------
| |
| borrowed value does not live long enough
| argument requires that `b` is borrowed for `'static`
11 | }
| - `b` dropped here while still borrowed
note: lifetime requirements from the method diverge from the trait declaration
Rationale and extra context
Today I had a compile error that I couldn't understand for a long time, and it'd be nice if the compiler could help a bit.
I had a function that called another function, passing a parameter as-is. And it tricked "lifetime does not live long enough" error. I checked the callee, and the lifetime was correct. I stared at this for a long time, trying to fiddle with changes but none helped.
Only after a long time I realized that this was a trait method, and when I changed the method lifetimes, I changed the impl and not the trait. rustc accepted this just fine, but the constraints were actually the stricter from the trait.
Deciding whether to allow refinements like this to actually work is a T-lang decision, but we can at least tell the users that the lifetimes they see are not the real ones.
Other cases
No response
Rust Version
Checked with both stable and nightly.
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 issue using the Rust code example and compare the stable and nightly rustc diagnostics. Trace the compiler diagnostic path for the lifetime error, then determine where the note about method and trait lifetime requirements should be emitted; done means the requested note appears without changing unrelated diagnostics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100