Skip to content

Commit c864d90

Browse files
committed
factor out useful helper.
1 parent a9d8065 commit c864d90

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/librustc_trans/trans/base.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,25 @@ fn cast_shift_rhs<F, G>(op: ast::BinOp_,
566566
}
567567
}
568568

569+
pub fn llty_and_min_for_signed_ty<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
570+
val_t: Ty<'tcx>) -> (Type, u64) {
571+
match val_t.sty {
572+
ty::ty_int(t) => {
573+
let llty = Type::int_from_ty(cx.ccx(), t);
574+
let min = match t {
575+
ast::TyIs if llty == Type::i32(cx.ccx()) => i32::MIN as u64,
576+
ast::TyIs => i64::MIN as u64,
577+
ast::TyI8 => i8::MIN as u64,
578+
ast::TyI16 => i16::MIN as u64,
579+
ast::TyI32 => i32::MIN as u64,
580+
ast::TyI64 => i64::MIN as u64,
581+
};
582+
(llty, min)
583+
}
584+
_ => unreachable!(),
585+
}
586+
}
587+
569588
pub fn fail_if_zero_or_overflows<'blk, 'tcx>(
570589
cx: Block<'blk, 'tcx>,
571590
call_info: NodeIdAndSpan,
@@ -620,21 +639,7 @@ pub fn fail_if_zero_or_overflows<'blk, 'tcx>(
620639
// signed division/remainder which would trigger overflow. For unsigned
621640
// integers, no action beyond checking for zero need be taken.
622641
if is_signed {
623-
let (llty, min) = match rhs_t.sty {
624-
ty::ty_int(t) => {
625-
let llty = Type::int_from_ty(cx.ccx(), t);
626-
let min = match t {
627-
ast::TyIs if llty == Type::i32(cx.ccx()) => i32::MIN as u64,
628-
ast::TyIs => i64::MIN as u64,
629-
ast::TyI8 => i8::MIN as u64,
630-
ast::TyI16 => i16::MIN as u64,
631-
ast::TyI32 => i32::MIN as u64,
632-
ast::TyI64 => i64::MIN as u64,
633-
};
634-
(llty, min)
635-
}
636-
_ => unreachable!(),
637-
};
642+
let (llty, min) = llty_and_min_for_signed_ty(cx, rhs_t);
638643
let minus_one = ICmp(bcx, llvm::IntEQ, rhs,
639644
C_integral(llty, !0, false), debug_loc);
640645
with_cond(bcx, minus_one, |bcx| {

0 commit comments

Comments
 (0)