about summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src
diff options
context:
space:
mode:
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2024-04-08 10:57:46 +0000
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2024-04-17 09:50:44 +0000
commit8b2a4f8b4323b6bdb2a0a8fa4e7aa206fc89ae0b (patch)
tree44137d313fb27615a3146700aacba186b60036c7 /compiler/rustc_const_eval/src
parent140c9e10bb21e808cd290add51e261ac059dd48b (diff)
downloadrust-8b2a4f8b4323b6bdb2a0a8fa4e7aa206fc89ae0b.tar.gz
rust-8b2a4f8b4323b6bdb2a0a8fa4e7aa206fc89ae0b.zip
Simplify alloc id mutability check
Diffstat (limited to 'compiler/rustc_const_eval/src')
-rw-r--r--compiler/rustc_const_eval/src/interpret/validity.rs25
1 files changed, 15 insertions, 10 deletions
diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs
index 1103ec75ec2..b8a1733e45a 100644
--- a/compiler/rustc_const_eval/src/interpret/validity.rs
+++ b/compiler/rustc_const_eval/src/interpret/validity.rs
@@ -10,7 +10,6 @@ use std::num::NonZero;
 use either::{Left, Right};
 
 use hir::def::DefKind;
-use hir::def_id::DefId;
 use rustc_ast::Mutability;
 use rustc_data_structures::fx::FxHashSet;
 use rustc_hir as hir;
@@ -450,8 +449,9 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
             // `!` is a ZST and we want to validate it.
             if let Ok((alloc_id, _offset, _prov)) = self.ecx.ptr_try_get_alloc_id(place.ptr()) {
                 let mut skip_recursive_check = false;
-                let (alloc_actual_mutbl, is_static) = mutability(self.ecx, alloc_id);
-                if let Some((did, nested)) = is_static {
+                let alloc_actual_mutbl = mutability(self.ecx, alloc_id);
+                if let GlobalAlloc::Static(did) = self.ecx.tcx.global_alloc(alloc_id) {
+                    let DefKind::Static { nested, .. } = self.ecx.tcx.def_kind(did) else { bug!() };
                     // Special handling for pointers to statics (irrespective of their type).
                     assert!(!self.ecx.tcx.is_thread_local_static(did));
                     assert!(self.ecx.tcx.is_static(did));
@@ -682,7 +682,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
     fn in_mutable_memory(&self, op: &OpTy<'tcx, M::Provenance>) -> bool {
         if let Some(mplace) = op.as_mplace_or_imm().left() {
             if let Some(alloc_id) = mplace.ptr().provenance.and_then(|p| p.get_alloc_id()) {
-                return mutability(self.ecx, alloc_id).0.is_mut();
+                return mutability(self.ecx, alloc_id).is_mut();
             }
         }
         false
@@ -695,13 +695,19 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
 fn mutability<'mir, 'tcx: 'mir>(
     ecx: &InterpCx<'mir, 'tcx, impl Machine<'mir, 'tcx>>,
     alloc_id: AllocId,
-) -> (Mutability, Option<(DefId, bool)>) {
+) -> Mutability {
     // Let's see what kind of memory this points to.
     // We're not using `try_global_alloc` since dangling pointers have already been handled.
     match ecx.tcx.global_alloc(alloc_id) {
         GlobalAlloc::Static(did) => {
             let DefKind::Static { mutability, nested } = ecx.tcx.def_kind(did) else { bug!() };
-            let mutability = if nested {
+            if nested {
+                assert!(
+                    ecx.memory.alloc_map.get(alloc_id).is_none(),
+                    "allocations of nested statics are already interned: {alloc_id:?}, {did:?}"
+                );
+                // Nested statics in a `static` are never interior mutable,
+                // so just use the declared mutability.
                 mutability
             } else {
                 let mutability = match mutability {
@@ -721,13 +727,12 @@ fn mutability<'mir, 'tcx: 'mir>(
                     assert_eq!(alloc.mutability, mutability);
                 }
                 mutability
-            };
-            (mutability, Some((did, nested)))
+            }
         }
-        GlobalAlloc::Memory(alloc) => (alloc.inner().mutability, None),
+        GlobalAlloc::Memory(alloc) => alloc.inner().mutability,
         GlobalAlloc::Function(..) | GlobalAlloc::VTable(..) => {
             // These are immutable, we better don't allow mutable pointers here.
-            (Mutability::Not, None)
+            Mutability::Not
         }
     }
 }