Skip to content

Improve error message for one named, one anonymous lifetimes - underline correct source #43434

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

Closed
gaurikholkar-zz opened this issue Jul 23, 2017 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@gaurikholkar-zz
Copy link

fn foo<'a>(x: &'a i32, y: Vec<&i32>) {
                       - consider changing the type of `y` to `std::vec::Vec<&'a i32>`
    y.push(x);
           ^ this reference must have lifetime 'a
}

The error message wrongly underlines x. It should underline y, which is the source of the error. The refined message will be like this

fn foo<'a>(x: &'a i32, y: Vec<&i32>) {
                       - consider changing the type of `y` to `std::vec::Vec<&'a i32>`
    y.push(x);
    ^ this reference must have lifetime 'a
}

cc @nikomatsakis

@Mark-Simulacrum Mark-Simulacrum added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Jul 26, 2017
@gaurikholkar-zz
Copy link
Author

gaurikholkar-zz commented Aug 21, 2017

cc @nikomatsakis
Status:

11 | fn foo<'a>((x, y): (&'a i32, &i32)) -> &'a i32 {
   |                              ---- consider changing type to `(&'a i32, &'a i32)`
12 |     if x > y { x } else { y }
   |                           ^ lifetime `'a` required

error: aborting due to previous error

Notice that the &'a i32 is being underlined while it says
consider changing type to (&'a i32, &'a i32) , pointing to the tuple (&'a i32, &'a i32) here.

Option 1

11 | fn foo<'a>((x, y): (&'a i32, &i32)) -> &'a i32 {
   |                     ------------- consider changing type to `(&'a i32, &'a i32)`
12 |     if x > y { x } else { y }
   |                           ^ lifetime `'a` required

Option 2

11 | fn foo<'a>((x, y): (&'a i32, &i32)) -> &'a i32 {
   |                              ---- consider changing type to `&'a i32`
12 |     if x > y { x } else { y }
   |                           ^ lifetime `'a` required

Closed the PR for now.

@estebank
Copy link
Contributor

Current output, added structured suggestion:

error[E0621]: explicit lifetime required in the type of `y`
 --> file.rs:2:12
  |
1 | fn foo<'a>(x: &'a i32, y: Vec<&i32>) {
  |                           --------- help: add explicit lifetime `'a` to the type of `y`: `std::vec::Vec<&'a i32>`
2 |     y.push(x);
  |            ^ lifetime `'a` required
error[E0621]: explicit lifetime required in parameter type
 --> file.rs:2:27
  |
1 | fn foo<'a>((x, y): (&'a i32, &i32)) -> &'a i32 {
  |                    --------------- help: add explicit lifetime `'a` to type: `(&'a i32, &'a i32)`
2 |     if x > y { x } else { y }
  |                           ^ lifetime `'a` required

I don't think there's anything else for us to do here.

@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 22, 2020
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: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

3 participants