Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dyn AsyncFn generates many independent errors #132713

Open
cramertj opened this issue Nov 6, 2024 · 0 comments · May be fixed by #133267
Open

dyn AsyncFn generates many independent errors #132713

cramertj opened this issue Nov 6, 2024 · 0 comments · May be fixed by #133267
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-objects Area: trait objects, vtable layout D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. F-async_closure `#![feature(async_closure)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@cramertj
Copy link
Member

cramertj commented Nov 6, 2024

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

@cramertj cramertj added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 6, 2024
@fmease fmease added D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. F-async_closure `#![feature(async_closure)]` A-trait-objects Area: trait objects, vtable layout labels Nov 7, 2024
@cramertj cramertj self-assigned this Nov 19, 2024
@cramertj cramertj linked a pull request Nov 20, 2024 that will close this issue
cramertj added a commit to cramertj/rust that referenced this issue Nov 20, 2024
cramertj added a commit to cramertj/rust that referenced this issue Nov 21, 2024
cramertj added a commit to cramertj/rust that referenced this issue Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-objects Area: trait objects, vtable layout D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. F-async_closure `#![feature(async_closure)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants