Skip to content

Commit 683c4c7

Browse files
committed
Add error message if Scalar::from_(u)int fails
1 parent 309f437 commit 683c4c7

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/librustc/mir/interpret/value.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ impl<'tcx, Tag> Scalar<Tag> {
248248

249249
#[inline]
250250
pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
251-
Self::try_from_uint(i, size).unwrap()
251+
let i = i.into();
252+
Self::try_from_uint(i, size).unwrap_or_else(|| {
253+
bug!("Unsigned value {:#x} does not fit in {} bits", i, size.bits())
254+
})
252255
}
253256

254257
#[inline]
@@ -285,7 +288,10 @@ impl<'tcx, Tag> Scalar<Tag> {
285288

286289
#[inline]
287290
pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
288-
Self::try_from_int(i, size).unwrap()
291+
let i = i.into();
292+
Self::try_from_int(i, size).unwrap_or_else(|| {
293+
bug!("Signed value {:#x} does not fit in {} bits", i, size.bits())
294+
})
289295
}
290296

291297
#[inline]

src/librustc_mir/interpret/operand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> {
224224
}
225225
#[inline]
226226
pub fn from_uint(i: impl Into<u128>, layout: TyLayout<'tcx>) -> Self {
227-
Self::try_from_uint(i, layout).unwrap()
227+
Self::from_scalar(Scalar::from_uint(i, layout.size), layout)
228228
}
229229

230230
#[inline]
@@ -234,7 +234,7 @@ impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> {
234234

235235
#[inline]
236236
pub fn from_int(i: impl Into<i128>, layout: TyLayout<'tcx>) -> Self {
237-
Self::try_from_int(i, layout).unwrap()
237+
Self::from_scalar(Scalar::from_int(i, layout.size), layout)
238238
}
239239

240240
#[inline]

0 commit comments

Comments
 (0)