Skip to content

Commit 238beae

Browse files
Fix upcasting with normalization in old solver, add a test
1 parent 4cc659e commit 238beae

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -887,9 +887,19 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
887887
let ty::Dynamic(b_data, b_region, ty::Dyn) = *b_ty.kind() else { bug!() };
888888

889889
let source_principal = a_data.principal().unwrap().with_self_ty(tcx, a_ty);
890-
let upcast_principal = util::supertraits(tcx, source_principal).nth(idx).unwrap();
890+
let unnormalized_upcast_principal =
891+
util::supertraits(tcx, source_principal).nth(idx).unwrap();
891892

892893
let mut nested = vec![];
894+
let upcast_principal = normalize_with_depth_to(
895+
self,
896+
obligation.param_env,
897+
obligation.cause.clone(),
898+
obligation.recursion_depth + 1,
899+
unnormalized_upcast_principal,
900+
&mut nested,
901+
);
902+
893903
for bound in b_data {
894904
match bound.skip_binder() {
895905
// Check that a's supertrait (upcast_principal) is compatible
@@ -973,7 +983,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
973983
}
974984
VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => {
975985
vptr_offset += count_own_vtable_entries(tcx, trait_ref);
976-
if trait_ref == upcast_principal {
986+
if trait_ref == unnormalized_upcast_principal {
977987
if emit_vptr {
978988
return ControlFlow::Break(Some(vptr_offset));
979989
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// check-pass
2+
// issue: 114113
3+
// revisions: current next
4+
//[next] compile-flags: -Ztrait-solver=next
5+
6+
#![feature(trait_upcasting)]
7+
8+
trait Mirror {
9+
type Assoc;
10+
}
11+
impl<T> Mirror for T {
12+
type Assoc = T;
13+
}
14+
15+
trait Bar<T> {}
16+
trait Foo<T>: Bar<<T as Mirror>::Assoc> {}
17+
18+
fn upcast<T>(x: &dyn Foo<T>) -> &dyn Bar<T> { x }
19+
20+
fn main() {}

0 commit comments

Comments
 (0)