Skip to content

Commit 97423d3

Browse files
committed
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`.
1 parent 0da281b commit 97423d3

19 files changed

+530
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// edition: 2021
2+
3+
#![feature(async_fn_in_trait)]
4+
#![allow(incomplete_features)]
5+
6+
use std::fmt::Debug;
7+
8+
trait MyTrait<'a, 'b, T> where Self: 'a, T: Debug + Sized + 'b {
9+
type MyAssoc;// = (&'a T, &'b U);
10+
11+
async fn foo(&'a self, key: &'b T) -> Self::MyAssoc;
12+
}
13+
14+
impl<'a, 'b, T: Debug + Sized + 'b, U: 'a> MyTrait<'a, 'b, T> for U {
15+
type MyAssoc = (&'a U, &'b T);
16+
17+
async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) {
18+
(self, key)
19+
}
20+
}
21+
//~^^^^ ERROR cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
22+
//~| ERROR cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements
23+
24+
fn main() {}
Lines changed: 57 additions & 0 deletions
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:17: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:14: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:17: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:17: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:17: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:14: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:17: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:17: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`.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// edition: 2021
2+
3+
#![feature(async_fn_in_trait)]
4+
#![allow(incomplete_features)]
5+
6+
use std::future::Future;
7+
8+
trait MyTrait {
9+
type Fut<'a>: Future<Output = i32>
10+
where
11+
Self: 'a;
12+
13+
async fn foo(&self) -> Self::Fut<'a>;
14+
//~^ ERROR use of undeclared lifetime name `'a`
15+
//~| ERROR the parameter type `Self` may not live long enough
16+
}
17+
18+
impl MyTrait for i32 {
19+
type Fut<'a> = impl Future + 'a
20+
where
21+
Self: 'a;
22+
//~^^^ ERROR `impl Trait` in type aliases is unstable
23+
24+
fn foo<'a>(&'a self) -> Self::Fut<'a> {
25+
async {
26+
*self
27+
}
28+
}
29+
}
30+
31+
fn main() {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
error[E0261]: use of undeclared lifetime name `'a`
2+
--> $DIR/async-associated-types2-desugared.rs:13:38
3+
|
4+
LL | async fn foo(&self) -> Self::Fut<'a>;
5+
| ^^ undeclared lifetime
6+
|
7+
help: consider introducing lifetime `'a` here
8+
|
9+
LL | async fn foo<'a>(&self) -> Self::Fut<'a>;
10+
| ++++
11+
help: consider introducing lifetime `'a` here
12+
|
13+
LL | trait MyTrait<'a> {
14+
| ++++
15+
16+
error[E0658]: `impl Trait` in type aliases is unstable
17+
--> $DIR/async-associated-types2-desugared.rs:19:20
18+
|
19+
LL | type Fut<'a> = impl Future + 'a
20+
| ^^^^^^^^^^^^^^^^
21+
|
22+
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
23+
= help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable
24+
25+
error[E0310]: the parameter type `Self` may not live long enough
26+
--> $DIR/async-associated-types2-desugared.rs:13:28
27+
|
28+
LL | async fn foo(&self) -> Self::Fut<'a>;
29+
| ^^^^^^^^^^^^^
30+
|
31+
= help: consider adding an explicit lifetime bound `Self: 'static`...
32+
= note: ...so that the type `Self` will meet its required lifetime bounds...
33+
note: ...that is required by this bound
34+
--> $DIR/async-associated-types2-desugared.rs:11:15
35+
|
36+
LL | Self: 'a;
37+
| ^^
38+
39+
error: aborting due to 3 previous errors
40+
41+
Some errors have detailed explanations: E0261, E0310, E0658.
42+
For more information about an error, try `rustc --explain E0261`.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// edition: 2021
2+
3+
#![feature(async_fn_in_trait)]
4+
#![allow(incomplete_features)]
5+
6+
use std::future::Future;
7+
8+
trait MyTrait {
9+
type Fut<'a>: Future<Output = i32>
10+
where
11+
Self: 'a;
12+
13+
fn foo(&self) -> Self::Fut<'a>;
14+
//~^ ERROR use of undeclared lifetime name `'a`
15+
}
16+
17+
impl MyTrait for i32 {
18+
type Fut<'a> = impl Future + 'a
19+
where
20+
Self: 'a;
21+
//~^^^ ERROR `impl Trait` in type aliases is unstable
22+
//~| ERROR expected `<i32 as MyTrait>::Fut<'a>` to be a future that resolves to `i32`, but it resolves to `<<i32 as MyTrait>::Fut<'a> as Future>::Output`
23+
24+
fn foo<'a>(&'a self) -> Self::Fut<'a> {
25+
//~^ ERROR `impl` item signature doesn't match `trait` item signature
26+
async {
27+
*self
28+
}
29+
}
30+
}
31+
32+
fn main() {}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
error[E0261]: use of undeclared lifetime name `'a`
2+
--> $DIR/async-associated-types2.rs:13:32
3+
|
4+
LL | fn foo(&self) -> Self::Fut<'a>;
5+
| ^^ undeclared lifetime
6+
|
7+
help: consider introducing lifetime `'a` here
8+
|
9+
LL | fn foo<'a>(&self) -> Self::Fut<'a>;
10+
| ++++
11+
help: consider introducing lifetime `'a` here
12+
|
13+
LL | trait MyTrait<'a> {
14+
| ++++
15+
16+
error[E0658]: `impl Trait` in type aliases is unstable
17+
--> $DIR/async-associated-types2.rs:18:20
18+
|
19+
LL | type Fut<'a> = impl Future + 'a
20+
| ^^^^^^^^^^^^^^^^
21+
|
22+
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
23+
= help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable
24+
25+
error[E0271]: expected `<i32 as MyTrait>::Fut<'a>` to be a future that resolves to `i32`, but it resolves to `<<i32 as MyTrait>::Fut<'a> as Future>::Output`
26+
--> $DIR/async-associated-types2.rs:18:20
27+
|
28+
LL | type Fut<'a> = impl Future + 'a
29+
| ^^^^^^^^^^^^^^^^ expected `i32`, found associated type
30+
|
31+
= note: expected type `i32`
32+
found associated type `<<i32 as MyTrait>::Fut<'a> as Future>::Output`
33+
note: required by a bound in `MyTrait::Fut`
34+
--> $DIR/async-associated-types2.rs:9:26
35+
|
36+
LL | type Fut<'a>: Future<Output = i32>
37+
| ^^^^^^^^^^^^ required by this bound in `MyTrait::Fut`
38+
help: consider constraining the associated type `<<i32 as MyTrait>::Fut<'a> as Future>::Output` to `i32`
39+
|
40+
LL | type Fut<'a> = impl Future<Output = i32> + 'a
41+
| ++++++++++++++
42+
43+
error: `impl` item signature doesn't match `trait` item signature
44+
--> $DIR/async-associated-types2.rs:24:5
45+
|
46+
LL | fn foo(&self) -> Self::Fut<'a>;
47+
| ------------------------------- expected `fn(&'1 i32) -> <i32 as MyTrait>::Fut<'static>`
48+
...
49+
LL | fn foo<'a>(&'a self) -> Self::Fut<'a> {
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 i32) -> <i32 as MyTrait>::Fut<'1>`
51+
|
52+
= note: expected `fn(&'1 i32) -> <i32 as MyTrait>::Fut<'static>`
53+
found `fn(&'1 i32) -> <i32 as MyTrait>::Fut<'1>`
54+
help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
55+
--> $DIR/async-associated-types2.rs:13:22
56+
|
57+
LL | fn foo(&self) -> Self::Fut<'a>;
58+
| ^^^^ consider borrowing this type parameter in the trait
59+
60+
error: aborting due to 4 previous errors
61+
62+
Some errors have detailed explanations: E0261, E0271, E0658.
63+
For more information about an error, try `rustc --explain E0261`.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}
10+
11+
impl MyTrait for i32 {
12+
async fn foo(&self) -> i32 {
13+
*self
14+
}
15+
}
16+
17+
fn main() {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// edition: 2021
2+
3+
#![feature(async_fn_in_trait)]
4+
#![allow(incomplete_features)]
5+
6+
use std::fmt::Debug;
7+
use std::hash::Hash;
8+
9+
trait MyTrait<T, U> {
10+
async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
11+
}
12+
//~^^ ERROR the parameter type `U` may not live long enough
13+
//~| ERROR the parameter type `T` may not live long enough
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() {}
Lines changed: 37 additions & 0 deletions
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:10: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:10: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:10: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:10: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:10: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:10: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`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// edition: 2021
2+
3+
#![feature(async_fn_in_trait)]
4+
#![allow(incomplete_features)]
5+
6+
trait MyTrait<T, U> {
7+
async fn foo(&self) -> &(T, U);
8+
}
9+
//~^^ ERROR the parameter type `U` may not live long enough
10+
//~| ERROR the parameter type `T` may not live long enough
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() {}
Lines changed: 37 additions & 0 deletions
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:7: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:7: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:7: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:7: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:7: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:7: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`.

0 commit comments

Comments
 (0)