Closed as duplicate of#63768
Description
Hello, I have a piece of code that fails to compile, but I believe it is a false positive.
use rand::Rng;
use std::future::Future;
use std::mem;
#[tokio::main]
async fn main() {
accept_send_future(foo()).await;
}
fn accept_send_future<F>(f: F) -> F
where
F: Future + Send,
{
f
}
async fn foo() {
let mut rng = rand::rng(); // rng is not Send.
let idx = rng.random_range(0..100);
mem::drop(rng); // drop the rng
bar(idx).await;
}
async fn bar(id: usize) {
println!("bar: {}", id);
}
i got an error:
error: future cannot be sent between threads safely
--> src/main.rs:7:24
|
7 | accept_send_future(foo()).await;
| ^^^^^ future returned by `foo` is not `Send`
|
= help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `Rc<UnsafeCell<ReseedingRng<rand_chacha::chacha::ChaCha12Core, OsRng>>>`, which is required by `impl Future<Output = ()>: Send`
note: future is not `Send` as this value is used across an await
--> src/main.rs:29:14
|
26 | let mut rng = rand::rng();
| ------- has type `ThreadRng` which is not `Send`
...
29 | bar(idx).await;
| ^^^^^ await occurs here, with `mut rng` maybe used later
note: required by a bound in `accept_send_future`
--> src/main.rs:12:17
|
10 | fn accept_send_future<F>(f: F) -> F
| ------------------ required by a bound in this function
11 | where
12 | F: Future + Send,
| ^^^^ required by this bound in `accept_send_future`
the 'rng' is expliciyly dropped, and it never cross the await.