]> git.lizzy.rs Git - rust.git/commitdiff
remove ScalarMaybeUndef::to_bits and make Scalar::to_bits private
authorRalf Jung <post@ralfj.de>
Fri, 28 Feb 2020 10:04:12 +0000 (11:04 +0100)
committerRalf Jung <post@ralfj.de>
Fri, 28 Feb 2020 15:07:12 +0000 (16:07 +0100)
src/librustc/mir/interpret/value.rs
src/librustc/ty/sty.rs
src/librustc_mir/interpret/intrinsics.rs
src/librustc_mir/interpret/operand.rs
src/librustc_mir/interpret/validity.rs
src/librustc_mir/transform/const_prop.rs

index 2eb5f7ca87823a2d9fd20081ea3edb0d695f129a..c931fcf71e39d3dcc43903cebfbc2eb3351534dc 100644 (file)
@@ -382,9 +382,10 @@ pub fn to_bits_or_ptr(
         }
     }
 
-    /// Do not call this method!  Use either `assert_bits` or `force_bits`.
+    /// This method is intentionally private!
+    /// It is just a helper for other methods in this file.
     #[inline]
-    pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
+    fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
         match self {
             Scalar::Raw { data, size } => {
                 assert_eq!(target_size.bytes(), size as u64);
@@ -405,7 +406,7 @@ pub fn assert_bits(self, target_size: Size) -> u128 {
     pub fn assert_ptr(self) -> Pointer<Tag> {
         match self {
             Scalar::Ptr(p) => p,
-            Scalar::Raw { .. } => bug!("expected a Pointer but got Raw bits")
+            Scalar::Raw { .. } => bug!("expected a Pointer but got Raw bits"),
         }
     }
 
@@ -586,12 +587,6 @@ pub fn not_undef(self) -> InterpResult<'static, Scalar<Tag>> {
         }
     }
 
-    /// Do not call this method!  Use either `assert_bits` or `force_bits`.
-    #[inline(always)]
-    pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
-        self.not_undef()?.to_bits(target_size)
-    }
-
     #[inline(always)]
     pub fn to_bool(self) -> InterpResult<'tcx, bool> {
         self.not_undef()?.to_bool()
index c3698f402a9d120f520b29d440cbcb05628b79b1..241781bb93d5492e997b5d76d736d4fd79d39367 100644 (file)
@@ -2576,7 +2576,7 @@ pub fn try_to_scalar(&self) -> Option<Scalar> {
 
     #[inline]
     pub fn try_to_bits(&self, size: ty::layout::Size) -> Option<u128> {
-        self.try_to_scalar()?.to_bits(size).ok()
+        if let ConstKind::Value(val) = self { val.try_to_bits(size) } else { None }
     }
 }
 
