Skip to content
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

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

Closed
msrd0 opened this issue Nov 22, 2022 · 0 comments · Fixed by #105005
Closed

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

msrd0 opened this issue Nov 22, 2022 · 0 comments · Fixed by #105005
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@msrd0
Copy link
Contributor

msrd0 commented Nov 22, 2022

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`.
@msrd0 msrd0 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 22, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 6, 2022
…ler-errors

On E0195 point at where clause lifetime bounds

Fix rust-lang#104733
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 6, 2022
…ler-errors

On E0195 point at where clause lifetime bounds

Fix rust-lang#104733
@bors bors closed this as completed in e802165 Dec 6, 2022
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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant