Skip to content

Commit

Permalink
Rollup merge of rust-lang#67299 - christianpoveda:try_immty_from_int,…
Browse files Browse the repository at this point in the history
… r=RalfJung

Add `ImmTy::try_from_(u)int` methods

r? @RalfJung
  • Loading branch information
Centril authored Dec 22, 2019
2 parents c200141 + 683c4c7 commit 074329b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/librustc/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,22 @@ impl<'tcx, Tag> Scalar<Tag> {
Scalar::Raw { data: c as u128, size: 4 }
}

#[inline]
pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
let i = i.into();
if truncate(i, size) == i {
Some(Scalar::Raw { data: i, size: size.bytes() as u8 })
} else {
None
}
}

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

#[inline]
Expand All @@ -267,15 +275,23 @@ 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) -> Option<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 {
Some(Scalar::Raw { data: truncated, size: size.bytes() as u8 })
} else {
None
}
}

#[inline]
pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
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
9 changes: 9 additions & 0 deletions src/librustc_mir/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,20 @@ impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> {
ImmTy { imm: val.into(), layout }
}

#[inline]
pub fn try_from_uint(i: impl Into<u128>, layout: TyLayout<'tcx>) -> Option<Self> {
Some(Self::from_scalar(Scalar::try_from_uint(i, layout.size)?, layout))
}
#[inline]
pub fn from_uint(i: impl Into<u128>, layout: TyLayout<'tcx>) -> Self {
Self::from_scalar(Scalar::from_uint(i, layout.size), layout)
}

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

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

0 comments on commit 074329b

Please sign in to comment.