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

"expected X found X" diagnostic for lifetime mismatch in method signature of trait impl #79643

Closed
SimonSapin opened this issue Dec 2, 2020 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. 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

@SimonSapin
Copy link
Contributor

With this code:

struct Foo(u32);

impl From<&Foo> for &u32 {
    fn from(foo: &Foo) -> &u32 {
        &foo.0
    }
}

The error message is:

error: `impl` item signature doesn't match `trait` item signature
   --> src/lib.rs:4:5
    |
4   |     fn from(foo: &Foo) -> &u32 {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&Foo) -> &u32`
    |
    = note: expected `fn(&Foo) -> &u32`
               found `fn(&Foo) -> &u32`
    = 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

Now, the two help lines do give relevant information on how to fix this, which is to have a single named lifetime and use it everywhere:

struct Foo(u32);

impl<'a> From<&'a Foo> for &'a u32 {
    fn from(foo: &'a Foo) -> &'a u32 {
        &foo.0
    }
}

However the part of the error that is more visible at first glance (at least to me) is:

    = note: expected `fn(&Foo) -> &u32`
               found `fn(&Foo) -> &u32`

My internal reaction was: if what was expected and found is the same thing, what’s the problem?

The two signature are in fact not the same because they each have their own implicit lifetime parameter which don’t necessarily match each other. Would it make sense to make those parameters explicit? Signatures in diagnostics don’t need to be entirely valid Rust syntax. Something like:

    = note: expected `fn(&'_1 Foo) -> &'_1 u32`
               found `fn(&'_2 Foo) -> &'_2 u32`

Meta

The output is the same in 1.48.0 and nightly-2020-11-30.

@SimonSapin SimonSapin added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. labels Dec 2, 2020
@jyn514
Copy link
Member

jyn514 commented Dec 2, 2020

Related: #76353, #41078

@jyn514 jyn514 added the A-lifetimes Area: Lifetimes / regions label Dec 2, 2020
@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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 13, 2021
@estebank
Copy link
Contributor

estebank commented Jan 8, 2023

Current output:

error: `impl` item signature doesn't match `trait` item signature
 --> src/lib.rs:6:5
  |
6 |     fn from(foo: &Foo) -> &u32 {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 Foo) -> &'1 u32`
  |
  = note: expected `fn(&'1 Foo) -> &'2 u32`
             found `fn(&'1 Foo) -> &'1 u32`
  = 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

@estebank estebank closed this as completed Jan 8, 2023
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-bug Category: This is a bug. 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

3 participants