@@ -27,11 +27,12 @@ use rustc_span::def_id::LocalDefId;
2727use tracing:: { instrument, trace} ;
2828
2929use super :: {
30- AllocId , Allocation , InterpCx , MPlaceTy , Machine , MemoryKind , PlaceTy , err_ub, interp_ok,
30+ AllocId , Allocation , InterpCx , IsConstHeap , MPlaceTy , Machine , MemoryKind , PlaceTy , err_ub,
31+ interp_ok,
3132} ;
3233use crate :: const_eval;
3334use crate :: const_eval:: DummyMachine ;
34- use crate :: errors:: NestedStaticInThreadLocal ;
35+ use crate :: errors:: { ConstHeapPtrInFinal , NestedStaticInThreadLocal } ;
3536
3637pub trait CompileTimeMachine < ' tcx , T > = Machine <
3738 ' tcx ,
@@ -62,7 +63,7 @@ impl HasStaticRootDefId for const_eval::CompileTimeMachine<'_> {
6263/// already mutable (as a sanity check).
6364///
6465/// Returns an iterator over all relocations referred to by this allocation.
65- fn intern_shallow < ' tcx , T , M : CompileTimeMachine < ' tcx , T > > (
66+ fn intern_shallow < ' tcx , T : IsConstHeap , M : CompileTimeMachine < ' tcx , T > > (
6667 ecx : & mut InterpCx < ' tcx , M > ,
6768 alloc_id : AllocId ,
6869 mutability : Mutability ,
@@ -71,9 +72,20 @@ fn intern_shallow<'tcx, T, M: CompileTimeMachine<'tcx, T>>(
7172 trace ! ( "intern_shallow {:?}" , alloc_id) ;
7273 // remove allocation
7374 // FIXME(#120456) - is `swap_remove` correct?
74- let Some ( ( _kind , mut alloc) ) = ecx. memory . alloc_map . swap_remove ( & alloc_id) else {
75+ let Some ( ( kind , mut alloc) ) = ecx. memory . alloc_map . swap_remove ( & alloc_id) else {
7576 return Err ( ( ) ) ;
7677 } ;
78+
79+ match kind {
80+ MemoryKind :: Machine ( x) if x. is_const_heap ( ) => {
81+ // attempting to intern a `const_allocate`d pointer that was not made global via
82+ // `const_make_global`. We emit an error here but don't return an `Err` as if we
83+ // did the caller assumes we found a dangling pointer.
84+ ecx. tcx . dcx ( ) . emit_err ( ConstHeapPtrInFinal { span : ecx. tcx . span } ) ;
85+ }
86+ MemoryKind :: Machine ( _) | MemoryKind :: Stack | MemoryKind :: CallerLocation => { }
87+ }
88+
7789 // Set allocation mutability as appropriate. This is used by LLVM to put things into
7890 // read-only memory, and also by Miri when evaluating other globals that
7991 // access this one.
@@ -99,7 +111,7 @@ fn intern_shallow<'tcx, T, M: CompileTimeMachine<'tcx, T>>(
99111 } else {
100112 ecx. tcx . set_alloc_id_memory ( alloc_id, alloc) ;
101113 }
102- Ok ( alloc. 0 . 0 . provenance ( ) . ptrs ( ) . iter ( ) . map ( |& ( _, prov) | prov) )
114+ Ok ( alloc. inner ( ) . provenance ( ) . ptrs ( ) . iter ( ) . map ( |& ( _, prov) | prov) )
103115}
104116
105117/// Creates a new `DefId` and feeds all the right queries to make this `DefId`
@@ -321,7 +333,7 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval
321333
322334/// Intern `ret`. This function assumes that `ret` references no other allocation.
323335#[ instrument( level = "debug" , skip( ecx) ) ]
324- pub fn intern_const_alloc_for_constprop < ' tcx , T , M : CompileTimeMachine < ' tcx , T > > (
336+ pub fn intern_const_alloc_for_constprop < ' tcx , T : IsConstHeap , M : CompileTimeMachine < ' tcx , T > > (
325337 ecx : & mut InterpCx < ' tcx , M > ,
326338 alloc_id : AllocId ,
327339) -> InterpResult < ' tcx , ( ) > {
0 commit comments