Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TAITs do not constrain generic params #114742

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.