diff --git a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs
index 946546263ea..b81a4bfe149 100644
--- a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs
+++ b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs
@@ -1,4 +1,4 @@
-use super::{CompileTimeEvalContext, CompileTimeInterpreter, ConstEvalErr, MemoryExtra};
+use super::{CompileTimeEvalContext, CompileTimeInterpreter, ConstEvalErr};
use crate::interpret::eval_nullary_intrinsic;
use crate::interpret::{
intern_const_alloc_recursive, Allocation, ConstAlloc, ConstValue, CtfeValidationMode, GlobalId,
@@ -100,8 +100,7 @@ pub(super) fn mk_eval_cx<'mir, 'tcx>(
tcx,
root_span,
param_env,
- CompileTimeInterpreter::new(tcx.const_eval_limit()),
- MemoryExtra { can_access_statics },
+ CompileTimeInterpreter::new(tcx.const_eval_limit(), can_access_statics),
)
}
@@ -285,10 +284,9 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
tcx,
tcx.def_span(def.did),
key.param_env,
- CompileTimeInterpreter::new(tcx.const_eval_limit()),
// Statics (and promoteds inside statics) may access other statics, because unlike consts
// they do not have to behave "as if" they were evaluated at runtime.
- MemoryExtra { can_access_statics: is_static },
+ CompileTimeInterpreter::new(tcx.const_eval_limit(), /*can_access_statics:*/ is_static),
);
let res = ecx.load_mir(cid.instance.def, cid.promoted);
diff --git a/compiler/rustc_const_eval/src/const_eval/machine.rs b/compiler/rustc_const_eval/src/const_eval/machine.rs
index 2b58c1e8233..5aae4a90960 100644
--- a/compiler/rustc_const_eval/src/const_eval/machine.rs
+++ b/compiler/rustc_const_eval/src/const_eval/machine.rs
@@ -93,10 +93,7 @@ pub struct CompileTimeInterpreter<'mir, 'tcx> {
/// The virtual call stack.
pub(crate) stack: Vec>,
-}
-#[derive(Copy, Clone, Debug)]
-pub struct MemoryExtra {
/// We need to make sure consts never point to anything mutable, even recursively. That is
/// relied on for pattern matching on consts with references.
/// To achieve this, two pieces have to work together:
@@ -107,8 +104,12 @@ pub struct MemoryExtra {
}
impl<'mir, 'tcx> CompileTimeInterpreter<'mir, 'tcx> {
- pub(super) fn new(const_eval_limit: Limit) -> Self {
- CompileTimeInterpreter { steps_remaining: const_eval_limit.0, stack: Vec::new() }
+ pub(super) fn new(const_eval_limit: Limit, can_access_statics: bool) -> Self {
+ CompileTimeInterpreter {
+ steps_remaining: const_eval_limit.0,
+ stack: Vec::new(),
+ can_access_statics,
+ }
}
}
@@ -233,8 +234,6 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
type MemoryKind = MemoryKind;
- type MemoryExtra = MemoryExtra;
-
const PANIC_ON_ALLOC_FAIL: bool = false; // will be raised as a proper error
fn load_mir(
@@ -345,7 +344,7 @@ fn call_intrinsic(
Err(err) => throw_ub_format!("align has to be a power of 2, {}", err),
};
- let ptr = ecx.memory.allocate(
+ let ptr = ecx.allocate_ptr(
Size::from_bytes(size as u64),
align,
interpret::MemoryKind::Machine(MemoryKind::Heap),
@@ -365,14 +364,14 @@ fn call_intrinsic(
// If an allocation is created in an another const,
// we don't deallocate it.
- let (alloc_id, _, _) = ecx.memory.ptr_get_alloc(ptr)?;
+ let (alloc_id, _, _) = ecx.ptr_get_alloc_id(ptr)?;
let is_allocated_in_another_const = matches!(
ecx.tcx.get_global_alloc(alloc_id),
Some(interpret::GlobalAlloc::Memory(_))
);
if !is_allocated_in_another_const {
- ecx.memory.deallocate(
+ ecx.deallocate_ptr(
ptr,
Some((size, align)),
interpret::MemoryKind::Machine(MemoryKind::Heap),
@@ -472,7 +471,7 @@ fn stack_mut<'a>(
}
fn before_access_global(
- memory_extra: &MemoryExtra,
+ machine: &Self,
alloc_id: AllocId,
alloc: ConstAllocation<'tcx>,
static_def_id: Option,
@@ -488,7 +487,7 @@ fn before_access_global(
}
} else {
// Read access. These are usually allowed, with some exceptions.
- if memory_extra.can_access_statics {
+ if machine.can_access_statics {
// Machine configuration allows us read from anything (e.g., `static` initializer).
Ok(())
} else if static_def_id.is_some() {
diff --git a/compiler/rustc_const_eval/src/interpret/cast.rs b/compiler/rustc_const_eval/src/interpret/cast.rs
index 5be0a183cf6..a244b79ed07 100644
--- a/compiler/rustc_const_eval/src/interpret/cast.rs
+++ b/compiler/rustc_const_eval/src/interpret/cast.rs
@@ -56,7 +56,7 @@ pub fn cast(
)
.ok_or_else(|| err_inval!(TooGeneric))?;
- let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
+ let fn_ptr = self.create_fn_alloc_ptr(FnVal::Instance(instance));
self.write_pointer(fn_ptr, dest)?;
}
_ => span_bug!(self.cur_span(), "reify fn pointer on {:?}", src.layout.ty),
@@ -87,7 +87,7 @@ pub fn cast(
substs,
ty::ClosureKind::FnOnce,
);
- let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
+ let fn_ptr = self.create_fn_alloc_ptr(FnVal::Instance(instance));
self.write_pointer(fn_ptr, dest)?;
}
_ => span_bug!(self.cur_span(), "closure fn pointer on {:?}", src.layout.ty),
@@ -153,8 +153,8 @@ pub fn misc_cast(
return Ok(**src);
} else {
// Casting the metadata away from a fat ptr.
- assert_eq!(src.layout.size, 2 * self.memory.pointer_size());
- assert_eq!(dest_layout.size, self.memory.pointer_size());
+ assert_eq!(src.layout.size, 2 * self.pointer_size());
+ assert_eq!(dest_layout.size, self.pointer_size());
assert!(src.layout.ty.is_unsafe_ptr());
return match **src {
Immediate::ScalarPair(data, _) => Ok(data.into()),
diff --git a/compiler/rustc_const_eval/src/interpret/eval_context.rs b/compiler/rustc_const_eval/src/interpret/eval_context.rs
index 1b8186b5aad..a2ea0f516bf 100644
--- a/compiler/rustc_const_eval/src/interpret/eval_context.rs
+++ b/compiler/rustc_const_eval/src/interpret/eval_context.rs
@@ -22,9 +22,9 @@
use rustc_target::abi::{call::FnAbi, Align, HasDataLayout, Size, TargetDataLayout};
use super::{
- AllocCheck, AllocId, GlobalId, Immediate, InterpErrorInfo, InterpResult, MPlaceTy, Machine,
- MemPlace, MemPlaceMeta, Memory, MemoryKind, Operand, Place, PlaceTy, Pointer,
- PointerArithmetic, Provenance, Scalar, ScalarMaybeUninit, StackPopJump,
+ AllocId, GlobalId, Immediate, InterpErrorInfo, InterpResult, MPlaceTy, Machine, MemPlace,
+ MemPlaceMeta, Memory, MemoryKind, Operand, Place, PlaceTy, PointerArithmetic, Provenance,
+ Scalar, ScalarMaybeUninit, StackPopJump,
};
use crate::transform::validate::equal_up_to_regions;
@@ -413,13 +413,12 @@ pub fn new(
root_span: Span,
param_env: ty::ParamEnv<'tcx>,
machine: M,
- memory_extra: M::MemoryExtra,
) -> Self {
InterpCx {
machine,
tcx: tcx.at(root_span),
param_env,
- memory: Memory::new(tcx, memory_extra),
+ memory: Memory::new(),
recursion_limit: tcx.recursion_limit(),
}
}
@@ -433,49 +432,6 @@ pub fn cur_span(&self) -> Span {
.map_or(self.tcx.span, |f| f.current_span())
}
- #[inline(always)]
- pub fn scalar_to_ptr(&self, scalar: Scalar) -> Pointer