Skip to content

Commit 5784e9e

Browse files
committed
Pull mplace validation logic out into an interpreter method
1 parent 0fce74e commit 5784e9e

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 32 additions & 20 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;
@@ -331,25 +331,9 @@ 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 =
335+
const_validate_mplace(&ecx, &mplace, is_static, cid.promoted.is_some());
336+
353337
let alloc_id = mplace.ptr().provenance.unwrap();
354338

355339
// Validation failed, report an error.
@@ -391,3 +375,31 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
391375
}
392376
}
393377
}
378+
379+
#[inline(always)]
380+
pub fn const_validate_mplace<'mir, 'tcx>(
381+
ecx: &InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
382+
mplace: &MPlaceTy<'tcx>,
383+
is_static: bool,
384+
is_promoted: bool,
385+
) -> InterpResult<'tcx> {
386+
let mut ref_tracking = RefTracking::new(mplace.clone());
387+
let mut inner = false;
388+
while let Some((mplace, path)) = ref_tracking.todo.pop() {
389+
let mode = if is_static {
390+
if is_promoted {
391+
// Promoteds in statics are allowed to point to statics.
392+
CtfeValidationMode::Const { inner, allow_static_ptrs: true }
393+
} else {
394+
// a `static`
395+
CtfeValidationMode::Regular
396+
}
397+
} else {
398+
CtfeValidationMode::Const { inner, allow_static_ptrs: false }
399+
};
400+
ecx.const_validate_operand(&mplace.into(), path, &mut ref_tracking, mode)?;
401+
inner = true;
402+
}
403+
404+
Ok(())
405+
}

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 1 addition & 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+
pub(crate) fn const_validate_operand(
933933
&self,
934934
op: &OpTy<'tcx, M::Provenance>,
935935
path: Vec<PathElem>,

0 commit comments

Comments
 (0)