Skip to content

NLL: "explicit lifetime required" error fails to highlight usage that introduces constraint  #51175

Closed
@pnkfelix

Description

@pnkfelix

There are a collection of tests where NLL is failing to highlight the expression relevant to debugging some lifetime error.

Here is the list of tests that pnkfelix believes falls into that category:

Here's an explanation of the kind of (mis)behavior being described here:

In the following test ( ex1-return-one-existing-name-if-else-3.rs ):

fn foo<'a>((x, y): (&'a i32, &i32)) -> &'a i32 {
    if x > y { x } else { y } //~ ERROR explicit lifetime
}

here is the error output from AST borrowck ( ex1-return-one-existing-name-if-else-3.stderr )

error[E0621]: explicit lifetime required in parameter type
  --> $DIR/ex1-return-one-existing-name-if-else-3.rs:12:27
   |
LL | fn foo<'a>((x, y): (&'a i32, &i32)) -> &'a i32 {
   |            ------ consider changing type to `(&'a i32, &'a i32)`
LL |     if x > y { x } else { y } //~ ERROR explicit lifetime
   |                           ^ lifetime `'a` required

while this is the error output from NLL: ( ex1-return-one-existing-name-if-else-3.nll.stderr ):

warning: not reporting region error due to nll
  --> $DIR/ex1-return-one-existing-name-if-else-3.rs:12:27
   |
LL |     if x > y { x } else { y } //~ ERROR explicit lifetime
   |                           ^

error[E0621]: explicit lifetime required in parameter type
  --> $DIR/ex1-return-one-existing-name-if-else-3.rs:11:13
   |
LL | fn foo<'a>((x, y): (&'a i32, &i32)) -> &'a i32 {
   |            -^----
   |            ||
   |            |lifetime `'a` required
   |            consider changing type to `(&'a i32, &'a i32)`

error: aborting due to previous error

Note in particular that the error is failing to report the expression (namely the occurrence of y in an expression context that is expecting a &'a i32) that is injecting the lifetime constraint that lead it to suggest the change to the parameter type.

(It seems likely that the span of interest is probably immediately available, given that the warning we emit about not reporting a region error does highlight the span in which we are interested.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)NLL-diagnosticsWorking towards the "diagnostic parity" goal

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions