@@ -104,6 +104,7 @@ enum AllocDiscriminant {
104
104
VTable ,
105
105
Static ,
106
106
Type ,
107
+ PartialHash ,
107
108
}
108
109
109
110
pub fn specialized_encode_alloc_id < ' tcx , E : TyEncoder < ' tcx > > (
@@ -133,6 +134,11 @@ pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder<'tcx>>(
133
134
AllocDiscriminant :: Type . encode ( encoder) ;
134
135
ty. encode ( encoder) ;
135
136
}
137
+ GlobalAlloc :: PartialHash ( ty) => {
138
+ trace ! ( "encoding {alloc_id:?} with {ty:#?}" ) ;
139
+ AllocDiscriminant :: PartialHash . encode ( encoder) ;
140
+ ty. encode ( encoder) ;
141
+ }
136
142
GlobalAlloc :: Static ( did) => {
137
143
assert ! ( !tcx. is_thread_local_static( did) ) ;
138
144
// References to statics doesn't need to know about their allocations,
@@ -240,6 +246,12 @@ impl<'s> AllocDecodingSession<'s> {
240
246
trace ! ( "decoded typid: {ty:?}" ) ;
241
247
decoder. interner ( ) . reserve_and_set_type_id_alloc ( ty)
242
248
}
249
+ AllocDiscriminant :: PartialHash => {
250
+ trace ! ( "creating typeid alloc ID" ) ;
251
+ let ty = Decodable :: decode ( decoder) ;
252
+ trace ! ( "decoded typid: {ty:?}" ) ;
253
+ decoder. interner ( ) . reserve_and_set_type_id_partial_hash ( ty)
254
+ }
243
255
AllocDiscriminant :: Static => {
244
256
trace ! ( "creating extern static alloc ID" ) ;
245
257
let did = <DefId as Decodable < D > >:: decode ( decoder) ;
@@ -270,9 +282,10 @@ pub enum GlobalAlloc<'tcx> {
270
282
Static ( DefId ) ,
271
283
/// The alloc ID points to memory.
272
284
Memory ( ConstAllocation < ' tcx > ) ,
273
- /// A TypeId pointer. For now cannot be turned into a runtime value.
274
- /// TODO: turn into actual TypeId?
285
+ /// A pointer to be stored within a TypeId pointing to the full hash.
275
286
Type ( Ty < ' tcx > ) ,
287
+ /// A partial type hash (the first `size_of<usize>()` bytes of the hash).
288
+ PartialHash ( Ty < ' tcx > ) ,
276
289
}
277
290
278
291
impl < ' tcx > GlobalAlloc < ' tcx > {
@@ -312,6 +325,7 @@ impl<'tcx> GlobalAlloc<'tcx> {
312
325
match self {
313
326
GlobalAlloc :: Function { .. } => cx. data_layout ( ) . instruction_address_space ,
314
327
GlobalAlloc :: Type ( _)
328
+ | GlobalAlloc :: PartialHash ( _)
315
329
| GlobalAlloc :: Static ( ..)
316
330
| GlobalAlloc :: Memory ( ..)
317
331
| GlobalAlloc :: VTable ( ..) => AddressSpace :: DATA ,
@@ -350,7 +364,10 @@ impl<'tcx> GlobalAlloc<'tcx> {
350
364
}
351
365
}
352
366
GlobalAlloc :: Memory ( alloc) => alloc. inner ( ) . mutability ,
353
- GlobalAlloc :: Type ( _) | GlobalAlloc :: Function { .. } | GlobalAlloc :: VTable ( ..) => {
367
+ GlobalAlloc :: PartialHash ( _)
368
+ | GlobalAlloc :: Type ( _)
369
+ | GlobalAlloc :: Function { .. }
370
+ | GlobalAlloc :: VTable ( ..) => {
354
371
// These are immutable.
355
372
Mutability :: Not
356
373
}
@@ -398,8 +415,8 @@ impl<'tcx> GlobalAlloc<'tcx> {
398
415
// No data to be accessed here. But vtables are pointer-aligned.
399
416
( Size :: ZERO , tcx. data_layout . pointer_align . abi )
400
417
}
401
- // TODO make this represent normal type ids somehow
402
- GlobalAlloc :: Type ( _) => ( Size :: from_bytes ( 16 ) , Align :: from_bytes ( 8 ) . unwrap ( ) ) ,
418
+ GlobalAlloc :: Type ( _ ) => ( Size :: from_bytes ( 16 ) , Align :: ONE ) ,
419
+ GlobalAlloc :: PartialHash ( _) => ( Size :: ZERO , Align :: ONE ) ,
403
420
}
404
421
}
405
422
}
@@ -505,11 +522,17 @@ impl<'tcx> TyCtxt<'tcx> {
505
522
self . reserve_and_set_dedup ( GlobalAlloc :: VTable ( ty, dyn_ty) , salt)
506
523
}
507
524
508
- /// Generates an [AllocId] for a [core::mem::type_info::TypeId ]. Will get deduplicated.
525
+ /// Generates an [AllocId] for a [core::mem::type_info::TypeIdData ]. Will get deduplicated.
509
526
pub fn reserve_and_set_type_id_alloc ( self , ty : Ty < ' tcx > ) -> AllocId {
510
527
self . reserve_and_set_dedup ( GlobalAlloc :: Type ( ty) , 0 )
511
528
}
512
529
530
+ /// Generates an [AllocId] that will get replaced by a partial hash of a type.
531
+ /// See [core::mem::type_info::TypeId] for more information.
532
+ pub fn reserve_and_set_type_id_partial_hash ( self , ty : Ty < ' tcx > ) -> AllocId {
533
+ self . reserve_and_set_dedup ( GlobalAlloc :: PartialHash ( ty) , 0 )
534
+ }
535
+
513
536
/// Interns the `Allocation` and return a new `AllocId`, even if there's already an identical
514
537
/// `Allocation` with a different `AllocId`.
515
538
/// Statics with identical content will still point to the same `Allocation`, i.e.,
0 commit comments