Skip to content

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

Open
@Aaron1011

Description

@Aaron1011
Member

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).

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
A-lifetimesArea: Lifetimes / regions
on Jun 30, 2021
rukai

rukai commented on Nov 27, 2021

@rukai
Contributor

This is solved by #90667
The example now outputs:

error[E0312]: lifetime of reference outlives lifetime of borrowed content...
 --> foo.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 anonymous lifetime defined here
 --> foo.rs:1:13
  |
1 | fn bar(val: &'_ str) {
  |             ^^^^^^^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @Aaron1011@rukai

        Issue actions

          Error message becomes worse when lifetime is elided via `'_` · Issue #86759 · rust-lang/rust