181
181
( InternMode :: Const , hir:: Mutability :: MutMutable ) =>
182
182
bug ! ( "const qualif failed to prevent mutable references" ) ,
183
183
}
184
+ // Compute the mutability with which we'll start visiting the allocation. This is
185
+ // what gets changed when we encounter an `UnsafeCell`
184
186
let mutability = match ( self . mutability , mutability) {
185
187
// The only way a mutable reference actually works as a mutable reference is
186
188
// by being in a `static mut` directly or behind another mutable reference.
190
192
( Mutability :: Mutable , hir:: Mutability :: MutMutable ) => Mutability :: Mutable ,
191
193
_ => Mutability :: Immutable ,
192
194
} ;
195
+ // Compute the mutability of the allocation
193
196
let intern_mutability = intern_mutability (
194
197
self . ecx . tcx . tcx ,
195
198
self . param_env ,
@@ -244,6 +247,7 @@ pub fn intern_const_alloc_recursive(
244
247
param_env : ty:: ParamEnv < ' tcx > ,
245
248
) -> InterpResult < ' tcx > {
246
249
let tcx = ecx. tcx ;
250
+ // this `mutability` is the mutability of the place, ignoring the type
247
251
let ( mutability, base_intern_mode) = match tcx. static_mutability ( def_id) {
248
252
Some ( hir:: Mutability :: MutImmutable ) => ( Mutability :: Immutable , InternMode :: Static ) ,
249
253
None => ( Mutability :: Immutable , InternMode :: ConstBase ) ,
@@ -255,6 +259,10 @@ pub fn intern_const_alloc_recursive(
255
259
let mut ref_tracking = RefTracking :: new ( ( ret, mutability, base_intern_mode) ) ;
256
260
let leftover_relocations = & mut FxHashSet :: default ( ) ;
257
261
262
+ // This mutability is the combination of the place mutability and the type mutability. If either
263
+ // is mutable, `alloc_mutability` is mutable. This exists because the entire allocation needs
264
+ // to be mutable if it contains an `UnsafeCell` anywhere. The other `mutability` exists so that
265
+ // the visitor does not treat everything outside the `UnsafeCell` as mutable.
258
266
let alloc_mutability = intern_mutability (
259
267
tcx. tcx , param_env, ret. layout . ty , tcx. span , mutability,
260
268
) ;
0 commit comments