Auto merge of #114742 - compiler-errors:opaques-are-not-injective, r=aliemjay

TAITs do not constrain generic params

Fixes #108425

Not sure if I should rework those two failing tests. I guess `tests/ui/type-alias-impl-trait/coherence.rs` could just have the type parameter removed from it? IDK what `tests/ui/type-alias-impl-trait/coherence_generalization.rs` is even testing, though.

r? `@aliemjay`
cc `@lcnr` `@oli-obk` (when he's back from 🌴)
This commit is contained in:
bors 2023-08-13 22:47:14 +00:00
commit e81522aa0e
6 changed files with 43 additions and 8 deletions

View File

@ -59,7 +59,7 @@ struct ParameterCollector {
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector {
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
match *t.kind() {
ty::Alias(ty::Projection | ty::Inherent, ..) if !self.include_nonconstraining => {
ty::Alias(..) if !self.include_nonconstraining => {
// projections are not injective
return ControlFlow::Continue(());
}

View File

@ -11,7 +11,7 @@ fn use_alias<T>(val: T) -> AliasOfForeignType<T> {
foreign_crate::ForeignType(val)
}
impl<T> foreign_crate::ForeignTrait for AliasOfForeignType<T> {}
impl foreign_crate::ForeignTrait for AliasOfForeignType<()> {}
//~^ ERROR only traits defined in the current crate can be implemented for arbitrary types
fn main() {}

View File

@ -1,10 +1,10 @@
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
--> $DIR/coherence.rs:14:1
|
LL | impl<T> foreign_crate::ForeignTrait for AliasOfForeignType<T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------
| | |
| | `AliasOfForeignType<T>` is not defined in the current crate
LL | impl foreign_crate::ForeignTrait for AliasOfForeignType<()> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------
| | |
| | `AliasOfForeignType<()>` is not defined in the current crate
| impl doesn't use only types from inside the current crate
|
= note: define and implement a trait or new type instead

View File

@ -2,6 +2,7 @@
// FIXME(type_alias_impl_trait): What does this test? This needs a comment
// explaining what we're worried about here.
#![feature(type_alias_impl_trait)]
trait Trait {}
type Opaque<T> = impl Sized;
@ -9,7 +10,7 @@ fn foo<T>() -> Opaque<T> {
()
}
impl<T, V> Trait for (T, V, V, u32) {}
impl<U, V> Trait for (Opaque<U>, V, i32, V) {}
impl<T, U, V> Trait for (T, U, V, V, u32) {}
impl<U, V> Trait for (Opaque<U>, U, V, i32, V) {}
fn main() {}

View File

@ -0,0 +1,25 @@
#![feature(type_alias_impl_trait)]
use std::fmt::Display;
type Opaque<X> = impl Sized + 'static;
fn define<X>() -> Opaque<X> {}
trait Trait {
type Assoc: Display;
}
impl<'a> Trait for Opaque<&'a str> {
//~^ ERROR the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
type Assoc = &'a str;
}
// ======= Exploit =======
fn extend<T: Trait + 'static>(s: T::Assoc) -> Box<dyn Display> {
Box::new(s)
}
fn main() {
let val = extend::<Opaque<&'_ str>>(&String::from("blah blah blah"));
println!("{}", val);
}

View File

@ -0,0 +1,9 @@
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
--> $DIR/unconstrained-impl-param.rs:11:6
|
LL | impl<'a> Trait for Opaque<&'a str> {
| ^^ unconstrained lifetime parameter
error: aborting due to previous error
For more information about this error, try `rustc --explain E0207`.