about summary refs log tree commit diff
diff options
context:
space:
mode:
authorvarkor <github@varkor.com>2018-05-21 23:51:01 +0100
committervarkor <github@varkor.com>2018-08-16 20:09:04 +0100
commit7695bd0be9cc6d08b9d58c1c0ef193458857ece7 (patch)
treee400d4ef361fd57adbe70a9e0662b755281c625d
parentc00fd8f58c9c7780b83fbb4e5e2e419f1ddd37e2 (diff)
downloadrust-7695bd0be9cc6d08b9d58c1c0ef193458857ece7.tar.gz
rust-7695bd0be9cc6d08b9d58c1c0ef193458857ece7.zip
Use bit operators for min_max_ty
-rw-r--r--src/librustc_mir/hair/pattern/_match.rs46
1 files changed, 27 insertions, 19 deletions
diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs
index 204d3ea8c9c..9f0f07f2a9c 100644
--- a/src/librustc_mir/hair/pattern/_match.rs
+++ b/src/librustc_mir/hair/pattern/_match.rs
@@ -472,18 +472,20 @@ fn all_constructors<'a, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
         }
         ty::TyInt(int_ty) if exhaustive_integer_patterns => {
             use syntax::ast::IntTy::*;
-            macro_rules! min_max_ty {
-                ($ity:ident, $uty:ty, $sty:expr) => {
-                    ($ity::MIN as $uty as u128, $ity::MAX as $uty as u128, $sty)
-                }
-            }
+            let min_max_ty = |sty| {
+                let size = cx.tcx.layout_of(ty::ParamEnv::reveal_all().and(sty))
+                                  .unwrap().size.bits() as i128;
+                let min = -(1 << (size - 1));
+                let max = (1 << (size - 1)) - 1;
+                (min as u128, max as u128, sty)
+            };
             let (min, max, ty) = match int_ty {
-                Isize => min_max_ty!(isize, usize, cx.tcx.types.isize),
-                I8    => min_max_ty!(i8, u8, cx.tcx.types.i8),
-                I16   => min_max_ty!(i16, u16, cx.tcx.types.i16),
-                I32   => min_max_ty!(i32, u32, cx.tcx.types.i32),
-                I64   => min_max_ty!(i64, u64, cx.tcx.types.i64),
-                I128  => min_max_ty!(i128, u128, cx.tcx.types.i128),
+                Isize => min_max_ty(cx.tcx.types.isize),
+                I8    => min_max_ty(cx.tcx.types.i8),
+                I16   => min_max_ty(cx.tcx.types.i16),
+                I32   => min_max_ty(cx.tcx.types.i32),
+                I64   => min_max_ty(cx.tcx.types.i64),
+                I128  => min_max_ty(cx.tcx.types.i128),
             };
             value_constructors = true;
             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>,
         }
         ty::TyUint(uint_ty) if exhaustive_integer_patterns => {
             use syntax::ast::UintTy::*;
-            let (min, (max, ty)) = (0u128, match uint_ty {
-                Usize => (usize::MAX as u128, cx.tcx.types.usize),
-                U8    => (   u8::MAX as u128, cx.tcx.types.u8),
-                U16   => (  u16::MAX as u128, cx.tcx.types.u16),
-                U32   => (  u32::MAX as u128, cx.tcx.types.u32),
-                U64   => (  u64::MAX as u128, cx.tcx.types.u64),
-                U128  => ( u128::MAX as u128, cx.tcx.types.u128),
-            });
+            let min_max_ty = |sty| {
+                let size = cx.tcx.layout_of(ty::ParamEnv::reveal_all().and(sty))
+                                  .unwrap().size.bits() as i128;
+                let max = (1 << size) - 1;
+                (0u128, max as u128, sty)
+            };
+            let (min, max, ty) = match uint_ty {
+                Usize => min_max_ty(cx.tcx.types.usize),
+                U8    => min_max_ty(cx.tcx.types.u8),
+                U16   => min_max_ty(cx.tcx.types.u16),
+                U32   => min_max_ty(cx.tcx.types.u32),
+                U64   => min_max_ty(cx.tcx.types.u64),
+                U128  => min_max_ty(cx.tcx.types.u128),
+            };
             value_constructors = true;
             vec![ConstantRange(ty::Const::from_bits(cx.tcx, min, ty),
                                ty::Const::from_bits(cx.tcx, max, ty),