forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure async trait impls are async (or otherwise return an opaque type)
As a workaround for the full `#[refine]` semantics not being implemented yet, forbit returning a concrete future type like `Box<dyn Future>` or a manually implemented Future. `-> impl Future` is still permitted; while that can also cause accidental refinement, that's behind a different feature gate (`return_position_impl_trait_in_trait`) and that problem exists regardless of whether the trait method is async, so will have to be solved more generally. Fixes rust-lang#102745
- Loading branch information
1 parent
b70baa4
commit da98ef9
Showing
12 changed files
with
146 additions
and
25 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
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
11 changes: 11 additions & 0 deletions
11
src/test/ui/async-await/in-trait/async-example-desugared-boxed.stderr
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,11 @@ | ||
error: method `foo` should be async because the method from the trait is async | ||
--> $DIR/async-example-desugared-boxed.rs:15:5 | ||
| | ||
LL | async fn foo(&self) -> i32; | ||
| --------------------------- required because the trait method is async | ||
... | ||
LL | fn foo(&self) -> Pin<Box<dyn Future<Output = i32> + '_>> { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
37 changes: 37 additions & 0 deletions
37
src/test/ui/async-await/in-trait/async-example-desugared-extra.rs
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 @@ | ||
// check-pass | ||
// edition: 2021 | ||
|
||
#![feature(async_fn_in_trait)] | ||
#![feature(return_position_impl_trait_in_trait)] | ||
#![allow(incomplete_features)] | ||
|
||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::task::Poll; | ||
|
||
trait MyTrait { | ||
async fn foo(&self) -> i32; | ||
} | ||
|
||
#[derive(Clone)] | ||
struct MyFuture(i32); | ||
|
||
impl Future for MyFuture { | ||
type Output = i32; | ||
fn poll( | ||
self: Pin<&mut Self>, | ||
_: &mut std::task::Context<'_>, | ||
) -> Poll<<Self as Future>::Output> { | ||
Poll::Ready(self.0) | ||
} | ||
} | ||
|
||
impl MyTrait for i32 { | ||
// FIXME: this should eventually require `#[refine]` to compile, because it also provides | ||
// `Clone`. | ||
fn foo(&self) -> impl Future<Output = i32> + Clone { | ||
MyFuture(*self) | ||
} | ||
} | ||
|
||
fn main() {} |
29 changes: 29 additions & 0 deletions
29
src/test/ui/async-await/in-trait/async-example-desugared-manual.rs
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,29 @@ | ||
// edition: 2021 | ||
|
||
#![feature(async_fn_in_trait)] | ||
#![feature(return_position_impl_trait_in_trait)] | ||
#![allow(incomplete_features)] | ||
|
||
use std::future::Future; | ||
use std::task::Poll; | ||
|
||
trait MyTrait { | ||
async fn foo(&self) -> i32; | ||
} | ||
|
||
struct MyFuture; | ||
impl Future for MyFuture { | ||
type Output = i32; | ||
fn poll(self: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>) -> Poll<Self::Output> { | ||
Poll::Ready(0) | ||
} | ||
} | ||
|
||
impl MyTrait for u32 { | ||
fn foo(&self) -> MyFuture { | ||
//~^ ERROR method `foo` should be async | ||
MyFuture | ||
} | ||
} | ||
|
||
fn main() {} |
11 changes: 11 additions & 0 deletions
11
src/test/ui/async-await/in-trait/async-example-desugared-manual.stderr
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,11 @@ | ||
error: method `foo` should be async because the method from the trait is async | ||
--> $DIR/async-example-desugared-manual.rs:23:5 | ||
| | ||
LL | async fn foo(&self) -> i32; | ||
| --------------------------- required because the trait method is async | ||
... | ||
LL | fn foo(&self) -> MyFuture { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
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 |
---|---|---|
@@ -1,17 +1,11 @@ | ||
error[E0277]: `i32` is not a future | ||
--> $DIR/fn-not-async-err.rs:11:22 | ||
| | ||
LL | fn foo(&self) -> i32 { | ||
| ^^^ `i32` is not a future | ||
| | ||
= help: the trait `Future` is not implemented for `i32` | ||
= note: i32 must be a future or must implement `IntoFuture` to be awaited | ||
note: required by a bound in `MyTrait::foo::{opaque#0}` | ||
--> $DIR/fn-not-async-err.rs:7:28 | ||
error: method `foo` should be async because the method from the trait is async | ||
--> $DIR/fn-not-async-err.rs:11:5 | ||
| | ||
LL | async fn foo(&self) -> i32; | ||
| ^^^ required by this bound in `MyTrait::foo::{opaque#0}` | ||
| --------------------------- required because the trait method is async | ||
... | ||
LL | fn foo(&self) -> i32 { | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
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