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:
parent
0dd5a1b622
commit
9f407ae5ee
3 changed files with 17 additions and 19 deletions
|
@ -78,7 +78,7 @@ impl<'tcx> ConstValue<'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn try_to_scalar_int(&self) -> Option<ScalarInt> {
|
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> {
|
pub fn try_to_bits(&self, size: Size) -> Option<u128> {
|
||||||
|
@ -367,13 +367,16 @@ impl<'tcx, Tag> Scalar<Tag> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
|
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");
|
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| {
|
match self {
|
||||||
|
Scalar::Int(int) => int.to_bits(target_size).map_err(|size| {
|
||||||
err_ub!(ScalarSizeMismatch {
|
err_ub!(ScalarSizeMismatch {
|
||||||
target_size: target_size.bytes(),
|
target_size: target_size.bytes(),
|
||||||
data_size: size.bytes(),
|
data_size: size.bytes(),
|
||||||
})
|
})
|
||||||
.into()
|
.into()
|
||||||
})
|
}),
|
||||||
|
Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
@ -383,7 +386,10 @@ impl<'tcx, Tag> Scalar<Tag> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn assert_int(self) -> ScalarInt {
|
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]
|
#[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> {
|
impl<Tag> From<ScalarInt> for Scalar<Tag> {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from(ptr: ScalarInt) -> Self {
|
fn from(ptr: ScalarInt) -> Self {
|
||||||
|
|
|
@ -2473,7 +2473,7 @@ impl ConstantKind<'tcx> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
|
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]
|
#[inline]
|
||||||
|
|
|
@ -57,7 +57,7 @@ impl<'tcx> ConstKind<'tcx> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
|
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]
|
#[inline]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue