Weird lifetime error on closure in thread::scope #95527
Closed
Description
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.
Activity