Repository navigation
Confusing "cannot infer an appropriate lifetime" error message #65866
Copy link
Copy link
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsA-trait-systemArea: Trait systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Consider example:
trait Bar {
fn bar(&self, r: &mut Re);
}
struct Re<'a> {
data: &'a u16,
}
struct Foo;
impl Bar for Foo {
fn bar<'a, 'b>(&'a self, r: &'b mut Re<'a>){}
}Errors:
Compiling playground v0.0.1 (/playground)
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter 'a in generic type due to conflicting requirements
--> src/lib.rs:12:5
|
12 | fn bar<'a, 'b>(&'a self, r: &'b mut Re<'a>){}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 12:5...
--> src/lib.rs:12:5
|
12 | fn bar<'a, 'b>(&'a self, r: &'b mut Re<'a>){}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...but the lifetime must also be valid for the anonymous lifetime #3 defined on the method body at 12:5...
--> src/lib.rs:12:5
|
12 | fn bar<'a, 'b>(&'a self, r: &'b mut Re<'a>){}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...so that the method type is compatible with trait:
expected fn(&Foo, &mut Re<'_>)
found fn(&Foo, &mut Re<'_>)
error: aborting due to previous error
For more information about this error, try `rustc --explain E0495`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.
All 3 notes of this error message is confusing:
note1, note2: Message not point where are the anonymous lifetime #1 and the anonymous lifetime #3. Also it's not obviously why there are two different anonymous lifetimes at one place (at 12:5).
=note3: Expected result looks totally equal to founded. So it's not obviously why it considered as different.
It is difficult to figure out what was going on.
May be some change of this message needed.
Reacted by zohnannor and Dmitry Opokin
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsA-trait-systemArea: Trait systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
zim32 commentedon Aug 28, 2021
I am here due to the same issue. This annoying error bothers me again and again. And I spend hours and hours to figure out whats wrong with my lifetimes. This is very demotivating and frustrating.
I was trying to implement something like nom parser, there is a lot of generic code, macrorules and so on. And this lifetimes just makes me crazy.
Every time I see this message it makes my head explode.
So I think my journey with rust will end today. Rust is great and writing some simple programs is easy. But when I try to implement something harder then hello world, things got very messy.
estebank commentedon Nov 3, 2025
Current output:
We should likely detect that the first
Rehas a lifetime param that isn't being specified and point that out. Beyond that, I don't know how we could improve this case any further.Rollup merge of rust-lang#152637 - JohnTitor:issue-65866, r=estebank
Rollup merge of rust-lang#152637 - JohnTitor:issue-65866, r=estebank
Rollup merge of rust-lang#152637 - JohnTitor:issue-65866, r=estebank
Unrolled build for #152637
Rollup merge of #152637 - JohnTitor:issue-65866, r=estebank