Borrow checker error does not figure out source of `'static` requirement through closure capture
Open
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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