Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mark-i-m opened this issue Dec 2, 2018 · 1 comment
Open

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

mark-i-m opened this issue Dec 2, 2018 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mark-i-m
Copy link
Member

mark-i-m commented Dec 2, 2018

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.

@Centril Centril added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 2, 2018
@estebank estebank added A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 26, 2019
@estebank
Copy link
Contributor

CC #48812

@estebank estebank added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Nov 5, 2019
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
teddywing added a commit to teddywing/fastcgi-conduit that referenced this issue Jun 28, 2020
Was getting this error about the lifetimes in the trait impl:

    error[E0308]: method not compatible with trait
       --> src/lib.rs:137:4
        |
    137 |    fn host(&'a self) -> conduit::Host<'a> {
        |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
        |
        = note: expected fn pointer `fn(&'a FastCgiRequest<'static>) -> conduit::Host<'a>`
                   found fn pointer `fn(&'a FastCgiRequest<'static>) -> conduit::Host<'a>`
    note: the lifetime `'a` as defined on the method body at 137:4...
       --> src/lib.rs:137:4
        |
    137 |    fn host(&'a self) -> conduit::Host<'a> {
        |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    note: ...does not necessarily outlive the lifetime `'a` as defined on the impl at 124:6
       --> src/lib.rs:124:6
        |
    124 | impl<'a> conduit::RequestExt for FastCgiRequest {
        |      ^^

    error[E0308]: method not compatible with trait
       --> src/lib.rs:137:4
        |
    137 |    fn host(&'a self) -> conduit::Host<'a> {
        |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
        |
        = note: expected fn pointer `fn(&'a FastCgiRequest<'static>) -> conduit::Host<'a>`
                   found fn pointer `fn(&'a FastCgiRequest<'static>) -> conduit::Host<'a>`
    note: the lifetime `'a` as defined on the impl at 124:6...
       --> src/lib.rs:124:6
        |
    124 | impl<'a> conduit::RequestExt for FastCgiRequest {
        |      ^^
    note: ...does not necessarily outlive the lifetime `'a` as defined on the method body at 137:4
       --> src/lib.rs:137:4
        |
    137 |    fn host(&'a self) -> conduit::Host<'a> {
        |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Learned from these resources that the problem arose because the `impl`
uses an `'a` lifetime, but the lifetime on the `host()` method need to
be a different one:

rust-lang/rust#56423
https://stackoverflow.com/questions/24847331/rust-lifetime-error-expected-concrete-lifetime-but-found-bound-lifetime/24848424#24848424
@estebank estebank removed their assignment Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants