Skip to content

[nll] improve the "fully elaborated type" case in region errors #52533

Closed
@nikomatsakis

Description

@nikomatsakis

Consider this example:

#![feature(nll)]

fn foo(_: impl for<'a> FnOnce(&'a u32, &u32) -> &'a u32) {
}

fn main() {
    foo(|a, b| b)
}

Here, we are returning b from the closure but we ought to be returning a. We get this error message, which could be much clearer:

error: unsatisfied lifetime constraints
 --> src/main.rs:8:16
  |
8 |     foo(|a, b| b)
  |          -  -  ^ free region requires that `'1` must outlive `'2`
  |          |  |
  |          |  lifetime `'1` appears in this argument
  |          lifetime `'2` appears in this argument

There are a number of problems here, but the part I wanted to focus on in this issue is the message "lifetime '1 appears in this argument".

This message occurs when we fail, for whatever reason, to identify part of the type that we can highlight. For example, if we compare against the case where we have given type annotations in the closure, we can see that in that case we do better:

error: unsatisfied lifetime constraints
 --> src/main.rs:8:28
  |
8 |     foo(|a: &u32, b: &u32| b)
  |             -        -     ^ free region requires that `'1` must outlive `'2`
  |             |        |
  |             |        let's call the lifetime of this reference `'1`
  |             let's call the lifetime of this reference `'2`

The goal of this issue is to do better in this case where no type annotations are available. We've been discussing this on an internals thread and there have been a number of helpful suggestions. I think for the purposes of this issue the wording isn't that important, I'm mostly interested in producing the "fully elaborated type" print out. So let's go with something simple like this as our goal:

error: unsatisfied lifetime constraints
 --> src/main.rs:8:16
  |
8 |     foo(|a, b| b)
  |          -  -  ^ free region requires that `'1` must outlive `'2`
  |          |  |
  |          |  has type `&'1 u32`
  |          has type `&'2 u32`

Metadata

Metadata

Assignees

Labels

A-NLLArea: Non-lexical lifetimes (NLL)A-diagnosticsArea: Messages for errors, warnings, and lintsNLL-diagnosticsWorking towards the "diagnostic parity" goal

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions