Skip to content

Commit a2aa3d6

Browse files
authored
Rollup merge of #79664 - vn-ki:move-memkind-heap, r=oli-obk
move interpret::MemoryKind::Heap to const eval r? ``@oli-obk``
2 parents 0fbbe94 + ff0ebd2 commit a2aa3d6

File tree

5 files changed

+48
-21
lines changed

5 files changed

+48
-21
lines changed

compiler/rustc_mir/src/const_eval/machine.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::collections::hash_map::Entry;
77
use std::hash::Hash;
88

99
use rustc_data_structures::fx::FxHashMap;
10+
use std::fmt;
1011

1112
use rustc_ast::Mutability;
1213
use rustc_hir::def_id::DefId;
@@ -179,6 +180,28 @@ impl<K: Hash + Eq, V> interpret::AllocMap<K, V> for FxHashMap<K, V> {
179180
crate type CompileTimeEvalContext<'mir, 'tcx> =
180181
InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>;
181182

183+
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
184+
pub enum MemoryKind {
185+
Heap,
186+
}
187+
188+
impl fmt::Display for MemoryKind {
189+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
190+
match self {
191+
MemoryKind::Heap => write!(f, "heap allocation"),
192+
}
193+
}
194+
}
195+
196+
impl interpret::MayLeak for MemoryKind {
197+
#[inline(always)]
198+
fn may_leak(self) -> bool {
199+
match self {
200+
MemoryKind::Heap => false,
201+
}
202+
}
203+
}
204+
182205
impl interpret::MayLeak for ! {
183206
#[inline(always)]
184207
fn may_leak(self) -> bool {
@@ -222,6 +245,8 @@ impl<'mir, 'tcx: 'mir> CompileTimeEvalContext<'mir, 'tcx> {
222245
impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir, 'tcx> {
223246
compile_time_machine!(<'mir, 'tcx>);
224247

248+
type MemoryKind = MemoryKind;
249+
225250
type MemoryExtra = MemoryExtra;
226251

227252
fn find_mir_or_eval_fn(
@@ -317,7 +342,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
317342
let ptr = ecx.memory.allocate(
318343
Size::from_bytes(size as u64),
319344
align,
320-
interpret::MemoryKind::ConstHeap,
345+
interpret::MemoryKind::Machine(MemoryKind::Heap),
321346
);
322347
ecx.write_scalar(Scalar::Ptr(ptr), dest)?;
323348
}

compiler/rustc_mir/src/interpret/intern.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,20 @@ use rustc_target::abi::Size;
2525
use rustc_ast::Mutability;
2626

2727
use super::{AllocId, Allocation, InterpCx, MPlaceTy, Machine, MemoryKind, Scalar, ValueVisitor};
28+
use crate::const_eval;
2829

29-
pub trait CompileTimeMachine<'mir, 'tcx> = Machine<
30+
pub trait CompileTimeMachine<'mir, 'tcx, T> = Machine<
3031
'mir,
3132
'tcx,
32-
MemoryKind = !,
33+
MemoryKind = T,
3334
PointerTag = (),
3435
ExtraFnVal = !,
3536
FrameExtra = (),
3637
AllocExtra = (),
37-
MemoryMap = FxHashMap<AllocId, (MemoryKind<!>, Allocation)>,
38+
MemoryMap = FxHashMap<AllocId, (MemoryKind<T>, Allocation)>,
3839
>;
3940

40-
struct InternVisitor<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx>> {
41+
struct InternVisitor<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx, const_eval::MemoryKind>> {
4142
/// The ectx from which we intern.
4243
ecx: &'rt mut InterpCx<'mir, 'tcx, M>,
4344
/// Previously encountered safe references.
@@ -74,7 +75,7 @@ struct IsStaticOrFn;
7475
/// `immutable` things might become mutable if `ty` is not frozen.
7576
/// `ty` can be `None` if there is no potential interior mutability
7677
/// to account for (e.g. for vtables).
77-
fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx>>(
78+
fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx, const_eval::MemoryKind>>(
7879
ecx: &'rt mut InterpCx<'mir, 'tcx, M>,
7980
leftover_allocations: &'rt mut FxHashSet<AllocId>,
8081
alloc_id: AllocId,
@@ -105,7 +106,7 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx>>(
105106
// changes in this function.
106107
match kind {
107108
MemoryKind::Stack
108-
| MemoryKind::ConstHeap
109+
| MemoryKind::Machine(const_eval::MemoryKind::Heap)
109110
| MemoryKind::Vtable
110111
| MemoryKind::CallerLocation => {}
111112
}
@@ -141,7 +142,9 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx>>(
141142
None
142143
}
143144

144-
impl<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx>> InternVisitor<'rt, 'mir, 'tcx, M> {
145+
impl<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx, const_eval::MemoryKind>>
146+
InternVisitor<'rt, 'mir, 'tcx, M>
147+
{
145148
fn intern_shallow(
146149
&mut self,
147150
alloc_id: AllocId,
@@ -152,8 +155,8 @@ impl<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx>> InternVisitor<'rt, 'mir
152155
}
153156
}
154157

155-
impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
156-
for InternVisitor<'rt, 'mir, 'tcx, M>
158+
impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx, const_eval::MemoryKind>>
159+
ValueVisitor<'mir, 'tcx, M> for InternVisitor<'rt, 'mir, 'tcx, M>
157160
{
158161
type V = MPlaceTy<'tcx>;
159162

@@ -290,7 +293,7 @@ pub enum InternKind {
290293
/// Any errors here would anyway be turned into `const_err` lints, whereas validation failures
291294
/// are hard errors.
292295
#[tracing::instrument(skip(ecx))]
293-
pub fn intern_const_alloc_recursive<M: CompileTimeMachine<'mir, 'tcx>>(
296+
pub fn intern_const_alloc_recursive<M: CompileTimeMachine<'mir, 'tcx, const_eval::MemoryKind>>(
294297
ecx: &mut InterpCx<'mir, 'tcx, M>,
295298
intern_kind: InternKind,
296299
ret: MPlaceTy<'tcx>,
@@ -421,7 +424,9 @@ where
421424
Ok(())
422425
}
423426

424-
impl<'mir, 'tcx: 'mir, M: super::intern::CompileTimeMachine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
427+
impl<'mir, 'tcx: 'mir, M: super::intern::CompileTimeMachine<'mir, 'tcx, !>>
428+
InterpCx<'mir, 'tcx, M>
429+
{
425430
/// A helper function that allocates memory for the layout given and gives you access to mutate
426431
/// it. Once your own mutation code is done, the backing `Allocation` is removed from the
427432
/// current `Memory` and returned.

compiler/rustc_mir/src/interpret/machine.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,9 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
366366
type PointerTag = ();
367367
type ExtraFnVal = !;
368368

369-
type MemoryKind = !;
370-
type MemoryMap = rustc_data_structures::fx::FxHashMap<AllocId, (MemoryKind<!>, Allocation)>;
371-
const GLOBAL_KIND: Option<!> = None; // no copying of globals from `tcx` to machine memory
369+
type MemoryMap =
370+
rustc_data_structures::fx::FxHashMap<AllocId, (MemoryKind<Self::MemoryKind>, Allocation)>;
371+
const GLOBAL_KIND: Option<Self::MemoryKind> = None; // no copying of globals from `tcx` to machine memory
372372

373373
type AllocExtra = ();
374374
type FrameExtra = ();
@@ -407,7 +407,7 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
407407
_memory_extra: &Self::MemoryExtra,
408408
_id: AllocId,
409409
alloc: Cow<'b, Allocation>,
410-
_kind: Option<MemoryKind<!>>,
410+
_kind: Option<MemoryKind<Self::MemoryKind>>,
411411
) -> (Cow<'b, Allocation<Self::PointerTag>>, Self::PointerTag) {
412412
// We do not use a tag so we can just cheaply forward the allocation
413413
(alloc, ())

compiler/rustc_mir/src/interpret/memory.rs

-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ use crate::util::pretty;
2727
pub enum MemoryKind<T> {
2828
/// Stack memory. Error if deallocated except during a stack pop.
2929
Stack,
30-
/// Heap memory.
31-
/// FIXME: this variant should be in const_eval
32-
ConstHeap,
3330
/// Memory backing vtables. Error if ever deallocated.
3431
Vtable,
3532
/// Memory allocated by `caller_location` intrinsic. Error if ever deallocated.
@@ -43,7 +40,6 @@ impl<T: MayLeak> MayLeak for MemoryKind<T> {
4340
fn may_leak(self) -> bool {
4441
match self {
4542
MemoryKind::Stack => false,
46-
MemoryKind::ConstHeap => false,
4743
MemoryKind::Vtable => true,
4844
MemoryKind::CallerLocation => true,
4945
MemoryKind::Machine(k) => k.may_leak(),
@@ -55,7 +51,6 @@ impl<T: fmt::Display> fmt::Display for MemoryKind<T> {
5551
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5652
match self {
5753
MemoryKind::Stack => write!(f, "stack variable"),
58-
MemoryKind::ConstHeap => write!(f, "heap allocation"),
5954
MemoryKind::Vtable => write!(f, "vtable"),
6055
MemoryKind::CallerLocation => write!(f, "caller location"),
6156
MemoryKind::Machine(m) => write!(f, "{}", m),

compiler/rustc_mir/src/transform/const_prop.rs

+2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ impl<'mir, 'tcx> ConstPropMachine<'mir, 'tcx> {
180180
impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx> {
181181
compile_time_machine!(<'mir, 'tcx>);
182182

183+
type MemoryKind = !;
184+
183185
type MemoryExtra = ();
184186

185187
fn find_mir_or_eval_fn(

0 commit comments

Comments
 (0)