about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/librustc/mir/interpret/value.rs34
1 files changed, 22 insertions, 12 deletions
diff --git a/src/librustc/mir/interpret/value.rs b/src/librustc/mir/interpret/value.rs
index a038ca23ae9..4f196cda5ae 100644
--- a/src/librustc/mir/interpret/value.rs
+++ b/src/librustc/mir/interpret/value.rs
@@ -237,13 +237,18 @@ impl<'tcx, Tag> Scalar<Tag> {
     }
 
     #[inline]
-    pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
+    pub fn try_from_uint(i: impl Into<u128>, size: Size) -> InterpResult<'tcx, Self> {
         let i = i.into();
-        assert_eq!(
-            truncate(i, size), i,
-            "Unsigned value {:#x} does not fit in {} bits", i, size.bits()
-        );
-        Scalar::Raw { data: i, size: size.bytes() as u8 }
+        if truncate(i, size) == i {
+            Ok(Scalar::Raw { data: i, size: size.bytes() as u8 })
+        } else {
+            throw_unsup_format!("Unsigned value {:#x} does not fit in {} bits", i, size.bits())
+        }
+    }
+
+    #[inline]
+    pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
+        Self::try_from_uint(i, size).unwrap()
     }
 
     #[inline]
@@ -267,15 +272,20 @@ impl<'tcx, Tag> Scalar<Tag> {
     }
 
     #[inline]
-    pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
+    pub fn try_from_int(i: impl Into<i128>, size: Size) -> InterpResult<'tcx, Self> {
         let i = i.into();
         // `into` performed sign extension, we have to truncate
         let truncated = truncate(i as u128, size);
-        assert_eq!(
-            sign_extend(truncated, size) as i128, i,
-            "Signed value {:#x} does not fit in {} bits", i, size.bits()
-        );
-        Scalar::Raw { data: truncated, size: size.bytes() as u8 }
+        if sign_extend(truncated, size) as i128 == i {
+            Ok(Scalar::Raw { data: truncated, size: size.bytes() as u8 })
+        } else {
+            throw_unsup_format!("Signed value {:#x} does not fit in {} bits", i, size.bits())
+        }
+    }
+
+    #[inline]
+    pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
+        Self::try_from_int(i, size).unwrap()
     }
 
     #[inline]