From 9f44caa8579d69e829d89ffaae0260c85141fdfc Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 10 Jan 2025 04:36:11 +0000 Subject: [PATCH 1/2] Do not treat vtable supertraits as distinct when bound with different bound vars --- src/constant.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/constant.rs b/src/constant.rs index 3e7b81a96b68..05ba53430a8a 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -245,7 +245,10 @@ pub(crate) fn data_id_for_vtable<'tcx>( ty: Ty<'tcx>, trait_ref: Option>>, ) -> DataId { - let alloc_id = tcx.vtable_allocation((ty, trait_ref)); + let alloc_id = tcx.vtable_allocation(( + ty, + trait_ref.map(|principal| tcx.instantiate_bound_regions_with_erased(principal)), + )); data_id_for_alloc_id(cx, module, alloc_id, Mutability::Not) } From 96bea7ac909c021538358f66ed9ef77895a1883f Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 10 Jan 2025 20:26:10 +0000 Subject: [PATCH 2/2] Use ExistentialTraitRef throughout codegen --- src/constant.rs | 25 +++++++++++++++---------- src/unsize.rs | 7 ++++++- src/vtable.rs | 2 +- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/constant.rs b/src/constant.rs index 05ba53430a8a..425b2adf32a3 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -6,7 +6,7 @@ use cranelift_module::*; use rustc_data_structures::fx::FxHashSet; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; use rustc_middle::mir::interpret::{AllocId, GlobalAlloc, Scalar, read_target_uint}; -use rustc_middle::ty::{Binder, ExistentialTraitRef, ScalarInt}; +use rustc_middle::ty::{ExistentialTraitRef, ScalarInt}; use crate::prelude::*; @@ -167,7 +167,9 @@ pub(crate) fn codegen_const_value<'tcx>( &mut fx.constants_cx, fx.module, ty, - dyn_ty.principal(), + dyn_ty.principal().map(|principal| { + fx.tcx.instantiate_bound_regions_with_erased(principal) + }), ); let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func); @@ -243,12 +245,9 @@ pub(crate) fn data_id_for_vtable<'tcx>( cx: &mut ConstantCx, module: &mut dyn Module, ty: Ty<'tcx>, - trait_ref: Option>>, + trait_ref: Option>, ) -> DataId { - let alloc_id = tcx.vtable_allocation(( - ty, - trait_ref.map(|principal| tcx.instantiate_bound_regions_with_erased(principal)), - )); + let alloc_id = tcx.vtable_allocation((ty, trait_ref)); data_id_for_alloc_id(cx, module, alloc_id, Mutability::Not) } @@ -463,9 +462,15 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant GlobalAlloc::Memory(target_alloc) => { data_id_for_alloc_id(cx, module, alloc_id, target_alloc.inner().mutability) } - GlobalAlloc::VTable(ty, dyn_ty) => { - data_id_for_vtable(tcx, cx, module, ty, dyn_ty.principal()) - } + GlobalAlloc::VTable(ty, dyn_ty) => data_id_for_vtable( + tcx, + cx, + module, + ty, + dyn_ty + .principal() + .map(|principal| tcx.instantiate_bound_regions_with_erased(principal)), + ), GlobalAlloc::Static(def_id) => { if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) { diff --git a/src/unsize.rs b/src/unsize.rs index 2843e5bbdfb2..f8bbb2149201 100644 --- a/src/unsize.rs +++ b/src/unsize.rs @@ -61,7 +61,12 @@ pub(crate) fn unsized_info<'tcx>( old_info } } - (_, ty::Dynamic(data, ..)) => crate::vtable::get_vtable(fx, source, data.principal()), + (_, ty::Dynamic(data, ..)) => crate::vtable::get_vtable( + fx, + source, + data.principal() + .map(|principal| fx.tcx.instantiate_bound_regions_with_erased(principal)), + ), _ => bug!("unsized_info: invalid unsizing {:?} -> {:?}", source, target), } } diff --git a/src/vtable.rs b/src/vtable.rs index 82b6178be9dc..a460023b59cb 100644 --- a/src/vtable.rs +++ b/src/vtable.rs @@ -90,7 +90,7 @@ pub(crate) fn get_ptr_and_method_ref<'tcx>( pub(crate) fn get_vtable<'tcx>( fx: &mut FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>, - trait_ref: Option>, + trait_ref: Option>, ) -> Value { let data_id = data_id_for_vtable(fx.tcx, &mut fx.constants_cx, fx.module, ty, trait_ref); let local_data_id = fx.module.declare_data_in_func(data_id, fx.bcx.func);