Skip to content

Commit f14a5fd

Browse files
check Projection supertrait bounds when confirming dyn candidate
1 parent 285fa7e commit f14a5fd

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
468468
.predicates
469469
.into_iter()
470470
{
471-
if let ty::PredicateKind::Trait(..) = super_trait.kind().skip_binder() {
471+
if let ty::PredicateKind::Trait(..) | ty::PredicateKind::Projection(..) =
472+
super_trait.kind().skip_binder()
473+
{
472474
let normalized_super_trait = normalize_with_depth_to(
473475
self,
474476
obligation.param_env,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
trait SuperTrait {
2+
type A;
3+
type B;
4+
}
5+
6+
trait Trait: SuperTrait<A = <Self as SuperTrait>::B> {}
7+
8+
fn transmute<A, B>(x: A) -> B {
9+
foo::<A, B, dyn Trait<A = A, B = B>>(x)
10+
//~^ ERROR type mismatch resolving `<dyn Trait<A = A, B = B> as SuperTrait>::A == B`
11+
}
12+
13+
fn foo<A, B, T: ?Sized>(x: T::A) -> B
14+
where
15+
T: Trait<B = B>,
16+
{
17+
x
18+
}
19+
20+
static X: u8 = 0;
21+
fn main() {
22+
let x = transmute::<&u8, &[u8; 1_000_000]>(&X);
23+
println!("{:?}", x[100_000]);
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0271]: type mismatch resolving `<dyn Trait<A = A, B = B> as SuperTrait>::A == B`
2+
--> $DIR/enforce-supertrait-projection.rs:9:5
3+
|
4+
LL | fn transmute<A, B>(x: A) -> B {
5+
| - - expected type parameter
6+
| |
7+
| found type parameter
8+
LL | foo::<A, B, dyn Trait<A = A, B = B>>(x)
9+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `B`, found type parameter `A`
10+
|
11+
= note: expected type parameter `B`
12+
found type parameter `A`
13+
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
14+
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
15+
note: required by a bound in `foo`
16+
--> $DIR/enforce-supertrait-projection.rs:15:8
17+
|
18+
LL | fn foo<A, B, T: ?Sized>(x: T::A) -> B
19+
| --- required by a bound in this
20+
LL | where
21+
LL | T: Trait<B = B>,
22+
| ^^^^^^^^^^^^ required by this bound in `foo`
23+
24+
error: aborting due to previous error
25+
26+
For more information about this error, try `rustc --explain E0271`.

0 commit comments

Comments
 (0)