diff --git a/compiler/rustc_middle/src/mir/interpret/value.rs b/compiler/rustc_middle/src/mir/interpret/value.rs index ba9b285e8e6..3dd905b35b2 100644 --- a/compiler/rustc_middle/src/mir/interpret/value.rs +++ b/compiler/rustc_middle/src/mir/interpret/value.rs @@ -346,16 +346,13 @@ impl<'tcx, Tag> Scalar { #[inline] 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"); - match self { - 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), - } + self.to_int()?.to_bits(target_size).map_err(|size| { + err_ub!(ScalarSizeMismatch { + target_size: target_size.bytes(), + data_size: size.bytes(), + }) + .into() + }) } #[inline(always)] @@ -364,13 +361,18 @@ impl<'tcx, Tag> Scalar { } #[inline] - pub fn assert_int(self) -> ScalarInt { + pub fn to_int(self) -> InterpResult<'tcx, ScalarInt> { match self { - Scalar::Ptr(_) => bug!("expected an int but got an abstract pointer"), - Scalar::Int(int) => int, + Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes), + Scalar::Int(int) => Ok(int), } } + #[inline] + pub fn assert_int(self) -> ScalarInt { + self.to_int().expect("expected an int but got an abstract pointer") + } + #[inline] pub fn assert_ptr(self) -> Pointer { match self {