Add helper for getting an `int` out of a `Scalar`

This commit is contained in:
oli 2020-11-01 17:21:33 +00:00
parent f03b18b99b
commit 500af76831
2 changed files with 9 additions and 4 deletions

View File

@ -357,6 +357,14 @@ impl<'tcx, Tag> Scalar<Tag> {
self.to_bits(target_size).expect("expected Raw bits but got a Pointer")
}
#[inline]
pub fn assert_int(self) -> ScalarInt {
match self {
Scalar::Ptr(_) => bug!("expected an int but got an abstract pointer"),
Scalar::Int(int) => int,
}
}
#[inline]
pub fn assert_ptr(self) -> Pointer<Tag> {
match self {

View File

@ -211,10 +211,7 @@ impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> {
#[inline]
pub fn to_const_int(self) -> ConstInt {
assert!(self.layout.ty.is_integral());
let int = match self.to_scalar().expect("to_const_int doesn't work on scalar pairs") {
Scalar::Int(int) => int,
Scalar::Ptr(_) => bug!("to_const_int doesn't work on pointers"),
};
let int = self.to_scalar().expect("to_const_int doesn't work on scalar pairs").assert_int();
ConstInt::new(int, self.layout.ty.is_signed(), self.layout.ty.is_ptr_sized_integral())
}
}