]> git.lizzy.rs Git - rust.git/commitdiff
fix const_prop ICE
authorRalf Jung <post@ralfj.de>
Sun, 22 Mar 2020 08:23:19 +0000 (09:23 +0100)
committerRalf Jung <post@ralfj.de>
Tue, 24 Mar 2020 07:34:28 +0000 (08:34 +0100)
src/librustc_mir/const_eval/error.rs
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

index fd46340f03ad6beb7e049d907ccb17782f9d66f0..aa30f43df93500018eebc19b9fb7abb95303dc7d 100644 (file)
@@ -34,11 +34,9 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                 write!(f, "\"{}\" needs an rfc before being allowed inside constants", msg)
             }
             ConstAccessesStatic => write!(f, "constant accesses static"),
-            ModifiedGlobal => write!(
-                f,
-                "modifying a static's initial value from another static's \
-                    initializer"
-            ),
+            ModifiedGlobal => {
+                write!(f, "modifying a static's initial value from another static's initializer")
+            }
             AssertFailure(ref msg) => write!(f, "{:?}", msg),
             Panic { msg, line, col, file } => {
                 write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col)
index ff91ddec946cbcadc36e29e78b667be814b5e54b..8f4501cc3fb69ace1fd213244a418c1bab5d4275 100644 (file)
@@ -358,6 +358,7 @@ fn before_access_global(
         } else if is_write {
             Err(ConstEvalErrKind::ModifiedGlobal.into())
         } else if memory_extra.can_access_statics || def_id.is_none() {
+            // `def_id.is_none()` indicates this is not a static, but a const or so.
             Ok(())
         } else {
             Err(ConstEvalErrKind::ConstAccessesStatic.into())
index b820b11e9460d0ed7f37f2c67a9ef91586685b4e..cc87c2916862be2e88b414ae56b07a530f4c2482 100644 (file)
@@ -209,6 +209,7 @@ fn before_terminator(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx
     }
 
     /// Called before a global allocation is accessed.
+    /// `def_id` is `Some` if this is the "lazy" allocation of a static.
     #[inline]
     fn before_access_global(
         _memory_extra: &Self::MemoryExtra,
index 87db44a96e7b3ce943459723469919c09e7f8aa2..110f2ffd9d78c8045affa8d9e6545e415d1785c3 100644 (file)
@@ -416,7 +416,10 @@ fn get_global_alloc(
     ) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::PointerTag, M::AllocExtra>>> {
         let alloc = tcx.alloc_map.lock().get(id);
         let (alloc, def_id) = match alloc {
-            Some(GlobalAlloc::Memory(mem)) => (mem, None),
+            Some(GlobalAlloc::Memory(mem)) => {
+                // Memory of a constant or promoted or anonymous memory referenced by a static.
+                (mem, None)
+            }
             Some(GlobalAlloc::Function(..)) => throw_ub!(DerefFunctionPointer(id)),
             None => throw_ub!(PointerUseAfterFree(id)),
             Some(GlobalAlloc::Static(def_id)) => {
index 17b8f3de7513880d83d021573f548a93df8abb79..ef2d5404541b9ae032465a7046b20df4b295e929 100644 (file)
@@ -274,7 +274,7 @@ fn before_access_global(
         _memory_extra: &(),
         _alloc_id: AllocId,
         allocation: &Allocation<Self::PointerTag, Self::AllocExtra>,
-        _def_id: Option<DefId>,
+        def_id: Option<DefId>,
         is_write: bool,
     ) -> InterpResult<'tcx> {
         if is_write {
@@ -282,7 +282,11 @@ fn before_access_global(
         }
         // If the static allocation is mutable or if it has relocations (it may be legal to mutate
         // the memory behind that in the future), then we can't const prop it.
-        if allocation.mutability == Mutability::Mut || allocation.relocations().len() > 0 {
+        // FIXME: we only check statics here (that have a `DefId`), not other mutable allocations.
+        // Why that?
+        if def_id.is_some()
+            && (allocation.mutability == Mutability::Mut || allocation.relocations().len() > 0)
+        {
             throw_machine_stop_str!("can't eval mutable statics in ConstProp");
         }