From 500af76831e2babd8a1189eac2dffd82bc6db6ac Mon Sep 17 00:00:00 2001 From: oli Date: Sun, 1 Nov 2020 17:21:33 +0000 Subject: [PATCH] Add helper for getting an `int` out of a `Scalar` --- compiler/rustc_middle/src/mir/interpret/value.rs | 8 ++++++++ compiler/rustc_mir/src/interpret/operand.rs | 5 +---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/value.rs b/compiler/rustc_middle/src/mir/interpret/value.rs index 2a879302d0d..53bbbb8cddb 100644 --- a/compiler/rustc_middle/src/mir/interpret/value.rs +++ b/compiler/rustc_middle/src/mir/interpret/value.rs @@ -357,6 +357,14 @@ impl<'tcx, Tag> Scalar { 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 { match self { diff --git a/compiler/rustc_mir/src/interpret/operand.rs b/compiler/rustc_mir/src/interpret/operand.rs index 55672a201ee..8716d4d9ad7 100644 --- a/compiler/rustc_mir/src/interpret/operand.rs +++ b/compiler/rustc_mir/src/interpret/operand.rs @@ -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()) } }