Skip to content

Commit 6229a8f

Browse files
committed
Elaborate some more on what mutability field means what
1 parent 521d38a commit 6229a8f

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/librustc_mir/interpret/intern.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ for
181181
(InternMode::Const, hir::Mutability::MutMutable) =>
182182
bug!("const qualif failed to prevent mutable references"),
183183
}
184+
// Compute the mutability with which we'll start visiting the allocation. This is
185+
// what gets changed when we encounter an `UnsafeCell`
184186
let mutability = match (self.mutability, mutability) {
185187
// The only way a mutable reference actually works as a mutable reference is
186188
// by being in a `static mut` directly or behind another mutable reference.
@@ -190,6 +192,7 @@ for
190192
(Mutability::Mutable, hir::Mutability::MutMutable) => Mutability::Mutable,
191193
_ => Mutability::Immutable,
192194
};
195+
// Compute the mutability of the allocation
193196
let intern_mutability = intern_mutability(
194197
self.ecx.tcx.tcx,
195198
self.param_env,
@@ -244,6 +247,7 @@ pub fn intern_const_alloc_recursive(
244247
param_env: ty::ParamEnv<'tcx>,
245248
) -> InterpResult<'tcx> {
246249
let tcx = ecx.tcx;
250+
// this `mutability` is the mutability of the place, ignoring the type
247251
let (mutability, base_intern_mode) = match tcx.static_mutability(def_id) {
248252
Some(hir::Mutability::MutImmutable) => (Mutability::Immutable, InternMode::Static),
249253
None => (Mutability::Immutable, InternMode::ConstBase),
@@ -255,6 +259,10 @@ pub fn intern_const_alloc_recursive(
255259
let mut ref_tracking = RefTracking::new((ret, mutability, base_intern_mode));
256260
let leftover_relocations = &mut FxHashSet::default();
257261

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.
258266
let alloc_mutability = intern_mutability(
259267
tcx.tcx, param_env, ret.layout.ty, tcx.span, mutability,
260268
);

0 commit comments

Comments
 (0)