use helper method for determining size of int type

This commit is contained in:
Ralf Jung 2020-05-24 19:17:30 +02:00
parent b9b1554dac
commit 7b98552cc0

View File

@ -3,13 +3,14 @@ use std::convert::TryFrom;
use rustc_apfloat::ieee::{Double, Single};
use rustc_apfloat::{Float, FloatConvert};
use rustc_ast::ast::FloatTy;
use rustc_attr as attr;
use rustc_middle::mir::interpret::{InterpResult, PointerArithmetic, Scalar};
use rustc_middle::mir::CastKind;
use rustc_middle::ty::adjustment::PointerCast;
use rustc_middle::ty::layout::TyAndLayout;
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
use rustc_middle::ty::{self, Ty, TypeAndMut, TypeFoldable};
use rustc_span::symbol::sym;
use rustc_target::abi::{LayoutOf, Size, Variants};
use rustc_target::abi::{Integer, LayoutOf, Variants};
use super::{truncate, FnVal, ImmTy, Immediate, InterpCx, Machine, OpTy, PlaceTy};
@ -195,13 +196,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
match cast_ty.kind {
Int(_) | Uint(_) | RawPtr(_) => {
let size = match cast_ty.kind {
// FIXME: Isn't there a helper for this? The same pattern occurs below.
Int(t) => {
t.bit_width().map(Size::from_bits).unwrap_or_else(|| self.pointer_size())
}
Uint(t) => {
t.bit_width().map(Size::from_bits).unwrap_or_else(|| self.pointer_size())
}
Int(t) => Integer::from_attr(self, attr::IntType::SignedInt(t)).size(),
Uint(t) => Integer::from_attr(self, attr::IntType::UnsignedInt(t)).size(),
RawPtr(_) => self.pointer_size(),
_ => bug!(),
};
@ -232,20 +228,20 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
match dest_ty.kind {
// float -> uint
Uint(t) => {
let width = t.bit_width().unwrap_or_else(|| self.pointer_size().bits());
let size = Integer::from_attr(self, attr::IntType::UnsignedInt(t)).size();
// `to_u128` is a saturating cast, which is what we need
// (https://doc.rust-lang.org/nightly/nightly-rustc/rustc_apfloat/trait.Float.html#method.to_i128_r).
let v = f.to_u128(usize::try_from(width).unwrap()).value;
let v = f.to_u128(size.bits_usize()).value;
// This should already fit the bit width
Scalar::from_uint(v, Size::from_bits(width))
Scalar::from_uint(v, size)
}
// float -> int
Int(t) => {
let width = t.bit_width().unwrap_or_else(|| self.pointer_size().bits());
let size = Integer::from_attr(self, attr::IntType::SignedInt(t)).size();
// `to_i128` is a saturating cast, which is what we need
// (https://doc.rust-lang.org/nightly/nightly-rustc/rustc_apfloat/trait.Float.html#method.to_i128_r).
let v = f.to_i128(usize::try_from(width).unwrap()).value;
Scalar::from_int(v, Size::from_bits(width))
let v = f.to_i128(size.bits_usize()).value;
Scalar::from_int(v, size)
}
// float -> f32
Float(FloatTy::F32) => Scalar::from_f32(f.convert(&mut false).value),