@@ -322,8 +322,11 @@ impl<'tcx> AllocMap<'tcx> {
322322 }
323323 }
324324
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
326326 /// 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.
327330 pub fn reserve (
328331 & mut self ,
329332 ) -> AllocId {
@@ -357,35 +360,50 @@ impl<'tcx> AllocMap<'tcx> {
357360 id
358361 }
359362
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.
360366 pub fn get ( & self , id : AllocId ) -> Option < AllocType < ' tcx > > {
361367 self . id_to_type . get ( & id) . cloned ( )
362368 }
363369
370+ /// Panics if the `AllocId` does not refer to an `Allocation`
364371 pub fn unwrap_memory ( & self , id : AllocId ) -> & ' tcx Allocation {
365372 match self . get ( id) {
366373 Some ( AllocType :: Memory ( mem) ) => mem,
367374 _ => bug ! ( "expected allocation id {} to point to memory" , id) ,
368375 }
369376 }
370377
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.
371380 pub fn intern_static ( & mut self , static_id : DefId ) -> AllocId {
372381 self . intern ( AllocType :: Static ( static_id) )
373382 }
374383
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?
375389 pub fn allocate ( & mut self , mem : & ' tcx Allocation ) -> AllocId {
376390 let id = self . reserve ( ) ;
377391 self . set_id_memory ( id, mem) ;
378392 id
379393 }
380394
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.
381397 pub fn set_id_memory ( & mut self , id : AllocId , mem : & ' tcx Allocation ) {
382398 if let Some ( old) = self . id_to_type . insert ( id, AllocType :: Memory ( mem) ) {
383399 bug ! ( "tried to set allocation id {}, but it was already existing as {:#?}" , id, old) ;
384400 }
385401 }
386402
403+ /// Freeze an `AllocId` created with `reserve` by pointing it at an `Allocation`. May be called
404+ /// twice for the same `(AllocId, Allocation)` pair.
387405 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) ) ;
389407 }
390408}
391409
0 commit comments