Skip to content

Commit 7695bd0

Browse files
committed
Use bit operators for min_max_ty
1 parent c00fd8f commit 7695bd0

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -472,18 +472,20 @@ fn all_constructors<'a, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
472472
}
473473
ty::TyInt(int_ty) if exhaustive_integer_patterns => {
474474
use syntax::ast::IntTy::*;
475-
macro_rules! min_max_ty {
476-
($ity:ident, $uty:ty, $sty:expr) => {
477-
($ity::MIN as $uty as u128, $ity::MAX as $uty as u128, $sty)
478-
}
479-
}
475+
let min_max_ty = |sty| {
476+
let size = cx.tcx.layout_of(ty::ParamEnv::reveal_all().and(sty))
477+
.unwrap().size.bits() as i128;
478+
let min = -(1 << (size - 1));
479+
let max = (1 << (size - 1)) - 1;
480+
(min as u128, max as u128, sty)
481+
};
480482
let (min, max, ty) = match int_ty {
481-
Isize => min_max_ty!(isize, usize, cx.tcx.types.isize),
482-
I8 => min_max_ty!(i8, u8, cx.tcx.types.i8),
483-
I16 => min_max_ty!(i16, u16, cx.tcx.types.i16),
484-
I32 => min_max_ty!(i32, u32, cx.tcx.types.i32),
485-
I64 => min_max_ty!(i64, u64, cx.tcx.types.i64),
486-
I128 => min_max_ty!(i128, u128, cx.tcx.types.i128),
483+
Isize => min_max_ty(cx.tcx.types.isize),
484+
I8 => min_max_ty(cx.tcx.types.i8),
485+
I16 => min_max_ty(cx.tcx.types.i16),
486+
I32 => min_max_ty(cx.tcx.types.i32),
487+
I64 => min_max_ty(cx.tcx.types.i64),
488+
I128 => min_max_ty(cx.tcx.types.i128),
487489
};
488490
value_constructors = true;
489491
vec![ConstantRange(ty::Const::from_bits(cx.tcx, min, ty),
@@ -492,14 +494,20 @@ fn all_constructors<'a, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
492494
}
493495
ty::TyUint(uint_ty) if exhaustive_integer_patterns => {
494496
use syntax::ast::UintTy::*;
495-
let (min, (max, ty)) = (0u128, match uint_ty {
496-
Usize => (usize::MAX as u128, cx.tcx.types.usize),
497-
U8 => ( u8::MAX as u128, cx.tcx.types.u8),
498-
U16 => ( u16::MAX as u128, cx.tcx.types.u16),
499-
U32 => ( u32::MAX as u128, cx.tcx.types.u32),
500-
U64 => ( u64::MAX as u128, cx.tcx.types.u64),
501-
U128 => ( u128::MAX as u128, cx.tcx.types.u128),
502-
});
497+
let min_max_ty = |sty| {
498+
let size = cx.tcx.layout_of(ty::ParamEnv::reveal_all().and(sty))
499+
.unwrap().size.bits() as i128;
500+
let max = (1 << size) - 1;
501+
(0u128, max as u128, sty)
502+
};
503+
let (min, max, ty) = match uint_ty {
504+
Usize => min_max_ty(cx.tcx.types.usize),
505+
U8 => min_max_ty(cx.tcx.types.u8),
506+
U16 => min_max_ty(cx.tcx.types.u16),
507+
U32 => min_max_ty(cx.tcx.types.u32),
508+
U64 => min_max_ty(cx.tcx.types.u64),
509+
U128 => min_max_ty(cx.tcx.types.u128),
510+
};
503511
value_constructors = true;
504512
vec![ConstantRange(ty::Const::from_bits(cx.tcx, min, ty),
505513
ty::Const::from_bits(cx.tcx, max, ty),

0 commit comments

Comments
 (0)