Rollup merge of #126265 - RalfJung:interpret-cast-validity, r=oli-obk
interpret: ensure we check bool/char for validity when they are used in a cast In general, `Scalar::to_bits` is a bit dangerous as it bypasses all type information. We should usually prefer matching on the type and acting according to that. So I also refactored `unary_op` handling of integers to do that. The remaining `to_bits` uses are operations that just fundamentally don't care about the sign (and only work on integers). invalid_char_cast.rs is the key new test, the others already passed before this PR. r? `@oli-obk`
This commit is contained in:
commit
cfd48bdd7e
12 changed files with 190 additions and 36 deletions
|
@ -209,8 +209,8 @@ impl ScalarInt {
|
|||
|
||||
#[inline]
|
||||
pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
|
||||
let data = i.into();
|
||||
if size.truncate(data) == data { Some(Self::raw(data, size)) } else { None }
|
||||
let (r, overflow) = Self::truncate_from_uint(i, size);
|
||||
if overflow { None } else { Some(r) }
|
||||
}
|
||||
|
||||
/// Returns the truncated result, and whether truncation changed the value.
|
||||
|
@ -223,20 +223,15 @@ impl ScalarInt {
|
|||
|
||||
#[inline]
|
||||
pub fn try_from_int(i: impl Into<i128>, size: Size) -> Option<Self> {
|
||||
let i = i.into();
|
||||
// `into` performed sign extension, we have to truncate
|
||||
let truncated = size.truncate(i as u128);
|
||||
if size.sign_extend(truncated) as i128 == i {
|
||||
Some(Self::raw(truncated, size))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
let (r, overflow) = Self::truncate_from_int(i, size);
|
||||
if overflow { None } else { Some(r) }
|
||||
}
|
||||
|
||||
/// Returns the truncated result, and whether truncation changed the value.
|
||||
#[inline]
|
||||
pub fn truncate_from_int(i: impl Into<i128>, size: Size) -> (Self, bool) {
|
||||
let data = i.into();
|
||||
// `into` performed sign extension, we have to truncate
|
||||
let r = Self::raw(size.truncate(data as u128), size);
|
||||
(r, size.sign_extend(r.data) as i128 != data)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue