Skip to content
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

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

Open
Aaron1011 opened this issue Jun 30, 2021 · 1 comment
Open

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

Aaron1011 opened this issue Jun 30, 2021 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related 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

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

@Aaron1011 Aaron1011 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. A-lifetimes Area: lifetime related labels Jun 30, 2021
@rukai
Copy link
Contributor

rukai commented Nov 27, 2021

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
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related 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