@@ -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,20 @@ 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
+ 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
+
77
89
// Set allocation mutability as appropriate. This is used by LLVM to put things into
78
90
// read-only memory, and also by Miri when evaluating other globals that
79
91
// access this one.
@@ -99,7 +111,7 @@ fn intern_shallow<'tcx, T, M: CompileTimeMachine<'tcx, T>>(
99
111
} else {
100
112
ecx. tcx . set_alloc_id_memory ( alloc_id, alloc) ;
101
113
}
102
- Ok ( alloc. 0 . 0 . provenance ( ) . ptrs ( ) . iter ( ) . map ( |& ( _, prov) | prov) )
114
+ Ok ( alloc. inner ( ) . provenance ( ) . ptrs ( ) . iter ( ) . map ( |& ( _, prov) | prov) )
103
115
}
104
116
105
117
/// 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
321
333
322
334
/// Intern `ret`. This function assumes that `ret` references no other allocation.
323
335
#[ 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 > > (
325
337
ecx : & mut InterpCx < ' tcx , M > ,
326
338
alloc_id : AllocId ,
327
339
) -> InterpResult < ' tcx , ( ) > {
0 commit comments