Skip to content

Commit

Permalink
get back the more precise error message
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Mar 24, 2020
1 parent f70af91 commit 69cf211
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/librustc_mir/const_eval/machine.rs
Expand Up @@ -8,6 +8,7 @@ use std::hash::Hash;
use rustc_data_structures::fx::FxHashMap;

use rustc::mir::AssertMessage;
use rustc_ast::ast::Mutability;
use rustc_span::symbol::Symbol;
use rustc_span::{def_id::DefId, Span};

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

fn before_access_global(
memory_extra: &MemoryExtra,
_allocation: &Allocation,
alloc_id: AllocId,
allocation: &Allocation,
def_id: Option<DefId>,
is_write: bool,
) -> InterpResult<'tcx> {
if is_write {
if is_write && allocation.mutability == Mutability::Not {
Err(err_ub!(WriteToReadOnly(alloc_id)).into())
} else if is_write {
Err(ConstEvalErrKind::ModifiedGlobal.into())
} else if memory_extra.can_access_statics || def_id.is_none() {
Ok(())
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/interpret/machine.rs
Expand Up @@ -212,6 +212,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
#[inline]
fn before_access_global(
_memory_extra: &Self::MemoryExtra,
_alloc_id: AllocId,
_allocation: &Allocation,
_def_id: Option<DefId>,
_is_write: bool,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/interpret/memory.rs
Expand Up @@ -456,7 +456,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
(allocation, Some(def_id))
}
};
M::before_access_global(memory_extra, alloc, def_id, is_write)?;
M::before_access_global(memory_extra, id, alloc, def_id, is_write)?;
let alloc = Cow::Borrowed(alloc);
// We got tcx memory. Let the machine initialize its "extra" stuff.
let (alloc, tag) = M::init_allocation_extra(
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/transform/const_prop.rs
Expand Up @@ -272,6 +272,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {

fn before_access_global(
_memory_extra: &(),
_alloc_id: AllocId,
allocation: &Allocation<Self::PointerTag, Self::AllocExtra>,
_def_id: Option<DefId>,
is_write: bool,
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/consts/miri_unleashed/mutable_const.stderr
Expand Up @@ -11,7 +11,7 @@ LL | / const MUTATING_BEHIND_RAW: () = {
LL | | // Test that `MUTABLE_BEHIND_RAW` is actually immutable, by doing this at const time.
LL | | unsafe {
LL | | *MUTABLE_BEHIND_RAW = 99
| | ^^^^^^^^^^^^^^^^^^^^^^^^ modifying a static's initial value from another static's initializer
| | ^^^^^^^^^^^^^^^^^^^^^^^^ writing to alloc1 which is read-only
LL | | }
LL | | };
| |__-
Expand Down

0 comments on commit 69cf211

Please sign in to comment.