-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add tests for static async functions in traits #102642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
97423d3
Add tests for static async functions in traits
bryangarza 8a0ebca
Update static AFIT tests based on feedback
bryangarza 11b1439
Update tests based on feedback
bryangarza 9a05081
Add additional tests for static AFIT
bryangarza 0b3b046
Update src/test/ui/async-await/in-trait/async-example.rs
bryangarza bfdefdb
Update tests based on feedback
bryangarza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/test/ui/async-await/in-trait/async-associated-types.rs
This file contains hidden or 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,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) | ||
bryangarza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
fn main() {} |
57 changes: 57 additions & 0 deletions
57
src/test/ui/async-await/in-trait/async-associated-types.stderr
This file contains hidden or 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,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`. |
30 changes: 30 additions & 0 deletions
30
src/test/ui/async-await/in-trait/async-associated-types2.rs
This file contains hidden or 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,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() {} |
21 changes: 21 additions & 0 deletions
21
src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.rs
This file contains hidden or 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,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() {} |
17 changes: 17 additions & 0 deletions
17
src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.stderr
This file contains hidden or 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,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`. |
24 changes: 24 additions & 0 deletions
24
src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs
This file contains hidden or 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,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 { | ||
bryangarza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*self | ||
}) | ||
} | ||
} | ||
|
||
fn main() {} |
21 changes: 21 additions & 0 deletions
21
src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs
This file contains hidden or 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,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 { | ||
bryangarza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*self | ||
} | ||
} | ||
|
||
fn main() {} |
23 changes: 23 additions & 0 deletions
23
src/test/ui/async-await/in-trait/async-example-desugared.rs
This file contains hidden or 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,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> + '_ { | ||
bryangarza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
async { | ||
*self | ||
} | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains hidden or 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,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; | ||
}; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/ui/async-await/in-trait/async-generics-and-bounds.rs
This file contains hidden or 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,21 @@ | ||
// check-fail | ||
// known-bug: #102682 | ||
// edition: 2021 | ||
bryangarza marked this conversation as resolved.
Show resolved
Hide resolved
bryangarza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#![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() {} |
37 changes: 37 additions & 0 deletions
37
src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr
This file contains hidden or 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 @@ | ||
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`. |
This file contains hidden or 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,18 @@ | ||
// check-fail | ||
// known-bug: #102682 | ||
// edition: 2021 | ||
bryangarza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#![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() {} |
This file contains hidden or 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 @@ | ||
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`. |
20 changes: 20 additions & 0 deletions
20
src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs
This file contains hidden or 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,20 @@ | ||
// check-fail | ||
// known-bug: #102682 | ||
// edition: 2021 | ||
bryangarza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#![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() {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.