Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 500dbd4

Browse files
committed
Pull mplace validation logic out into an interpreter method
1 parent 37e79eb commit 500dbd4

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::mem;
33
use either::{Left, Right};
44

55
use rustc_hir::def::DefKind;
6-
use rustc_middle::mir::interpret::{ErrorHandled, InterpErrorInfo};
6+
use rustc_middle::mir::interpret::ErrorHandled;
77
use rustc_middle::mir::pretty::write_allocation_bytes;
88
use rustc_middle::mir::{self, ConstAlloc, ConstValue};
99
use rustc_middle::traits::Reveal;
@@ -19,8 +19,8 @@ use crate::errors;
1919
use crate::errors::ConstEvalError;
2020
use crate::interpret::eval_nullary_intrinsic;
2121
use crate::interpret::{
22-
intern_const_alloc_recursive, CtfeValidationMode, GlobalId, Immediate, InternKind, InterpCx,
23-
InterpError, InterpResult, MPlaceTy, MemoryKind, OpTy, RefTracking, StackPopCleanup,
22+
intern_const_alloc_recursive, GlobalId, Immediate, InternKind, InterpCx, InterpError,
23+
InterpResult, MPlaceTy, MemoryKind, OpTy, StackPopCleanup,
2424
};
2525

2626
// Returns a pointer to where the result lives
@@ -331,25 +331,8 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
331331
Ok(mplace) => {
332332
// Since evaluation had no errors, validate the resulting constant.
333333
// This is a separate `try` block to provide more targeted error reporting.
334-
let validation: Result<_, InterpErrorInfo<'_>> = try {
335-
let mut ref_tracking = RefTracking::new(mplace.clone());
336-
let mut inner = false;
337-
while let Some((mplace, path)) = ref_tracking.todo.pop() {
338-
let mode = if is_static {
339-
if cid.promoted.is_some() {
340-
// Promoteds in statics are allowed to point to statics.
341-
CtfeValidationMode::Const { inner, allow_static_ptrs: true }
342-
} else {
343-
// a `static`
344-
CtfeValidationMode::Regular
345-
}
346-
} else {
347-
CtfeValidationMode::Const { inner, allow_static_ptrs: false }
348-
};
349-
ecx.const_validate_operand(&mplace.into(), path, &mut ref_tracking, mode)?;
350-
inner = true;
351-
}
352-
};
334+
let validation = ecx.const_validate_mplace(&mplace, is_static, cid.promoted.is_some());
335+
353336
let alloc_id = mplace.ptr().provenance.unwrap();
354337

355338
// Validation failed, report an error.

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
929929
/// - no pointers to statics.
930930
/// - no `UnsafeCell` or non-ZST `&mut`.
931931
#[inline(always)]
932-
pub fn const_validate_operand(
932+
fn const_validate_operand(
933933
&self,
934934
op: &OpTy<'tcx, M::Provenance>,
935935
path: Vec<PathElem>,
@@ -939,6 +939,34 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
939939
self.validate_operand_internal(op, path, Some(ref_tracking), Some(ctfe_mode))
940940
}
941941

942+
#[inline(always)]
943+
pub fn const_validate_mplace(
944+
&self,
945+
mplace: &MPlaceTy<'tcx, M::Provenance>,
946+
is_static: bool,
947+
is_promoted: bool,
948+
) -> InterpResult<'tcx> {
949+
let mut ref_tracking = RefTracking::new(mplace.clone());
950+
let mut inner = false;
951+
while let Some((mplace, path)) = ref_tracking.todo.pop() {
952+
let mode = if is_static {
953+
if is_promoted {
954+
// Promoteds in statics are allowed to point to statics.
955+
CtfeValidationMode::Const { inner, allow_static_ptrs: true }
956+
} else {
957+
// a `static`
958+
CtfeValidationMode::Regular
959+
}
960+
} else {
961+
CtfeValidationMode::Const { inner, allow_static_ptrs: false }
962+
};
963+
self.const_validate_operand(&mplace.into(), path, &mut ref_tracking, mode)?;
964+
inner = true;
965+
}
966+
967+
Ok(())
968+
}
969+
942970
/// This function checks the data at `op` to be runtime-valid.
943971
/// `op` is assumed to cover valid memory if it is an indirect operand.
944972
/// It will error if the bits at the destination do not match the ones described by the layout.

0 commit comments

Comments
 (0)