From 238beae5e588fb3dc4550d970085b5bf29f40776 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 27 Jul 2023 01:00:01 +0000 Subject: [PATCH] Fix upcasting with normalization in old solver, add a test --- .../src/traits/select/confirmation.rs | 14 +++++++++++-- .../traits/trait-upcasting/normalization.rs | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/ui/traits/trait-upcasting/normalization.rs diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 3cf2735b46bd..3d6df08f6db3 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -887,9 +887,19 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let ty::Dynamic(b_data, b_region, ty::Dyn) = *b_ty.kind() else { bug!() }; let source_principal = a_data.principal().unwrap().with_self_ty(tcx, a_ty); - let upcast_principal = util::supertraits(tcx, source_principal).nth(idx).unwrap(); + let unnormalized_upcast_principal = + util::supertraits(tcx, source_principal).nth(idx).unwrap(); let mut nested = vec![]; + let upcast_principal = normalize_with_depth_to( + self, + obligation.param_env, + obligation.cause.clone(), + obligation.recursion_depth + 1, + unnormalized_upcast_principal, + &mut nested, + ); + for bound in b_data { match bound.skip_binder() { // Check that a's supertrait (upcast_principal) is compatible @@ -973,7 +983,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => { vptr_offset += count_own_vtable_entries(tcx, trait_ref); - if trait_ref == upcast_principal { + if trait_ref == unnormalized_upcast_principal { if emit_vptr { return ControlFlow::Break(Some(vptr_offset)); } else { diff --git a/tests/ui/traits/trait-upcasting/normalization.rs b/tests/ui/traits/trait-upcasting/normalization.rs new file mode 100644 index 000000000000..c78338b0da93 --- /dev/null +++ b/tests/ui/traits/trait-upcasting/normalization.rs @@ -0,0 +1,20 @@ +// check-pass +// issue: 114113 +// revisions: current next +//[next] compile-flags: -Ztrait-solver=next + +#![feature(trait_upcasting)] + +trait Mirror { + type Assoc; +} +impl Mirror for T { + type Assoc = T; +} + +trait Bar {} +trait Foo: Bar<::Assoc> {} + +fn upcast(x: &dyn Foo) -> &dyn Bar { x } + +fn main() {}