Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 500af76

Browse files
committed
Add helper for getting an int out of a Scalar
1 parent f03b18b commit 500af76

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,14 @@ impl<'tcx, Tag> Scalar<Tag> {
357357
self.to_bits(target_size).expect("expected Raw bits but got a Pointer")
358358
}
359359

360+
#[inline]
361+
pub fn assert_int(self) -> ScalarInt {
362+
match self {
363+
Scalar::Ptr(_) => bug!("expected an int but got an abstract pointer"),
364+
Scalar::Int(int) => int,
365+
}
366+
}
367+
360368
#[inline]
361369
pub fn assert_ptr(self) -> Pointer<Tag> {
362370
match self {

compiler/rustc_mir/src/interpret/operand.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,7 @@ impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> {
211211
#[inline]
212212
pub fn to_const_int(self) -> ConstInt {
213213
assert!(self.layout.ty.is_integral());
214-
let int = match self.to_scalar().expect("to_const_int doesn't work on scalar pairs") {
215-
Scalar::Int(int) => int,
216-
Scalar::Ptr(_) => bug!("to_const_int doesn't work on pointers"),
217-
};
214+
let int = self.to_scalar().expect("to_const_int doesn't work on scalar pairs").assert_int();
218215
ConstInt::new(int, self.layout.ty.is_signed(), self.layout.ty.is_ptr_sized_integral())
219216
}
220217
}

0 commit comments

Comments
 (0)