Skip to content

Commit 858216c

Browse files
committed
Add fallible Scalar to ScalarInt conversion method
1 parent 0fe4f38 commit 858216c

File tree

1 file changed

+15
-13
lines changed
  • compiler/rustc_middle/src/mir/interpret

1 file changed

+15
-13
lines changed

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,13 @@ impl<'tcx, Tag> Scalar<Tag> {
346346
#[inline]
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");
349-
match self {
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-
}),
357-
Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),
358-
}
349+
self.to_int()?.to_bits(target_size).map_err(|size| {
350+
err_ub!(ScalarSizeMismatch {
351+
target_size: target_size.bytes(),
352+
data_size: size.bytes(),
353+
})
354+
.into()
355+
})
359356
}
360357

361358
#[inline(always)]
@@ -364,13 +361,18 @@ impl<'tcx, Tag> Scalar<Tag> {
364361
}
365362

366363
#[inline]
367-
pub fn assert_int(self) -> ScalarInt {
364+
pub fn to_int(self) -> InterpResult<'tcx, ScalarInt> {
368365
match self {
369-
Scalar::Ptr(_) => bug!("expected an int but got an abstract pointer"),
370-
Scalar::Int(int) => int,
366+
Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),
367+
Scalar::Int(int) => Ok(int),
371368
}
372369
}
373370

371+
#[inline]
372+
pub fn assert_int(self) -> ScalarInt {
373+
self.to_int().expect("expected an int but got an abstract pointer")
374+
}
375+
374376
#[inline]
375377
pub fn assert_ptr(self) -> Pointer<Tag> {
376378
match self {

0 commit comments

Comments
 (0)