@@ -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,16 @@ 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+ if matches ! ( kind, MemoryKind :: Machine ( x) if x. is_const_heap( ) ) {
80+ // emit an error but don't return an `Err` as if we did the caller assumes we found
81+ // a dangling pointer.
82+ ecx. tcx . dcx ( ) . emit_err ( ConstHeapPtrInFinal { span : ecx. tcx . span } ) ;
83+ }
84+
7785 // Set allocation mutability as appropriate. This is used by LLVM to put things into
7886 // read-only memory, and also by Miri when evaluating other globals that
7987 // access this one.
@@ -99,7 +107,7 @@ fn intern_shallow<'tcx, T, M: CompileTimeMachine<'tcx, T>>(
99107 } else {
100108 ecx. tcx . set_alloc_id_memory ( alloc_id, alloc) ;
101109 }
102- Ok ( alloc. 0 . 0 . provenance ( ) . ptrs ( ) . iter ( ) . map ( |& ( _, prov) | prov) )
110+ Ok ( alloc. inner ( ) . provenance ( ) . ptrs ( ) . iter ( ) . map ( |& ( _, prov) | prov) )
103111}
104112
105113/// Creates a new `DefId` and feeds all the right queries to make this `DefId`
@@ -321,7 +329,7 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval
321329
322330/// Intern `ret`. This function assumes that `ret` references no other allocation.
323331#[ instrument( level = "debug" , skip( ecx) ) ]
324- pub fn intern_const_alloc_for_constprop < ' tcx , T , M : CompileTimeMachine < ' tcx , T > > (
332+ pub fn intern_const_alloc_for_constprop < ' tcx , T : IsConstHeap , M : CompileTimeMachine < ' tcx , T > > (
325333 ecx : & mut InterpCx < ' tcx , M > ,
326334 alloc_id : AllocId ,
327335) -> InterpResult < ' tcx , ( ) > {
0 commit comments