Skip to content

Commit d0c826c

Browse files
Opaques do not constrain generic params
1 parent f1b8548 commit d0c826c

File tree

7 files changed

+51
-13
lines changed

7 files changed

+51
-13
lines changed

compiler/rustc_hir_analysis/src/constrained_generic_params.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct ParameterCollector {
5959
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector {
6060
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
6161
match *t.kind() {
62-
ty::Alias(ty::Projection | ty::Inherent, ..) if !self.include_nonconstraining => {
62+
ty::Alias(..) if !self.include_nonconstraining => {
6363
// projections are not injective
6464
return ControlFlow::Continue(());
6565
}

tests/ui/type-alias-impl-trait/coherence.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ fn use_alias<T>(val: T) -> AliasOfForeignType<T> {
1212
}
1313

1414
impl<T> foreign_crate::ForeignTrait for AliasOfForeignType<T> {}
15-
//~^ ERROR only traits defined in the current crate can be implemented for arbitrary types
15+
//~^ ERROR the type parameter `T` is not constrained by the impl trait, self type, or predicates
1616

1717
fn main() {}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
2-
--> $DIR/coherence.rs:14:1
1+
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
2+
--> $DIR/coherence.rs:14:6
33
|
44
LL | impl<T> foreign_crate::ForeignTrait for AliasOfForeignType<T> {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------
6-
| | |
7-
| | `AliasOfForeignType<T>` is not defined in the current crate
8-
| impl doesn't use only types from inside the current crate
9-
|
10-
= note: define and implement a trait or new type instead
5+
| ^ unconstrained type parameter
116

127
error: aborting due to previous error
138

14-
For more information about this error, try `rustc --explain E0117`.
9+
For more information about this error, try `rustc --explain E0207`.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// check-pass
2-
31
// FIXME(type_alias_impl_trait): What does this test? This needs a comment
42
// explaining what we're worried about here.
3+
54
#![feature(type_alias_impl_trait)]
65
trait Trait {}
76
type Opaque<T> = impl Sized;
@@ -11,5 +10,6 @@ fn foo<T>() -> Opaque<T> {
1110

1211
impl<T, V> Trait for (T, V, V, u32) {}
1312
impl<U, V> Trait for (Opaque<U>, V, i32, V) {}
13+
//~^ ERROR the type parameter `U` is not constrained by the impl trait, self type, or predicates
1414

1515
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
2+
--> $DIR/coherence_generalization.rs:12:6
3+
|
4+
LL | impl<U, V> Trait for (Opaque<U>, V, i32, V) {}
5+
| ^ unconstrained type parameter
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0207`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
use std::fmt::Display;
4+
5+
type Opaque<'a> = impl Sized + 'static;
6+
fn define<'a>() -> Opaque<'a> {}
7+
8+
trait Trait {
9+
type Assoc: Display;
10+
}
11+
impl<'a> Trait for Opaque<'a> {
12+
//~^ ERROR the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
13+
type Assoc = &'a str;
14+
}
15+
16+
// ======= Exploit =======
17+
18+
fn extend<T: Trait + 'static>(s: T::Assoc) -> Box<dyn Display> {
19+
Box::new(s)
20+
}
21+
22+
fn main() {
23+
let val = extend::<Opaque<'_>>(&String::from("blah blah blah"));
24+
println!("{}", val);
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
2+
--> $DIR/unconstrained-impl-param.rs:11:6
3+
|
4+
LL | impl<'a> Trait for Opaque<'a> {
5+
| ^^ unconstrained lifetime parameter
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0207`.

0 commit comments

Comments
 (0)