async fn foo() {
}
fn test() -> impl std::future::Future<Output = ()> + Send {
let a = 1u32;
let mtx = std::sync::Mutex::new(a);
async move {
let g = mtx.lock().unwrap();
if a > 0 {
drop(g); // manually drop MutexGuard here
foo().await;
}
}
}
fn main() {
test();
println!("Hello, world!");
}
I know MutexGuard can not cross await, but when drop it manually, compiler still raise error
Compiling playground v0.0.1 (/playground)
error: future cannot be sent between threads safely
--> src/main.rs:5:14
|
5 | fn test() -> impl std::future::Future<Output = ()> + Send {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ future created by async block is not `Send`
|
= help: within `impl Future<Output = [async output]>`, the trait `Send` is not implemented for `MutexGuard<'_, u32>`
note: future is not `Send` as this value is used across an await
--> src/main.rs:13:13
|
10 | let g = mtx.lock().unwrap();
| - has type `MutexGuard<'_, u32>` which is not `Send`
...
13 | foo().await;
| ^^^^^^^^^^^ await occurs here, with `g` maybe used later
14 | }
15 | }
| - `g` is later dropped here
it says g is later dropped at end of block, but in fact it has been dropped before await point.
Is this a compiler problem?