@@ -31,7 +31,7 @@ use super::{
31
31
} ;
32
32
use crate :: const_eval;
33
33
use crate :: const_eval:: DummyMachine ;
34
- use crate :: errors:: NestedStaticInThreadLocal ;
34
+ use crate :: errors:: { ConstHeapPtrInFinal , NestedStaticInThreadLocal } ;
35
35
36
36
pub trait CompileTimeMachine < ' tcx , T > = Machine <
37
37
' tcx ,
@@ -55,14 +55,37 @@ impl HasStaticRootDefId for const_eval::CompileTimeMachine<'_> {
55
55
}
56
56
}
57
57
58
+ pub enum DisallowInternReason {
59
+ ConstHeap ,
60
+ }
61
+
62
+ pub trait CanIntern {
63
+ fn disallows_intern ( & self ) -> Option < DisallowInternReason > ;
64
+ }
65
+
66
+ impl CanIntern for const_eval:: MemoryKind {
67
+ fn disallows_intern ( & self ) -> Option < DisallowInternReason > {
68
+ match self {
69
+ const_eval:: MemoryKind :: Heap { was_made_immut : false } => Some ( DisallowInternReason :: ConstHeap ) ,
70
+ const_eval:: MemoryKind :: Heap { was_made_immut : true } => None ,
71
+ }
72
+ }
73
+ }
74
+
75
+ impl CanIntern for ! {
76
+ fn disallows_intern ( & self ) -> Option < DisallowInternReason > {
77
+ * self
78
+ }
79
+ }
80
+
58
81
/// Intern an allocation. Returns `Err` if the allocation does not exist in the local memory.
59
82
///
60
83
/// `mutability` can be used to force immutable interning: if it is `Mutability::Not`, the
61
84
/// allocation is interned immutably; if it is `Mutability::Mut`, then the allocation *must be*
62
85
/// already mutable (as a sanity check).
63
86
///
64
87
/// Returns an iterator over all relocations referred to by this allocation.
65
- fn intern_shallow < ' tcx , T , M : CompileTimeMachine < ' tcx , T > > (
88
+ fn intern_shallow < ' tcx , T : CanIntern , M : CompileTimeMachine < ' tcx , T > > (
66
89
ecx : & mut InterpCx < ' tcx , M > ,
67
90
alloc_id : AllocId ,
68
91
mutability : Mutability ,
@@ -71,9 +94,22 @@ fn intern_shallow<'tcx, T, M: CompileTimeMachine<'tcx, T>>(
71
94
trace ! ( "intern_shallow {:?}" , alloc_id) ;
72
95
// remove allocation
73
96
// FIXME(#120456) - is `swap_remove` correct?
74
- let Some ( ( _kind , mut alloc) ) = ecx. memory . alloc_map . swap_remove ( & alloc_id) else {
97
+ let Some ( ( kind , mut alloc) ) = ecx. memory . alloc_map . swap_remove ( & alloc_id) else {
75
98
return Err ( ( ) ) ;
76
99
} ;
100
+
101
+ match kind {
102
+ MemoryKind :: Machine ( x) if let Some ( reason) = x. disallows_intern ( ) => match reason {
103
+ // attempting to intern a `const_allocate`d pointer that was not made global via
104
+ // `const_make_global`. We emit an error here but don't return an `Err` as if we
105
+ // did the caller assumes we found a dangling pointer.
106
+ DisallowInternReason :: ConstHeap => {
107
+ ecx. tcx . dcx ( ) . emit_err ( ConstHeapPtrInFinal { span : ecx. tcx . span } ) ;
108
+ }
109
+ } ,
110
+ MemoryKind :: Machine ( _) | MemoryKind :: Stack | MemoryKind :: CallerLocation => { }
111
+ }
112
+
77
113
// Set allocation mutability as appropriate. This is used by LLVM to put things into
78
114
// read-only memory, and also by Miri when evaluating other globals that
79
115
// access this one.
@@ -99,7 +135,7 @@ fn intern_shallow<'tcx, T, M: CompileTimeMachine<'tcx, T>>(
99
135
} else {
100
136
ecx. tcx . set_alloc_id_memory ( alloc_id, alloc) ;
101
137
}
102
- Ok ( alloc. 0 . 0 . provenance ( ) . ptrs ( ) . iter ( ) . map ( |& ( _, prov) | prov) )
138
+ Ok ( alloc. inner ( ) . provenance ( ) . ptrs ( ) . iter ( ) . map ( |& ( _, prov) | prov) )
103
139
}
104
140
105
141
/// Creates a new `DefId` and feeds all the right queries to make this `DefId`
@@ -181,7 +217,7 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval
181
217
}
182
218
InternKind :: Static ( Mutability :: Not ) => {
183
219
(
184
- // Outermost allocation is mutable if `!Freeze`.
220
+ // Outermost allocation is mutable if `!Freeze` i.e. contains interior mutable types .
185
221
if ret. layout . ty . is_freeze ( * ecx. tcx , ecx. typing_env ) {
186
222
Mutability :: Not
187
223
} else {
@@ -309,7 +345,12 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval
309
345
// `static mut`.
310
346
just_interned. insert ( alloc_id) ;
311
347
match intern_shallow ( ecx, alloc_id, inner_mutability, Some ( & mut disambiguator) ) {
312
- Ok ( nested) => todo. extend ( nested) ,
348
+ Ok ( nested) => {
349
+ // filtering here is necessary for self-referential values created through
350
+ // `const_allocate`.
351
+ // see tests/ui/consts/const-eval/heap/make-global-cyclic.rs
352
+ todo. extend ( nested. filter ( |prov| !just_interned. contains ( & prov. alloc_id ( ) ) ) )
353
+ }
313
354
Err ( ( ) ) => {
314
355
ecx. tcx . dcx ( ) . delayed_bug ( "found dangling pointer during const interning" ) ;
315
356
result = Err ( InternResult :: FoundDanglingPointer ) ;
@@ -321,7 +362,7 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval
321
362
322
363
/// Intern `ret`. This function assumes that `ret` references no other allocation.
323
364
#[ instrument( level = "debug" , skip( ecx) ) ]
324
- pub fn intern_const_alloc_for_constprop < ' tcx , T , M : CompileTimeMachine < ' tcx , T > > (
365
+ pub fn intern_const_alloc_for_constprop < ' tcx , T : CanIntern , M : CompileTimeMachine < ' tcx , T > > (
325
366
ecx : & mut InterpCx < ' tcx , M > ,
326
367
alloc_id : AllocId ,
327
368
) -> InterpResult < ' tcx , ( ) > {
0 commit comments