Skip to content

dyn AsyncFn generates many independent errors #132713

Open
@cramertj

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-objectsArea: trait objects, vtable layoutD-verboseDiagnostics: Too much output caused by a single piece of incorrect code.F-async_closure`#![feature(async_closure)]`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