Skip to content

Commit d43b5fc

Browse files
committed
check that required_consts really contains all required consts
1 parent c94ed5c commit d43b5fc

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/librustc_mir/interpret/machine.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ pub trait Machine<'mir, 'tcx>: Sized {
119119
/// that is added to the memory so that the work is not done twice.
120120
const GLOBAL_KIND: Option<Self::MemoryKind>;
121121

122+
/// Whether this machine allow const evaluation of `mir::Operand` to fail.
123+
/// Usually machines do not, as frame pushing already makes sure that all consts evaluated.
124+
/// However, ConstProp does not do regular frame pushing.
125+
const PERMIT_LATE_CONST_EVAL_FAIL: bool = false;
126+
122127
/// Whether memory accesses should be alignment-checked.
123128
fn enforce_alignment(memory_extra: &Self::MemoryExtra) -> bool;
124129

src/librustc_mir/interpret/operand.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,15 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
517517
Constant(ref constant) => {
518518
let val =
519519
self.subst_from_current_frame_and_normalize_erasing_regions(constant.literal);
520-
self.const_to_op(val, layout)?
520+
// This should not raise any errors, except for TooGeneric (during ConstProp).
521+
self.const_to_op(val, layout).map_err(|err|
522+
if M::PERMIT_LATE_CONST_EVAL_FAIL {
523+
err
524+
} else {
525+
err.print_backtrace();
526+
span_bug!(self.cur_span(), "const eval should have failed already when evaluating required_consts, so this error is unexpected: {:?}", err)
527+
}
528+
)?
521529
}
522530
};
523531
trace!("{:?}: {:?}", mir_op, *op);

src/librustc_mir/transform/const_prop.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx>
179179

180180
type MemoryExtra = ();
181181

182+
const PERMIT_LATE_CONST_EVAL_FAIL: bool = true;
183+
182184
fn find_mir_or_eval_fn(
183185
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
184186
_instance: ty::Instance<'tcx>,

0 commit comments

Comments
 (0)