Skip to content

E0195: Bad error message on mismatched lifetimes when missing where clause #104733

Closed
@msrd0

Description

@msrd0

Given the following code:

struct State;

trait Foo<T> {
    fn foo<'a>(&self, state: &'a State) -> &'a T
    where
        T: 'a;
}

impl<F, T> Foo<T> for F
where
    F: Fn(&State) -> &T,
{
    fn foo<'a>(&self, state: &'a State) -> &'a T {
        self(state)
    }
}

The current output is:

error[E0195]: lifetime parameters or bounds on method `foo` do not match the trait declaration
  --> src/lib.rs:13:11
   |
4  |     fn foo<'a>(&self, state: &'a State) -> &'a T
   |           ---- lifetimes in impl do not match this method in trait
...
13 |     fn foo<'a>(&self, state: &'a State) -> &'a T {
   |           ^^^^ lifetimes do not match method in trait

For more information about this error, try `rustc --explain E0195`.

This error message, especially due to the highlighted code, makes me believe that there is some problem with the generic lifetime itself. However, the problem is the missing where bound that never appears in the error message.

Ideally, the error message should highlight the where clause of the trait that is missing from the implementation, along the lines of this:

error[E0195]: lifetime parameters or bounds on method `foo` do not match the trait declaration
  --> src/lib.rs:13:11
   |
4  |     fn foo<'a>(&self, state: &'a State) -> &'a T
   |           ---- lifetimes in impl do not match this method in trait
...
13 |     fn foo<'a>(&self, state: &'a State) -> &'a T {
   |           ^^^^ lifetimes do not match method in trait

help: Try adding the following bound:

  --> src/lib.rs:6:
   |
 6 |        T: 'a;
   |        ^^^^^

For more information about this error, try `rustc --explain E0195`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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