about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2025-07-16 15:19:17 +0200
committerRalf Jung <post@ralfj.de>2025-07-16 15:29:52 +0200
commit0bf0860a0ef285fe3e4eb3b176006c14d8ca9d8d (patch)
treebb08ff171c3e0d11fd33f8c8a91fb9af98cb728e
parent8f854d9cb2d108a2d4f980ccb4d5909e214e6ef0 (diff)
downloadrust-0bf0860a0ef285fe3e4eb3b176006c14d8ca9d8d.tar.gz
rust-0bf0860a0ef285fe3e4eb3b176006c14d8ca9d8d.zip
simplfy memory kind handling during interning
-rw-r--r--compiler/rustc_const_eval/src/const_eval/dummy_machine.rs1
-rw-r--r--compiler/rustc_const_eval/src/const_eval/machine.rs2
-rw-r--r--compiler/rustc_const_eval/src/interpret/intern.rs49
-rw-r--r--compiler/rustc_const_eval/src/interpret/machine.rs1
4 files changed, 11 insertions, 42 deletions
diff --git a/compiler/rustc_const_eval/src/const_eval/dummy_machine.rs b/compiler/rustc_const_eval/src/const_eval/dummy_machine.rs
index b6e2682af36..438aed41b8b 100644
--- a/compiler/rustc_const_eval/src/const_eval/dummy_machine.rs
+++ b/compiler/rustc_const_eval/src/const_eval/dummy_machine.rs
@@ -49,7 +49,6 @@ impl HasStaticRootDefId for DummyMachine {
 
 impl<'tcx> interpret::Machine<'tcx> for DummyMachine {
     interpret::compile_time_machine!(<'tcx>);
-    type MemoryKind = !;
     const PANIC_ON_ALLOC_FAIL: bool = true;
 
     // We want to just eval random consts in the program, so `eval_mir_const` can fail.
diff --git a/compiler/rustc_const_eval/src/const_eval/machine.rs b/compiler/rustc_const_eval/src/const_eval/machine.rs
index 0a6bdef2225..70ad57b2b11 100644
--- a/compiler/rustc_const_eval/src/const_eval/machine.rs
+++ b/compiler/rustc_const_eval/src/const_eval/machine.rs
@@ -320,8 +320,6 @@ impl<'tcx> CompileTimeMachine<'tcx> {
 impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
     compile_time_machine!(<'tcx>);
 
-    type MemoryKind = MemoryKind;
-
     const PANIC_ON_ALLOC_FAIL: bool = false; // will be raised as a proper error
 
     #[inline(always)]
diff --git a/compiler/rustc_const_eval/src/interpret/intern.rs b/compiler/rustc_const_eval/src/interpret/intern.rs
index f25bb222bcf..bb59b9f5418 100644
--- a/compiler/rustc_const_eval/src/interpret/intern.rs
+++ b/compiler/rustc_const_eval/src/interpret/intern.rs
@@ -30,14 +30,14 @@ use super::{AllocId, Allocation, InterpCx, MPlaceTy, Machine, MemoryKind, PlaceT
 use crate::const_eval::DummyMachine;
 use crate::{const_eval, errors};
 
-pub trait CompileTimeMachine<'tcx, T> = Machine<
+pub trait CompileTimeMachine<'tcx> = Machine<
         'tcx,
-        MemoryKind = T,
+        MemoryKind = const_eval::MemoryKind,
         Provenance = CtfeProvenance,
         ExtraFnVal = !,
         FrameExtra = (),
         AllocExtra = (),
-        MemoryMap = FxIndexMap<AllocId, (MemoryKind<T>, Allocation)>,
+        MemoryMap = FxIndexMap<AllocId, (MemoryKind<const_eval::MemoryKind>, Allocation)>,
     > + HasStaticRootDefId;
 
 pub trait HasStaticRootDefId {
@@ -52,35 +52,6 @@ impl HasStaticRootDefId for const_eval::CompileTimeMachine<'_> {
     }
 }
 
-pub enum DisallowInternReason {
-    ConstHeap,
-}
-
-/// A trait for controlling whether memory allocated in the interpreter can be interned.
-///
-/// This prevents us from interning `const_allocate` pointers that have not been made
-/// global through `const_make_global`.
-pub trait CanIntern: Copy {
-    fn disallows_intern(&self) -> Option<DisallowInternReason>;
-}
-
-impl CanIntern for const_eval::MemoryKind {
-    fn disallows_intern(&self) -> Option<DisallowInternReason> {
-        match self {
-            const_eval::MemoryKind::Heap { was_made_global: false } => {
-                Some(DisallowInternReason::ConstHeap)
-            }
-            const_eval::MemoryKind::Heap { was_made_global: true } => None,
-        }
-    }
-}
-
-impl CanIntern for ! {
-    fn disallows_intern(&self) -> Option<DisallowInternReason> {
-        *self
-    }
-}
-
 /// Intern an allocation. Returns `Err` if the allocation does not exist in the local memory.
 ///
 /// `mutability` can be used to force immutable interning: if it is `Mutability::Not`, the
@@ -88,7 +59,7 @@ impl CanIntern for ! {
 /// already mutable (as a sanity check).
 ///
 /// Returns an iterator over all relocations referred to by this allocation.
-fn intern_shallow<'tcx, T: CanIntern, M: CompileTimeMachine<'tcx, T>>(
+fn intern_shallow<'tcx, M: CompileTimeMachine<'tcx>>(
     ecx: &mut InterpCx<'tcx, M>,
     alloc_id: AllocId,
     mutability: Mutability,
@@ -102,16 +73,16 @@ fn intern_shallow<'tcx, T: CanIntern, M: CompileTimeMachine<'tcx, T>>(
     };
 
     match kind {
-        MemoryKind::Machine(x) if let Some(reason) = x.disallows_intern() => match reason {
-            DisallowInternReason::ConstHeap => {
+        MemoryKind::Machine(const_eval::MemoryKind::Heap { was_made_global }) => {
+            if !was_made_global {
                 // Attempting to intern a `const_allocate`d pointer that was not made global via
                 // `const_make_global`. We want to error here, but we have to first put the
                 // allocation back into the `alloc_map` to keep things in a consistent state.
                 ecx.memory.alloc_map.insert(alloc_id, (kind, alloc));
                 return Err(InternError::ConstAllocNotGlobal);
             }
-        },
-        MemoryKind::Machine(_) | MemoryKind::Stack | MemoryKind::CallerLocation => {}
+        }
+        MemoryKind::Stack | MemoryKind::CallerLocation => {}
     }
 
     // Set allocation mutability as appropriate. This is used by LLVM to put things into
@@ -204,7 +175,7 @@ pub enum InternError {
 ///
 /// For `InternKind::Static` the root allocation will not be interned, but must be handled by the caller.
 #[instrument(level = "debug", skip(ecx))]
-pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval::MemoryKind>>(
+pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
     ecx: &mut InterpCx<'tcx, M>,
     intern_kind: InternKind,
     ret: &MPlaceTy<'tcx>,
@@ -365,7 +336,7 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval
 
 /// Intern `ret`. This function assumes that `ret` references no other allocation.
 #[instrument(level = "debug", skip(ecx))]
-pub fn intern_const_alloc_for_constprop<'tcx, T: CanIntern, M: CompileTimeMachine<'tcx, T>>(
+pub fn intern_const_alloc_for_constprop<'tcx, M: CompileTimeMachine<'tcx>>(
     ecx: &mut InterpCx<'tcx, M>,
     alloc_id: AllocId,
 ) -> InterpResult<'tcx, ()> {
diff --git a/compiler/rustc_const_eval/src/interpret/machine.rs b/compiler/rustc_const_eval/src/interpret/machine.rs
index d150ed69250..e981f3973ae 100644
--- a/compiler/rustc_const_eval/src/interpret/machine.rs
+++ b/compiler/rustc_const_eval/src/interpret/machine.rs
@@ -649,6 +649,7 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
 
     type ExtraFnVal = !;
 
+    type MemoryKind = $crate::const_eval::MemoryKind;
     type MemoryMap =
         rustc_data_structures::fx::FxIndexMap<AllocId, (MemoryKind<Self::MemoryKind>, Allocation)>;
     const GLOBAL_KIND: Option<Self::MemoryKind> = None; // no copying of globals from `tcx` to machine memory