Skip to content

Error message becomes worse when lifetime is elided via '_ #86759

Open
@Aaron1011

Description

@Aaron1011

Given the following code:

fn bar(val: &'_ str) {
    let a: &'static str = val;
}

The current output is:

error[E0621]: explicit lifetime required in the type of `val`
 --> src/lib.rs:2:27
  |
2 |     let a: &'static str = val;
  |                           ^^^ lifetime `'static` required

error: aborting due to previous error

However, if we explicitly name the lifetime:

fn bar<'a>(val: &'a str) {
    let a: &'static str = val;
}

the error message is much better:

error[E0312]: lifetime of reference outlives lifetime of borrowed content...
 --> src/lib.rs:2:27
  |
2 |     let a: &'static str = val;
  |                           ^^^
  |
  = note: ...the reference is valid for the static lifetime...
note: ...but the borrowed content is only valid for the lifetime `'a` as defined on the function body at 1:8
 --> src/lib.rs:1:8
  |
1 | fn bar<'a>(val: &'a str) {
  |        ^^

error: aborting due to previous error

We should emit the same error message when the lifetime is elided (referring to it as 'this elided lifetime' or something similar).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsD-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