Add fallible Scalar to ScalarInt conversion method

This commit is contained in:
Oli Scherer 2021-02-23 10:16:24 +00:00
parent 0fe4f38769
commit 858216cabf

View File

@ -346,16 +346,13 @@ impl<'tcx, Tag> Scalar<Tag> {
#[inline]
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");
match self {
Scalar::Int(int) => int.to_bits(target_size).map_err(|size| {
err_ub!(ScalarSizeMismatch {
target_size: target_size.bytes(),
data_size: size.bytes(),
})
.into()
}),
Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),
}
self.to_int()?.to_bits(target_size).map_err(|size| {
err_ub!(ScalarSizeMismatch {
target_size: target_size.bytes(),
data_size: size.bytes(),
})
.into()
})
}
#[inline(always)]
@ -364,13 +361,18 @@ impl<'tcx, Tag> Scalar<Tag> {
}
#[inline]
pub fn assert_int(self) -> ScalarInt {
pub fn to_int(self) -> InterpResult<'tcx, ScalarInt> {
match self {
Scalar::Ptr(_) => bug!("expected an int but got an abstract pointer"),
Scalar::Int(int) => int,
Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),
Scalar::Int(int) => Ok(int),
}
}
#[inline]
pub fn assert_int(self) -> ScalarInt {
self.to_int().expect("expected an int but got an abstract pointer")
}
#[inline]
pub fn assert_ptr(self) -> Pointer<Tag> {
match self {