rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.83k stars 12.77k forks source link

`dyn AsyncFn` generates many independent errors #132713

Open cramertj opened 2 weeks ago

cramertj commented 2 weeks ago

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 commented 1 day ago

Note: this issue generalizes somewhat to other GAT-containing traits, for example:

trait Super {
    type Assoc<'a>;
}

trait Child: Super {}

fn take_dyn(_: &dyn Child) {}

reports

error[E0038]: the trait `Super` cannot be made into an object
 --> src/lib.rs:7:21
  |
7 | fn take_dyn(_: &dyn Child) {}
  |                     ^^^^^ `Super` cannot be made into an object
  |
note: for a trait to be "object safe" 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>
 --> src/lib.rs:2:10
  |
1 | trait Super {
  |       ----- this trait cannot be made into an object...
2 |     type Assoc<'a>;
  |          ^^^^^ ...because it contains the generic associated type `Assoc`
  = help: consider moving `Assoc` to another trait

error[E0038]: the trait `Child` cannot be made into an object
 --> src/lib.rs:7:17
  |
7 | fn take_dyn(_: &dyn Child) {}
  |                 ^^^^^^^^^ `Child` cannot be made into an object
  |
note: for a trait to be "object safe" 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>
 --> src/lib.rs:2:10
  |
2 |     type Assoc<'a>;
  |          ^^^^^ ...because it contains the generic associated type `Assoc`
...
5 | trait Child: Super {}
  |       ----- this trait cannot be made into an object...
  = help: consider moving `Assoc` to another trait

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