@@ -195,9 +195,13 @@ pub(crate) fn codegen_const_value<'tcx>(
195
195
}
196
196
Scalar :: Ptr ( ptr, _size) => {
197
197
let ( alloc_id, offset) = ptr. into_parts ( ) ; // we know the `offset` is relative
198
- let alloc_kind = fx. tcx . get_global_alloc ( alloc_id) ;
199
- let base_addr = match alloc_kind {
200
- Some ( GlobalAlloc :: Memory ( alloc) ) => {
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
+ } ;
203
+ let base_addr = match fx. tcx . global_alloc ( alloc_id) {
204
+ GlobalAlloc :: Memory ( alloc) => {
201
205
let data_id = data_id_for_alloc_id (
202
206
& mut fx. constants_cx ,
203
207
fx. module ,
@@ -211,13 +215,14 @@ pub(crate) fn codegen_const_value<'tcx>(
211
215
}
212
216
fx. bcx . ins ( ) . global_value ( fx. pointer_type , local_data_id)
213
217
}
214
- Some ( GlobalAlloc :: Function ( instance) ) => {
218
+ GlobalAlloc :: Function ( instance) => {
215
219
let func_id = crate :: abi:: import_function ( fx. tcx , fx. module , instance) ;
216
220
let local_func_id =
217
221
fx. module . declare_func_in_func ( func_id, & mut fx. bcx . func ) ;
218
222
fx. bcx . ins ( ) . func_addr ( fx. pointer_type , local_func_id)
219
223
}
220
- Some ( GlobalAlloc :: Static ( def_id) ) => {
224
+ GlobalAlloc :: Vtable ( ..) => bug ! ( "vtables are already handled" ) ,
225
+ GlobalAlloc :: Static ( def_id) => {
221
226
assert ! ( fx. tcx. is_static( def_id) ) ;
222
227
let data_id = data_id_for_static ( fx. tcx , fx. module , def_id, false ) ;
223
228
let local_data_id =
@@ -227,7 +232,6 @@ pub(crate) fn codegen_const_value<'tcx>(
227
232
}
228
233
fx. bcx . ins ( ) . global_value ( fx. pointer_type , local_data_id)
229
234
}
230
- None => bug ! ( "missing allocation {:?}" , alloc_id) ,
231
235
} ;
232
236
let val = if offset. bytes ( ) != 0 {
233
237
fx. bcx . ins ( ) . iadd_imm ( base_addr, i64:: try_from ( offset. bytes ( ) ) . unwrap ( ) )
@@ -360,7 +364,9 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
360
364
//println!("alloc_id {}", alloc_id);
361
365
let alloc = match tcx. get_global_alloc ( alloc_id) . unwrap ( ) {
362
366
GlobalAlloc :: Memory ( alloc) => alloc,
363
- GlobalAlloc :: Function ( _) | GlobalAlloc :: Static ( _) => unreachable ! ( ) ,
367
+ GlobalAlloc :: Function ( _) | GlobalAlloc :: Static ( _) | GlobalAlloc :: Vtable ( ..) => {
368
+ unreachable ! ( )
369
+ }
364
370
} ;
365
371
let data_id = * cx. anon_allocs . entry ( alloc_id) . or_insert_with ( || {
366
372
module
@@ -424,7 +430,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
424
430
read_target_uint ( endianness, bytes) . unwrap ( )
425
431
} ;
426
432
427
- let reloc_target_alloc = tcx. get_global_alloc ( alloc_id) . unwrap ( ) ;
433
+ let reloc_target_alloc = tcx. global_alloc ( alloc_id) ;
428
434
let data_id = match reloc_target_alloc {
429
435
GlobalAlloc :: Function ( instance) => {
430
436
assert_eq ! ( addend, 0 ) ;
@@ -436,6 +442,10 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
436
442
GlobalAlloc :: Memory ( target_alloc) => {
437
443
data_id_for_alloc_id ( cx, module, alloc_id, target_alloc. inner ( ) . mutability )
438
444
}
445
+ GlobalAlloc :: Vtable ( ty, trait_ref) => {
446
+ let alloc_id = tcx. vtable_allocation ( ( ty, trait_ref) ) ;
447
+ data_id_for_alloc_id ( cx, module, alloc_id, Mutability :: Not )
448
+ }
439
449
GlobalAlloc :: Static ( def_id) => {
440
450
if tcx. codegen_fn_attrs ( def_id) . flags . contains ( CodegenFnAttrFlags :: THREAD_LOCAL )
441
451
{
0 commit comments