Skip to content

Commit a1f7e9a

Browse files
committed
assert that only statics can possibly be mutable
1 parent d69c668 commit a1f7e9a

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/librustc_mir/const_eval/machine.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -360,18 +360,18 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter {
360360
} else {
361361
// Read access. These are usually allowed, with some exceptions.
362362
if memory_extra.can_access_statics {
363-
// This is allowed to read from anything.
363+
// Machine configuration allows us read from anything (e.g., `static` initializer).
364364
Ok(())
365-
} else if allocation.mutability == Mutability::Mut || static_def_id.is_some() {
366-
// This is a potentially dangerous read.
367-
// We *must* error on any access to a mutable global here, as the content of
368-
// this allocation may be different now and at run-time, so if we permit reading
369-
// now we might return the wrong value.
370-
// We conservatively also reject all statics here, but that could be relaxed
371-
// in the future.
365+
} else if static_def_id.is_some() {
366+
// Machine configuration does not allow us to read statics
367+
// (e.g., `const` initializer).
372368
Err(ConstEvalErrKind::ConstAccessesStatic.into())
373369
} else {
374370
// Immutable global, this read is fine.
371+
// But make sure we never accept a read from something mutable, that would be
372+
// unsound. The reason is that as the content of this allocation may be different
373+
// now and at run-time, so if we permit reading now we might return the wrong value.
374+
assert_eq!(allocation.mutability, Mutability::Not);
375375
Ok(())
376376
}
377377
}

0 commit comments

Comments
 (0)