Skip to content

Unhelpful error message when multiple lifetimes have the same name #56423

Open
@mark-i-m

Description

@mark-i-m

I was just looking for some lifetime errors for a project, and I came across the following example:

https://stackoverflow.com/questions/24847331/rust-lifetime-error-expected-concrete-lifetime-but-found-bound-lifetime

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 'as.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: 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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions