diff options
| author | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2022-03-31 13:52:40 +0000 |
|---|---|---|
| committer | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2022-03-31 13:52:40 +0000 |
| commit | eca0ead17d3b591ed1e1cdeac434179fcda5ca33 (patch) | |
| tree | 787208501919c098c75806cbf0c9d129c2ed5fd7 /src | |
| parent | e50ff9b4521234e56ff46f8ed0372d5cb5689654 (diff) | |
Enforce well formedness for type alias impl trait's hidden type
Diffstat (limited to 'src')
4 files changed, 101 insertions, 0 deletions
diff --git a/src/test/ui/type-alias-impl-trait/underconstrained_generic.rs b/src/test/ui/type-alias-impl-trait/underconstrained_generic.rs new file mode 100644 index 00000000000..22264670f37 --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/underconstrained_generic.rs @@ -0,0 +1,28 @@ +#![feature(type_alias_impl_trait)] + +use std::marker::PhantomData; + +trait Trait { + fn foo<T, U>(t: T) -> U; +} + +trait ProofForConversion<X> { + fn convert<T, U>(_: PhantomData<Self>, r: T) -> U; +} + +impl<X: Trait> ProofForConversion<X> for () { + fn convert<T, U>(_: PhantomData<Self>, r: T) -> U { + X::foo(r) + } +} + +type Converter<T> = impl ProofForConversion<T>; +//~^ ERROR the trait bound `T: Trait` is not satisfied + +fn _defining_use<T: Trait>() -> Converter<T> { + () +} + + +fn main() { +} diff --git a/src/test/ui/type-alias-impl-trait/underconstrained_generic.stderr b/src/test/ui/type-alias-impl-trait/underconstrained_generic.stderr new file mode 100644 index 00000000000..1c305abcfeb --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/underconstrained_generic.stderr @@ -0,0 +1,19 @@ +error[E0277]: the trait bound `T: Trait` is not satisfied + --> $DIR/underconstrained_generic.rs:19:21 + | +LL | type Converter<T> = impl ProofForConversion<T>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `T` + | +note: required because of the requirements on the impl of `ProofForConversion<T>` for `()` + --> $DIR/underconstrained_generic.rs:13:16 + | +LL | impl<X: Trait> ProofForConversion<X> for () { + | ^^^^^^^^^^^^^^^^^^^^^ ^^ +help: consider restricting type parameter `T` + | +LL | type Converter<T: Trait> = impl ProofForConversion<T>; + | +++++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/type-alias-impl-trait/underconstrained_lifetime.rs b/src/test/ui/type-alias-impl-trait/underconstrained_lifetime.rs new file mode 100644 index 00000000000..c5b2e8a1c5e --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/underconstrained_lifetime.rs @@ -0,0 +1,34 @@ +#![feature(type_alias_impl_trait)] + +use std::marker::PhantomData; + +trait ProofForConversion<'a, 'b> { + fn convert<T: ?Sized>(_: PhantomData<Self>, r: &'a T) -> &'b T; +} + +impl<'a, 'b> ProofForConversion<'a, 'b> for &'b &'a () { + fn convert<T: ?Sized>(_: PhantomData<Self>, r: &'a T) -> &'b T { + r + } +} + +type Converter<'a, 'b> = impl ProofForConversion<'a, 'b>; +//~^ ERROR reference has a longer lifetime than the data it references + +// Even _defining_use with an explicit `'a: 'b` compiles fine, too. +fn _defining_use<'a, 'b>(x: &'b &'a ()) -> Converter<'a, 'b> { + x +} + +fn extend_lifetime<'a, 'b, T: ?Sized>(x: &'a T) -> &'b T { + Converter::<'a, 'b>::convert(PhantomData, x) +} + +fn main() { + let d; + { + let x = "Hello World".to_string(); + d = extend_lifetime(&x); + } + println!("{}", d); +} diff --git a/src/test/ui/type-alias-impl-trait/underconstrained_lifetime.stderr b/src/test/ui/type-alias-impl-trait/underconstrained_lifetime.stderr new file mode 100644 index 00000000000..12d85a49d01 --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/underconstrained_lifetime.stderr @@ -0,0 +1,20 @@ +error[E0491]: in type `&'b &'a ()`, reference has a longer lifetime than the data it references + --> $DIR/underconstrained_lifetime.rs:15:26 + | +LL | type Converter<'a, 'b> = impl ProofForConversion<'a, 'b>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: the pointer is valid for the lifetime `'b` as defined here + --> $DIR/underconstrained_lifetime.rs:15:20 + | +LL | type Converter<'a, 'b> = impl ProofForConversion<'a, 'b>; + | ^^ +note: but the referenced data is only valid for the lifetime `'a` as defined here + --> $DIR/underconstrained_lifetime.rs:15:16 + | +LL | type Converter<'a, 'b> = impl ProofForConversion<'a, 'b>; + | ^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0491`. |
