Skip to content

Commit 355bf65

Browse files
Do not treat vtable supertraits as distinct when bound with different bound vars
1 parent 62bf38f commit 355bf65

File tree

18 files changed

+151
-118
lines changed

18 files changed

+151
-118
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ pub(crate) fn data_id_for_vtable<'tcx>(
245245
ty: Ty<'tcx>,
246246
trait_ref: Option<Binder<'tcx, ExistentialTraitRef<'tcx>>>,
247247
) -> DataId {
248-
let alloc_id = tcx.vtable_allocation((ty, trait_ref));
248+
let alloc_id = tcx.vtable_allocation((
249+
ty,
250+
trait_ref.map(|principal| tcx.instantiate_bound_regions_with_erased(principal)),
251+
));
249252
data_id_for_alloc_id(cx, module, alloc_id, Mutability::Not)
250253
}
251254

compiler/rustc_codegen_gcc/src/common.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,12 @@ impl<'gcc, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
229229
GlobalAlloc::VTable(ty, dyn_ty) => {
230230
let alloc = self
231231
.tcx
232-
.global_alloc(self.tcx.vtable_allocation((ty, dyn_ty.principal())))
232+
.global_alloc(self.tcx.vtable_allocation((
233+
ty,
234+
dyn_ty.principal().map(|principal| {
235+
self.tcx.instantiate_bound_regions_with_erased(principal)
236+
}),
237+
)))
233238
.unwrap_memory();
234239
let init = const_alloc_to_gcc(self, alloc);
235240
self.static_addr_of(init, alloc.inner().align, None)

compiler/rustc_codegen_llvm/src/common.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,12 @@ impl<'ll, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
308308
GlobalAlloc::VTable(ty, dyn_ty) => {
309309
let alloc = self
310310
.tcx
311-
.global_alloc(self.tcx.vtable_allocation((ty, dyn_ty.principal())))
311+
.global_alloc(self.tcx.vtable_allocation((
312+
ty,
313+
dyn_ty.principal().map(|principal| {
314+
self.tcx.instantiate_bound_regions_with_erased(principal)
315+
}),
316+
)))
312317
.unwrap_memory();
313318
let init = const_alloc_to_llvm(self, alloc, /*static*/ false);
314319
let value = self.static_addr_of(init, alloc.inner().align, None);

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ fn build_vtable_type_di_node<'ll, 'tcx>(
14061406

14071407
let vtable_entries = if let Some(poly_trait_ref) = poly_trait_ref {
14081408
let trait_ref = poly_trait_ref.with_self_ty(tcx, ty);
1409-
let trait_ref = tcx.erase_regions(trait_ref);
1409+
let trait_ref = tcx.erase_regions(tcx.instantiate_bound_regions_with_erased(trait_ref));
14101410

14111411
tcx.vtable_entries(trait_ref)
14121412
} else {

compiler/rustc_codegen_ssa/src/meth.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,28 @@ fn dyn_trait_in_self(ty: Ty<'_>) -> Option<ty::PolyExistentialTraitRef<'_>> {
9696
pub(crate) fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
9797
cx: &Cx,
9898
ty: Ty<'tcx>,
99-
trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
99+
poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
100100
) -> Cx::Value {
101101
let tcx = cx.tcx();
102102

103103
// Check the cache.
104-
if let Some(&val) = cx.vtables().borrow().get(&(ty, trait_ref)) {
104+
if let Some(&val) = cx.vtables().borrow().get(&(ty, poly_trait_ref)) {
105105
return val;
106106
}
107107

108+
// FIXME(trait_upcasting): Take a non-higher-ranked vtable as arg.
109+
let trait_ref =
110+
poly_trait_ref.map(|trait_ref| tcx.instantiate_bound_regions_with_erased(trait_ref));
111+
108112
let vtable_alloc_id = tcx.vtable_allocation((ty, trait_ref));
109113
let vtable_allocation = tcx.global_alloc(vtable_alloc_id).unwrap_memory();
110114
let vtable_const = cx.const_data_from_alloc(vtable_allocation);
111115
let align = cx.data_layout().pointer_align.abi;
112116
let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
113117

114-
cx.apply_vcall_visibility_metadata(ty, trait_ref, vtable);
115-
cx.create_vtable_debuginfo(ty, trait_ref, vtable);
116-
cx.vtables().borrow_mut().insert((ty, trait_ref), vtable);
118+
cx.apply_vcall_visibility_metadata(ty, poly_trait_ref, vtable);
119+
cx.create_vtable_debuginfo(ty, poly_trait_ref, vtable);
120+
cx.vtables().borrow_mut().insert((ty, poly_trait_ref), vtable);
117121
vtable
118122
}
119123

compiler/rustc_const_eval/src/interpret/cast.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
419419
self.tcx.supertrait_vtable_slot((src_pointee_ty, dest_pointee_ty));
420420
let vtable_entries = self.vtable_entries(data_a.principal(), ty);
421421
if let Some(entry_idx) = vptr_entry_idx {
422-
let Some(&ty::VtblEntry::TraitVPtr(upcast_trait_ref)) =
423-
vtable_entries.get(entry_idx)
422+
let Some(&ty::VtblEntry::TraitVPtr(_)) = vtable_entries.get(entry_idx)
424423
else {
425424
span_bug!(
426425
self.cur_span(),
@@ -429,13 +428,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
429428
dest_pointee_ty
430429
);
431430
};
432-
let erased_trait_ref = upcast_trait_ref
433-
.map_bound(|r| ty::ExistentialTraitRef::erase_self_ty(*self.tcx, r));
434-
assert!(
435-
data_b
436-
.principal()
437-
.is_some_and(|b| self.eq_in_param_env(erased_trait_ref, b))
438-
);
439431
} else {
440432
// In this case codegen would keep using the old vtable. We don't want to do
441433
// that as it has the wrong trait. The reason codegen can do this is that

compiler/rustc_const_eval/src/interpret/traits.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
5454
) -> &'tcx [VtblEntry<'tcx>] {
5555
if let Some(trait_) = trait_ {
5656
let trait_ref = trait_.with_self_ty(*self.tcx, dyn_ty);
57-
let trait_ref = self.tcx.erase_regions(trait_ref);
57+
let trait_ref =
58+
self.tcx.erase_regions(self.tcx.instantiate_bound_regions_with_erased(trait_ref));
5859
self.tcx.vtable_entries(trait_ref)
5960
} else {
6061
TyCtxt::COMMON_VTABLE_ENTRIES

compiler/rustc_interface/src/passes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) {
10031003
// Number of vtable entries needed solely for upcasting
10041004
let mut entries_for_upcasting = 0;
10051005

1006-
let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(tcx, tr));
1006+
let trait_ref = ty::TraitRef::identity(tcx, tr);
10071007

10081008
// A slightly edited version of the code in
10091009
// `rustc_trait_selection::traits::vtable::vtable_entries`, that works without self
@@ -1041,7 +1041,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) {
10411041
traits::vtable::VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => {
10421042
// Lookup the shape of vtable for the trait.
10431043
let own_existential_entries =
1044-
tcx.own_existential_vtable_entries(trait_ref.def_id());
1044+
tcx.own_existential_vtable_entries(trait_ref.def_id);
10451045

10461046
// The original code here ignores the method if its predicates are
10471047
// impossible. We can't really do that as, for example, all not trivial

compiler/rustc_middle/src/query/keys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
9595
}
9696
}
9797

98-
impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
98+
impl<'tcx> Key for (Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>) {
9999
type Cache<V> = DefaultCache<Self, V>;
100100

101101
fn default_span(&self, _: TyCtxt<'_>) -> Span {

compiler/rustc_middle/src/query/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,9 +1435,9 @@ rustc_queries! {
14351435
desc { |tcx| "finding all existential vtable entries for trait `{}`", tcx.def_path_str(key) }
14361436
}
14371437

1438-
query vtable_entries(key: ty::PolyTraitRef<'tcx>)
1438+
query vtable_entries(key: ty::TraitRef<'tcx>)
14391439
-> &'tcx [ty::VtblEntry<'tcx>] {
1440-
desc { |tcx| "finding all vtable entries for trait `{}`", tcx.def_path_str(key.def_id()) }
1440+
desc { |tcx| "finding all vtable entries for trait `{}`", tcx.def_path_str(key.def_id) }
14411441
}
14421442

14431443
query first_method_vtable_slot(key: ty::TraitRef<'tcx>) -> usize {
@@ -1449,7 +1449,7 @@ rustc_queries! {
14491449
key.1, key.0 }
14501450
}
14511451

1452-
query vtable_allocation(key: (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>)) -> mir::interpret::AllocId {
1452+
query vtable_allocation(key: (Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>)) -> mir::interpret::AllocId {
14531453
desc { |tcx| "vtable const allocation for <{} as {}>",
14541454
key.0,
14551455
key.1.map(|trait_ref| format!("{trait_ref}")).unwrap_or("_".to_owned())

0 commit comments

Comments
 (0)