Skip to content

Commit db45044

Browse files
committed
Move valiation into InterpretationResult
1 parent aa89b9d commit db45044

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -293,19 +293,25 @@ pub fn eval_static_initializer_provider<'tcx>(
293293
eval_in_interpreter(&mut ecx, cid, true)
294294
}
295295

296-
pub trait InterpretationResult<'tcx> {
296+
pub trait InterpretationResult<'tcx>: Sized {
297297
fn make_result<'mir>(
298-
mplace: MPlaceTy<'tcx>,
298+
mplace: &MPlaceTy<'tcx>,
299299
ecx: &mut InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
300-
) -> Self;
300+
cid: GlobalId<'tcx>,
301+
) -> InterpResult<'tcx, Self>;
301302
}
302303

303304
impl<'tcx> InterpretationResult<'tcx> for ConstAlloc<'tcx> {
304305
fn make_result<'mir>(
305-
mplace: MPlaceTy<'tcx>,
306-
_ecx: &mut InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
307-
) -> Self {
308-
ConstAlloc { alloc_id: mplace.ptr().provenance.unwrap().alloc_id(), ty: mplace.layout.ty }
306+
mplace: &MPlaceTy<'tcx>,
307+
ecx: &mut InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
308+
cid: GlobalId<'tcx>,
309+
) -> InterpResult<'tcx, Self> {
310+
const_validate_mplace(ecx, mplace, cid)?;
311+
Ok(ConstAlloc {
312+
alloc_id: mplace.ptr().provenance.unwrap().alloc_id(),
313+
ty: mplace.layout.ty,
314+
})
309315
}
310316
}
311317

@@ -393,15 +399,13 @@ fn eval_in_interpreter<'mir, 'tcx, R: InterpretationResult<'tcx>>(
393399
}
394400
Ok(mplace) => {
395401
// Since evaluation had no errors, validate the resulting constant.
396-
let res = const_validate_mplace(&ecx, &mplace, cid);
402+
let res = R::make_result(&mplace, ecx, cid);
397403

398404
// Validation failed, report an error.
399-
if let Err(error) = res {
405+
res.map_err(|error| {
400406
let alloc_id = mplace.ptr().provenance.unwrap().alloc_id();
401-
Err(const_report_error(&ecx, error, alloc_id))
402-
} else {
403-
Ok(R::make_result(mplace, ecx))
404-
}
407+
const_report_error(&ecx, error, alloc_id)
408+
})
405409
}
406410
}
407411
}

compiler/rustc_const_eval/src/interpret/util.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::const_eval::{CompileTimeEvalContext, CompileTimeInterpreter, InterpretationResult};
22
use crate::interpret::{MemPlaceMeta, MemoryKind};
33
use rustc_middle::mir;
4-
use rustc_middle::mir::interpret::{Allocation, InterpResult, Pointer};
4+
use rustc_middle::mir::interpret::{Allocation, GlobalId, InterpResult, Pointer};
55
use rustc_middle::ty::layout::TyAndLayout;
66
use rustc_middle::ty::{
77
self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
@@ -83,12 +83,14 @@ where
8383

8484
impl<'tcx> InterpretationResult<'tcx> for mir::interpret::ConstAllocation<'tcx> {
8585
fn make_result<'mir>(
86-
mplace: MPlaceTy<'tcx>,
86+
mplace: &MPlaceTy<'tcx>,
8787
ecx: &mut InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
88-
) -> Self {
88+
cid: GlobalId<'tcx>,
89+
) -> InterpResult<'tcx, Self> {
90+
crate::const_eval::const_validate_mplace(ecx, mplace, cid)?;
8991
let alloc_id = mplace.ptr().provenance.unwrap().alloc_id();
9092
let alloc = ecx.memory.alloc_map.swap_remove(&alloc_id).unwrap().1;
91-
ecx.tcx.mk_const_alloc(alloc)
93+
Ok(ecx.tcx.mk_const_alloc(alloc))
9294
}
9395
}
9496

0 commit comments

Comments
 (0)