Skip to content

Weird lifetime error on closure in thread::scope #95527

Closed
@m-ou-se

Description

@m-ou-se

This was reported here: #93203 (comment)

This should compile, but this fails to compile:

#![feature(scoped_threads)]

pub fn foo(x: &u8) {
    std::thread::scope(|s| {
        s.spawn(|| drop(x));
    });
}

Error:

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
 --> src/main.rs:5:17
  |
5 |         s.spawn(|| drop(x));
  |                 ^^^^^^^^^^
  |
note: first, the lifetime cannot outlive the anonymous lifetime defined here...
 --> src/main.rs:3:15
  |
3 | pub fn foo(x: &u8) {
  |               ^^^
note: ...so that the types are compatible
 --> src/main.rs:5:17
  |
5 |         s.spawn(|| drop(x));
  |                 ^^^^^^^^^^
  = note: expected `(&&u8,)`
             found `(&&u8,)`
note: but, the lifetime must be valid for the anonymous lifetime #1 defined here...
 --> src/main.rs:4:24
  |
4 |       std::thread::scope(|s| {
  |  ________________________^
5 | |         s.spawn(|| drop(x));
6 | |     });
  | |_____^
note: ...so that the types are compatible
 --> src/main.rs:5:11
  |
5 |         s.spawn(|| drop(x));
  |           ^^^^^
  = note: expected `&Scope<'_, '_>`
             found `&Scope<'_, '_>`

(rustc 1.61.0-nightly (1eb7258 2022-03-08))

It seems to be complaining that it cannot find a lifetime no longer than the one of the x: &u8 argument, but no shorter than the scope closure. The error is very hard to read though.

It's interesting how the capture tuple (&&u8,) of the closure is visible in the diagnostic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.T-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