Bug when using .flatten()
method that has Item = &'a T
when calling async function inside loop #126044
Description
I tried this code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f560d75b3eb32ab6d2e0e51590b0337f
I expected to see this happen: code compiles
Instead, this happened: error: implementation of std::marker::Send is not general enough
Meta
rustc --version --verbose
:
rustc 1.77.1 (7cf61ebde 2024-03-27)
binary: rustc
commit-hash: 7cf61ebde7b22796c69757901dd346d0fe70bd97
commit-date: 2024-03-27
host: x86_64-unknown-linux-gnu
release: 1.77.1
LLVM version: 17.0.6
Bug exists on beta
and nightly
versions also.
Backtrace
error: implementation of `std::marker::Send` is not general enough
--> src/main.rs:63:5
|
63 | tokio::spawn(join_with_cancellation(f, cancellation.clone()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `std::marker::Send` is not general enough
|
= note: `std::marker::Send` would have to be implemented for the type `&CancellationToken`
= note: ...but `std::marker::Send` is actually implemented for the type `&'0 CancellationToken`, for some specific lifetime `'0`
I have working example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=19183aa875e3dadf501f3b6bdfb61ce4.
It seems like the reason is combination of .flatten()
method that has Item = &'a T
when calling async function inside loop. If you try to remove .await
from the line 33, it compiles. If you try to remove .flatten()
as in second link - it compiles.
Also, the error seems kinda random. The pattern is as follows:
= note: ...but `std::marker::Send` is actually implemented for the type `&'0 T`, for some specific lifetime `'0`
But every time I change the code, the compiler shows another T
type in error hint.
Activity