Skip to content

Commit 6ac9447

Browse files
committed
Simplify alloc id mutability check
1 parent 2d936b4 commit 6ac9447

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use std::num::NonZero;
1010
use either::{Left, Right};
1111

1212
use hir::def::DefKind;
13-
use hir::def_id::DefId;
1413
use rustc_ast::Mutability;
1514
use rustc_data_structures::fx::FxHashSet;
1615
use rustc_hir as hir;
@@ -450,8 +449,9 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
450449
// `!` is a ZST and we want to validate it.
451450
if let Ok((alloc_id, _offset, _prov)) = self.ecx.ptr_try_get_alloc_id(place.ptr()) {
452451
let mut skip_recursive_check = false;
453-
let (alloc_actual_mutbl, is_static) = mutability(self.ecx, alloc_id);
454-
if let Some((did, nested)) = is_static {
452+
let alloc_actual_mutbl = mutability(self.ecx, alloc_id);
453+
if let GlobalAlloc::Static(did) = self.ecx.tcx.global_alloc(alloc_id) {
454+
let DefKind::Static { nested, .. } = self.ecx.tcx.def_kind(did) else { bug!() };
455455
// Special handling for pointers to statics (irrespective of their type).
456456
assert!(!self.ecx.tcx.is_thread_local_static(did));
457457
assert!(self.ecx.tcx.is_static(did));
@@ -681,7 +681,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
681681
fn in_mutable_memory(&self, op: &OpTy<'tcx, M::Provenance>) -> bool {
682682
if let Some(mplace) = op.as_mplace_or_imm().left() {
683683
if let Some(alloc_id) = mplace.ptr().provenance.and_then(|p| p.get_alloc_id()) {
684-
return mutability(self.ecx, alloc_id).0.is_mut();
684+
return mutability(self.ecx, alloc_id).is_mut();
685685
}
686686
}
687687
false
@@ -694,13 +694,19 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
694694
fn mutability<'mir, 'tcx: 'mir>(
695695
ecx: &InterpCx<'mir, 'tcx, impl Machine<'mir, 'tcx>>,
696696
alloc_id: AllocId,
697-
) -> (Mutability, Option<(DefId, bool)>) {
697+
) -> Mutability {
698698
// Let's see what kind of memory this points to.
699699
// We're not using `try_global_alloc` since dangling pointers have already been handled.
700700
match ecx.tcx.global_alloc(alloc_id) {
701701
GlobalAlloc::Static(did) => {
702702
let DefKind::Static { mutability, nested } = ecx.tcx.def_kind(did) else { bug!() };
703-
let mutability = if nested {
703+
if nested {
704+
assert!(
705+
ecx.memory.alloc_map.get(alloc_id).is_none(),
706+
"allocations of nested statics are already interned: {alloc_id:?}, {did:?}"
707+
);
708+
// Nested statics in a `static` are never interior mutable,
709+
// so just use the declared mutability.
704710
mutability
705711
} else {
706712
let mutability = match mutability {
@@ -720,13 +726,12 @@ fn mutability<'mir, 'tcx: 'mir>(
720726
assert_eq!(alloc.mutability, mutability);
721727
}
722728
mutability
723-
};
724-
(mutability, Some((did, nested)))
729+
}
725730
}
726-
GlobalAlloc::Memory(alloc) => (alloc.inner().mutability, None),
731+
GlobalAlloc::Memory(alloc) => alloc.inner().mutability,
727732
GlobalAlloc::Function(..) | GlobalAlloc::VTable(..) => {
728733
// These are immutable, we better don't allow mutable pointers here.
729-
(Mutability::Not, None)
734+
Mutability::Not
730735
}
731736
}
732737
}

0 commit comments

Comments
 (0)