Skip to content

Trying to pass an AsyncFn to a function expecting a Send future suggests unstable fixes #137624

Closed
@elichai

Description

@elichai

Code

fn spawn<F: AsyncFn() + Send>(f: F) {
    check_send(f());
}

fn check_send<T: Send>(_: T) {}

Current output

error[E0277]: `<F as AsyncFnMut<()>>::CallRefFuture<'_>` cannot be sent between threads safely
 --> src/lib.rs:2:16
  |
2 |     check_send(f());
  |     ---------- ^^^ `<F as AsyncFnMut<()>>::CallRefFuture<'_>` cannot be sent between threads safely
  |     |
  |     required by a bound introduced by this call
  |
  = help: the trait `Send` is not implemented for `<F as AsyncFnMut<()>>::CallRefFuture<'_>`
note: required by a bound in `check_send`
 --> src/lib.rs:5:18
  |
5 | fn check_send<T: Send>(_: T) {}
  |                  ^^^^ required by this bound in `check_send`
help: consider further restricting the associated type
  |
1 | fn spawn<F: AsyncFn() + Send>(f: F) where <F as AsyncFnMut<()>>::CallRefFuture<'_>: Send {
  |                                     ++++++++++++++++++++++++++++++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 1 previous error

Desired output

Say that it's currently not possible to specify and link to a github issue tracking this feature

Rationale and extra context

No response

Other cases

Rust Version

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: aarch64-apple-darwin
release: 1.85.0
LLVM version: 19.1.7

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-closures`async || {}`A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions