From 97423d331f5dbed3bbbf474e3b6bd2d8b7bd9af2 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Tue, 4 Oct 2022 01:27:11 +0000 Subject: [PATCH 1/6] 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`. --- .../in-trait/async-associated-types.rs | 24 +++++++ .../in-trait/async-associated-types.stderr | 57 +++++++++++++++++ .../async-associated-types2-desugared.rs | 31 +++++++++ .../async-associated-types2-desugared.stderr | 42 +++++++++++++ .../in-trait/async-associated-types2.rs | 32 ++++++++++ .../in-trait/async-associated-types2.stderr | 63 +++++++++++++++++++ .../ui/async-await/in-trait/async-example.rs | 17 +++++ .../in-trait/async-generics-and-bounds.rs | 21 +++++++ .../in-trait/async-generics-and-bounds.stderr | 37 +++++++++++ .../ui/async-await/in-trait/async-generics.rs | 18 ++++++ .../in-trait/async-generics.stderr | 37 +++++++++++ .../in-trait/async-lifetimes-and-bounds.rs | 20 ++++++ .../async-lifetimes-and-bounds.stderr | 23 +++++++ .../async-await/in-trait/async-lifetimes.rs | 18 ++++++ .../in-trait/async-lifetimes.stderr | 23 +++++++ .../async-await/in-trait/fn-not-async-err.rs | 17 +++++ .../in-trait/fn-not-async-err.stderr | 17 +++++ .../async-await/in-trait/fn-not-async-err2.rs | 21 +++++++ .../in-trait/fn-not-async-err2.stderr | 12 ++++ 19 files changed, 530 insertions(+) create mode 100644 src/test/ui/async-await/in-trait/async-associated-types.rs create mode 100644 src/test/ui/async-await/in-trait/async-associated-types.stderr create mode 100644 src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs create mode 100644 src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr create mode 100644 src/test/ui/async-await/in-trait/async-associated-types2.rs create mode 100644 src/test/ui/async-await/in-trait/async-associated-types2.stderr create mode 100644 src/test/ui/async-await/in-trait/async-example.rs create mode 100644 src/test/ui/async-await/in-trait/async-generics-and-bounds.rs create mode 100644 src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr create mode 100644 src/test/ui/async-await/in-trait/async-generics.rs create mode 100644 src/test/ui/async-await/in-trait/async-generics.stderr create mode 100644 src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs create mode 100644 src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr create mode 100644 src/test/ui/async-await/in-trait/async-lifetimes.rs create mode 100644 src/test/ui/async-await/in-trait/async-lifetimes.stderr create mode 100644 src/test/ui/async-await/in-trait/fn-not-async-err.rs create mode 100644 src/test/ui/async-await/in-trait/fn-not-async-err.stderr create mode 100644 src/test/ui/async-await/in-trait/fn-not-async-err2.rs create mode 100644 src/test/ui/async-await/in-trait/fn-not-async-err2.stderr diff --git a/src/test/ui/async-await/in-trait/async-associated-types.rs b/src/test/ui/async-await/in-trait/async-associated-types.rs new file mode 100644 index 0000000000000..0269f00bbab73 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-associated-types.rs @@ -0,0 +1,24 @@ +// 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;// = (&'a T, &'b U); + + 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) + } +} +//~^^^^ ERROR cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements +//~| ERROR cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-associated-types.stderr b/src/test/ui/async-await/in-trait/async-associated-types.stderr new file mode 100644 index 0000000000000..c1c66f9023bd7 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-associated-types.stderr @@ -0,0 +1,57 @@ +error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements + --> $DIR/async-associated-types.rs:17: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:14: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:17: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:17: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:17: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:14: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:17: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:17: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`. diff --git a/src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs b/src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs new file mode 100644 index 0000000000000..85d29eb703e76 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs @@ -0,0 +1,31 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +use std::future::Future; + +trait MyTrait { + type Fut<'a>: Future + where + Self: 'a; + + async fn foo(&self) -> Self::Fut<'a>; + //~^ ERROR use of undeclared lifetime name `'a` + //~| ERROR the parameter type `Self` may not live long enough +} + +impl MyTrait for i32 { + type Fut<'a> = impl Future + 'a + where + Self: 'a; + //~^^^ ERROR `impl Trait` in type aliases is unstable + + fn foo<'a>(&'a self) -> Self::Fut<'a> { + async { + *self + } + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr b/src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr new file mode 100644 index 0000000000000..6bc553b26350a --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr @@ -0,0 +1,42 @@ +error[E0261]: use of undeclared lifetime name `'a` + --> $DIR/async-associated-types2-desugared.rs:13:38 + | +LL | async fn foo(&self) -> Self::Fut<'a>; + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | async fn foo<'a>(&self) -> Self::Fut<'a>; + | ++++ +help: consider introducing lifetime `'a` here + | +LL | trait MyTrait<'a> { + | ++++ + +error[E0658]: `impl Trait` in type aliases is unstable + --> $DIR/async-associated-types2-desugared.rs:19:20 + | +LL | type Fut<'a> = impl Future + 'a + | ^^^^^^^^^^^^^^^^ + | + = note: see issue #63063 for more information + = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable + +error[E0310]: the parameter type `Self` may not live long enough + --> $DIR/async-associated-types2-desugared.rs:13:28 + | +LL | async fn foo(&self) -> Self::Fut<'a>; + | ^^^^^^^^^^^^^ + | + = help: consider adding an explicit lifetime bound `Self: 'static`... + = note: ...so that the type `Self` will meet its required lifetime bounds... +note: ...that is required by this bound + --> $DIR/async-associated-types2-desugared.rs:11:15 + | +LL | Self: 'a; + | ^^ + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0261, E0310, E0658. +For more information about an error, try `rustc --explain E0261`. diff --git a/src/test/ui/async-await/in-trait/async-associated-types2.rs b/src/test/ui/async-await/in-trait/async-associated-types2.rs new file mode 100644 index 0000000000000..c2133b04bc847 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-associated-types2.rs @@ -0,0 +1,32 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +use std::future::Future; + +trait MyTrait { + type Fut<'a>: Future + where + Self: 'a; + + fn foo(&self) -> Self::Fut<'a>; + //~^ ERROR use of undeclared lifetime name `'a` +} + +impl MyTrait for i32 { + type Fut<'a> = impl Future + 'a + where + Self: 'a; + //~^^^ ERROR `impl Trait` in type aliases is unstable + //~| ERROR expected `::Fut<'a>` to be a future that resolves to `i32`, but it resolves to `<::Fut<'a> as Future>::Output` + + fn foo<'a>(&'a self) -> Self::Fut<'a> { + //~^ ERROR `impl` item signature doesn't match `trait` item signature + async { + *self + } + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-associated-types2.stderr b/src/test/ui/async-await/in-trait/async-associated-types2.stderr new file mode 100644 index 0000000000000..f3343852fa093 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-associated-types2.stderr @@ -0,0 +1,63 @@ +error[E0261]: use of undeclared lifetime name `'a` + --> $DIR/async-associated-types2.rs:13:32 + | +LL | fn foo(&self) -> Self::Fut<'a>; + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | fn foo<'a>(&self) -> Self::Fut<'a>; + | ++++ +help: consider introducing lifetime `'a` here + | +LL | trait MyTrait<'a> { + | ++++ + +error[E0658]: `impl Trait` in type aliases is unstable + --> $DIR/async-associated-types2.rs:18:20 + | +LL | type Fut<'a> = impl Future + 'a + | ^^^^^^^^^^^^^^^^ + | + = note: see issue #63063 for more information + = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable + +error[E0271]: expected `::Fut<'a>` to be a future that resolves to `i32`, but it resolves to `<::Fut<'a> as Future>::Output` + --> $DIR/async-associated-types2.rs:18:20 + | +LL | type Fut<'a> = impl Future + 'a + | ^^^^^^^^^^^^^^^^ expected `i32`, found associated type + | + = note: expected type `i32` + found associated type `<::Fut<'a> as Future>::Output` +note: required by a bound in `MyTrait::Fut` + --> $DIR/async-associated-types2.rs:9:26 + | +LL | type Fut<'a>: Future + | ^^^^^^^^^^^^ required by this bound in `MyTrait::Fut` +help: consider constraining the associated type `<::Fut<'a> as Future>::Output` to `i32` + | +LL | type Fut<'a> = impl Future + 'a + | ++++++++++++++ + +error: `impl` item signature doesn't match `trait` item signature + --> $DIR/async-associated-types2.rs:24:5 + | +LL | fn foo(&self) -> Self::Fut<'a>; + | ------------------------------- expected `fn(&'1 i32) -> ::Fut<'static>` +... +LL | fn foo<'a>(&'a self) -> Self::Fut<'a> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 i32) -> ::Fut<'1>` + | + = note: expected `fn(&'1 i32) -> ::Fut<'static>` + found `fn(&'1 i32) -> ::Fut<'1>` +help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait` + --> $DIR/async-associated-types2.rs:13:22 + | +LL | fn foo(&self) -> Self::Fut<'a>; + | ^^^^ consider borrowing this type parameter in the trait + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0261, E0271, E0658. +For more information about an error, try `rustc --explain E0261`. diff --git a/src/test/ui/async-await/in-trait/async-example.rs b/src/test/ui/async-await/in-trait/async-example.rs new file mode 100644 index 0000000000000..2c2297c6b584b --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-example.rs @@ -0,0 +1,17 @@ +// check-pass +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +trait MyTrait { + async fn foo(&self) -> i32; +} + +impl MyTrait for i32 { + async fn foo(&self) -> i32 { + *self + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-generics-and-bounds.rs b/src/test/ui/async-await/in-trait/async-generics-and-bounds.rs new file mode 100644 index 0000000000000..d748430c5ee8e --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-generics-and-bounds.rs @@ -0,0 +1,21 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +use std::fmt::Debug; +use std::hash::Hash; + +trait MyTrait { + async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; +} +//~^^ ERROR the parameter type `U` may not live long enough +//~| ERROR the parameter type `T` may not live long enough + +impl MyTrait for (T, U) { + async fn foo(&self) -> &(T, U) { + self + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr b/src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr new file mode 100644 index 0000000000000..8975ec21a015d --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr @@ -0,0 +1,37 @@ +error[E0311]: the parameter type `U` may not live long enough + --> $DIR/async-generics-and-bounds.rs:10: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:10: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:10: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:10: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:10: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:10: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`. diff --git a/src/test/ui/async-await/in-trait/async-generics.rs b/src/test/ui/async-await/in-trait/async-generics.rs new file mode 100644 index 0000000000000..3e6f70cd49f66 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-generics.rs @@ -0,0 +1,18 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +trait MyTrait { + async fn foo(&self) -> &(T, U); +} +//~^^ ERROR the parameter type `U` may not live long enough +//~| ERROR the parameter type `T` may not live long enough + +impl MyTrait for (T, U) { + async fn foo(&self) -> &(T, U) { + self + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-generics.stderr b/src/test/ui/async-await/in-trait/async-generics.stderr new file mode 100644 index 0000000000000..84c7b3095368c --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-generics.stderr @@ -0,0 +1,37 @@ +error[E0311]: the parameter type `U` may not live long enough + --> $DIR/async-generics.rs:7: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:7: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:7:28 + | +LL | async fn foo(&self) -> &(T, U); + | ^^^^^^^ + +error[E0311]: the parameter type `T` may not live long enough + --> $DIR/async-generics.rs:7: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:7: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:7: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`. diff --git a/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs new file mode 100644 index 0000000000000..6a2276c96c8c1 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs @@ -0,0 +1,20 @@ +// 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; +} +//~^^ ERROR the parameter type `Self` may not live long enough +//~| ERROR the parameter type `T` may not live long enough + +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() {} diff --git a/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr new file mode 100644 index 0000000000000..d74f80917a48d --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr @@ -0,0 +1,23 @@ +error[E0309]: the parameter type `Self` may not live long enough + --> $DIR/async-lifetimes-and-bounds.rs:9: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:9: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`. diff --git a/src/test/ui/async-await/in-trait/async-lifetimes.rs b/src/test/ui/async-await/in-trait/async-lifetimes.rs new file mode 100644 index 0000000000000..1032a3435479d --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-lifetimes.rs @@ -0,0 +1,18 @@ +// 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); +} +//~^^ ERROR the parameter type `Self` may not live long enough +//~| ERROR the parameter type `T` may not live long enough + +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() {} diff --git a/src/test/ui/async-await/in-trait/async-lifetimes.stderr b/src/test/ui/async-await/in-trait/async-lifetimes.stderr new file mode 100644 index 0000000000000..780f75cf683d4 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-lifetimes.stderr @@ -0,0 +1,23 @@ +error[E0309]: the parameter type `Self` may not live long enough + --> $DIR/async-lifetimes.rs:7: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:7: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`. diff --git a/src/test/ui/async-await/in-trait/fn-not-async-err.rs b/src/test/ui/async-await/in-trait/fn-not-async-err.rs new file mode 100644 index 0000000000000..f94d32145a290 --- /dev/null +++ b/src/test/ui/async-await/in-trait/fn-not-async-err.rs @@ -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() {} diff --git a/src/test/ui/async-await/in-trait/fn-not-async-err.stderr b/src/test/ui/async-await/in-trait/fn-not-async-err.stderr new file mode 100644 index 0000000000000..03321dc5b5af1 --- /dev/null +++ b/src/test/ui/async-await/in-trait/fn-not-async-err.stderr @@ -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`. diff --git a/src/test/ui/async-await/in-trait/fn-not-async-err2.rs b/src/test/ui/async-await/in-trait/fn-not-async-err2.rs new file mode 100644 index 0000000000000..f617a19ab3415 --- /dev/null +++ b/src/test/ui/async-await/in-trait/fn-not-async-err2.rs @@ -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 { + //~^ ERROR `impl Trait` only allowed in function and inherent method return types, not in `impl` method return [E0562] + async { + *self + } + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/fn-not-async-err2.stderr b/src/test/ui/async-await/in-trait/fn-not-async-err2.stderr new file mode 100644 index 0000000000000..f591f18477290 --- /dev/null +++ b/src/test/ui/async-await/in-trait/fn-not-async-err2.stderr @@ -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 { + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #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`. From 8a0ebca97eabaed14a36d90aa25327dcec2fa8b1 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Tue, 4 Oct 2022 23:35:49 +0000 Subject: [PATCH 2/6] Update static AFIT tests based on feedback --- .../in-trait/async-associated-types.rs | 2 +- .../async-associated-types2-desugared.rs | 31 --------- .../async-associated-types2-desugared.stderr | 42 ------------- .../in-trait/async-associated-types2.rs | 10 ++- .../in-trait/async-associated-types2.stderr | 63 ------------------- .../in-trait/fn-not-async-err2-rpitit.rs | 22 +++++++ .../async-await/in-trait/fn-not-async-err2.rs | 2 +- 7 files changed, 28 insertions(+), 144 deletions(-) delete mode 100644 src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs delete mode 100644 src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr delete mode 100644 src/test/ui/async-await/in-trait/async-associated-types2.stderr create mode 100644 src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs diff --git a/src/test/ui/async-await/in-trait/async-associated-types.rs b/src/test/ui/async-await/in-trait/async-associated-types.rs index 0269f00bbab73..8f679190d7a12 100644 --- a/src/test/ui/async-await/in-trait/async-associated-types.rs +++ b/src/test/ui/async-await/in-trait/async-associated-types.rs @@ -6,7 +6,7 @@ use std::fmt::Debug; trait MyTrait<'a, 'b, T> where Self: 'a, T: Debug + Sized + 'b { - type MyAssoc;// = (&'a T, &'b U); + type MyAssoc; async fn foo(&'a self, key: &'b T) -> Self::MyAssoc; } diff --git a/src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs b/src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs deleted file mode 100644 index 85d29eb703e76..0000000000000 --- a/src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs +++ /dev/null @@ -1,31 +0,0 @@ -// edition: 2021 - -#![feature(async_fn_in_trait)] -#![allow(incomplete_features)] - -use std::future::Future; - -trait MyTrait { - type Fut<'a>: Future - where - Self: 'a; - - async fn foo(&self) -> Self::Fut<'a>; - //~^ ERROR use of undeclared lifetime name `'a` - //~| ERROR the parameter type `Self` may not live long enough -} - -impl MyTrait for i32 { - type Fut<'a> = impl Future + 'a - where - Self: 'a; - //~^^^ ERROR `impl Trait` in type aliases is unstable - - fn foo<'a>(&'a self) -> Self::Fut<'a> { - async { - *self - } - } -} - -fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr b/src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr deleted file mode 100644 index 6bc553b26350a..0000000000000 --- a/src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr +++ /dev/null @@ -1,42 +0,0 @@ -error[E0261]: use of undeclared lifetime name `'a` - --> $DIR/async-associated-types2-desugared.rs:13:38 - | -LL | async fn foo(&self) -> Self::Fut<'a>; - | ^^ undeclared lifetime - | -help: consider introducing lifetime `'a` here - | -LL | async fn foo<'a>(&self) -> Self::Fut<'a>; - | ++++ -help: consider introducing lifetime `'a` here - | -LL | trait MyTrait<'a> { - | ++++ - -error[E0658]: `impl Trait` in type aliases is unstable - --> $DIR/async-associated-types2-desugared.rs:19:20 - | -LL | type Fut<'a> = impl Future + 'a - | ^^^^^^^^^^^^^^^^ - | - = note: see issue #63063 for more information - = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable - -error[E0310]: the parameter type `Self` may not live long enough - --> $DIR/async-associated-types2-desugared.rs:13:28 - | -LL | async fn foo(&self) -> Self::Fut<'a>; - | ^^^^^^^^^^^^^ - | - = help: consider adding an explicit lifetime bound `Self: 'static`... - = note: ...so that the type `Self` will meet its required lifetime bounds... -note: ...that is required by this bound - --> $DIR/async-associated-types2-desugared.rs:11:15 - | -LL | Self: 'a; - | ^^ - -error: aborting due to 3 previous errors - -Some errors have detailed explanations: E0261, E0310, E0658. -For more information about an error, try `rustc --explain E0261`. diff --git a/src/test/ui/async-await/in-trait/async-associated-types2.rs b/src/test/ui/async-await/in-trait/async-associated-types2.rs index c2133b04bc847..e546a0579c66f 100644 --- a/src/test/ui/async-await/in-trait/async-associated-types2.rs +++ b/src/test/ui/async-await/in-trait/async-associated-types2.rs @@ -1,6 +1,8 @@ +// check-pass // edition: 2021 #![feature(async_fn_in_trait)] +#![feature(type_alias_impl_trait)] #![allow(incomplete_features)] use std::future::Future; @@ -10,19 +12,15 @@ trait MyTrait { where Self: 'a; - fn foo(&self) -> Self::Fut<'a>; - //~^ ERROR use of undeclared lifetime name `'a` + fn foo<'a>(&'a self) -> Self::Fut<'a>; } impl MyTrait for i32 { - type Fut<'a> = impl Future + 'a + type Fut<'a> = impl Future + 'a where Self: 'a; - //~^^^ ERROR `impl Trait` in type aliases is unstable - //~| ERROR expected `::Fut<'a>` to be a future that resolves to `i32`, but it resolves to `<::Fut<'a> as Future>::Output` fn foo<'a>(&'a self) -> Self::Fut<'a> { - //~^ ERROR `impl` item signature doesn't match `trait` item signature async { *self } diff --git a/src/test/ui/async-await/in-trait/async-associated-types2.stderr b/src/test/ui/async-await/in-trait/async-associated-types2.stderr deleted file mode 100644 index f3343852fa093..0000000000000 --- a/src/test/ui/async-await/in-trait/async-associated-types2.stderr +++ /dev/null @@ -1,63 +0,0 @@ -error[E0261]: use of undeclared lifetime name `'a` - --> $DIR/async-associated-types2.rs:13:32 - | -LL | fn foo(&self) -> Self::Fut<'a>; - | ^^ undeclared lifetime - | -help: consider introducing lifetime `'a` here - | -LL | fn foo<'a>(&self) -> Self::Fut<'a>; - | ++++ -help: consider introducing lifetime `'a` here - | -LL | trait MyTrait<'a> { - | ++++ - -error[E0658]: `impl Trait` in type aliases is unstable - --> $DIR/async-associated-types2.rs:18:20 - | -LL | type Fut<'a> = impl Future + 'a - | ^^^^^^^^^^^^^^^^ - | - = note: see issue #63063 for more information - = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable - -error[E0271]: expected `::Fut<'a>` to be a future that resolves to `i32`, but it resolves to `<::Fut<'a> as Future>::Output` - --> $DIR/async-associated-types2.rs:18:20 - | -LL | type Fut<'a> = impl Future + 'a - | ^^^^^^^^^^^^^^^^ expected `i32`, found associated type - | - = note: expected type `i32` - found associated type `<::Fut<'a> as Future>::Output` -note: required by a bound in `MyTrait::Fut` - --> $DIR/async-associated-types2.rs:9:26 - | -LL | type Fut<'a>: Future - | ^^^^^^^^^^^^ required by this bound in `MyTrait::Fut` -help: consider constraining the associated type `<::Fut<'a> as Future>::Output` to `i32` - | -LL | type Fut<'a> = impl Future + 'a - | ++++++++++++++ - -error: `impl` item signature doesn't match `trait` item signature - --> $DIR/async-associated-types2.rs:24:5 - | -LL | fn foo(&self) -> Self::Fut<'a>; - | ------------------------------- expected `fn(&'1 i32) -> ::Fut<'static>` -... -LL | fn foo<'a>(&'a self) -> Self::Fut<'a> { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 i32) -> ::Fut<'1>` - | - = note: expected `fn(&'1 i32) -> ::Fut<'static>` - found `fn(&'1 i32) -> ::Fut<'1>` -help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait` - --> $DIR/async-associated-types2.rs:13:22 - | -LL | fn foo(&self) -> Self::Fut<'a>; - | ^^^^ consider borrowing this type parameter in the trait - -error: aborting due to 4 previous errors - -Some errors have detailed explanations: E0261, E0271, E0658. -For more information about an error, try `rustc --explain E0261`. diff --git a/src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs b/src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs new file mode 100644 index 0000000000000..faf9a43d352d5 --- /dev/null +++ b/src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs @@ -0,0 +1,22 @@ +// 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 { + fn foo(&self) -> impl Future + '_ { + async { + *self + } + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/fn-not-async-err2.rs b/src/test/ui/async-await/in-trait/fn-not-async-err2.rs index f617a19ab3415..594baa91ad8ba 100644 --- a/src/test/ui/async-await/in-trait/fn-not-async-err2.rs +++ b/src/test/ui/async-await/in-trait/fn-not-async-err2.rs @@ -8,7 +8,7 @@ use std::future::Future; trait MyTrait { async fn foo(&self) -> i32; } - + impl MyTrait for i32 { fn foo(&self) -> impl Future { //~^ ERROR `impl Trait` only allowed in function and inherent method return types, not in `impl` method return [E0562] From 11b1439380d2e7e915207cb708e78a9060af82b8 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Fri, 7 Oct 2022 16:08:56 +0000 Subject: [PATCH 3/6] Update tests based on feedback --- .../in-trait/async-associated-types.rs | 4 ++-- .../in-trait/async-associated-types.stderr | 16 ++++++++-------- .../ui/async-await/in-trait/async-example.rs | 17 ++++++++++++++++- .../in-trait/async-generics-and-bounds.rs | 4 ++-- .../in-trait/async-generics-and-bounds.stderr | 12 ++++++------ .../ui/async-await/in-trait/async-generics.rs | 4 ++-- .../async-await/in-trait/async-generics.stderr | 12 ++++++------ .../in-trait/async-lifetimes-and-bounds.rs | 4 ++-- .../in-trait/async-lifetimes-and-bounds.stderr | 4 ++-- .../ui/async-await/in-trait/async-lifetimes.rs | 4 ++-- 10 files changed, 48 insertions(+), 33 deletions(-) diff --git a/src/test/ui/async-await/in-trait/async-associated-types.rs b/src/test/ui/async-await/in-trait/async-associated-types.rs index 8f679190d7a12..a6f928f3b1bac 100644 --- a/src/test/ui/async-await/in-trait/async-associated-types.rs +++ b/src/test/ui/async-await/in-trait/async-associated-types.rs @@ -1,3 +1,5 @@ +// check-fail +// known-bug: #102682 // edition: 2021 #![feature(async_fn_in_trait)] @@ -18,7 +20,5 @@ impl<'a, 'b, T: Debug + Sized + 'b, U: 'a> MyTrait<'a, 'b, T> for U { (self, key) } } -//~^^^^ ERROR cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements -//~| ERROR cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-associated-types.stderr b/src/test/ui/async-await/in-trait/async-associated-types.stderr index c1c66f9023bd7..0985150eee0de 100644 --- a/src/test/ui/async-await/in-trait/async-associated-types.stderr +++ b/src/test/ui/async-await/in-trait/async-associated-types.stderr @@ -1,16 +1,16 @@ error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements - --> $DIR/async-associated-types.rs:17:43 + --> $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:14:6 + --> $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:17:43 + --> $DIR/async-associated-types.rs:19:43 | LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { | ^^^^^^^^^^^^^^ @@ -18,7 +18,7 @@ LL | async fn foo(&'a self, key: &'b T) -> (&'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:17:43 + --> $DIR/async-associated-types.rs:19:43 | LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { | ^^^^^^^^^^^^^^ @@ -26,18 +26,18 @@ LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { found `MyTrait<'_, '_, T>` error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements - --> $DIR/async-associated-types.rs:17:43 + --> $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:14:10 + --> $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:17:43 + --> $DIR/async-associated-types.rs:19:43 | LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { | ^^^^^^^^^^^^^^ @@ -45,7 +45,7 @@ LL | async fn foo(&'a self, key: &'b T) -> (&'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:17:43 + --> $DIR/async-associated-types.rs:19:43 | LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { | ^^^^^^^^^^^^^^ diff --git a/src/test/ui/async-await/in-trait/async-example.rs b/src/test/ui/async-await/in-trait/async-example.rs index 2c2297c6b584b..10387f09f0e9d 100644 --- a/src/test/ui/async-await/in-trait/async-example.rs +++ b/src/test/ui/async-await/in-trait/async-example.rs @@ -6,12 +6,27 @@ 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() {} +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 _ = x.foo(); + let _ = x.bar(); + }; +} diff --git a/src/test/ui/async-await/in-trait/async-generics-and-bounds.rs b/src/test/ui/async-await/in-trait/async-generics-and-bounds.rs index d748430c5ee8e..a73d55adfeced 100644 --- a/src/test/ui/async-await/in-trait/async-generics-and-bounds.rs +++ b/src/test/ui/async-await/in-trait/async-generics-and-bounds.rs @@ -1,3 +1,5 @@ +// check-fail +// known-bug: #102682 // edition: 2021 #![feature(async_fn_in_trait)] @@ -9,8 +11,6 @@ use std::hash::Hash; trait MyTrait { async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; } -//~^^ ERROR the parameter type `U` may not live long enough -//~| ERROR the parameter type `T` may not live long enough impl MyTrait for (T, U) { async fn foo(&self) -> &(T, U) { diff --git a/src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr b/src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr index 8975ec21a015d..5c8d64fc6cb4c 100644 --- a/src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr +++ b/src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr @@ -1,33 +1,33 @@ error[E0311]: the parameter type `U` may not live long enough - --> $DIR/async-generics-and-bounds.rs:10:28 + --> $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:10:18 + --> $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:10:28 + --> $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:10:28 + --> $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:10:18 + --> $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:10:28 + --> $DIR/async-generics-and-bounds.rs:12:28 | LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; | ^^^^^^^ diff --git a/src/test/ui/async-await/in-trait/async-generics.rs b/src/test/ui/async-await/in-trait/async-generics.rs index 3e6f70cd49f66..67000e5770ee8 100644 --- a/src/test/ui/async-await/in-trait/async-generics.rs +++ b/src/test/ui/async-await/in-trait/async-generics.rs @@ -1,3 +1,5 @@ +// check-fail +// known-bug: #102682 // edition: 2021 #![feature(async_fn_in_trait)] @@ -6,8 +8,6 @@ trait MyTrait { async fn foo(&self) -> &(T, U); } -//~^^ ERROR the parameter type `U` may not live long enough -//~| ERROR the parameter type `T` may not live long enough impl MyTrait for (T, U) { async fn foo(&self) -> &(T, U) { diff --git a/src/test/ui/async-await/in-trait/async-generics.stderr b/src/test/ui/async-await/in-trait/async-generics.stderr index 84c7b3095368c..6ae73d9e3a601 100644 --- a/src/test/ui/async-await/in-trait/async-generics.stderr +++ b/src/test/ui/async-await/in-trait/async-generics.stderr @@ -1,33 +1,33 @@ error[E0311]: the parameter type `U` may not live long enough - --> $DIR/async-generics.rs:7:28 + --> $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:7:18 + --> $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:7:28 + --> $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:7:28 + --> $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:7:18 + --> $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:7:28 + --> $DIR/async-generics.rs:9:28 | LL | async fn foo(&self) -> &(T, U); | ^^^^^^^ diff --git a/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs index 6a2276c96c8c1..3f7448cecd157 100644 --- a/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs +++ b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs @@ -1,3 +1,5 @@ +// check-fail +// known-bug: #102682 // edition: 2021 #![feature(async_fn_in_trait)] @@ -8,8 +10,6 @@ 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; } -//~^^ ERROR the parameter type `Self` may not live long enough -//~| ERROR the parameter type `T` may not live long enough impl<'a, 'b, T, U> MyTrait<'a, 'b, T> for U { async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { diff --git a/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr index d74f80917a48d..0f02420274333 100644 --- a/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr +++ b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr @@ -1,5 +1,5 @@ error[E0309]: the parameter type `Self` may not live long enough - --> $DIR/async-lifetimes-and-bounds.rs:9:43 + --> $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; | ^^^^^^^^^^^^^^^^^ @@ -8,7 +8,7 @@ LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T) where T: Debug = 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:9:43 + --> $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 diff --git a/src/test/ui/async-await/in-trait/async-lifetimes.rs b/src/test/ui/async-await/in-trait/async-lifetimes.rs index 1032a3435479d..5dc1672da68fe 100644 --- a/src/test/ui/async-await/in-trait/async-lifetimes.rs +++ b/src/test/ui/async-await/in-trait/async-lifetimes.rs @@ -5,9 +5,9 @@ trait MyTrait<'a, 'b, T> { async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T); + //~^ ERROR the parameter type `Self` may not live long enough + //~| ERROR the parameter type `T` may not live long enough } -//~^^ ERROR the parameter type `Self` may not live long enough -//~| ERROR the parameter type `T` may not live long enough impl<'a, 'b, T, U> MyTrait<'a, 'b, T> for U { async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { From 9a05081d5fb13b3bee9d41b43692c9f38d521c96 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Fri, 7 Oct 2022 16:15:13 +0000 Subject: [PATCH 4/6] Add additional tests for static AFIT --- .../async-example-desugared-boxed-in-trait.rs | 21 +++++++++++++++++ ...nc-example-desugared-boxed-in-trait.stderr | 17 ++++++++++++++ .../in-trait/async-example-desugared-boxed.rs | 23 +++++++++++++++++++ .../async-example-desugared-in-trait.rs | 20 ++++++++++++++++ .../in-trait/async-example-desugared.rs | 22 ++++++++++++++++++ .../in-trait/async-recursive-generic.rs | 21 +++++++++++++++++ .../in-trait/async-recursive-generic.stderr | 12 ++++++++++ .../async-await/in-trait/async-recursive.rs | 21 +++++++++++++++++ .../in-trait/async-recursive.stderr | 12 ++++++++++ 9 files changed, 169 insertions(+) create mode 100644 src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.rs create mode 100644 src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.stderr create mode 100644 src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs create mode 100644 src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs create mode 100644 src/test/ui/async-await/in-trait/async-example-desugared.rs create mode 100644 src/test/ui/async-await/in-trait/async-recursive-generic.rs create mode 100644 src/test/ui/async-await/in-trait/async-recursive-generic.stderr create mode 100644 src/test/ui/async-await/in-trait/async-recursive.rs create mode 100644 src/test/ui/async-await/in-trait/async-recursive.stderr diff --git a/src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.rs b/src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.rs new file mode 100644 index 0000000000000..38ba297189c69 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.rs @@ -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 + '_>>; +} + +impl MyTrait for i32 { + async fn foo(&self) -> i32 { + //~^ ERROR method `foo` has an incompatible type for trait + *self + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.stderr b/src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.stderr new file mode 100644 index 0000000000000..22d2928f2f564 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.stderr @@ -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 + '_>>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: expected fn pointer `fn(&i32) -> Pin>>` + found fn pointer `fn(&i32) -> impl Future` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0053`. diff --git a/src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs b/src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs new file mode 100644 index 0000000000000..e1131753885b8 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs @@ -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; +use std::pin::Pin; + +trait MyTrait { + async fn foo(&self) -> i32; +} + +impl MyTrait for i32 { + fn foo(&self) -> Pin + '_>> { + Box::pin(async { + *self + }) + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs b/src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs new file mode 100644 index 0000000000000..6f61781709e73 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs @@ -0,0 +1,20 @@ +// 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 + '_; +} + +impl MyTrait for i32 { + async fn foo(&self) -> i32 { + *self + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-example-desugared.rs b/src/test/ui/async-await/in-trait/async-example-desugared.rs new file mode 100644 index 0000000000000..faf9a43d352d5 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-example-desugared.rs @@ -0,0 +1,22 @@ +// 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 { + fn foo(&self) -> impl Future + '_ { + async { + *self + } + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-recursive-generic.rs b/src/test/ui/async-await/in-trait/async-recursive-generic.rs new file mode 100644 index 0000000000000..6839abd381c3f --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-recursive-generic.rs @@ -0,0 +1,21 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +trait MyTrait { + async fn foo_recursive(&self, n: usize) -> T; +} + +impl MyTrait 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() {} diff --git a/src/test/ui/async-await/in-trait/async-recursive-generic.stderr b/src/test/ui/async-await/in-trait/async-recursive-generic.stderr new file mode 100644 index 0000000000000..cab173bdd5b70 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-recursive-generic.stderr @@ -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`. diff --git a/src/test/ui/async-await/in-trait/async-recursive.rs b/src/test/ui/async-await/in-trait/async-recursive.rs new file mode 100644 index 0000000000000..61119f8095bcc --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-recursive.rs @@ -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() {} diff --git a/src/test/ui/async-await/in-trait/async-recursive.stderr b/src/test/ui/async-await/in-trait/async-recursive.stderr new file mode 100644 index 0000000000000..9feff37b3fe46 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-recursive.stderr @@ -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`. From 0b3b04643624027aadc891212dacfdcfa931ed0b Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Thu, 27 Oct 2022 15:47:30 -0700 Subject: [PATCH 5/6] Update src/test/ui/async-await/in-trait/async-example.rs Co-authored-by: Michael Goulet --- src/test/ui/async-await/in-trait/async-example.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/ui/async-await/in-trait/async-example.rs b/src/test/ui/async-await/in-trait/async-example.rs index 10387f09f0e9d..abf94ef7450fc 100644 --- a/src/test/ui/async-await/in-trait/async-example.rs +++ b/src/test/ui/async-await/in-trait/async-example.rs @@ -26,7 +26,7 @@ fn main() { let _ = x.bar(); // Calling from async block in non-async context async { - let _ = x.foo(); - let _ = x.bar(); + let _: i32 = x.foo().await; + let _: i32 = x.bar().await; }; } From bfdefdbfb740a2b41dbdf01299ecdf1610c54907 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Thu, 27 Oct 2022 22:55:50 +0000 Subject: [PATCH 6/6] Update tests based on feedback - Add comment to some tests that will break when #102745 is implemented - Mark a test with known-bug - Delete duplicate test --- .../in-trait/async-example-desugared-boxed.rs | 1 + .../async-example-desugared-in-trait.rs | 1 + .../in-trait/async-example-desugared.rs | 1 + .../async-await/in-trait/async-lifetimes.rs | 4 ++-- .../in-trait/async-lifetimes.stderr | 4 ++-- .../in-trait/fn-not-async-err2-rpitit.rs | 22 ------------------- 6 files changed, 7 insertions(+), 26 deletions(-) delete mode 100644 src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs diff --git a/src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs b/src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs index e1131753885b8..61d7e2520eab7 100644 --- a/src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs +++ b/src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs @@ -13,6 +13,7 @@ trait MyTrait { } impl MyTrait for i32 { + // This will break once a PR that implements #102745 is merged fn foo(&self) -> Pin + '_>> { Box::pin(async { *self diff --git a/src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs b/src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs index 6f61781709e73..feeda719e0306 100644 --- a/src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs +++ b/src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs @@ -12,6 +12,7 @@ trait MyTrait { } impl MyTrait for i32 { + // This will break once a PR that implements #102745 is merged async fn foo(&self) -> i32 { *self } diff --git a/src/test/ui/async-await/in-trait/async-example-desugared.rs b/src/test/ui/async-await/in-trait/async-example-desugared.rs index faf9a43d352d5..1313c9edd861c 100644 --- a/src/test/ui/async-await/in-trait/async-example-desugared.rs +++ b/src/test/ui/async-await/in-trait/async-example-desugared.rs @@ -12,6 +12,7 @@ trait MyTrait { } impl MyTrait for i32 { + // This will break once a PR that implements #102745 is merged fn foo(&self) -> impl Future + '_ { async { *self diff --git a/src/test/ui/async-await/in-trait/async-lifetimes.rs b/src/test/ui/async-await/in-trait/async-lifetimes.rs index 5dc1672da68fe..acbac471cf7e8 100644 --- a/src/test/ui/async-await/in-trait/async-lifetimes.rs +++ b/src/test/ui/async-await/in-trait/async-lifetimes.rs @@ -1,3 +1,5 @@ +// check-fail +// known-bug: #102682 // edition: 2021 #![feature(async_fn_in_trait)] @@ -5,8 +7,6 @@ trait MyTrait<'a, 'b, T> { async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T); - //~^ ERROR the parameter type `Self` may not live long enough - //~| ERROR the parameter type `T` may not live long enough } impl<'a, 'b, T, U> MyTrait<'a, 'b, T> for U { diff --git a/src/test/ui/async-await/in-trait/async-lifetimes.stderr b/src/test/ui/async-await/in-trait/async-lifetimes.stderr index 780f75cf683d4..9a7d294bb1766 100644 --- a/src/test/ui/async-await/in-trait/async-lifetimes.stderr +++ b/src/test/ui/async-await/in-trait/async-lifetimes.stderr @@ -1,5 +1,5 @@ error[E0309]: the parameter type `Self` may not live long enough - --> $DIR/async-lifetimes.rs:7:43 + --> $DIR/async-lifetimes.rs:9:43 | LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T); | ^^^^^^^^^^^^^^^^^ @@ -8,7 +8,7 @@ LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T); = 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:7:43 + --> $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 diff --git a/src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs b/src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs deleted file mode 100644 index faf9a43d352d5..0000000000000 --- a/src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs +++ /dev/null @@ -1,22 +0,0 @@ -// 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 { - fn foo(&self) -> impl Future + '_ { - async { - *self - } - } -} - -fn main() {}