Skip to content

Commit

Permalink
Auto merge of #114742 - compiler-errors:opaques-are-not-injective, r=…
Browse files Browse the repository at this point in the history
…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 🌴)
  • Loading branch information
bors committed Aug 13, 2023
2 parents 1b198b3 + 5c95e77 commit e81522a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/type-alias-impl-trait/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
8 changes: 4 additions & 4 deletions tests/ui/type-alias-impl-trait/coherence.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/type-alias-impl-trait/coherence_generalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

// 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;
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() {}
25 changes: 25 additions & 0 deletions tests/ui/type-alias-impl-trait/unconstrained-impl-param.rs
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit e81522a

Please sign in to comment.