Migrating from Fn -> impl Future
to AsyncFn
breaks Send
#137698
Labels
A-async-await
Area: Async & Await
C-bug
Category: This is a bug.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Posted here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=48d50965069b89953a04b10aff63c9cb
I want to convert some code to the new
AsyncFn
traits, and expectedAsyncFn
to be a drop-in replacement for code that previously usedimpl Fn () -> F
withF: Future
, but it doesn't seem to be and I am running into lifetime and/or Send trait issues.Minimalized toy example, pre-conversion, this code works fine:
playground
and here the version converting it to AsyncFn:
playground
The converted code fails with
Interestingly, using a higher ranked type bound will fix it:
But I do not understand why the converted version requires that and why the previous version does not require it (also note that in my particular case this solution doesn't work because in the non-minimized example, I actually don't have a lifetime but a type carrying a lifetime there and
for<T>
isn't allowed in stable Rust yet).Another interesting point is that this doesn't actually seem to be a lifetime issue. When I remove the call to
assert_is_send
, everything works. So it seems the lifetimes work just fine, just the generated Future isn't Send for some reason.Any idea what's going on and why the compiler behaves in this way here?
Meta
Rust 1.85
reproducible with rust playground links above
The text was updated successfully, but these errors were encountered: