Skip to content

Commit

Permalink
Add error message if Scalar::from_(u)int fails
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdrz committed Dec 22, 2019
1 parent 309f437 commit 683c4c7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/librustc/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ impl<'tcx, Tag> Scalar<Tag> {

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

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

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

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> {
}
#[inline]
pub fn from_uint(i: impl Into<u128>, layout: TyLayout<'tcx>) -> Self {
Self::try_from_uint(i, layout).unwrap()
Self::from_scalar(Scalar::from_uint(i, layout.size), layout)
}

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

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

#[inline]
Expand Down

0 comments on commit 683c4c7

Please sign in to comment.