about summary refs log tree commit diff
path: root/compiler/rustc_middle/src/mir/interpret
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2024-04-18 00:33:46 +0200
committerRalf Jung <post@ralfj.de>2024-04-18 14:25:06 +0200
commit5e6184cdb7685cbfceb2cfe80357715d0d04ec53 (patch)
treee811e9ab25228620457ff293fd9ef8e1bb664b1a /compiler/rustc_middle/src/mir/interpret
parent38104f3a8838f8662ad3cccc4d7262a96bf9724e (diff)
downloadrust-5e6184cdb7685cbfceb2cfe80357715d0d04ec53.tar.gz
rust-5e6184cdb7685cbfceb2cfe80357715d0d04ec53.zip
interpret/binary_int_op: avoid dropping to raw ints until we determined the sign
Diffstat (limited to 'compiler/rustc_middle/src/mir/interpret')
-rw-r--r--compiler/rustc_middle/src/mir/interpret/value.rs22
1 files changed, 12 insertions, 10 deletions
diff --git a/compiler/rustc_middle/src/mir/interpret/value.rs b/compiler/rustc_middle/src/mir/interpret/value.rs
index 9f9433e483b..38ddd34777a 100644
--- a/compiler/rustc_middle/src/mir/interpret/value.rs
+++ b/compiler/rustc_middle/src/mir/interpret/value.rs
@@ -301,6 +301,11 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> {
     }
 
     #[inline(always)]
+    pub fn to_scalar_int(self) -> InterpResult<'tcx, ScalarInt> {
+        self.try_to_int().map_err(|_| err_unsup!(ReadPointerAsInt(None)).into())
+    }
+
+    #[inline(always)]
     #[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980)
     pub fn assert_int(self) -> ScalarInt {
         self.try_to_int().unwrap()
@@ -311,16 +316,13 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> {
     #[inline]
     pub 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.try_to_int()
-            .map_err(|_| err_unsup!(ReadPointerAsInt(None)))?
-            .to_bits(target_size)
-            .map_err(|size| {
-                err_ub!(ScalarSizeMismatch(ScalarSizeMismatch {
-                    target_size: target_size.bytes(),
-                    data_size: size.bytes(),
-                }))
-                .into()
-            })
+        self.to_scalar_int()?.to_bits(target_size).map_err(|size| {
+            err_ub!(ScalarSizeMismatch(ScalarSizeMismatch {
+                target_size: target_size.bytes(),
+                data_size: size.bytes(),
+            }))
+            .into()
+        })
     }
 
     #[inline(always)]