Skip to content

Commit 6c9abfa

Browse files
committed
slightly cleaner, if more verbose, vtable handling in codegen backends
1 parent a1b1722 commit 6c9abfa

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/constant.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,6 @@ pub(crate) fn codegen_const_value<'tcx>(
195195
}
196196
Scalar::Ptr(ptr, _size) => {
197197
let (alloc_id, offset) = ptr.into_parts(); // we know the `offset` is relative
198-
// For vtables, get the underlying data allocation.
199-
let alloc_id = match fx.tcx.global_alloc(alloc_id) {
200-
GlobalAlloc::VTable(ty, trait_ref) => fx.tcx.vtable_allocation((ty, trait_ref)),
201-
_ => alloc_id,
202-
};
203198
let base_addr = match fx.tcx.global_alloc(alloc_id) {
204199
GlobalAlloc::Memory(alloc) => {
205200
let data_id = data_id_for_alloc_id(
@@ -221,7 +216,20 @@ pub(crate) fn codegen_const_value<'tcx>(
221216
fx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
222217
fx.bcx.ins().func_addr(fx.pointer_type, local_func_id)
223218
}
224-
GlobalAlloc::VTable(..) => bug!("vtables are already handled"),
219+
GlobalAlloc::VTable(ty, trait_ref) => {
220+
let alloc_id = fx.tcx.vtable_allocation((ty, trait_ref));
221+
let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
222+
// FIXME: factor this common code with the `Memory` arm into a function?
223+
let data_id = data_id_for_alloc_id(
224+
&mut fx.constants_cx,
225+
fx.module,
226+
alloc_id,
227+
alloc.inner().mutability,
228+
);
229+
let local_data_id =
230+
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
231+
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
232+
}
225233
GlobalAlloc::Static(def_id) => {
226234
assert!(fx.tcx.is_static(def_id));
227235
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);

0 commit comments

Comments
 (0)