@@ -27,11 +27,12 @@ use rustc_span::def_id::LocalDefId;
27
27
use tracing:: { instrument, trace} ;
28
28
29
29
use 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,
31
32
} ;
32
33
use crate :: const_eval;
33
34
use crate :: const_eval:: DummyMachine ;
34
- use crate :: errors:: NestedStaticInThreadLocal ;
35
+ use crate :: errors:: { ConstHeapPtrInFinal , NestedStaticInThreadLocal } ;
35
36
36
37
pub trait CompileTimeMachine < ' tcx , T > = Machine <
37
38
' tcx ,
@@ -62,7 +63,7 @@ impl HasStaticRootDefId for const_eval::CompileTimeMachine<'_> {
62
63
/// already mutable (as a sanity check).
63
64
///
64
65
/// 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 > > (
66
67
ecx : & mut InterpCx < ' tcx , M > ,
67
68
alloc_id : AllocId ,
68
69
mutability : Mutability ,
@@ -71,9 +72,16 @@ fn intern_shallow<'tcx, T, M: CompileTimeMachine<'tcx, T>>(
71
72
trace ! ( "intern_shallow {:?}" , alloc_id) ;
72
73
// remove allocation
73
74
// 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 {
75
76
return Err ( ( ) ) ;
76
77
} ;
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
+
77
85
// Set allocation mutability as appropriate. This is used by LLVM to put things into
78
86
// read-only memory, and also by Miri when evaluating other globals that
79
87
// access this one.
@@ -99,7 +107,7 @@ fn intern_shallow<'tcx, T, M: CompileTimeMachine<'tcx, T>>(
99
107
} else {
100
108
ecx. tcx . set_alloc_id_memory ( alloc_id, alloc) ;
101
109
}
102
- Ok ( alloc. 0 . 0 . provenance ( ) . ptrs ( ) . iter ( ) . map ( |& ( _, prov) | prov) )
110
+ Ok ( alloc. inner ( ) . provenance ( ) . ptrs ( ) . iter ( ) . map ( |& ( _, prov) | prov) )
103
111
}
104
112
105
113
/// 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
321
329
322
330
/// Intern `ret`. This function assumes that `ret` references no other allocation.
323
331
#[ 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 > > (
325
333
ecx : & mut InterpCx < ' tcx , M > ,
326
334
alloc_id : AllocId ,
327
335
) -> InterpResult < ' tcx , ( ) > {
0 commit comments