Skip to content

Commit fdc4bd2

Browse files
Do not treat vtable supertraits as distinct when bound with different bound vars
1 parent 37a430e commit fdc4bd2

File tree

18 files changed

+146
-115
lines changed

18 files changed

+146
-115
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/constant.rs

+4-1
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

Diff for: compiler/rustc_codegen_gcc/src/common.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,12 @@ impl<'gcc, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
234234
GlobalAlloc::VTable(ty, dyn_ty) => {
235235
let alloc = self
236236
.tcx
237-
.global_alloc(self.tcx.vtable_allocation((ty, dyn_ty.principal())))
237+
.global_alloc(self.tcx.vtable_allocation((
238+
ty,
239+
dyn_ty.principal().map(|principal| {
240+
self.tcx.instantiate_bound_regions_with_erased(principal)
241+
}),
242+
)))
238243
.unwrap_memory();
239244
let init = const_alloc_to_gcc(self, alloc);
240245
self.static_addr_of(init, alloc.inner().align, None)

Diff for: compiler/rustc_codegen_llvm/src/common.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,12 @@ impl<'ll, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
312312
GlobalAlloc::VTable(ty, dyn_ty) => {
313313
let alloc = self
314314
.tcx
315-
.global_alloc(self.tcx.vtable_allocation((ty, dyn_ty.principal())))
315+
.global_alloc(self.tcx.vtable_allocation((
316+
ty,
317+
dyn_ty.principal().map(|principal| {
318+
self.tcx.instantiate_bound_regions_with_erased(principal)
319+
}),
320+
)))
316321
.unwrap_memory();
317322
let init = const_alloc_to_llvm(self, alloc, /*static*/ false);
318323
let value = self.static_addr_of(init, alloc.inner().align, None);

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+1-1
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 {

Diff for: compiler/rustc_codegen_ssa/src/meth.rs

+9-5
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

Diff for: compiler/rustc_const_eval/src/interpret/cast.rs

+1-9
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

Diff for: compiler/rustc_const_eval/src/interpret/traits.rs

+2-1
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

Diff for: compiler/rustc_hir_analysis/src/collect/dump.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
123123
);
124124
continue;
125125
};
126-
tcx.vtable_entries(ty::Binder::dummy(trait_ref))
126+
tcx.vtable_entries(trait_ref)
127127
}
128128
hir::ItemKind::TyAlias(_, _) => {
129129
let ty = tcx.type_of(def_id).instantiate_identity();
@@ -149,7 +149,7 @@ pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
149149
};
150150
if let Some(principal) = data.principal() {
151151
tcx.vtable_entries(
152-
principal.map_bound(|principal| principal.with_self_ty(tcx, ty)),
152+
tcx.instantiate_bound_regions_with_erased(principal).with_self_ty(tcx, ty),
153153
)
154154
} else {
155155
TyCtxt::COMMON_VTABLE_ENTRIES

Diff for: compiler/rustc_middle/src/query/keys.rs

+1-1
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 {

Diff for: compiler/rustc_middle/src/query/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1437,9 +1437,9 @@ rustc_queries! {
14371437
desc { |tcx| "finding all existential vtable entries for trait `{}`", tcx.def_path_str(key) }
14381438
}
14391439

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

14451445
query first_method_vtable_slot(key: ty::TraitRef<'tcx>) -> usize {
@@ -1451,7 +1451,7 @@ rustc_queries! {
14511451
key.1, key.0 }
14521452
}
14531453

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

Diff for: compiler/rustc_middle/src/ty/layout.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,12 @@ where
855855
}
856856

857857
let mk_dyn_vtable = |principal: Option<ty::PolyExistentialTraitRef<'tcx>>| {
858-
let min_count = ty::vtable_min_entries(tcx, principal);
858+
let min_count = ty::vtable_min_entries(
859+
tcx,
860+
principal.map(|principal| {
861+
tcx.instantiate_bound_regions_with_erased(principal)
862+
}),
863+
);
859864
Ty::new_imm_ref(
860865
tcx,
861866
tcx.lifetimes.re_static,

Diff for: compiler/rustc_middle/src/ty/vtable.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_type_ir::elaborate;
77
use crate::mir::interpret::{
88
AllocId, AllocInit, Allocation, CTFE_ALLOC_SALT, Pointer, Scalar, alloc_range,
99
};
10-
use crate::ty::{self, Instance, PolyTraitRef, Ty, TyCtxt};
10+
use crate::ty::{self, Instance, TraitRef, Ty, TyCtxt};
1111

1212
#[derive(Clone, Copy, PartialEq, HashStable)]
1313
pub enum VtblEntry<'tcx> {
@@ -22,7 +22,7 @@ pub enum VtblEntry<'tcx> {
2222
/// dispatchable associated function
2323
Method(Instance<'tcx>),
2424
/// pointer to a separate supertrait vtable, can be used by trait upcasting coercion
25-
TraitVPtr(PolyTraitRef<'tcx>),
25+
TraitVPtr(TraitRef<'tcx>),
2626
}
2727

2828
impl<'tcx> fmt::Debug for VtblEntry<'tcx> {
@@ -59,15 +59,15 @@ pub const COMMON_VTABLE_ENTRIES_ALIGN: usize = 2;
5959
// function is an accurate approximation. We verify this when actually computing the vtable below.
6060
pub(crate) fn vtable_min_entries<'tcx>(
6161
tcx: TyCtxt<'tcx>,
62-
trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
62+
trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
6363
) -> usize {
6464
let mut count = TyCtxt::COMMON_VTABLE_ENTRIES.len();
6565
let Some(trait_ref) = trait_ref else {
6666
return count;
6767
};
6868

6969
// This includes self in supertraits.
70-
for def_id in elaborate::supertrait_def_ids(tcx, trait_ref.def_id()) {
70+
for def_id in elaborate::supertrait_def_ids(tcx, trait_ref.def_id) {
7171
count += tcx.own_existential_vtable_entries(def_id).len();
7272
}
7373

@@ -83,7 +83,7 @@ pub(crate) fn vtable_min_entries<'tcx>(
8383
/// initial contents.)
8484
pub(super) fn vtable_allocation_provider<'tcx>(
8585
tcx: TyCtxt<'tcx>,
86-
key: (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>),
86+
key: (Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>),
8787
) -> AllocId {
8888
let (ty, poly_trait_ref) = key;
8989

@@ -118,7 +118,7 @@ pub(super) fn vtable_allocation_provider<'tcx>(
118118

119119
for (idx, entry) in vtable_entries.iter().enumerate() {
120120
let idx: u64 = u64::try_from(idx).unwrap();
121-
let scalar = match entry {
121+
let scalar = match *entry {
122122
VtblEntry::MetadataDropInPlace => {
123123
if ty.needs_drop(tcx, ty::TypingEnv::fully_monomorphized()) {
124124
let instance = ty::Instance::resolve_drop_in_place(tcx, ty);
@@ -134,13 +134,12 @@ pub(super) fn vtable_allocation_provider<'tcx>(
134134
VtblEntry::Vacant => continue,
135135
VtblEntry::Method(instance) => {
136136
// Prepare the fn ptr we write into the vtable.
137-
let fn_alloc_id = tcx.reserve_and_set_fn_alloc(*instance, CTFE_ALLOC_SALT);
137+
let fn_alloc_id = tcx.reserve_and_set_fn_alloc(instance, CTFE_ALLOC_SALT);
138138
let fn_ptr = Pointer::from(fn_alloc_id);
139139
Scalar::from_pointer(fn_ptr, &tcx)
140140
}
141141
VtblEntry::TraitVPtr(trait_ref) => {
142-
let super_trait_ref = trait_ref
143-
.map_bound(|trait_ref| ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref));
142+
let super_trait_ref = ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref);
144143
let supertrait_alloc_id = tcx.vtable_allocation((ty, Some(super_trait_ref)));
145144
let vptr = Pointer::from(supertrait_alloc_id);
146145
Scalar::from_pointer(vptr, &tcx)

Diff for: compiler/rustc_monomorphize/src/collector.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1138,11 +1138,12 @@ fn create_mono_items_for_vtable_methods<'tcx>(
11381138
bug!("create_mono_items_for_vtable_methods: {trait_ty:?} not a trait type");
11391139
};
11401140
if let Some(principal) = trait_ty.principal() {
1141-
let poly_trait_ref = principal.with_self_ty(tcx, impl_ty);
1142-
assert!(!poly_trait_ref.has_escaping_bound_vars());
1141+
let trait_ref =
1142+
tcx.instantiate_bound_regions_with_erased(principal.with_self_ty(tcx, impl_ty));
1143+
assert!(!trait_ref.has_escaping_bound_vars());
11431144

11441145
// Walk all methods of the trait, including those of its supertraits
1145-
let entries = tcx.vtable_entries(poly_trait_ref);
1146+
let entries = tcx.vtable_entries(trait_ref);
11461147
debug!(?entries);
11471148
let methods = entries
11481149
.iter()
@@ -1197,7 +1198,12 @@ fn collect_alloc<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, output: &mut MonoIt
11971198
}
11981199
}
11991200
GlobalAlloc::VTable(ty, dyn_ty) => {
1200-
let alloc_id = tcx.vtable_allocation((ty, dyn_ty.principal()));
1201+
let alloc_id = tcx.vtable_allocation((
1202+
ty,
1203+
dyn_ty
1204+
.principal()
1205+
.map(|principal| tcx.instantiate_bound_regions_with_erased(principal)),
1206+
));
12011207
collect_alloc(tcx, alloc_id, output)
12021208
}
12031209
}

Diff for: compiler/rustc_smir/src/rustc_smir/context.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,9 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
746746
let tcx = tables.tcx;
747747
let alloc_id = tables.tcx.vtable_allocation((
748748
ty.internal(&mut *tables, tcx),
749-
trait_ref.internal(&mut *tables, tcx),
749+
trait_ref
750+
.internal(&mut *tables, tcx)
751+
.map(|principal| tcx.instantiate_bound_regions_with_erased(principal)),
750752
));
751753
Some(alloc_id.stable(&mut *tables))
752754
}

0 commit comments

Comments
 (0)