about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOli Scherer <github35764891676564198441@oli-obk.de>2021-03-15 12:06:07 +0000
committerOli Scherer <github35764891676564198441@oli-obk.de>2021-03-15 12:06:52 +0000
commit9f407ae5ee53a0c1e2ec12ee1087c8f58e5f0ac0 (patch)
tree45fbc7bbc9ebe69f84c4a473168fd80893564b7b
parent0dd5a1b622f1ba1ca702e079b2ce2ab5b513e2be (diff)
downloadrust-9f407ae5ee53a0c1e2ec12ee1087c8f58e5f0ac0.tar.gz
rust-9f407ae5ee53a0c1e2ec12ee1087c8f58e5f0ac0.zip
Do not expose fallible `to_int` operation on `Scalar`.
Any use of it has been shown to be a bug in the past.
-rw-r--r--compiler/rustc_middle/src/mir/interpret/value.rs32
-rw-r--r--compiler/rustc_middle/src/mir/mod.rs2
-rw-r--r--compiler/rustc_middle/src/ty/consts/kind.rs2
3 files changed, 17 insertions, 19 deletions
diff --git a/compiler/rustc_middle/src/mir/interpret/value.rs b/compiler/rustc_middle/src/mir/interpret/value.rs
index 44810a9b2e6..5172dfd041a 100644
--- a/compiler/rustc_middle/src/mir/interpret/value.rs
+++ b/compiler/rustc_middle/src/mir/interpret/value.rs
@@ -78,7 +78,7 @@ impl<'tcx> ConstValue<'tcx> {
     }
 
     pub fn try_to_scalar_int(&self) -> Option<ScalarInt> {
-        self.try_to_scalar()?.to_int().ok()
+        Some(self.try_to_scalar()?.assert_int())
     }
 
     pub fn try_to_bits(&self, size: Size) -> Option<u128> {
@@ -367,13 +367,16 @@ impl<'tcx, Tag> Scalar<Tag> {
     #[inline]
     fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
         assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
-        self.to_int()?.to_bits(target_size).map_err(|size| {
-            err_ub!(ScalarSizeMismatch {
-                target_size: target_size.bytes(),
-                data_size: size.bytes(),
-            })
-            .into()
-        })
+        match self {
+            Scalar::Int(int) => int.to_bits(target_size).map_err(|size| {
+                err_ub!(ScalarSizeMismatch {
+                    target_size: target_size.bytes(),
+                    data_size: size.bytes(),
+                })
+                .into()
+            }),
+            Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),
+        }
     }
 
     #[inline(always)]
@@ -383,7 +386,10 @@ impl<'tcx, Tag> Scalar<Tag> {
 
     #[inline]
     pub fn assert_int(self) -> ScalarInt {
-        self.to_int().expect("expected an int but got an abstract pointer")
+        match self {
+            Scalar::Ptr(_) => bug!("expected an int but got an abstract pointer"),
+            Scalar::Int(int) => int,
+        }
     }
 
     #[inline]
@@ -518,14 +524,6 @@ impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
     }
 }
 
-impl TryFrom<Scalar> for ScalarInt {
-    type Error = super::InterpErrorInfo<'static>;
-    #[inline]
-    fn try_from(scalar: Scalar) -> InterpResult<'static, Self> {
-        scalar.to_int()
-    }
-}
-
 impl<Tag> From<ScalarInt> for Scalar<Tag> {
     #[inline(always)]
     fn from(ptr: ScalarInt) -> Self {
diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs
index 0ba0429b4e2..90fda9ec91c 100644
--- a/compiler/rustc_middle/src/mir/mod.rs
+++ b/compiler/rustc_middle/src/mir/mod.rs
@@ -2473,7 +2473,7 @@ impl ConstantKind<'tcx> {
 
     #[inline]
     pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
-        self.try_to_value()?.try_to_scalar()?.to_int().ok()
+        Some(self.try_to_value()?.try_to_scalar()?.assert_int())
     }
 
     #[inline]
diff --git a/compiler/rustc_middle/src/ty/consts/kind.rs b/compiler/rustc_middle/src/ty/consts/kind.rs
index 5a6f219a413..43e22ce8f87 100644
--- a/compiler/rustc_middle/src/ty/consts/kind.rs
+++ b/compiler/rustc_middle/src/ty/consts/kind.rs
@@ -57,7 +57,7 @@ impl<'tcx> ConstKind<'tcx> {
 
     #[inline]
     pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
-        self.try_to_value()?.try_to_scalar()?.to_int().ok()
+        Some(self.try_to_value()?.try_to_scalar()?.assert_int())
     }
 
     #[inline]