forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shorter and more direct error for dyn AsyncFn
Fix rust-lang#132713
- Loading branch information
Showing
11 changed files
with
282 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
The `Async{Fn, FnMut, FnOnce}` traits are not yet dyn-compatible. | ||
|
||
Erroneous code example: | ||
|
||
```compile_fail,E0802,edition2018 | ||
#![feature(async_closure)] | ||
use core::ops::AsyncFn; | ||
async fn call_async_fn_twice(some_fn: &dyn AsyncFn()) { | ||
some_fn().await; | ||
some_fn().await; | ||
} | ||
``` | ||
|
||
One workaround to this issue is to use `impl Async...` instead: | ||
|
||
```edition2018 | ||
#![feature(async_closure)] | ||
use core::ops::AsyncFn; | ||
async fn call_async_fn_twice(some_fn: &impl AsyncFn()) { | ||
some_fn().await; | ||
some_fn().await; | ||
} | ||
``` | ||
|
||
This error indicates that you attempted to use `dyn AsyncFn`, | ||
`dyn AsyncFnMut`, or `dyn AsyncFnOnce`. | ||
|
||
This is not yet possible because the `Async...` traits internally return | ||
a concrete `Future` associated type. For dynamic callsites, it is impossible | ||
to know the size of the returned `Future` object since different | ||
`Async...` implementations may return differently-sized `Future` objects. | ||
|
||
This is analogous to the more general issue of creating a `dyn` type without | ||
specifying associated types, e.g. `dyn Iterator` as opposed to | ||
`dyn Iterator<Item = SomeItemType>`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -541,6 +541,7 @@ E0798: 0798, | |
E0799: 0799, | ||
E0800: 0800, | ||
E0801: 0801, | ||
E0802: 0802, | ||
); | ||
) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Test the diagnostic output (error descriptions) resulting from `dyn AsyncFn{,Mut,Once}`. | ||
//@ edition:2018 | ||
|
||
#![feature(async_closure)] | ||
|
||
use core::ops::{AsyncFn, AsyncFnMut, AsyncFnOnce}; | ||
|
||
// --- Explicit `dyn` --- | ||
|
||
fn takes_async_fn(_: &dyn AsyncFn()) {} | ||
//~^ ERROR the trait `AsyncFn` is not yet dyn-compatible | ||
|
||
fn takes_async_fn_mut(_: &mut dyn AsyncFnMut()) {} | ||
//~^ ERROR the trait `AsyncFnMut` is not yet dyn-compatible | ||
|
||
fn takes_async_fn_once(_: Box<dyn AsyncFnOnce()>) {} | ||
//~^ ERROR the trait `AsyncFnOnce` is not yet dyn-compatible | ||
|
||
// --- Non-explicit `dyn` --- | ||
|
||
#[allow(bare_trait_objects)] | ||
fn takes_async_fn_implicit_dyn(_: &AsyncFn()) {} | ||
//~^ ERROR the trait `AsyncFn` is not yet dyn-compatible | ||
|
||
#[allow(bare_trait_objects)] | ||
fn takes_async_fn_mut_implicit_dyn(_: &mut AsyncFnMut()) {} | ||
//~^ ERROR the trait `AsyncFnMut` is not yet dyn-compatible | ||
|
||
#[allow(bare_trait_objects)] | ||
fn takes_async_fn_once_implicit_dyn(_: Box<AsyncFnOnce()>) {} | ||
//~^ ERROR the trait `AsyncFnOnce` is not yet dyn-compatible | ||
|
||
// --- Supertrait --- | ||
|
||
trait SubAsyncFn: AsyncFn() {} | ||
fn takes_sub_async_fn(_: &dyn SubAsyncFn) {} | ||
//~^ ERROR the trait `SubAsyncFn` cannot be made into an object | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
error[E0802]: the trait `AsyncFn` is not yet dyn-compatible | ||
--> $DIR/dyn.rs:10:23 | ||
| | ||
LL | fn takes_async_fn(_: &dyn AsyncFn()) {} | ||
| ^^^^^^^^^^^^^ `AsyncFn` is not yet dyn compatible | ||
| | ||
help: consider using an opaque type instead | ||
| | ||
LL | fn takes_async_fn(_: &impl AsyncFn()) {} | ||
| ~~~~ | ||
|
||
error[E0802]: the trait `AsyncFnMut` is not yet dyn-compatible | ||
--> $DIR/dyn.rs:13:31 | ||
| | ||
LL | fn takes_async_fn_mut(_: &mut dyn AsyncFnMut()) {} | ||
| ^^^^^^^^^^^^^^^^ `AsyncFnMut` is not yet dyn compatible | ||
| | ||
help: consider using an opaque type instead | ||
| | ||
LL | fn takes_async_fn_mut(_: &mut impl AsyncFnMut()) {} | ||
| ~~~~ | ||
|
||
error[E0802]: the trait `AsyncFnOnce` is not yet dyn-compatible | ||
--> $DIR/dyn.rs:16:31 | ||
| | ||
LL | fn takes_async_fn_once(_: Box<dyn AsyncFnOnce()>) {} | ||
| ^^^^^^^^^^^^^^^^^ `AsyncFnOnce` is not yet dyn compatible | ||
| | ||
help: consider using an opaque type instead | ||
| | ||
LL | fn takes_async_fn_once(_: Box<impl AsyncFnOnce()>) {} | ||
| ~~~~ | ||
|
||
error[E0802]: the trait `AsyncFn` is not yet dyn-compatible | ||
--> $DIR/dyn.rs:22:36 | ||
| | ||
LL | fn takes_async_fn_implicit_dyn(_: &AsyncFn()) {} | ||
| ^^^^^^^^^ `AsyncFn` is not yet dyn compatible | ||
| | ||
help: consider using an opaque type instead | ||
| | ||
LL | fn takes_async_fn_implicit_dyn(_: &impl AsyncFn()) {} | ||
| ++++ | ||
|
||
error[E0802]: the trait `AsyncFnMut` is not yet dyn-compatible | ||
--> $DIR/dyn.rs:26:44 | ||
| | ||
LL | fn takes_async_fn_mut_implicit_dyn(_: &mut AsyncFnMut()) {} | ||
| ^^^^^^^^^^^^ `AsyncFnMut` is not yet dyn compatible | ||
| | ||
help: consider using an opaque type instead | ||
| | ||
LL | fn takes_async_fn_mut_implicit_dyn(_: &mut impl AsyncFnMut()) {} | ||
| ++++ | ||
|
||
error[E0802]: the trait `AsyncFnOnce` is not yet dyn-compatible | ||
--> $DIR/dyn.rs:30:44 | ||
| | ||
LL | fn takes_async_fn_once_implicit_dyn(_: Box<AsyncFnOnce()>) {} | ||
| ^^^^^^^^^^^^^ `AsyncFnOnce` is not yet dyn compatible | ||
| | ||
help: consider using an opaque type instead | ||
| | ||
LL | fn takes_async_fn_once_implicit_dyn(_: Box<impl AsyncFnOnce()>) {} | ||
| ++++ | ||
|
||
error[E0038]: the trait `SubAsyncFn` cannot be made into an object | ||
--> $DIR/dyn.rs:36:27 | ||
| | ||
LL | fn takes_sub_async_fn(_: &dyn SubAsyncFn) {} | ||
| ^^^^^^^^^^^^^^ `SubAsyncFn` cannot be made into an object | ||
| | ||
= note: the trait cannot be made into an object because `async` function traits are not yet dyn-compatible | ||
= 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> | ||
help: consider using an opaque type instead | ||
| | ||
LL | fn takes_sub_async_fn(_: &impl SubAsyncFn) {} | ||
| ~~~~ | ||
|
||
error: aborting due to 7 previous errors | ||
|
||
Some errors have detailed explanations: E0038, E0802. | ||
For more information about an error, try `rustc --explain E0038`. |
Oops, something went wrong.