@@ -103,6 +103,7 @@ enum AllocDiscriminant {
103103 Fn ,
104104 VTable ,
105105 Static ,
106+ Type ,
106107}
107108
108109pub fn specialized_encode_alloc_id < ' tcx , E : TyEncoder < ' tcx > > (
@@ -127,6 +128,11 @@ pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder<'tcx>>(
127128 ty. encode ( encoder) ;
128129 poly_trait_ref. encode ( encoder) ;
129130 }
131+ GlobalAlloc :: Type ( ty) => {
132+ trace ! ( "encoding {alloc_id:?} with {ty:#?}" ) ;
133+ AllocDiscriminant :: Type . encode ( encoder) ;
134+ ty. encode ( encoder) ;
135+ }
130136 GlobalAlloc :: Static ( did) => {
131137 assert ! ( !tcx. is_thread_local_static( did) ) ;
132138 // References to statics doesn't need to know about their allocations,
@@ -228,6 +234,12 @@ impl<'s> AllocDecodingSession<'s> {
228234 trace ! ( "decoded vtable alloc instance: {ty:?}, {poly_trait_ref:?}" ) ;
229235 decoder. interner ( ) . reserve_and_set_vtable_alloc ( ty, poly_trait_ref, CTFE_ALLOC_SALT )
230236 }
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+ }
231243 AllocDiscriminant :: Static => {
232244 trace ! ( "creating extern static alloc ID" ) ;
233245 let did = <DefId as Decodable < D > >:: decode ( decoder) ;
@@ -258,6 +270,9 @@ pub enum GlobalAlloc<'tcx> {
258270 Static ( DefId ) ,
259271 /// The alloc ID points to memory.
260272 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 > ) ,
261276}
262277
263278impl < ' tcx > GlobalAlloc < ' tcx > {
@@ -296,9 +311,10 @@ impl<'tcx> GlobalAlloc<'tcx> {
296311 pub fn address_space ( & self , cx : & impl HasDataLayout ) -> AddressSpace {
297312 match self {
298313 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 ,
302318 }
303319 }
304320
@@ -334,7 +350,7 @@ impl<'tcx> GlobalAlloc<'tcx> {
334350 }
335351 }
336352 GlobalAlloc :: Memory ( alloc) => alloc. inner ( ) . mutability ,
337- GlobalAlloc :: Function { .. } | GlobalAlloc :: VTable ( ..) => {
353+ GlobalAlloc :: Type ( _ ) | GlobalAlloc :: Function { .. } | GlobalAlloc :: VTable ( ..) => {
338354 // These are immutable.
339355 Mutability :: Not
340356 }
@@ -380,8 +396,10 @@ impl<'tcx> GlobalAlloc<'tcx> {
380396 GlobalAlloc :: Function { .. } => ( Size :: ZERO , Align :: ONE ) ,
381397 GlobalAlloc :: VTable ( ..) => {
382398 // 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 )
384400 }
401+ // TODO make this represent normal type ids somehow
402+ GlobalAlloc :: Type ( _) => ( Size :: from_bytes ( 16 ) , Align :: from_bytes ( 8 ) . unwrap ( ) ) ,
385403 }
386404 }
387405}
@@ -487,6 +505,11 @@ impl<'tcx> TyCtxt<'tcx> {
487505 self . reserve_and_set_dedup ( GlobalAlloc :: VTable ( ty, dyn_ty) , salt)
488506 }
489507
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+
490513 /// Interns the `Allocation` and return a new `AllocId`, even if there's already an identical
491514 /// `Allocation` with a different `AllocId`.
492515 /// Statics with identical content will still point to the same `Allocation`, i.e.,
0 commit comments