about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOliver Schneider <github35764891676564198441@oli-obk.de>2018-05-24 11:26:42 +0200
committerOliver Schneider <github35764891676564198441@oli-obk.de>2018-05-24 20:49:38 +0200
commit1f9fa5373899decb96e5456c2395a5576948a0b3 (patch)
tree7248283c036025263745202e5347cb63f64e4045
parentf1ea9ef315746fbe9989ef0e0e3a40dcd23509f6 (diff)
downloadrust-1f9fa5373899decb96e5456c2395a5576948a0b3.tar.gz
rust-1f9fa5373899decb96e5456c2395a5576948a0b3.zip
Sanity check the `bits` argument to the `from_bits` function
-rw-r--r--src/librustc/ty/sty.rs9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs
index 4c03b708648..43cf838c248 100644
--- a/src/librustc/ty/sty.rs
+++ b/src/librustc/ty/sty.rs
@@ -1824,10 +1824,13 @@ impl<'tcx> Const<'tcx> {
         ty: ParamEnvAnd<'tcx, Ty<'tcx>>,
     ) -> &'tcx Self {
         let ty = tcx.global_tcx().lift(&ty).unwrap();
-        let defined = tcx.global_tcx().layout_of(ty).unwrap_or_else(|e| {
+        let size = tcx.global_tcx().layout_of(ty).unwrap_or_else(|e| {
             panic!("could not compute layout for {:?}: {:?}", ty, e)
-        }).size.bits() as u8;
-        Self::from_scalar(tcx, Scalar::Bits { bits, defined }, ty.value)
+        }).size;
+        let amt = 128 - size.bits();
+        let truncated = (bits << amt) >> amt;
+        assert_eq!(truncated, bits, "from_bits called with untruncated value");
+        Self::from_scalar(tcx, Scalar::Bits { bits, defined: size.bits() as u8 }, ty.value)
     }
 
     #[inline]