Skip to content

Commit 09f7f91

Browse files
committed
Add convenience conversion methods for ScalarInt
1 parent 858216c commit 09f7f91

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::convert::TryFrom;
1+
use std::convert::{TryFrom, TryInto};
22
use std::fmt;
33

44
use rustc_apfloat::{
@@ -56,20 +56,20 @@ impl<'tcx> ConstValue<'tcx> {
5656
}
5757
}
5858

59+
pub fn try_to_scalar_int(&self) -> Option<ScalarInt> {
60+
self.try_to_scalar()?.to_int().ok()
61+
}
62+
5963
pub fn try_to_bits(&self, size: Size) -> Option<u128> {
60-
self.try_to_scalar()?.to_bits(size).ok()
64+
self.try_to_scalar_int()?.to_bits(size).ok()
6165
}
6266

6367
pub fn try_to_bool(&self) -> Option<bool> {
64-
match self.try_to_bits(Size::from_bytes(1))? {
65-
0 => Some(false),
66-
1 => Some(true),
67-
_ => None,
68-
}
68+
self.try_to_scalar_int()?.try_into().ok()
6969
}
7070

7171
pub fn try_to_machine_usize(&self, tcx: TyCtxt<'tcx>) -> Option<u64> {
72-
Some(self.try_to_bits(tcx.data_layout.pointer_size)? as u64)
72+
self.try_to_scalar_int()?.try_to_machine_usize(tcx).ok()
7373
}
7474

7575
pub fn try_to_bits_for_ty(
@@ -505,6 +505,21 @@ impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
505505
}
506506
}
507507

508+
impl TryFrom<Scalar> for ScalarInt {
509+
type Error = super::InterpErrorInfo<'static>;
510+
#[inline]
511+
fn try_from(scalar: Scalar) -> InterpResult<'static, Self> {
512+
scalar.to_int()
513+
}
514+
}
515+
516+
impl<Tag> From<ScalarInt> for Scalar<Tag> {
517+
#[inline(always)]
518+
fn from(ptr: ScalarInt) -> Self {
519+
Scalar::Int(ptr)
520+
}
521+
}
522+
508523
#[derive(Clone, Copy, Eq, PartialEq, TyEncodable, TyDecodable, HashStable, Hash)]
509524
pub enum ScalarMaybeUninit<Tag = ()> {
510525
Scalar(Scalar<Tag>),

compiler/rustc_middle/src/ty/consts/int.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use rustc_target::abi::{Size, TargetDataLayout};
55
use std::convert::{TryFrom, TryInto};
66
use std::fmt;
77

8+
use crate::ty::TyCtxt;
9+
810
#[derive(Copy, Clone)]
911
/// A type for representing any integer. Only used for printing.
1012
pub struct ConstInt {
@@ -239,6 +241,11 @@ impl ScalarInt {
239241
Err(self.size())
240242
}
241243
}
244+
245+
#[inline]
246+
pub fn try_to_machine_usize(&self, tcx: TyCtxt<'tcx>) -> Result<u64, Size> {
247+
Ok(self.to_bits(tcx.data_layout.pointer_size)? as u64)
248+
}
242249
}
243250

244251
macro_rules! from {
@@ -277,6 +284,18 @@ macro_rules! try_from {
277284
from!(u8, u16, u32, u64, u128, bool);
278285
try_from!(u8, u16, u32, u64, u128);
279286

287+
impl TryFrom<ScalarInt> for bool {
288+
type Error = Size;
289+
#[inline]
290+
fn try_from(int: ScalarInt) -> Result<Self, Size> {
291+
int.to_bits(Size::from_bytes(1)).and_then(|u| match u {
292+
0 => Ok(false),
293+
1 => Ok(true),
294+
_ => Err(Size::from_bytes(1)),
295+
})
296+
}
297+
}
298+
280299
impl From<char> for ScalarInt {
281300
#[inline]
282301
fn from(c: char) -> Self {

compiler/rustc_middle/src/ty/consts/kind.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::convert::TryInto;
2+
13
use crate::mir::interpret::ConstValue;
24
use crate::mir::interpret::Scalar;
35
use crate::mir::Promoted;
@@ -9,6 +11,8 @@ use rustc_hir::def_id::DefId;
911
use rustc_macros::HashStable;
1012
use rustc_target::abi::Size;
1113

14+
use super::ScalarInt;
15+
1216
/// Represents a constant in Rust.
1317
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)]
1418
#[derive(HashStable)]
@@ -51,14 +55,19 @@ impl<'tcx> ConstKind<'tcx> {
5155
self.try_to_value()?.try_to_scalar()
5256
}
5357

58+
#[inline]
59+
pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
60+
self.try_to_value()?.try_to_scalar()?.to_int().ok()
61+
}
62+
5463
#[inline]
5564
pub fn try_to_bits(self, size: Size) -> Option<u128> {
56-
self.try_to_value()?.try_to_bits(size)
65+
self.try_to_scalar_int()?.to_bits(size).ok()
5766
}
5867

5968
#[inline]
6069
pub fn try_to_bool(self) -> Option<bool> {
61-
self.try_to_value()?.try_to_bool()
70+
self.try_to_scalar_int()?.try_into().ok()
6271
}
6372

6473
#[inline]

0 commit comments

Comments
 (0)