Skip to content

Commit c404092

Browse files
authored
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.
2 parents cdd7afe + bfdefdb commit c404092

25 files changed

+579
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// check-fail
2+
// known-bug: #102682
3+
// edition: 2021
4+
5+
#![feature(async_fn_in_trait)]
6+
#![allow(incomplete_features)]
7+
8+
use std::fmt::Debug;
9+
10+
trait MyTrait<'a, 'b, T> where Self: 'a, T: Debug + Sized + 'b {
11+
type MyAssoc;
12+
13+
async fn foo(&'a self, key: &'b T) -> Self::MyAssoc;
14+
}
15+
16+
impl<'a, 'b, T: Debug + Sized + 'b, U: 'a> MyTrait<'a, 'b, T> for U {
17+
type MyAssoc = (&'a U, &'b T);
18+
19+
async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
20+
(self, key)
21+
}
22+
}
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
2+
--> $DIR/async-associated-types.rs:19:43
3+
|
4+
LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
5+
| ^^^^^^^^^^^^^^
6+
|
7+
note: first, the lifetime cannot outlive the lifetime `'a` as defined here...
8+
--> $DIR/async-associated-types.rs:16:6
9+
|
10+
LL | impl<'a, 'b, T: Debug + Sized + 'b, U: 'a> MyTrait<'a, 'b, T> for U {
11+
| ^^
12+
note: ...so that the types are compatible
13+
--> $DIR/async-associated-types.rs:19:43
14+
|
15+
LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
16+
| ^^^^^^^^^^^^^^
17+
= note: expected `(&'a U, &'b T)`
18+
found `(&U, &T)`
19+
= note: but, the lifetime must be valid for the static lifetime...
20+
note: ...so that the types are compatible
21+
--> $DIR/async-associated-types.rs:19:43
22+
|
23+
LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
24+
| ^^^^^^^^^^^^^^
25+
= note: expected `MyTrait<'static, 'static, T>`
26+
found `MyTrait<'_, '_, T>`
27+
28+
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements
29+
--> $DIR/async-associated-types.rs:19:43
30+
|
31+
LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
32+
| ^^^^^^^^^^^^^^
33+
|
34+
note: first, the lifetime cannot outlive the lifetime `'b` as defined here...
35+
--> $DIR/async-associated-types.rs:16:10
36+
|
37+
LL | impl<'a, 'b, T: Debug + Sized + 'b, U: 'a> MyTrait<'a, 'b, T> for U {
38+
| ^^
39+
note: ...so that the types are compatible
40+
--> $DIR/async-associated-types.rs:19:43
41+
|
42+
LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
43+
| ^^^^^^^^^^^^^^
44+
= note: expected `(&'a U, &'b T)`
45+
found `(&U, &T)`
46+
= note: but, the lifetime must be valid for the static lifetime...
47+
note: ...so that the types are compatible
48+
--> $DIR/async-associated-types.rs:19:43
49+
|
50+
LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
51+
| ^^^^^^^^^^^^^^
52+
= note: expected `MyTrait<'static, 'static, T>`
53+
found `MyTrait<'_, '_, T>`
54+
55+
error: aborting due to 2 previous errors
56+
57+
For more information about this error, try `rustc --explain E0495`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// check-pass
2+
// edition: 2021
3+
4+
#![feature(async_fn_in_trait)]
5+
#![feature(type_alias_impl_trait)]
6+
#![allow(incomplete_features)]
7+
8+
use std::future::Future;
9+
10+
trait MyTrait {
11+
type Fut<'a>: Future<Output = i32>
12+
where
13+
Self: 'a;
14+
15+
fn foo<'a>(&'a self) -> Self::Fut<'a>;
16+
}
17+
18+
impl MyTrait for i32 {
19+
type Fut<'a> = impl Future<Output = i32> + 'a
20+
where
21+
Self: 'a;
22+
23+
fn foo<'a>(&'a self) -> Self::Fut<'a> {
24+
async {
25+
*self
26+
}
27+
}
28+
}
29+
30+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// edition: 2021
2+
3+
#![feature(async_fn_in_trait)]
4+
#![feature(return_position_impl_trait_in_trait)]
5+
#![allow(incomplete_features)]
6+
7+
use std::future::Future;
8+
use std::pin::Pin;
9+
10+
trait MyTrait {
11+
fn foo(&self) -> Pin<Box<dyn Future<Output = i32> + '_>>;
12+
}
13+
14+
impl MyTrait for i32 {
15+
async fn foo(&self) -> i32 {
16+
//~^ ERROR method `foo` has an incompatible type for trait
17+
*self
18+
}
19+
}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0053]: method `foo` has an incompatible type for trait
2+
--> $DIR/async-example-desugared-boxed-in-trait.rs:15:28
3+
|
4+
LL | async fn foo(&self) -> i32 {
5+
| ^^^ expected struct `Pin`, found opaque type
6+
|
7+
note: type in trait
8+
--> $DIR/async-example-desugared-boxed-in-trait.rs:11:22
9+
|
10+
LL | fn foo(&self) -> Pin<Box<dyn Future<Output = i32> + '_>>;
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
= note: expected fn pointer `fn(&i32) -> Pin<Box<dyn Future<Output = i32>>>`
13+
found fn pointer `fn(&i32) -> impl Future<Output = i32>`
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0053`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// check-pass
2+
// edition: 2021
3+
4+
#![feature(async_fn_in_trait)]
5+
#![feature(return_position_impl_trait_in_trait)]
6+
#![allow(incomplete_features)]
7+
8+
use std::future::Future;
9+
use std::pin::Pin;
10+
11+
trait MyTrait {
12+
async fn foo(&self) -> i32;
13+
}
14+
15+
impl MyTrait for i32 {
16+
// This will break once a PR that implements #102745 is merged
17+
fn foo(&self) -> Pin<Box<dyn Future<Output = i32> + '_>> {
18+
Box::pin(async {
19+
*self
20+
})
21+
}
22+
}
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// check-pass
2+
// edition: 2021
3+
4+
#![feature(async_fn_in_trait)]
5+
#![feature(return_position_impl_trait_in_trait)]
6+
#![allow(incomplete_features)]
7+
8+
use std::future::Future;
9+
10+
trait MyTrait {
11+
fn foo(&self) -> impl Future<Output = i32> + '_;
12+
}
13+
14+
impl MyTrait for i32 {
15+
// This will break once a PR that implements #102745 is merged
16+
async fn foo(&self) -> i32 {
17+
*self
18+
}
19+
}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// check-pass
2+
// edition: 2021
3+
4+
#![feature(async_fn_in_trait)]
5+
#![feature(return_position_impl_trait_in_trait)]
6+
#![allow(incomplete_features)]
7+
8+
use std::future::Future;
9+
10+
trait MyTrait {
11+
async fn foo(&self) -> i32;
12+
}
13+
14+
impl MyTrait for i32 {
15+
// This will break once a PR that implements #102745 is merged
16+
fn foo(&self) -> impl Future<Output = i32> + '_ {
17+
async {
18+
*self
19+
}
20+
}
21+
}
22+
23+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// check-pass
2+
// edition: 2021
3+
4+
#![feature(async_fn_in_trait)]
5+
#![allow(incomplete_features)]
6+
7+
trait MyTrait {
8+
async fn foo(&self) -> i32;
9+
async fn bar(&self) -> i32;
10+
}
11+
12+
impl MyTrait for i32 {
13+
async fn foo(&self) -> i32 {
14+
*self
15+
}
16+
17+
async fn bar(&self) -> i32 {
18+
self.foo().await
19+
}
20+
}
21+
22+
fn main() {
23+
let x = 5;
24+
// Calling from non-async context
25+
let _ = x.foo();
26+
let _ = x.bar();
27+
// Calling from async block in non-async context
28+
async {
29+
let _: i32 = x.foo().await;
30+
let _: i32 = x.bar().await;
31+
};
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// check-fail
2+
// known-bug: #102682
3+
// edition: 2021
4+
5+
#![feature(async_fn_in_trait)]
6+
#![allow(incomplete_features)]
7+
8+
use std::fmt::Debug;
9+
use std::hash::Hash;
10+
11+
trait MyTrait<T, U> {
12+
async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
13+
}
14+
15+
impl<T, U> MyTrait<T, U> for (T, U) {
16+
async fn foo(&self) -> &(T, U) {
17+
self
18+
}
19+
}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error[E0311]: the parameter type `U` may not live long enough
2+
--> $DIR/async-generics-and-bounds.rs:12:28
3+
|
4+
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
5+
| ^^^^^^^
6+
|
7+
note: the parameter type `U` must be valid for the anonymous lifetime as defined here...
8+
--> $DIR/async-generics-and-bounds.rs:12:18
9+
|
10+
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
11+
| ^
12+
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
13+
--> $DIR/async-generics-and-bounds.rs:12:28
14+
|
15+
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
16+
| ^^^^^^^
17+
18+
error[E0311]: the parameter type `T` may not live long enough
19+
--> $DIR/async-generics-and-bounds.rs:12:28
20+
|
21+
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
22+
| ^^^^^^^
23+
|
24+
note: the parameter type `T` must be valid for the anonymous lifetime as defined here...
25+
--> $DIR/async-generics-and-bounds.rs:12:18
26+
|
27+
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
28+
| ^
29+
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
30+
--> $DIR/async-generics-and-bounds.rs:12:28
31+
|
32+
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
33+
| ^^^^^^^
34+
35+
error: aborting due to 2 previous errors
36+
37+
For more information about this error, try `rustc --explain E0311`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// check-fail
2+
// known-bug: #102682
3+
// edition: 2021
4+
5+
#![feature(async_fn_in_trait)]
6+
#![allow(incomplete_features)]
7+
8+
trait MyTrait<T, U> {
9+
async fn foo(&self) -> &(T, U);
10+
}
11+
12+
impl<T, U> MyTrait<T, U> for (T, U) {
13+
async fn foo(&self) -> &(T, U) {
14+
self
15+
}
16+
}
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error[E0311]: the parameter type `U` may not live long enough
2+
--> $DIR/async-generics.rs:9:28
3+
|
4+
LL | async fn foo(&self) -> &(T, U);
5+
| ^^^^^^^
6+
|
7+
note: the parameter type `U` must be valid for the anonymous lifetime as defined here...
8+
--> $DIR/async-generics.rs:9:18
9+
|
10+
LL | async fn foo(&self) -> &(T, U);
11+
| ^
12+
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
13+
--> $DIR/async-generics.rs:9:28
14+
|
15+
LL | async fn foo(&self) -> &(T, U);
16+
| ^^^^^^^
17+
18+
error[E0311]: the parameter type `T` may not live long enough
19+
--> $DIR/async-generics.rs:9:28
20+
|
21+
LL | async fn foo(&self) -> &(T, U);
22+
| ^^^^^^^
23+
|
24+
note: the parameter type `T` must be valid for the anonymous lifetime as defined here...
25+
--> $DIR/async-generics.rs:9:18
26+
|
27+
LL | async fn foo(&self) -> &(T, U);
28+
| ^
29+
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
30+
--> $DIR/async-generics.rs:9:28
31+
|
32+
LL | async fn foo(&self) -> &(T, U);
33+
| ^^^^^^^
34+
35+
error: aborting due to 2 previous errors
36+
37+
For more information about this error, try `rustc --explain E0311`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// check-fail
2+
// known-bug: #102682
3+
// edition: 2021
4+
5+
#![feature(async_fn_in_trait)]
6+
#![allow(incomplete_features)]
7+
8+
use std::fmt::Debug;
9+
10+
trait MyTrait<'a, 'b, T> {
11+
async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T) where T: Debug + Sized;
12+
}
13+
14+
impl<'a, 'b, T, U> MyTrait<'a, 'b, T> for U {
15+
async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
16+
(self, key)
17+
}
18+
}
19+
20+
fn main() {}

0 commit comments

Comments
 (0)