rust-lang / rust-lang/rust

`dyn AsyncFn` generates many independent errors

Open
#132,713 1 comment 2 reactions 1 assignee View on GitHub

@cramertj is already working on this.

Since Nov 19, 2024.

A-async-closures A-diagnostics A-dyn-trait D-verbose T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
#![feature(async_closure)]

use std::ops::AsyncFn;

async fn foo(x: &dyn AsyncFn()) {
    x().await;
}
Current output
error[E0038]: the trait `AsyncFnMut` cannot be made into an object
  --> src/lib.rs:5:22
   |
5  | async fn foo(x: &dyn AsyncFn()) {
   |                      ^^^^^^^^^ `AsyncFnMut` cannot be made into an object
   |
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
  --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/async_function.rs:30:10
   |
30 |     type CallRefFuture<'a>: Future<Output = Self::Output>
   |          ^^^^^^^^^^^^^ the trait cannot be made into an object because it contains the generic associated type `CallRefFuture`
   = help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `AsyncFnMut` for this new enum and using it instead:
             &F
             &mut F
             std::boxed::Box<F, A>

error[E0038]: the trait `AsyncFn` cannot be made into an object
  --> src/lib.rs:5:18
   |
5  | async fn foo(x: &dyn AsyncFn()) {
   |                  ^^^^^^^^^^^^^ `AsyncFn` cannot be made into an object
   |
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
  --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/async_function.rs:30:10
   |
30 |     type CallRefFuture<'a>: Future<Output = Self::Output>
   |          ^^^^^^^^^^^^^ the trait cannot be made into an object because it contains the generic associated type `CallRefFuture`
   = help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `AsyncFn` for this new enum and using it instead:
             &F
             std::boxed::Box<F, A>

error[E0038]: the trait `AsyncFn` cannot be made into an object
  --> src/lib.rs:6:5
   |
6  |     x().await;
   |     ^^^ `AsyncFn` cannot be made into an object
   |
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
  --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/async_function.rs:30:10
   |
30 |     type CallRefFuture<'a>: Future<Output = Self::Output>
   |          ^^^^^^^^^^^^^ the trait cannot be made into an object because it contains the generic associated type `CallRefFuture`
   = help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `AsyncFn` for this new enum and using it instead:
             &F
             std::boxed::Box<F, A>

error[E0038]: the trait `AsyncFn` cannot be made into an object
  --> src/lib.rs:6:9
   |
6  |     x().await;
   |         ^^^^^ `AsyncFn` cannot be made into an object
   |
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
  --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/async_function.rs:30:10
   |
30 |     type CallRefFuture<'a>: Future<Output = Self::Output>
   |          ^^^^^^^^^^^^^ the trait cannot be made into an object because it contains the generic associated type `CallRefFuture`
   = help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `AsyncFn` for this new enum and using it instead:
             &F
             std::boxed::Box<F, A>

For more information about this error, try `rustc --explain E0038`.
error: could not compile `playground` (lib) due to 4 previous errors
Desired output
error[E0038]: the trait `AsyncFn` cannot be made into an object
  --> src/lib.rs:5:22
   |
5  | async fn foo(x: &dyn AsyncFn()) {
   |                      ^^^^^^^^^ `AsyncFn` cannot be made into an object
   |
note: `async` closures are not yet usable with dynamic dispatch. See <link for more information>
error: could not compile `playground` (lib) due to 1 previous error
Rationale and extra context

The current error output spits out four related issues where only one is needed. Additionally, none provides quite the right context, and all reference implementation details of AsyncFn that users may not be concerned with.

Rust Version

2024-11-05 nightly

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.