@@ -103,6 +103,7 @@ enum AllocDiscriminant {
103
103
Fn ,
104
104
VTable ,
105
105
Static ,
106
+ Type ,
106
107
}
107
108
108
109
pub fn specialized_encode_alloc_id < ' tcx , E : TyEncoder < ' tcx > > (
@@ -127,6 +128,11 @@ pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder<'tcx>>(
127
128
ty. encode ( encoder) ;
128
129
poly_trait_ref. encode ( encoder) ;
129
130
}
131
+ GlobalAlloc :: Type ( ty) => {
132
+ trace ! ( "encoding {alloc_id:?} with {ty:#?}" ) ;
133
+ AllocDiscriminant :: Type . encode ( encoder) ;
134
+ ty. encode ( encoder) ;
135
+ }
130
136
GlobalAlloc :: Static ( did) => {
131
137
assert ! ( !tcx. is_thread_local_static( did) ) ;
132
138
// References to statics doesn't need to know about their allocations,
@@ -228,6 +234,12 @@ impl<'s> AllocDecodingSession<'s> {
228
234
trace ! ( "decoded vtable alloc instance: {ty:?}, {poly_trait_ref:?}" ) ;
229
235
decoder. interner ( ) . reserve_and_set_vtable_alloc ( ty, poly_trait_ref, CTFE_ALLOC_SALT )
230
236
}
237
+ AllocDiscriminant :: Type => {
238
+ trace ! ( "creating typeid alloc ID" ) ;
239
+ let ty = Decodable :: decode ( decoder) ;
240
+ trace ! ( "decoded typid: {ty:?}" ) ;
241
+ decoder. interner ( ) . reserve_and_set_type_id_alloc ( ty)
242
+ }
231
243
AllocDiscriminant :: Static => {
232
244
trace ! ( "creating extern static alloc ID" ) ;
233
245
let did = <DefId as Decodable < D > >:: decode ( decoder) ;
@@ -258,6 +270,9 @@ pub enum GlobalAlloc<'tcx> {
258
270
Static ( DefId ) ,
259
271
/// The alloc ID points to memory.
260
272
Memory ( ConstAllocation < ' tcx > ) ,
273
+ /// A TypeId pointer. For now cannot be turned into a runtime value.
274
+ /// TODO: turn into actual TypeId?
275
+ Type ( Ty < ' tcx > ) ,
261
276
}
262
277
263
278
impl < ' tcx > GlobalAlloc < ' tcx > {
@@ -296,9 +311,10 @@ impl<'tcx> GlobalAlloc<'tcx> {
296
311
pub fn address_space ( & self , cx : & impl HasDataLayout ) -> AddressSpace {
297
312
match self {
298
313
GlobalAlloc :: Function { .. } => cx. data_layout ( ) . instruction_address_space ,
299
- GlobalAlloc :: Static ( ..) | GlobalAlloc :: Memory ( ..) | GlobalAlloc :: VTable ( ..) => {
300
- AddressSpace :: DATA
301
- }
314
+ GlobalAlloc :: Type ( _)
315
+ | GlobalAlloc :: Static ( ..)
316
+ | GlobalAlloc :: Memory ( ..)
317
+ | GlobalAlloc :: VTable ( ..) => AddressSpace :: DATA ,
302
318
}
303
319
}
304
320
@@ -334,7 +350,7 @@ impl<'tcx> GlobalAlloc<'tcx> {
334
350
}
335
351
}
336
352
GlobalAlloc :: Memory ( alloc) => alloc. inner ( ) . mutability ,
337
- GlobalAlloc :: Function { .. } | GlobalAlloc :: VTable ( ..) => {
353
+ GlobalAlloc :: Type ( _ ) | GlobalAlloc :: Function { .. } | GlobalAlloc :: VTable ( ..) => {
338
354
// These are immutable.
339
355
Mutability :: Not
340
356
}
@@ -380,8 +396,10 @@ impl<'tcx> GlobalAlloc<'tcx> {
380
396
GlobalAlloc :: Function { .. } => ( Size :: ZERO , Align :: ONE ) ,
381
397
GlobalAlloc :: VTable ( ..) => {
382
398
// No data to be accessed here. But vtables are pointer-aligned.
383
- return ( Size :: ZERO , tcx. data_layout . pointer_align . abi ) ;
399
+ ( Size :: ZERO , tcx. data_layout . pointer_align . abi )
384
400
}
401
+ // TODO make this represent normal type ids somehow
402
+ GlobalAlloc :: Type ( _) => ( Size :: from_bytes ( 16 ) , Align :: from_bytes ( 8 ) . unwrap ( ) ) ,
385
403
}
386
404
}
387
405
}
@@ -487,6 +505,11 @@ impl<'tcx> TyCtxt<'tcx> {
487
505
self . reserve_and_set_dedup ( GlobalAlloc :: VTable ( ty, dyn_ty) , salt)
488
506
}
489
507
508
+ /// Generates an [AllocId] for a [core::mem::type_info::TypeId]. Will get deduplicated.
509
+ pub fn reserve_and_set_type_id_alloc ( self , ty : Ty < ' tcx > ) -> AllocId {
510
+ self . reserve_and_set_dedup ( GlobalAlloc :: Type ( ty) , 0 )
511
+ }
512
+
490
513
/// Interns the `Allocation` and return a new `AllocId`, even if there's already an identical
491
514
/// `Allocation` with a different `AllocId`.
492
515
/// Statics with identical content will still point to the same `Allocation`, i.e.,
0 commit comments