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 · 1 comment
Open

dyn AsyncFn generates many independent errors #132713

cramertj opened this issue Nov 6, 2024 · 1 comment
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 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
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
@cramertj
Copy link
Member Author

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

cramertj added a commit to cramertj/rust that referenced this issue Nov 23, 2024
This CL makes a number of small changes to dyn compatibility errors:
- "object safety" has been renamed to "dyn-compatibility" throughout
- "Convert to enum" suggestions are no longer generated when there
  exists a type-generic impl of the trait or an impl for `dyn OtherTrait`
- Several error messages are reorganized for user readability

Additionally, the dyn compatibility error creation code has been
split out into functions.

cc rust-lang#132713
cc rust-lang#133267
cramertj added a commit to cramertj/rust that referenced this issue Nov 23, 2024
This CL makes a number of small changes to dyn compatibility errors:
- "object safety" has been renamed to "dyn-compatibility" throughout
- "Convert to enum" suggestions are no longer generated when there
  exists a type-generic impl of the trait or an impl for `dyn OtherTrait`
- Several error messages are reorganized for user readability

Additionally, the dyn compatibility error creation code has been
split out into functions.

cc rust-lang#132713
cc rust-lang#133267
cramertj added a commit to cramertj/rust that referenced this issue Nov 26, 2024
This CL makes a number of small changes to dyn compatibility errors:
- "object safety" has been renamed to "dyn-compatibility" throughout
- "Convert to enum" suggestions are no longer generated when there
  exists a type-generic impl of the trait or an impl for `dyn OtherTrait`
- Several error messages are reorganized for user readability

Additionally, the dyn compatibility error creation code has been
split out into functions.

cc rust-lang#132713
cc rust-lang#133267
cramertj added a commit to cramertj/rust that referenced this issue Nov 26, 2024
This CL makes a number of small changes to dyn compatibility errors:
- "object safety" has been renamed to "dyn-compatibility" throughout
- "Convert to enum" suggestions are no longer generated when there
  exists a type-generic impl of the trait or an impl for `dyn OtherTrait`
- Several error messages are reorganized for user readability

Additionally, the dyn compatibility error creation code has been
split out into functions.

cc rust-lang#132713
cc rust-lang#133267
cramertj added a commit to cramertj/rust that referenced this issue Nov 27, 2024
This CL makes a number of small changes to dyn compatibility errors:
- "object safety" has been renamed to "dyn-compatibility" throughout
- "Convert to enum" suggestions are no longer generated when there
  exists a type-generic impl of the trait or an impl for `dyn OtherTrait`
- Several error messages are reorganized for user readability

Additionally, the dyn compatibility error creation code has been
split out into functions.

cc rust-lang#132713
cc rust-lang#133267
cramertj added a commit to cramertj/rust that referenced this issue Dec 2, 2024
This CL makes a number of small changes to dyn compatibility errors:
- "object safety" has been renamed to "dyn-compatibility" throughout
- "Convert to enum" suggestions are no longer generated when there
  exists a type-generic impl of the trait or an impl for `dyn OtherTrait`
- Several error messages are reorganized for user readability

Additionally, the dyn compatibility error creation code has been
split out into functions.

cc rust-lang#132713
cc rust-lang#133267
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