Do not raise interp errors from the scalar int module

This commit is contained in:
oli 2020-11-01 17:30:33 +00:00
parent 500af76831
commit dad0036cb4
3 changed files with 23 additions and 20 deletions

View file

@ -71,7 +71,7 @@ macro_rules! throw_inval {
#[macro_export] #[macro_export]
macro_rules! throw_ub { macro_rules! throw_ub {
($($tt:tt)*) => { Err::<!, _>($crate::err_ub!($($tt)*))? }; ($($tt:tt)*) => { Err::<!, _>(err_ub!($($tt)*))? };
} }
#[macro_export] #[macro_export]

View file

@ -347,7 +347,13 @@ impl<'tcx, Tag> Scalar<Tag> {
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");
match self { match self {
Scalar::Int(int) => int.to_bits(target_size), 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), Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),
} }
} }

View file

@ -1,5 +1,4 @@
use crate::mir::interpret::{sign_extend, truncate, InterpErrorInfo, InterpResult}; use crate::mir::interpret::{sign_extend, truncate, InterpResult};
use crate::throw_ub;
use rustc_apfloat::ieee::{Double, Single}; use rustc_apfloat::ieee::{Double, Single};
use rustc_apfloat::Float; use rustc_apfloat::Float;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
@ -233,16 +232,14 @@ impl ScalarInt {
} }
#[inline] #[inline]
pub fn to_bits(self, target_size: Size) -> InterpResult<'static, u128> { pub fn to_bits(self, target_size: Size) -> Result<u128, Size> {
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");
if target_size.bytes() != u64::from(self.size) { if target_size.bytes() == u64::from(self.size) {
throw_ub!(ScalarSizeMismatch { self.check_data();
target_size: target_size.bytes(), Ok(self.data)
data_size: u64::from(self.size), } else {
}); Err(self.size())
} }
self.check_data();
Ok(self.data)
} }
} }
@ -266,9 +263,9 @@ macro_rules! try_from {
($($ty:ty),*) => { ($($ty:ty),*) => {
$( $(
impl TryFrom<ScalarInt> for $ty { impl TryFrom<ScalarInt> for $ty {
type Error = InterpErrorInfo<'static>; type Error = Size;
#[inline] #[inline]
fn try_from(int: ScalarInt) -> InterpResult<'static, Self> { fn try_from(int: ScalarInt) -> Result<Self, Size> {
int.to_bits(Size::from_bytes(std::mem::size_of::<$ty>())).map(|u| u.try_into().unwrap()) int.to_bits(Size::from_bytes(std::mem::size_of::<$ty>())).map(|u| u.try_into().unwrap())
} }
} }
@ -287,9 +284,9 @@ impl From<char> for ScalarInt {
} }
impl TryFrom<ScalarInt> for char { impl TryFrom<ScalarInt> for char {
type Error = InterpErrorInfo<'static>; type Error = Size;
#[inline] #[inline]
fn try_from(int: ScalarInt) -> InterpResult<'static, Self> { fn try_from(int: ScalarInt) -> Result<Self, Size> {
int.to_bits(Size::from_bytes(std::mem::size_of::<char>())) int.to_bits(Size::from_bytes(std::mem::size_of::<char>()))
.map(|u| char::from_u32(u.try_into().unwrap()).unwrap()) .map(|u| char::from_u32(u.try_into().unwrap()).unwrap())
} }
@ -304,9 +301,9 @@ impl From<Single> for ScalarInt {
} }
impl TryFrom<ScalarInt> for Single { impl TryFrom<ScalarInt> for Single {
type Error = InterpErrorInfo<'static>; type Error = Size;
#[inline] #[inline]
fn try_from(int: ScalarInt) -> InterpResult<'static, Self> { fn try_from(int: ScalarInt) -> Result<Self, Size> {
int.to_bits(Size::from_bytes(4)).map(Self::from_bits) int.to_bits(Size::from_bytes(4)).map(Self::from_bits)
} }
} }
@ -320,9 +317,9 @@ impl From<Double> for ScalarInt {
} }
impl TryFrom<ScalarInt> for Double { impl TryFrom<ScalarInt> for Double {
type Error = InterpErrorInfo<'static>; type Error = Size;
#[inline] #[inline]
fn try_from(int: ScalarInt) -> InterpResult<'static, Self> { fn try_from(int: ScalarInt) -> Result<Self, Size> {
int.to_bits(Size::from_bytes(8)).map(Self::from_bits) int.to_bits(Size::from_bytes(8)).map(Self::from_bits)
} }
} }