Skip to content

Commit

Permalink
Add ImmTy::from_uint
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Aug 17, 2019
1 parent 211d1e0 commit 0a2a517
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/librustc_mir/interpret/operand.rs
Expand Up @@ -187,13 +187,22 @@ impl<'tcx, Tag> From<ImmTy<'tcx, Tag>> for OpTy<'tcx, Tag> {
}
}

impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag>
{
impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> {
#[inline]
pub fn from_scalar(val: Scalar<Tag>, layout: TyLayout<'tcx>) -> Self {
ImmTy { imm: val.into(), layout }
}

#[inline]
pub fn from_uint(i: impl Into<u128>, layout: TyLayout<'tcx>) -> Self {
Self::from_scalar(Scalar::from_uint(i, layout.size), layout)
}

#[inline]
pub fn from_int(i: impl Into<i128>, layout: TyLayout<'tcx>) -> Self {
Self::from_scalar(Scalar::from_int(i, layout.size), layout)
}

#[inline]
pub fn to_bits(self) -> InterpResult<'tcx, u128> {
self.to_scalar()?.to_bits(self.layout.size)
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_mir/interpret/terminator.rs
Expand Up @@ -7,7 +7,7 @@ use syntax::source_map::Span;
use rustc_target::spec::abi::Abi;

use super::{
InterpResult, PointerArithmetic, Scalar,
InterpResult, PointerArithmetic,
InterpCx, Machine, OpTy, ImmTy, PlaceTy, MPlaceTy, StackPopCleanup, FnVal,
};

Expand Down Expand Up @@ -50,10 +50,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {

for (index, &const_int) in values.iter().enumerate() {
// Compare using binary_op, to also support pointer values
let const_int = Scalar::from_uint(const_int, discr.layout.size);
let (res, _) = self.binary_op(mir::BinOp::Eq,
discr,
ImmTy::from_scalar(const_int, discr.layout),
ImmTy::from_uint(const_int, discr.layout),
)?;
if res.to_bool()? {
target_block = targets[index];
Expand Down

0 comments on commit 0a2a517

Please sign in to comment.