Skip to content

Confusing error message when eliding lifetimes in trait impl #73520

Closed
@Aaron1011

Description

@Aaron1011

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.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