Skip to content

[NLL] "borrowed value must be valid for lifetime '_#2r..." #48428

Closed
@nikomatsakis

Description

@nikomatsakis

In NLL, there is one rule of diagnostics:

Thou must-eth never print-eth a region.

Unfortunately, we break it. Twice. Here is one example:

#![feature(nll)]

fn gimme(x: &(u32,)) -> &u32 { &x.0 }

fn main() {
    let v = 22;
    let x = gimme(&(v,));
    println!("{:?}", x);
}

This prints the following:

error[E0597]: borrowed value does not live long enough
 --> src/main.rs:7:20
  |
7 |     let x = gimme(&(v,));
  |                    ^^^^ - temporary value only lives until here
  |                    |
  |                    temporary value does not live long enough
  |
  = note: borrowed value must be valid for lifetime '_#2r...

Note the final line. In comparison, the AST-based version gets the following error, which is much better:

error[E0597]: borrowed value does not live long enough
 --> src/main.rs:7:20
  |
7 |     let x = gimme(&(v,));
  |                    ^^^^ - temporary value dropped here while still borrowed
  |                    |
  |                    temporary value does not live long enough
8 |     println!("{:?}", x);
9 | }
  | - temporary value needs to live until here
  |
  = note: consider using a `let` binding to increase its lifetime

We probably want to integrate with the "dump-cause" mode and something like this:

error[E0597]: borrowed value does not live long enough
 --> src/main.rs:7:20
  |
7 |     let x = gimme(&(v,));
  |                    ^^^^ - temporary value only lives until here
  |                    |
  |                    temporary value does not live long enough
 8 |    println!("{}", x);
   |                   ^ reference later used here

Metadata

Metadata

Assignees

Labels

A-NLLArea: Non-lexical lifetimes (NLL)A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions