Skip to content

Confusing "cannot infer an appropriate lifetime" error message #65866

Closed

Description

@DustinByfuglien

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>){}
}

(Playground)

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.

Activity

  1. added
    A-lifetimesArea: Lifetimes / regions
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Oct 27, 2019
  2. zim32 commented on Aug 28, 2021

    @zim32

    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.

  3. estebank commented on Nov 3, 2025

    @estebank
    Contributor

    Current output:

    error: `impl` item signature doesn't match `trait` item signature
      --> src/lib.rs:12:5
       |
     2 |     fn bar(&self, r: &mut Re);
       |     -------------------------- expected `fn(&'1 Foo, &'2 mut Re<'3>)`
    ...
    12 |     fn bar<'a, 'b>(&'a self, r: &'b mut Re<'a>){}
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 Foo, &'2 mut Re<'1>)`
       |
       = note: expected signature `fn(&'1 Foo, &'2 mut Re<'3>)`
                  found signature `fn(&'1 Foo, &'2 mut Re<'1>)`
       = help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
       = help: verify the lifetime relationships in the `trait` and `impl` between the `self` argument, the other inputs and its output
    

    We should likely detect that the first Re has 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.

  4. added
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    and removed on Nov 3, 2025
  5. added 2 commits that reference this issue on Feb 18, 2026
    f5b12b4
    d687277
  6. added a commit that references this issue on Feb 18, 2026
    9e38745
  7. added a commit that references this issue on Feb 18, 2026
    eb41ee0
  8. added a commit that references this issue on Feb 19, 2026
    ebbeb83
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.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