Rollup merge of #102642 - bryangarza:afit-tests, r=compiler-errors

Add tests for static async functions in traits

This patch adds test cases for AFIT, the majority of which are currently expected to run as `check-fail`.

---

Note: I grabbed the cases from https://hackmd.io/SwRcXCiWQV-WRJ4BYs53fA

Also, I'm not sure if the `async-associated-types2` and `async-associated-types2-desugared` are correct, I modified them a bit from the examples in the HackMD.
This commit is contained in:
Matthias Krüger 2022-10-28 07:06:41 +02:00 committed by GitHub
commit c404092084
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 579 additions and 0 deletions

View File

@ -0,0 +1,24 @@
// check-fail
// known-bug: #102682
// edition: 2021
#![feature(async_fn_in_trait)]
#![allow(incomplete_features)]
use std::fmt::Debug;
trait MyTrait<'a, 'b, T> where Self: 'a, T: Debug + Sized + 'b {
type MyAssoc;
async fn foo(&'a self, key: &'b T) -> Self::MyAssoc;
}
impl<'a, 'b, T: Debug + Sized + 'b, U: 'a> MyTrait<'a, 'b, T> for U {
type MyAssoc = (&'a U, &'b T);
async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
(self, key)
}
}
fn main() {}

View File

@ -0,0 +1,57 @@
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> $DIR/async-associated-types.rs:19:43
|
LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
| ^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'a` as defined here...
--> $DIR/async-associated-types.rs:16:6
|
LL | impl<'a, 'b, T: Debug + Sized + 'b, U: 'a> MyTrait<'a, 'b, T> for U {
| ^^
note: ...so that the types are compatible
--> $DIR/async-associated-types.rs:19:43
|
LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
| ^^^^^^^^^^^^^^
= note: expected `(&'a U, &'b T)`
found `(&U, &T)`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the types are compatible
--> $DIR/async-associated-types.rs:19:43
|
LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
| ^^^^^^^^^^^^^^
= note: expected `MyTrait<'static, 'static, T>`
found `MyTrait<'_, '_, T>`
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements
--> $DIR/async-associated-types.rs:19:43
|
LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
| ^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'b` as defined here...
--> $DIR/async-associated-types.rs:16:10
|
LL | impl<'a, 'b, T: Debug + Sized + 'b, U: 'a> MyTrait<'a, 'b, T> for U {
| ^^
note: ...so that the types are compatible
--> $DIR/async-associated-types.rs:19:43
|
LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
| ^^^^^^^^^^^^^^
= note: expected `(&'a U, &'b T)`
found `(&U, &T)`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the types are compatible
--> $DIR/async-associated-types.rs:19:43
|
LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
| ^^^^^^^^^^^^^^
= note: expected `MyTrait<'static, 'static, T>`
found `MyTrait<'_, '_, T>`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0495`.

View File

@ -0,0 +1,30 @@
// check-pass
// edition: 2021
#![feature(async_fn_in_trait)]
#![feature(type_alias_impl_trait)]
#![allow(incomplete_features)]
use std::future::Future;
trait MyTrait {
type Fut<'a>: Future<Output = i32>
where
Self: 'a;
fn foo<'a>(&'a self) -> Self::Fut<'a>;
}
impl MyTrait for i32 {
type Fut<'a> = impl Future<Output = i32> + 'a
where
Self: 'a;
fn foo<'a>(&'a self) -> Self::Fut<'a> {
async {
*self
}
}
}
fn main() {}

View File

@ -0,0 +1,21 @@
// 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;
trait MyTrait {
fn foo(&self) -> Pin<Box<dyn Future<Output = i32> + '_>>;
}
impl MyTrait for i32 {
async fn foo(&self) -> i32 {
//~^ ERROR method `foo` has an incompatible type for trait
*self
}
}
fn main() {}

View File

@ -0,0 +1,17 @@
error[E0053]: method `foo` has an incompatible type for trait
--> $DIR/async-example-desugared-boxed-in-trait.rs:15:28
|
LL | async fn foo(&self) -> i32 {
| ^^^ expected struct `Pin`, found opaque type
|
note: type in trait
--> $DIR/async-example-desugared-boxed-in-trait.rs:11:22
|
LL | fn foo(&self) -> Pin<Box<dyn Future<Output = i32> + '_>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: expected fn pointer `fn(&i32) -> Pin<Box<dyn Future<Output = i32>>>`
found fn pointer `fn(&i32) -> impl Future<Output = i32>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0053`.

View File

@ -0,0 +1,24 @@
// 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;
trait MyTrait {
async fn foo(&self) -> i32;
}
impl MyTrait for i32 {
// This will break once a PR that implements #102745 is merged
fn foo(&self) -> Pin<Box<dyn Future<Output = i32> + '_>> {
Box::pin(async {
*self
})
}
}
fn main() {}

View File

@ -0,0 +1,21 @@
// check-pass
// edition: 2021
#![feature(async_fn_in_trait)]
#![feature(return_position_impl_trait_in_trait)]
#![allow(incomplete_features)]
use std::future::Future;
trait MyTrait {
fn foo(&self) -> impl Future<Output = i32> + '_;
}
impl MyTrait for i32 {
// This will break once a PR that implements #102745 is merged
async fn foo(&self) -> i32 {
*self
}
}
fn main() {}

View File

@ -0,0 +1,23 @@
// check-pass
// edition: 2021
#![feature(async_fn_in_trait)]
#![feature(return_position_impl_trait_in_trait)]
#![allow(incomplete_features)]
use std::future::Future;
trait MyTrait {
async fn foo(&self) -> i32;
}
impl MyTrait for i32 {
// This will break once a PR that implements #102745 is merged
fn foo(&self) -> impl Future<Output = i32> + '_ {
async {
*self
}
}
}
fn main() {}

View File

@ -0,0 +1,32 @@
// check-pass
// edition: 2021
#![feature(async_fn_in_trait)]
#![allow(incomplete_features)]
trait MyTrait {
async fn foo(&self) -> i32;
async fn bar(&self) -> i32;
}
impl MyTrait for i32 {
async fn foo(&self) -> i32 {
*self
}
async fn bar(&self) -> i32 {
self.foo().await
}
}
fn main() {
let x = 5;
// Calling from non-async context
let _ = x.foo();
let _ = x.bar();
// Calling from async block in non-async context
async {
let _: i32 = x.foo().await;
let _: i32 = x.bar().await;
};
}

View File

@ -0,0 +1,21 @@
// check-fail
// known-bug: #102682
// edition: 2021
#![feature(async_fn_in_trait)]
#![allow(incomplete_features)]
use std::fmt::Debug;
use std::hash::Hash;
trait MyTrait<T, U> {
async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
}
impl<T, U> MyTrait<T, U> for (T, U) {
async fn foo(&self) -> &(T, U) {
self
}
}
fn main() {}

View File

@ -0,0 +1,37 @@
error[E0311]: the parameter type `U` may not live long enough
--> $DIR/async-generics-and-bounds.rs:12:28
|
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
| ^^^^^^^
|
note: the parameter type `U` must be valid for the anonymous lifetime as defined here...
--> $DIR/async-generics-and-bounds.rs:12:18
|
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
| ^
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
--> $DIR/async-generics-and-bounds.rs:12:28
|
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
| ^^^^^^^
error[E0311]: the parameter type `T` may not live long enough
--> $DIR/async-generics-and-bounds.rs:12:28
|
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
| ^^^^^^^
|
note: the parameter type `T` must be valid for the anonymous lifetime as defined here...
--> $DIR/async-generics-and-bounds.rs:12:18
|
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
| ^
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
--> $DIR/async-generics-and-bounds.rs:12:28
|
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
| ^^^^^^^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0311`.

View File

@ -0,0 +1,18 @@
// check-fail
// known-bug: #102682
// edition: 2021
#![feature(async_fn_in_trait)]
#![allow(incomplete_features)]
trait MyTrait<T, U> {
async fn foo(&self) -> &(T, U);
}
impl<T, U> MyTrait<T, U> for (T, U) {
async fn foo(&self) -> &(T, U) {
self
}
}
fn main() {}

View File

@ -0,0 +1,37 @@
error[E0311]: the parameter type `U` may not live long enough
--> $DIR/async-generics.rs:9:28
|
LL | async fn foo(&self) -> &(T, U);
| ^^^^^^^
|
note: the parameter type `U` must be valid for the anonymous lifetime as defined here...
--> $DIR/async-generics.rs:9:18
|
LL | async fn foo(&self) -> &(T, U);
| ^
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
--> $DIR/async-generics.rs:9:28
|
LL | async fn foo(&self) -> &(T, U);
| ^^^^^^^
error[E0311]: the parameter type `T` may not live long enough
--> $DIR/async-generics.rs:9:28
|
LL | async fn foo(&self) -> &(T, U);
| ^^^^^^^
|
note: the parameter type `T` must be valid for the anonymous lifetime as defined here...
--> $DIR/async-generics.rs:9:18
|
LL | async fn foo(&self) -> &(T, U);
| ^
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
--> $DIR/async-generics.rs:9:28
|
LL | async fn foo(&self) -> &(T, U);
| ^^^^^^^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0311`.

View File

@ -0,0 +1,20 @@
// check-fail
// known-bug: #102682
// edition: 2021
#![feature(async_fn_in_trait)]
#![allow(incomplete_features)]
use std::fmt::Debug;
trait MyTrait<'a, 'b, T> {
async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T) where T: Debug + Sized;
}
impl<'a, 'b, T, U> MyTrait<'a, 'b, T> for U {
async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
(self, key)
}
}
fn main() {}

View File

@ -0,0 +1,23 @@
error[E0309]: the parameter type `Self` may not live long enough
--> $DIR/async-lifetimes-and-bounds.rs:11:43
|
LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T) where T: Debug + Sized;
| ^^^^^^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `Self: 'a`...
= note: ...so that the reference type `&'a Self` does not outlive the data it points at
error[E0309]: the parameter type `T` may not live long enough
--> $DIR/async-lifetimes-and-bounds.rs:11:43
|
LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T) where T: Debug + Sized;
| ^^^^^^^^^^^^^^^^^ ...so that the reference type `&'b T` does not outlive the data it points at
|
help: consider adding an explicit lifetime bound...
|
LL | trait MyTrait<'a, 'b, T: 'b> {
| ++++
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0309`.

View File

@ -0,0 +1,18 @@
// check-fail
// known-bug: #102682
// edition: 2021
#![feature(async_fn_in_trait)]
#![allow(incomplete_features)]
trait MyTrait<'a, 'b, T> {
async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T);
}
impl<'a, 'b, T, U> MyTrait<'a, 'b, T> for U {
async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
(self, key)
}
}
fn main() {}

View File

@ -0,0 +1,23 @@
error[E0309]: the parameter type `Self` may not live long enough
--> $DIR/async-lifetimes.rs:9:43
|
LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T);
| ^^^^^^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `Self: 'a`...
= note: ...so that the reference type `&'a Self` does not outlive the data it points at
error[E0309]: the parameter type `T` may not live long enough
--> $DIR/async-lifetimes.rs:9:43
|
LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T);
| ^^^^^^^^^^^^^^^^^ ...so that the reference type `&'b T` does not outlive the data it points at
|
help: consider adding an explicit lifetime bound...
|
LL | trait MyTrait<'a, 'b, T: 'b> {
| ++++
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0309`.

View File

@ -0,0 +1,21 @@
// edition: 2021
#![feature(async_fn_in_trait)]
#![allow(incomplete_features)]
trait MyTrait<T> {
async fn foo_recursive(&self, n: usize) -> T;
}
impl<T> MyTrait<T> for T where T: Copy {
async fn foo_recursive(&self, n: usize) -> T {
//~^ ERROR recursion in an `async fn` requires boxing
if n > 0 {
self.foo_recursive(n - 1).await
} else {
*self
}
}
}
fn main() {}

View File

@ -0,0 +1,12 @@
error[E0733]: recursion in an `async fn` requires boxing
--> $DIR/async-recursive-generic.rs:11:48
|
LL | async fn foo_recursive(&self, n: usize) -> T {
| ^ recursive `async fn`
|
= note: a recursive `async fn` must be rewritten to return a boxed `dyn Future`
= note: consider using the `async_recursion` crate: https://crates.io/crates/async_recursion
error: aborting due to previous error
For more information about this error, try `rustc --explain E0733`.

View File

@ -0,0 +1,21 @@
// edition: 2021
#![feature(async_fn_in_trait)]
#![allow(incomplete_features)]
trait MyTrait {
async fn foo_recursive(&self, n: usize) -> i32;
}
impl MyTrait for i32 {
async fn foo_recursive(&self, n: usize) -> i32 {
//~^ ERROR recursion in an `async fn` requires boxing
if n > 0 {
self.foo_recursive(n - 1).await
} else {
*self
}
}
}
fn main() {}

View File

@ -0,0 +1,12 @@
error[E0733]: recursion in an `async fn` requires boxing
--> $DIR/async-recursive.rs:11:48
|
LL | async fn foo_recursive(&self, n: usize) -> i32 {
| ^^^ recursive `async fn`
|
= note: a recursive `async fn` must be rewritten to return a boxed `dyn Future`
= note: consider using the `async_recursion` crate: https://crates.io/crates/async_recursion
error: aborting due to previous error
For more information about this error, try `rustc --explain E0733`.

View File

@ -0,0 +1,17 @@
// edition: 2021
#![feature(async_fn_in_trait)]
#![allow(incomplete_features)]
trait MyTrait {
async fn foo(&self) -> i32;
}
impl MyTrait for i32 {
fn foo(&self) -> i32 {
//~^ ERROR: `i32` is not a future [E0277]
*self
}
}
fn main() {}

View File

@ -0,0 +1,17 @@
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
|
LL | async fn foo(&self) -> i32;
| ^^^ required by this bound in `MyTrait::foo::{opaque#0}`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.

View File

@ -0,0 +1,21 @@
// edition: 2021
#![feature(async_fn_in_trait)]
#![allow(incomplete_features)]
use std::future::Future;
trait MyTrait {
async fn foo(&self) -> i32;
}
impl MyTrait for i32 {
fn foo(&self) -> impl Future<Output = i32> {
//~^ ERROR `impl Trait` only allowed in function and inherent method return types, not in `impl` method return [E0562]
async {
*self
}
}
}
fn main() {}

View File

@ -0,0 +1,12 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `impl` method return
--> $DIR/fn-not-async-err2.rs:13:22
|
LL | fn foo(&self) -> impl Future<Output = i32> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #91611 <https://github.com/rust-lang/rust/issues/91611> for more information
= help: add `#![feature(return_position_impl_trait_in_trait)]` to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0562`.