Skip to content

Commit 8f12b87

Browse files
committed
Implement shl and shr overflow checks
cc #6
1 parent 6ea4cbd commit 8f12b87

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/num.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,10 @@ pub fn trans_checked_int_binop<'tcx>(
258258
}
259259
BinOp::Shl => {
260260
let val = fx.bcx.ins().ishl(lhs, rhs);
261-
// TODO: check for overflow
262-
let has_overflow = fx.bcx.ins().bconst(types::B1, false);
261+
let ty = fx.bcx.func.dfg.value_type(val);
262+
let max_shift = i64::from(ty.bits()) - 1;
263+
let has_overflow =
264+
fx.bcx.ins().icmp_imm(IntCC::UnsignedGreaterThan, rhs, max_shift);
263265
(val, has_overflow)
264266
}
265267
BinOp::Shr => {
@@ -268,8 +270,10 @@ pub fn trans_checked_int_binop<'tcx>(
268270
} else {
269271
fx.bcx.ins().sshr(lhs, rhs)
270272
};
271-
// TODO: check for overflow
272-
let has_overflow = fx.bcx.ins().bconst(types::B1, false);
273+
let ty = fx.bcx.func.dfg.value_type(val);
274+
let max_shift = i64::from(ty.bits()) - 1;
275+
let has_overflow =
276+
fx.bcx.ins().icmp_imm(IntCC::UnsignedGreaterThan, rhs, max_shift);
273277
(val, has_overflow)
274278
}
275279
_ => bug!(

0 commit comments

Comments
 (0)