Skip to content

Confusing error message when eliding lifetimes in trait impl #73520

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

Closed
Aaron1011 opened this issue Jun 19, 2020 · 2 comments
Closed

Confusing error message when eliding lifetimes in trait impl #73520

Aaron1011 opened this issue Jun 19, 2020 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions A-trait-system Area: Trait system 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

@Aaron1011
Copy link
Member

Aaron1011 commented Jun 19, 2020

The following code:

trait Foo<T> {
    fn make_it(&self) -> T;
}

impl Foo<&u8> for () {
    fn make_it(&self) -> &u8 {
        &25
    }
}

produces the following error:

error: `impl` item signature doesn't match `trait` item signature
 --> src/lib.rs:6:5
  |
2 |     fn make_it(&self) -> T;
  |     ----------------------- expected fn(&()) -> &u8
...
6 |     fn make_it(&self) -> &u8 {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^ found fn(&()) -> &u8
  |
  = note: expected `fn(&()) -> &u8`
             found `fn(&()) -> &u8`

The issue is that the elided lifetimes in the impl header (impl Foo<&u8> for ()) and the method implementation fn make_it(&self) -> &u8 do not desugar to the same lifetime. However, we currently emit a nonsensical error message instead of explaining this.

We should explain the behavior of lifetime elision, and suggest introducing a lifetime parameter in the impl header.

@Aaron1011 Aaron1011 added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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. labels Jun 19, 2020
@estebank
Copy link
Contributor

Current output in nightly:

error: `impl` item signature doesn't match `trait` item signature
 --> src/lib.rs:6:5
  |
2 |     fn make_it(&self) -> T;
  |     ----------------------- expected `fn(&()) -> &u8`
...
6 |     fn make_it(&self) -> &u8 {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&()) -> &u8`
  |
  = note: expected `fn(&()) -> &u8`
             found `fn(&()) -> &u8`
help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
 --> src/lib.rs:2:26
  |
2 |     fn make_it(&self) -> T;
  |                          ^ consider borrowing this type parameter in the trait

@estebank
Copy link
Contributor

estebank commented Dec 4, 2024

Current output:

error: `impl` item signature doesn't match `trait` item signature
 --> src/lib.rs:6:5
  |
2 |     fn make_it(&self) -> T;
  |     ----------------------- expected `fn(&'1 ()) -> &'2 u8`
...
6 |     fn make_it(&self) -> &u8 {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 ()) -> &'1 u8`
  |
  = note: expected signature `fn(&'1 ()) -> &'2 u8`
             found signature `fn(&'1 ()) -> &'1 u8`
help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
 --> src/lib.rs:2:26
  |
2 |     fn make_it(&self) -> T;
  |                          ^ consider borrowing this type parameter in the trait

@estebank estebank closed this as completed Dec 4, 2024
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 A-trait-system Area: Trait system 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

2 participants