]> git.lizzy.rs Git - rust.git/commitdiff
get back the more precise error message
authorRalf Jung <post@ralfj.de>
Sat, 21 Mar 2020 19:44:39 +0000 (20:44 +0100)
committerRalf Jung <post@ralfj.de>
Tue, 24 Mar 2020 07:27:46 +0000 (08:27 +0100)
src/librustc_mir/const_eval/machine.rs
src/librustc_mir/interpret/machine.rs
src/librustc_mir/interpret/memory.rs
src/librustc_mir/transform/const_prop.rs
src/test/ui/consts/miri_unleashed/mutable_const.stderr

index 65dca16e739f5d8365c5128d141bc715524003f3..ff91ddec946cbcadc36e29e78b667be814b5e54b 100644 (file)
@@ -8,6 +8,7 @@
 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};
 
@@ -347,11 +348,14 @@ fn stack_push(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
 
     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(())
index 6fe70548554809297d48fd534f330bca085e175d..b820b11e9460d0ed7f37f2c67a9ef91586685b4e 100644 (file)
@@ -212,6 +212,7 @@ fn before_terminator(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx
     #[inline]
     fn before_access_global(
         _memory_extra: &Self::MemoryExtra,
+        _alloc_id: AllocId,
         _allocation: &Allocation,
         _def_id: Option<DefId>,
         _is_write: bool,
index df56b3f957782b65ed76133defb13f7609a9dcff..87db44a96e7b3ce943459723469919c09e7f8aa2 100644 (file)
@@ -456,7 +456,7 @@ fn get_global_alloc(
                 (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(
index 850fc15f12cf7c9bbc423c8a8af04a49a7452124..17b8f3de7513880d83d021573f548a93df8abb79 100644 (file)
@@ -272,6 +272,7 @@ fn access_local(
 
     fn before_access_global(
         _memory_extra: &(),
+        _alloc_id: AllocId,
         allocation: &Allocation<Self::PointerTag, Self::AllocExtra>,
         _def_id: Option<DefId>,
         is_write: bool,
index 29a13b18e5ba319518d9bada666d89594e729701..8456e8ec6870dcd4c0d2204b8e8ac737bfb662d7 100644 (file)
@@ -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 | | };
    | |__-