Skip to content

Commit 28f85c6

Browse files
committed
bring back extra check for int_min%-1
1 parent 202d401 commit 28f85c6

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/librustc_mir/interpret/operator.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,17 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
195195
if let Some(op) = op {
196196
let l128 = self.sign_extend(l, left_layout) as i128;
197197
let r = self.sign_extend(r, right_layout) as i128;
198+
// We need a special check for overflowing remainder:
199+
// "int_min % -1" overflows and returns 0, but after casting things to a larger int
200+
// type it does *not* overflow nor give an unrepresentable result!
201+
match bin_op {
202+
Rem => {
203+
if r == -1 && l == (1 << (size.bits() - 1)) {
204+
return Ok((Scalar::from_int(0, size), true, left_layout.ty));
205+
}
206+
}
207+
_ => {}
208+
}
198209

199210
let (result, oflo) = op(l128, r);
200211
// This may be out-of-bounds for the result type, so we have to truncate ourselves.

0 commit comments

Comments
 (0)