Skip to content

Commit dad0036

Browse files
committed
Do not raise interp errors from the scalar int module
1 parent 500af76 commit dad0036

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

compiler/rustc_middle/src/mir/interpret/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ macro_rules! throw_inval {
7171

7272
#[macro_export]
7373
macro_rules! throw_ub {
74-
($($tt:tt)*) => { Err::<!, _>($crate::err_ub!($($tt)*))? };
74+
($($tt:tt)*) => { Err::<!, _>(err_ub!($($tt)*))? };
7575
}
7676

7777
#[macro_export]

compiler/rustc_middle/src/mir/interpret/value.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,13 @@ impl<'tcx, Tag> Scalar<Tag> {
347347
fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
348348
assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
349349
match self {
350-
Scalar::Int(int) => int.to_bits(target_size),
350+
Scalar::Int(int) => int.to_bits(target_size).map_err(|size| {
351+
err_ub!(ScalarSizeMismatch {
352+
target_size: target_size.bytes(),
353+
data_size: size.bytes(),
354+
})
355+
.into()
356+
}),
351357
Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),
352358
}
353359
}

compiler/rustc_middle/src/ty/consts/int.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::mir::interpret::{sign_extend, truncate, InterpErrorInfo, InterpResult};
2-
use crate::throw_ub;
1+
use crate::mir::interpret::{sign_extend, truncate, InterpResult};
32
use rustc_apfloat::ieee::{Double, Single};
43
use rustc_apfloat::Float;
54
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
@@ -233,16 +232,14 @@ impl ScalarInt {
233232
}
234233

235234
#[inline]
236-
pub fn to_bits(self, target_size: Size) -> InterpResult<'static, u128> {
235+
pub fn to_bits(self, target_size: Size) -> Result<u128, Size> {
237236
assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
238-
if target_size.bytes() != u64::from(self.size) {
239-
throw_ub!(ScalarSizeMismatch {
240-
target_size: target_size.bytes(),
241-
data_size: u64::from(self.size),
242-
});
237+
if target_size.bytes() == u64::from(self.size) {
238+
self.check_data();
239+
Ok(self.data)
240+
} else {
241+
Err(self.size())
243242
}
244-
self.check_data();
245-
Ok(self.data)
246243
}
247244
}
248245

@@ -266,9 +263,9 @@ macro_rules! try_from {
266263
($($ty:ty),*) => {
267264
$(
268265
impl TryFrom<ScalarInt> for $ty {
269-
type Error = InterpErrorInfo<'static>;
266+
type Error = Size;
270267
#[inline]
271-
fn try_from(int: ScalarInt) -> InterpResult<'static, Self> {
268+
fn try_from(int: ScalarInt) -> Result<Self, Size> {
272269
int.to_bits(Size::from_bytes(std::mem::size_of::<$ty>())).map(|u| u.try_into().unwrap())
273270
}
274271
}
@@ -287,9 +284,9 @@ impl From<char> for ScalarInt {
287284
}
288285

289286
impl TryFrom<ScalarInt> for char {
290-
type Error = InterpErrorInfo<'static>;
287+
type Error = Size;
291288
#[inline]
292-
fn try_from(int: ScalarInt) -> InterpResult<'static, Self> {
289+
fn try_from(int: ScalarInt) -> Result<Self, Size> {
293290
int.to_bits(Size::from_bytes(std::mem::size_of::<char>()))
294291
.map(|u| char::from_u32(u.try_into().unwrap()).unwrap())
295292
}
@@ -304,9 +301,9 @@ impl From<Single> for ScalarInt {
304301
}
305302

306303
impl TryFrom<ScalarInt> for Single {
307-
type Error = InterpErrorInfo<'static>;
304+
type Error = Size;
308305
#[inline]
309-
fn try_from(int: ScalarInt) -> InterpResult<'static, Self> {
306+
fn try_from(int: ScalarInt) -> Result<Self, Size> {
310307
int.to_bits(Size::from_bytes(4)).map(Self::from_bits)
311308
}
312309
}
@@ -320,9 +317,9 @@ impl From<Double> for ScalarInt {
320317
}
321318

322319
impl TryFrom<ScalarInt> for Double {
323-
type Error = InterpErrorInfo<'static>;
320+
type Error = Size;
324321
#[inline]
325-
fn try_from(int: ScalarInt) -> InterpResult<'static, Self> {
322+
fn try_from(int: ScalarInt) -> Result<Self, Size> {
326323
int.to_bits(Size::from_bytes(8)).map(Self::from_bits)
327324
}
328325
}

0 commit comments

Comments
 (0)