index cd06cf01bfa8111a1537fb2acd6da72820cbbe21..d63abdc356234340e12c5e15c99d1b8e5555ccb8 100644 (file)
@@ -384,7 +384,7 @@ pub fn exact_div(
         // `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`.
         // First, check x % y != 0 (or if that computation overflows).
         let (res, overflow, _ty) = self.overflowing_binary_op(BinOp::Rem, a, b)?;
-        if overflow || res.to_bits(a.layout.size)? != 0 {
+        if overflow || res.assert_bits(a.layout.size) != 0 {
             // Then, check if `b` is -1, which is the "min_value / -1" case.
             let minus1 = Scalar::from_int(-1, dest.layout.size);
             let b_scalar = b.to_scalar().unwrap();
index 14b8a341e26a0e82841c9961be2e74b0050edf23..20efe7dfc03679050dc51b41e58430905e8889b1 100644 (file)
@@ -96,40 +96,40 @@ pub struct ImmTy<'tcx, Tag = ()> {
 impl<Tag: Copy> std::fmt::Display for ImmTy<'tcx, Tag> {
     fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         match &self.imm {
-            Immediate::Scalar(ScalarMaybeUndef::Scalar(s)) => match s.to_bits(self.layout.size) {
-                Ok(s) => {
-                    match self.layout.ty.kind {
-                        ty::Int(_) => {
-                            return write!(
-                                fmt,
-                                "{}",
-                                super::sign_extend(s, self.layout.size) as i128,
-                            );
-                        }
-                        ty::Uint(_) => return write!(fmt, "{}", s),
-                        ty::Bool if s == 0 => return fmt.write_str("false"),
-                        ty::Bool if s == 1 => return fmt.write_str("true"),
-                        ty::Char => {
-                            if let Some(c) = u32::try_from(s).ok().and_then(std::char::from_u32) {
-                                return write!(fmt, "{}", c);
-                            }
+            // We cannot use `to_bits_or_ptr` as we do not have a `tcx`.
+            // So we use `is_bits` and circumvent a bunch of sanity checking -- but
+            // this is anyway only for printing.
+            Immediate::Scalar(ScalarMaybeUndef::Scalar(s)) if s.is_ptr() => {
+                fmt.write_str("{pointer}")
+            }
+            Immediate::Scalar(ScalarMaybeUndef::Scalar(s)) => {
+                let s = s.assert_bits(self.layout.size);
+                match self.layout.ty.kind {
+                    ty::Int(_) => {
+                        return write!(fmt, "{}", super::sign_extend(s, self.layout.size) as i128,);
+                    }
+                    ty::Uint(_) => return write!(fmt, "{}", s),
+                    ty::Bool if s == 0 => return fmt.write_str("false"),
+                    ty::Bool if s == 1 => return fmt.write_str("true"),
+                    ty::Char => {
+                        if let Some(c) = u32::try_from(s).ok().and_then(std::char::from_u32) {
+                            return write!(fmt, "{}", c);
                         }
-                        ty::Float(ast::FloatTy::F32) => {
-                            if let Ok(u) = u32::try_from(s) {
-                                return write!(fmt, "{}", f32::from_bits(u));
-                            }
+                    }
+                    ty::Float(ast::FloatTy::F32) => {
+                        if let Ok(u) = u32::try_from(s) {
+                            return write!(fmt, "{}", f32::from_bits(u));
                         }
-                        ty::Float(ast::FloatTy::F64) => {
-                            if let Ok(u) = u64::try_from(s) {
-                                return write!(fmt, "{}", f64::from_bits(u));
-                            }
+                    }
+                    ty::Float(ast::FloatTy::F64) => {
+                        if let Ok(u) = u64::try_from(s) {
+                            return write!(fmt, "{}", f64::from_bits(u));
                         }
-                        _ => {}
                     }
-                    write!(fmt, "{:x}", s)
+                    _ => {}
                 }
-                Err(_) => fmt.write_str("{pointer}"),
-            },
+                write!(fmt, "{:x}", s)
+            }
             Immediate::Scalar(ScalarMaybeUndef::Undef) => fmt.write_str("{undef}"),
             Immediate::ScalarPair(..) => fmt.write_str("{wide pointer or tuple}"),
         }
@@ -205,11 +205,6 @@ pub fn try_from_int(i: impl Into<i128>, layout: TyLayout<'tcx>) -> Option<Self>
     pub fn from_int(i: impl Into<i128>, layout: TyLayout<'tcx>) -> Self {
         Self::from_scalar(Scalar::from_int(i, layout.size), layout)
     }
-
-    #[inline]
-    pub fn to_bits(self) -> InterpResult<'tcx, u128> {
-        self.to_scalar()?.to_bits(self.layout.size)
-    }
 }
 
 // Use the existing layout if given (but sanity check in debug mode),
index aa2b3040a716f49a757207d8c33b88d1bd3386e5..48c0e7930e97cd22cb65d0117c7171e887beaf4e 100644 (file)
@@ -361,16 +361,17 @@ fn visit_primitive(&mut self, value: OpTy<'tcx, M::PointerTag>) -> InterpResult<
             ty::Float(_) | ty::Int(_) | ty::Uint(_) => {
                 // NOTE: Keep this in sync with the array optimization for int/float
                 // types below!
-                let size = value.layout.size;
                 let value = value.to_scalar_or_undef();
                 if self.ref_tracking_for_consts.is_some() {
                     // Integers/floats in CTFE: Must be scalar bits, pointers are dangerous
-                    try_validation!(
-                        value.to_bits(size),
-                        value,
-                        self.path,
-                        "initialized plain (non-pointer) bytes"
-                    );
+                    let is_bits = value.not_undef().map_or(false, |v| v.is_bits());
+                    if !is_bits {
+                        throw_validation_failure!(
+                            value,
+                            self.path,
+                            "initialized plain (non-pointer) bytes"
+                        )
+                    }
                 } else {
                     // At run-time, for now, we accept *anything* for these types, including
                     // undef. We should fix that, but let's start low.
index 9e05133132e05508917c0a9c269491d84b09fe96..fe736321df745020fe3698abb9d2545fdd552b1f 100644 (file)
@@ -557,7 +557,9 @@ fn check_binary_op(
             let left_ty = left.ty(&self.local_decls, self.tcx);
             let left_size_bits = self.ecx.layout_of(left_ty).ok()?.size.bits();
             let right_size = r.layout.size;
-            let r_bits = r.to_scalar().and_then(|r| r.to_bits(right_size));
+            let r_bits = r.to_scalar().ok();
+            // This is basically `force_bits`.
+            let r_bits = r_bits.and_then(|r| r.to_bits_or_ptr(right_size, &self.tcx).ok());
             if r_bits.map_or(false, |b| b >= left_size_bits as u128) {
                 self.report_assert_as_lint(
                     lint::builtin::ARITHMETIC_OVERFLOW,