Skip to content

Commit 967b34a

Browse files
committed
simplfy memory kind handling during interning
1 parent f081e85 commit 967b34a

File tree

4 files changed

+11
-42
lines changed

4 files changed

+11
-42
lines changed

compiler/rustc_const_eval/src/const_eval/dummy_machine.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ impl HasStaticRootDefId for DummyMachine {
4949

5050
impl<'tcx> interpret::Machine<'tcx> for DummyMachine {
5151
interpret::compile_time_machine!(<'tcx>);
52-
type MemoryKind = !;
5352
const PANIC_ON_ALLOC_FAIL: bool = true;
5453

5554
// We want to just eval random consts in the program, so `eval_mir_const` can fail.

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,6 @@ impl<'tcx> CompileTimeMachine<'tcx> {
320320
impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
321321
compile_time_machine!(<'tcx>);
322322

323-
type MemoryKind = MemoryKind;
324-
325323
const PANIC_ON_ALLOC_FAIL: bool = false; // will be raised as a proper error
326324

327325
#[inline(always)]

compiler/rustc_const_eval/src/interpret/intern.rs

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ use super::{AllocId, Allocation, InterpCx, MPlaceTy, Machine, MemoryKind, PlaceT
3030
use crate::const_eval::DummyMachine;
3131
use crate::{const_eval, errors};
3232

33-
pub trait CompileTimeMachine<'tcx, T> = Machine<
33+
pub trait CompileTimeMachine<'tcx> = Machine<
3434
'tcx,
35-
MemoryKind = T,
35+
MemoryKind = const_eval::MemoryKind,
3636
Provenance = CtfeProvenance,
3737
ExtraFnVal = !,
3838
FrameExtra = (),
3939
AllocExtra = (),
40-
MemoryMap = FxIndexMap<AllocId, (MemoryKind<T>, Allocation)>,
40+
MemoryMap = FxIndexMap<AllocId, (MemoryKind<const_eval::MemoryKind>, Allocation)>,
4141
> + HasStaticRootDefId;
4242

4343
pub trait HasStaticRootDefId {
@@ -52,43 +52,14 @@ impl HasStaticRootDefId for const_eval::CompileTimeMachine<'_> {
5252
}
5353
}
5454

55-
pub enum DisallowInternReason {
56-
ConstHeap,
57-
}
58-
59-
/// A trait for controlling whether memory allocated in the interpreter can be interned.
60-
///
61-
/// This prevents us from interning `const_allocate` pointers that have not been made
62-
/// global through `const_make_global`.
63-
pub trait CanIntern: Copy {
64-
fn disallows_intern(&self) -> Option<DisallowInternReason>;
65-
}
66-
67-
impl CanIntern for const_eval::MemoryKind {
68-
fn disallows_intern(&self) -> Option<DisallowInternReason> {
69-
match self {
70-
const_eval::MemoryKind::Heap { was_made_global: false } => {
71-
Some(DisallowInternReason::ConstHeap)
72-
}
73-
const_eval::MemoryKind::Heap { was_made_global: true } => None,
74-
}
75-
}
76-
}
77-
78-
impl CanIntern for ! {
79-
fn disallows_intern(&self) -> Option<DisallowInternReason> {
80-
*self
81-
}
82-
}
83-
8455
/// Intern an allocation. Returns `Err` if the allocation does not exist in the local memory.
8556
///
8657
/// `mutability` can be used to force immutable interning: if it is `Mutability::Not`, the
8758
/// allocation is interned immutably; if it is `Mutability::Mut`, then the allocation *must be*
8859
/// already mutable (as a sanity check).
8960
///
9061
/// Returns an iterator over all relocations referred to by this allocation.
91-
fn intern_shallow<'tcx, T: CanIntern, M: CompileTimeMachine<'tcx, T>>(
62+
fn intern_shallow<'tcx, M: CompileTimeMachine<'tcx>>(
9263
ecx: &mut InterpCx<'tcx, M>,
9364
alloc_id: AllocId,
9465
mutability: Mutability,
@@ -102,16 +73,16 @@ fn intern_shallow<'tcx, T: CanIntern, M: CompileTimeMachine<'tcx, T>>(
10273
};
10374

10475
match kind {
105-
MemoryKind::Machine(x) if let Some(reason) = x.disallows_intern() => match reason {
106-
DisallowInternReason::ConstHeap => {
76+
MemoryKind::Machine(const_eval::MemoryKind::Heap { was_made_global }) => {
77+
if !was_made_global {
10778
// Attempting to intern a `const_allocate`d pointer that was not made global via
10879
// `const_make_global`. We want to error here, but we have to first put the
10980
// allocation back into the `alloc_map` to keep things in a consistent state.
11081
ecx.memory.alloc_map.insert(alloc_id, (kind, alloc));
11182
return Err(InternError::ConstAllocNotGlobal);
11283
}
113-
},
114-
MemoryKind::Machine(_) | MemoryKind::Stack | MemoryKind::CallerLocation => {}
84+
}
85+
MemoryKind::Stack | MemoryKind::CallerLocation => {}
11586
}
11687

11788
// Set allocation mutability as appropriate. This is used by LLVM to put things into
@@ -204,7 +175,7 @@ pub enum InternError {
204175
///
205176
/// For `InternKind::Static` the root allocation will not be interned, but must be handled by the caller.
206177
#[instrument(level = "debug", skip(ecx))]
207-
pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval::MemoryKind>>(
178+
pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
208179
ecx: &mut InterpCx<'tcx, M>,
209180
intern_kind: InternKind,
210181
ret: &MPlaceTy<'tcx>,
@@ -365,7 +336,7 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval
365336

366337
/// Intern `ret`. This function assumes that `ret` references no other allocation.
367338
#[instrument(level = "debug", skip(ecx))]
368-
pub fn intern_const_alloc_for_constprop<'tcx, T: CanIntern, M: CompileTimeMachine<'tcx, T>>(
339+
pub fn intern_const_alloc_for_constprop<'tcx, M: CompileTimeMachine<'tcx>>(
369340
ecx: &mut InterpCx<'tcx, M>,
370341
alloc_id: AllocId,
371342
) -> InterpResult<'tcx, ()> {

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
649649

650650
type ExtraFnVal = !;
651651

652+
type MemoryKind = $crate::const_eval::MemoryKind;
652653
type MemoryMap =
653654
rustc_data_structures::fx::FxIndexMap<AllocId, (MemoryKind<Self::MemoryKind>, Allocation)>;
654655
const GLOBAL_KIND: Option<Self::MemoryKind> = None; // no copying of globals from `tcx` to machine memory

0 commit comments

Comments
 (0)