Skip to content
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

Add tests for static async functions in traits #102642

Merged
merged 6 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/test/ui/async-await/in-trait/async-associated-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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> 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 src/test/ui/async-await/in-trait/async-associated-types.stderr
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 src/test/ui/async-await/in-trait/async-associated-types2.rs
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() {}
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() {}
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 src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs
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() {}
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 src/test/ui/async-await/in-trait/async-example-desugared.rs
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() {}
32 changes: 32 additions & 0 deletions src/test/ui/async-await/in-trait/async-example.rs
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 src/test/ui/async-await/in-trait/async-generics-and-bounds.rs
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 src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr
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`.
18 changes: 18 additions & 0 deletions src/test/ui/async-await/in-trait/async-generics.rs
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() {}
37 changes: 37 additions & 0 deletions src/test/ui/async-await/in-trait/async-generics.stderr
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 src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs
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() {}
Loading