-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.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
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.
scottmcm, estebank and adaszko
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.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.