Skip to content

Regression in helpful compiler error message when working with lifetimes #66406

Closed
@petergarnaes

Description

@petergarnaes

So I have the following code:

struct Article {
    proof_reader: ProofReader,
}

struct ProofReader {
    name: String,
}

pub trait HaveRelationship<To> {
    fn get_relation(&self) -> To;
}

impl HaveRelationship<&ProofReader> for Article {
    fn get_relation(&self) -> &ProofReader {
        &self.proof_reader
    }
}

Which on the stable compiler gives this helpful error message:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in generic type due to conflicting requirements
  --> src/main.rs:14:5
   |
14 |     fn get_relation(&self) -> &ProofReader {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 14:5...
  --> src/main.rs:14:5
   |
14 | /     fn get_relation(&self) -> &ProofReader {
15 | |         &self.proof_reader
16 | |     }
   | |_____^
note: ...but the lifetime must also be valid for the lifetime '_ as defined on the impl at 13:23...
  --> src/main.rs:13:23
   |
13 | impl HaveRelationship<&ProofReader> for Article {
   |                       ^
   = note: ...so that the method type is compatible with trait:
           expected fn(&Article) -> &ProofReader
              found fn(&Article) -> &ProofReader

Yet on the nightly compiler the compiler error does not inform me about lifetimes, but tells me the signature needs to be what it already is:

error: `impl` item signature doesn't match `trait` item signature
  --> src/main.rs:14:5
   |
10 |     fn get_relation(&self) -> To;
   |     ----------------------------- expected fn(&Article) -> &ProofReader
...
14 |     fn get_relation(&self) -> &ProofReader {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found fn(&Article) -> &ProofReader
   |
   = note: expected `fn(&Article) -> &ProofReader`
              found `fn(&Article) -> &ProofReader`

playground stable version
playground nightly version

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-papercutDiagnostics: An error or lint that needs small tweaks.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions