Skip to content

Commit bf021ea

Browse files
committed
interpret: sanity-check that required_consts captures all consts that can fail
1 parent b2b617a commit bf021ea

File tree

7 files changed

+62
-34
lines changed

7 files changed

+62
-34
lines changed

compiler/rustc_const_eval/src/const_eval/dummy_machine.rs

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ impl<'mir, 'tcx: 'mir> interpret::Machine<'mir, 'tcx> for DummyMachine {
4646
type MemoryKind = !;
4747
const PANIC_ON_ALLOC_FAIL: bool = true;
4848

49+
#[inline(always)]
50+
fn all_required_consts_are_checked(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
51+
// We want to just eval random consts in the program, so `eval_mir_const` can fail.
52+
false
53+
}
54+
4955
#[inline(always)]
5056
fn enforce_alignment(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
5157
false // no reason to enforce alignment

compiler/rustc_const_eval/src/const_eval/machine.rs

+14
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,20 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
375375

376376
const PANIC_ON_ALLOC_FAIL: bool = false; // will be raised as a proper error
377377

378+
#[inline]
379+
fn all_required_consts_are_checked(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
380+
// Generally we expect required_consts to be properly filled, except for promoteds where
381+
// storing these consts shows up negatively in benchmarks. A promoted can only be relevant
382+
// if its parent MIR is relevant, and the consts in the promoted will be in the parent's
383+
// `required_consts`, so we are still sure to catch any const-eval bugs, just a bit less
384+
// directly.
385+
if ecx.frame_idx() == 0 && ecx.frame().body.source.promoted.is_some() {
386+
false
387+
} else {
388+
true
389+
}
390+
}
391+
378392
#[inline(always)]
379393
fn enforce_alignment(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
380394
matches!(ecx.machine.check_alignment, CheckAlignment::Error)

compiler/rustc_const_eval/src/interpret/eval_context.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -822,15 +822,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
822822
self.stack_mut().push(frame);
823823

824824
// Make sure all the constants required by this frame evaluate successfully (post-monomorphization check).
825-
if M::POST_MONO_CHECKS {
826-
for &const_ in &body.required_consts {
827-
let c = self
828-
.instantiate_from_current_frame_and_normalize_erasing_regions(const_.const_)?;
829-
c.eval(*self.tcx, self.param_env, const_.span).map_err(|err| {
830-
err.emit_note(*self.tcx);
831-
err
832-
})?;
833-
}
825+
for &const_ in &body.required_consts {
826+
let c =
827+
self.instantiate_from_current_frame_and_normalize_erasing_regions(const_.const_)?;
828+
c.eval(*self.tcx, self.param_env, const_.span).map_err(|err| {
829+
err.emit_note(*self.tcx);
830+
err
831+
})?;
834832
}
835833

836834
// done
@@ -1181,8 +1179,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
11811179
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
11821180
M::eval_mir_constant(self, *val, span, layout, |ecx, val, span, layout| {
11831181
let const_val = val.eval(*ecx.tcx, ecx.param_env, span).map_err(|err| {
1184-
// FIXME: somehow this is reachable even when POST_MONO_CHECKS is on.
1185-
// Are we not always populating `required_consts`?
1182+
if M::all_required_consts_are_checked(self)
1183+
&& !matches!(err, ErrorHandled::TooGeneric(..))
1184+
{
1185+
// Looks like the const is not captued by `required_consts`, that's bad.
1186+
bug!("interpret const eval failure of {val:?} which is not in required_consts");
1187+
}
11861188
err.emit_note(*ecx.tcx);
11871189
err
11881190
})?;

compiler/rustc_const_eval/src/interpret/machine.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ pub trait Machine<'mir, 'tcx: 'mir>: Sized {
140140
/// Should the machine panic on allocation failures?
141141
const PANIC_ON_ALLOC_FAIL: bool;
142142

143-
/// Should post-monomorphization checks be run when a stack frame is pushed?
144-
const POST_MONO_CHECKS: bool = true;
143+
/// Determines whether `eval_mir_constant` can never fail because all required consts have
144+
/// already been checked before.
145+
fn all_required_consts_are_checked(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
145146

146147
/// Whether memory accesses should be alignment-checked.
147148
fn enforce_alignment(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;

compiler/rustc_mir_transform/src/inline.rs

+5-14
Original file line numberDiff line numberDiff line change
@@ -720,18 +720,8 @@ impl<'tcx> Inliner<'tcx> {
720720
kind: TerminatorKind::Goto { target: integrator.map_block(START_BLOCK) },
721721
});
722722

723-
// Copy only unevaluated constants from the callee_body into the caller_body.
724-
// Although we are only pushing `ConstKind::Unevaluated` consts to
725-
// `required_consts`, here we may not only have `ConstKind::Unevaluated`
726-
// because we are calling `instantiate_and_normalize_erasing_regions`.
727-
caller_body.required_consts.extend(callee_body.required_consts.iter().copied().filter(
728-
|&ct| match ct.const_ {
729-
Const::Ty(_) => {
730-
bug!("should never encounter ty::UnevaluatedConst in `required_consts`")
731-
}
732-
Const::Val(..) | Const::Unevaluated(..) => true,
733-
},
734-
));
723+
// Copy required constants from the callee_body into the caller_body.
724+
caller_body.required_consts.extend(callee_body.required_consts);
735725
// Now that we incorporated the callee's `required_consts`, we can remove the callee from
736726
// `mentioned_items` -- but we have to take their `mentioned_items` in return. This does
737727
// some extra work here to save the monomorphization collector work later. It helps a lot,
@@ -747,8 +737,9 @@ impl<'tcx> Inliner<'tcx> {
747737
caller_body.mentioned_items.remove(idx);
748738
caller_body.mentioned_items.extend(callee_body.mentioned_items);
749739
} else {
750-
// If we can't find the callee, there's no point in adding its items.
751-
// Probably it already got removed by being inlined elsewhere in the same function.
740+
// If we can't find the callee, there's no point in adding its items. Probably it
741+
// already got removed by being inlined elsewhere in the same function, so we already
742+
// took its items.
752743
}
753744
}
754745

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_middle::mir::visit::Visitor;
22
use rustc_middle::mir::{Const, ConstOperand, Location};
3-
use rustc_middle::ty::ConstKind;
3+
use rustc_middle::ty;
44

55
pub struct RequiredConstsVisitor<'a, 'tcx> {
66
required_consts: &'a mut Vec<ConstOperand<'tcx>>,
@@ -14,14 +14,22 @@ impl<'a, 'tcx> RequiredConstsVisitor<'a, 'tcx> {
1414

1515
impl<'tcx> Visitor<'tcx> for RequiredConstsVisitor<'_, 'tcx> {
1616
fn visit_constant(&mut self, constant: &ConstOperand<'tcx>, _: Location) {
17-
let const_ = constant.const_;
18-
match const_ {
17+
// Only unevaluated consts have to be added to `required_consts` as only those can possibly
18+
// still have latent const-eval errors.
19+
let is_required = match constant.const_ {
1920
Const::Ty(c) => match c.kind() {
20-
ConstKind::Param(_) | ConstKind::Error(_) | ConstKind::Value(_) => {}
21-
_ => bug!("only ConstKind::Param/Value should be encountered here, got {:#?}", c),
21+
ty::ConstKind::Value(_) => false, // already a value, cannot error
22+
ty::ConstKind::Param(_) | ty::ConstKind::Error(_) => true, // these are errors or could be replaced by errors
23+
_ => bug!(
24+
"only ConstKind::Param/Value/Error should be encountered here, got {:#?}",
25+
c
26+
),
2227
},
23-
Const::Unevaluated(..) => self.required_consts.push(*constant),
24-
Const::Val(..) => {}
28+
Const::Unevaluated(..) => true,
29+
Const::Val(..) => false, // already a value, cannot error
30+
};
31+
if is_required {
32+
self.required_consts.push(*constant);
2533
}
2634
}
2735
}

src/tools/miri/src/machine.rs

+6
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,12 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
872872

873873
const PANIC_ON_ALLOC_FAIL: bool = false;
874874

875+
#[inline(always)]
876+
fn all_required_consts_are_checked(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
877+
// We want to catch any cases of missing required_consts
878+
true
879+
}
880+
875881
#[inline(always)]
876882
fn enforce_alignment(ecx: &MiriInterpCx<'mir, 'tcx>) -> bool {
877883
ecx.machine.check_alignment != AlignmentCheck::None

0 commit comments

Comments
 (0)