Skip to content

"lifetime of borrowed pointer outlives lifetime of captured variable" error message very unclear #42574

Closed
@jonas-schievink

Description

@jonas-schievink

For this code:

fn doit(data: &'static mut ()) {
    || doit(data);
}

(works fine if you drop the mut, still happens with a move closure).

The compiler (stable and nightly) spits out this error:

error[E0313]: lifetime of borrowed pointer outlives lifetime of captured variable `data`...
 --> <anon>:3:13
  |
3 |     || doit(data);
  |             ^^^^
  |
  = note: ...the borrowed pointer is valid for the static lifetime...
note: ...but `data` is only valid for the lifetime  as defined on the body at 3:7
 --> <anon>:3:8
  |
3 |     || doit(data);
  |        ^^^^^^^^^^

error: aborting due to previous error
  • The last note is broken: for the lifetime as defined on the actual lifetime is invisible. There is also no lifetime defined in the entire program, so it's not clear which lifetime the note is talking about.
  • The main error message is very unclear. Is it saying that data itself must live as long as the data behind the reference? Why?

According to the rustc source code, this error is related to an implicit reborrow done by the closure. The error mentions nothing of that sort.

Metadata

Metadata

Assignees

Labels

A-NLLArea: Non-lexical lifetimes (NLL)A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.NLL-diagnosticsWorking towards the "diagnostic parity" goalP-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.fixed-by-NLLBugs fixed, but only when NLL is enabled.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions