Miri/CTFE: properly treat overflow in (signed) division/rem as UB

This commit is contained in:
Ralf Jung 2022-03-01 20:02:59 -05:00
parent f0c4da4998
commit 6739299d18
10 changed files with 93 additions and 76 deletions

View file

@ -1200,12 +1200,21 @@ impl<'tcx> MutVisitor<'tcx> for ConstPropagator<'_, 'tcx> {
AssertKind::RemainderByZero(op) => {
Some(AssertKind::RemainderByZero(eval_to_int(op)))
}
AssertKind::Overflow(bin_op @ (BinOp::Div | BinOp::Rem), op1, op2) => {
// Division overflow is *UB* in the MIR, and different than the
// other overflow checks.
Some(AssertKind::Overflow(
*bin_op,
eval_to_int(op1),
eval_to_int(op2),
))
}
AssertKind::BoundsCheck { ref len, ref index } => {
let len = eval_to_int(len);
let index = eval_to_int(index);
Some(AssertKind::BoundsCheck { len, index })
}
// Overflow is are already covered by checks on the binary operators.
// Remaining overflow errors are already covered by checks on the binary operators.
AssertKind::Overflow(..) | AssertKind::OverflowNeg(_) => None,
// Need proper const propagator for these.
_ => None,