]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_mir/hair/cx/mod.rs
Reintroduce `Undef` and properly check constant value sizes
[rust.git] / src / librustc_mir / hair / cx / mod.rs
index 4a7225c3a76d1a93e718dd31c5551003480ab8fd..70148fc91760443bb6d31b18de8988fa5eb6ac21 100644 (file)
@@ -151,14 +151,14 @@ pub fn const_eval_literal(
 
         let trunc = |n| {
             let param_ty = self.param_env.and(self.tcx.lift_to_global(&ty).unwrap());
-            let bit_width = self.tcx.layout_of(param_ty).unwrap().size.bits();
-            trace!("trunc {} with size {} and shift {}", n, bit_width, 128 - bit_width);
-            let shift = 128 - bit_width;
+            let width = self.tcx.layout_of(param_ty).unwrap().size;
+            trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
+            let shift = 128 - width.bits();
             let result = (n << shift) >> shift;
             trace!("trunc result: {}", result);
             ConstValue::Scalar(Scalar::Bits {
                 bits: result,
-                defined: bit_width as u8,
+                size: width.bytes() as u8,
             })
         };
 
@@ -168,7 +168,7 @@ pub fn const_eval_literal(
                 let s = s.as_str();
                 let id = self.tcx.allocate_bytes(s.as_bytes());
                 let value = Scalar::Ptr(id.into()).to_value_with_len(s.len() as u64, self.tcx);
-                ConstValue::from_byval_value(value)
+                ConstValue::from_byval_value(value).unwrap()
             },
             LitKind::ByteStr(ref data) => {
                 let id = self.tcx.allocate_bytes(data);
@@ -176,7 +176,7 @@ pub fn const_eval_literal(
             },
             LitKind::Byte(n) => ConstValue::Scalar(Scalar::Bits {
                 bits: n as u128,
-                defined: 8,
+                size: 1,
             }),
             LitKind::Int(n, _) if neg => {
                 let n = n as i128;
@@ -194,14 +194,8 @@ pub fn const_eval_literal(
                 };
                 parse_float(n, fty)
             }
-            LitKind::Bool(b) => ConstValue::Scalar(Scalar::Bits {
-                bits: b as u128,
-                defined: 8,
-            }),
-            LitKind::Char(c) => ConstValue::Scalar(Scalar::Bits {
-                bits: c as u128,
-                defined: 32,
-            }),
+            LitKind::Bool(b) => ConstValue::Scalar(Scalar::from_bool(b)),
+            LitKind::Char(c) => ConstValue::Scalar(Scalar::from_char(c)),
         };
         ty::Const::from_const_value(self.tcx, lit, ty)
     }