Open
Description
I was just looking for some lifetime errors for a project, and I came across the following example:
The solution in that thread is correct, but the error message is somewhat unhelpful with today's stable and nightly compilers:
error[E0308]: method not compatible with trait
--> src/l3.rs:19:5
|
19 | fn to_c(&self, r: &'a Ref) -> Container<'a> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected type `fn(&l3::ContainerB<'a>, &'a l3::Ref) -> l3::Container<'a>`
found type `fn(&l3::ContainerB<'a>, &'a l3::Ref) -> l3::Container<'a>`
note: the lifetime 'a as defined on the method body at 19:5...
--> src/l3.rs:19:5
|
19 | fn to_c(&self, r: &'a Ref) -> Container<'a> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...does not necessarily outlive the lifetime 'a as defined on the impl at 17:6
--> src/l3.rs:17:6
|
17 | impl<'a> ToC for ContainerB<'a> {
| ^^
error[E0308]: method not compatible with trait
--> src/l3.rs:19:5
|
19 | fn to_c(&self, r: &'a Ref) -> Container<'a> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected type `fn(&l3::ContainerB<'a>, &'a l3::Ref) -> l3::Container<'a>`
found type `fn(&l3::ContainerB<'a>, &'a l3::Ref) -> l3::Container<'a>`
note: the lifetime 'a as defined on the impl at 17:6...
--> src/l3.rs:17:6
|
17 | impl<'a> ToC for ContainerB<'a> {
| ^^
note: ...does not necessarily outlive the lifetime 'a as defined on the method body at 19:5
--> src/l3.rs:19:5
|
19 | fn to_c(&self, r: &'a Ref) -> Container<'a> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors
Basically the problem is that there are two lifetimes called 'a
and the "expected" and "found" types use different 'a
s.
Metadata
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsArea: Lifetimes / regionsCategory: An issue proposing an enhancement or a PR with one.Diagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint; hard to understand for new users.Relevant to the compiler team, which will review and decide on the PR/issue.