Skip to content

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

Open
@Noratrieb

Description

@Noratrieb

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsP-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions