Do not expose fallible `to_int` operation on `Scalar`.

Any use of it has been shown to be a bug in the past.
This commit is contained in:
Oli Scherer 2021-03-15 12:06:07 +00:00
parent 0dd5a1b622
commit 9f407ae5ee
3 changed files with 17 additions and 19 deletions

View File

@ -78,7 +78,7 @@ impl<'tcx> ConstValue<'tcx> {
}
pub fn try_to_scalar_int(&self) -> Option<ScalarInt> {
self.try_to_scalar()?.to_int().ok()
Some(self.try_to_scalar()?.assert_int())
}
pub fn try_to_bits(&self, size: Size) -> Option<u128> {
@ -367,13 +367,16 @@ 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");
self.to_int()?.to_bits(target_size).map_err(|size| {
err_ub!(ScalarSizeMismatch {
target_size: target_size.bytes(),
data_size: size.bytes(),
})
.into()
})
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),
}
}
#[inline(always)]
@ -383,7 +386,10 @@ impl<'tcx, Tag> Scalar<Tag> {
#[inline]
pub fn assert_int(self) -> ScalarInt {
self.to_int().expect("expected an int but got an abstract pointer")
match self {
Scalar::Ptr(_) => bug!("expected an int but got an abstract pointer"),
Scalar::Int(int) => int,
}
}
#[inline]
@ -518,14 +524,6 @@ impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
}
}
impl TryFrom<Scalar> for ScalarInt {
type Error = super::InterpErrorInfo<'static>;
#[inline]
fn try_from(scalar: Scalar) -> InterpResult<'static, Self> {
scalar.to_int()
}
}
impl<Tag> From<ScalarInt> for Scalar<Tag> {
#[inline(always)]
fn from(ptr: ScalarInt) -> Self {

View File

@ -2473,7 +2473,7 @@ impl ConstantKind<'tcx> {
#[inline]
pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
self.try_to_value()?.try_to_scalar()?.to_int().ok()
Some(self.try_to_value()?.try_to_scalar()?.assert_int())
}
#[inline]

View File

@ -57,7 +57,7 @@ impl<'tcx> ConstKind<'tcx> {
#[inline]
pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
self.try_to_value()?.try_to_scalar()?.to_int().ok()
Some(self.try_to_value()?.try_to_scalar()?.assert_int())
}
#[inline]