Opaques do not constrain generic params

This commit is contained in:
Michael Goulet 2023-08-11 19:05:03 +00:00
parent f1b854818d
commit d0c826cfc2
7 changed files with 51 additions and 13 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

@ -12,6 +12,6 @@ fn use_alias<T>(val: T) -> AliasOfForeignType<T> {
}
impl<T> foreign_crate::ForeignTrait for AliasOfForeignType<T> {}
//~^ ERROR only traits defined in the current crate can be implemented for arbitrary types
//~^ ERROR the type parameter `T` is not constrained by the impl trait, self type, or predicates
fn main() {}

View File

@ -1,14 +1,9 @@
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
--> $DIR/coherence.rs:14:1
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
--> $DIR/coherence.rs:14:6
|
LL | impl<T> foreign_crate::ForeignTrait for AliasOfForeignType<T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------
| | |
| | `AliasOfForeignType<T>` 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
| ^ unconstrained type parameter
error: aborting due to previous error
For more information about this error, try `rustc --explain E0117`.
For more information about this error, try `rustc --explain E0207`.

View File

@ -1,7 +1,6 @@
// check-pass
// 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;
@ -11,5 +10,6 @@ fn foo<T>() -> Opaque<T> {
impl<T, V> Trait for (T, V, V, u32) {}
impl<U, V> Trait for (Opaque<U>, V, i32, V) {}
//~^ ERROR the type parameter `U` is not constrained by the impl trait, self type, or predicates
fn main() {}

View File

@ -0,0 +1,9 @@
error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
--> $DIR/coherence_generalization.rs:12:6
|
LL | impl<U, V> Trait for (Opaque<U>, V, i32, V) {}
| ^ unconstrained type parameter
error: aborting due to previous error
For more information about this error, try `rustc --explain E0207`.

View File

@ -0,0 +1,25 @@
#![feature(type_alias_impl_trait)]
use std::fmt::Display;
type Opaque<'a> = impl Sized + 'static;
fn define<'a>() -> Opaque<'a> {}
trait Trait {
type Assoc: Display;
}
impl<'a> Trait for Opaque<'a> {
//~^ 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<'_>>(&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> {
| ^^ unconstrained lifetime parameter
error: aborting due to previous error
For more information about this error, try `rustc --explain E0207`.