@@ -322,8 +322,11 @@ impl<'tcx> AllocMap<'tcx> {
322
322
}
323
323
}
324
324
325
- /// obtains a new allocation ID that can be referenced but does not
325
+ /// Obtains a new allocation ID that can be referenced but does not
326
326
/// yet have an allocation backing it.
327
+ ///
328
+ /// Make sure to call `set_id_memory` or `set_id_same_memory` before returning such an
329
+ /// `AllocId` from a query.
327
330
pub fn reserve (
328
331
& mut self ,
329
332
) -> AllocId {
@@ -357,35 +360,50 @@ impl<'tcx> AllocMap<'tcx> {
357
360
id
358
361
}
359
362
363
+ /// Returns `None` in case the `AllocId` is dangling.
364
+ /// This function exists to allow const eval to detect the difference between evaluation-
365
+ /// local dangling pointers and allocations in constants/statics.
360
366
pub fn get ( & self , id : AllocId ) -> Option < AllocType < ' tcx > > {
361
367
self . id_to_type . get ( & id) . cloned ( )
362
368
}
363
369
370
+ /// Panics if the `AllocId` does not refer to an `Allocation`
364
371
pub fn unwrap_memory ( & self , id : AllocId ) -> & ' tcx Allocation {
365
372
match self . get ( id) {
366
373
Some ( AllocType :: Memory ( mem) ) => mem,
367
374
_ => bug ! ( "expected allocation id {} to point to memory" , id) ,
368
375
}
369
376
}
370
377
378
+ /// Generate an `AllocId` for a static or return a cached one in case this function has been
379
+ /// called on the same static before.
371
380
pub fn intern_static ( & mut self , static_id : DefId ) -> AllocId {
372
381
self . intern ( AllocType :: Static ( static_id) )
373
382
}
374
383
384
+ /// Intern the `Allocation` and return a new `AllocId`, even if there's already an identical
385
+ /// `Allocation` with a different `AllocId`.
386
+ // FIXME: is this really necessary? Can we ensure `FOO` and `BAR` being different after codegen
387
+ // in `static FOO: u32 = 42; static BAR: u32 = 42;` even if they reuse the same allocation
388
+ // inside rustc?
375
389
pub fn allocate ( & mut self , mem : & ' tcx Allocation ) -> AllocId {
376
390
let id = self . reserve ( ) ;
377
391
self . set_id_memory ( id, mem) ;
378
392
id
379
393
}
380
394
395
+ /// Freeze an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
396
+ /// call this function twice, even with the same `Allocation` will ICE the compiler.
381
397
pub fn set_id_memory ( & mut self , id : AllocId , mem : & ' tcx Allocation ) {
382
398
if let Some ( old) = self . id_to_type . insert ( id, AllocType :: Memory ( mem) ) {
383
399
bug ! ( "tried to set allocation id {}, but it was already existing as {:#?}" , id, old) ;
384
400
}
385
401
}
386
402
403
+ /// Freeze an `AllocId` created with `reserve` by pointing it at an `Allocation`. May be called
404
+ /// twice for the same `(AllocId, Allocation)` pair.
387
405
pub fn set_id_same_memory ( & mut self , id : AllocId , mem : & ' tcx Allocation ) {
388
- self . id_to_type . insert_same ( id, AllocType :: Memory ( mem) ) ;
406
+ self . id_to_type . insert_same ( id, AllocType :: Memory ( mem) ) ;
389
407
}
390
408
}
391
409
0 commit comments