Skip to content

Commit 69cf211

Browse files
committed
get back the more precise error message
1 parent f70af91 commit 69cf211

File tree

5 files changed

+10
-4
lines changed

5 files changed

+10
-4
lines changed

src/librustc_mir/const_eval/machine.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::hash::Hash;
88
use rustc_data_structures::fx::FxHashMap;
99

1010
use rustc::mir::AssertMessage;
11+
use rustc_ast::ast::Mutability;
1112
use rustc_span::symbol::Symbol;
1213
use rustc_span::{def_id::DefId, Span};
1314

@@ -347,11 +348,14 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter {
347348

348349
fn before_access_global(
349350
memory_extra: &MemoryExtra,
350-
_allocation: &Allocation,
351+
alloc_id: AllocId,
352+
allocation: &Allocation,
351353
def_id: Option<DefId>,
352354
is_write: bool,
353355
) -> InterpResult<'tcx> {
354-
if is_write {
356+
if is_write && allocation.mutability == Mutability::Not {
357+
Err(err_ub!(WriteToReadOnly(alloc_id)).into())
358+
} else if is_write {
355359
Err(ConstEvalErrKind::ModifiedGlobal.into())
356360
} else if memory_extra.can_access_statics || def_id.is_none() {
357361
Ok(())

src/librustc_mir/interpret/machine.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
212212
#[inline]
213213
fn before_access_global(
214214
_memory_extra: &Self::MemoryExtra,
215+
_alloc_id: AllocId,
215216
_allocation: &Allocation,
216217
_def_id: Option<DefId>,
217218
_is_write: bool,

src/librustc_mir/interpret/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
456456
(allocation, Some(def_id))
457457
}
458458
};
459-
M::before_access_global(memory_extra, alloc, def_id, is_write)?;
459+
M::before_access_global(memory_extra, id, alloc, def_id, is_write)?;
460460
let alloc = Cow::Borrowed(alloc);
461461
// We got tcx memory. Let the machine initialize its "extra" stuff.
462462
let (alloc, tag) = M::init_allocation_extra(

src/librustc_mir/transform/const_prop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
272272

273273
fn before_access_global(
274274
_memory_extra: &(),
275+
_alloc_id: AllocId,
275276
allocation: &Allocation<Self::PointerTag, Self::AllocExtra>,
276277
_def_id: Option<DefId>,
277278
is_write: bool,

src/test/ui/consts/miri_unleashed/mutable_const.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | / const MUTATING_BEHIND_RAW: () = {
1111
LL | | // Test that `MUTABLE_BEHIND_RAW` is actually immutable, by doing this at const time.
1212
LL | | unsafe {
1313
LL | | *MUTABLE_BEHIND_RAW = 99
14-
| | ^^^^^^^^^^^^^^^^^^^^^^^^ modifying a static's initial value from another static's initializer
14+
| | ^^^^^^^^^^^^^^^^^^^^^^^^ writing to alloc1 which is read-only
1515
LL | | }
1616
LL | | };
1717
| |__-

0 commit comments

Comments
 (0)