rust-lang / rust-lang/rust

Borrow checker error does not figure out source of `'static` requirement through closure capture

Open
#126,342 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics P-medium regression-from-stable-to-stable T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
fn main() {
    let targets = String::new();
    let queue = targets.as_str();

    (move || require_static(queue))();
}

fn require_static(_: &'static str) {}
Current output
error[E0597]: `targets` does not live long enough
 --> src/main.rs:3:17
  |
2 |     let targets = String::new();
  |         ------- binding `targets` declared here
3 |     let queue = targets.as_str();
  |                 ^^^^^^^---------
  |                 |
  |                 borrowed value does not live long enough
  |                 argument requires that `targets` is borrowed for `'static`
...
6 | }
  | - `targets` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Desired output
error[E0597]: `targets` does not live long enough
 --> src/main.rs:3:17
  |
2 |     let targets = String::new();
  |         ------- binding `targets` declared here
3 |     let queue = targets.as_str();
  |                 ^^^^^^^ borrowed value does not live long enough
4 |
5 |     (move || require_static(queue))();
  |              --------------------- argument requires that `targets` is borrowed for `'static`
6 | }
  | - `targets` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Rationale and extra context

No response

Other cases
This works on stable, but regressed on nightly.
Rust Version
rustc 1.80.0-nightly (804421dff 2024-06-07)
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied Rust snippet with rustc 1.80.0-nightly and compare its E0597 diagnostic with stable Rust. Trace how the closure capture and require_static(queue) argument are rendered in the compiler diagnostics; done means the borrow error points at the closure call as shown in the desired output.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.