Skip to content

Commit 7b98552

Browse files
committed
use helper method for determining size of int type
1 parent b9b1554 commit 7b98552

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/librustc_mir/interpret/cast.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ use std::convert::TryFrom;
33
use rustc_apfloat::ieee::{Double, Single};
44
use rustc_apfloat::{Float, FloatConvert};
55
use rustc_ast::ast::FloatTy;
6+
use rustc_attr as attr;
67
use rustc_middle::mir::interpret::{InterpResult, PointerArithmetic, Scalar};
78
use rustc_middle::mir::CastKind;
89
use rustc_middle::ty::adjustment::PointerCast;
9-
use rustc_middle::ty::layout::TyAndLayout;
10+
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
1011
use rustc_middle::ty::{self, Ty, TypeAndMut, TypeFoldable};
1112
use rustc_span::symbol::sym;
12-
use rustc_target::abi::{LayoutOf, Size, Variants};
13+
use rustc_target::abi::{Integer, LayoutOf, Variants};
1314

1415
use super::{truncate, FnVal, ImmTy, Immediate, InterpCx, Machine, OpTy, PlaceTy};
1516

@@ -195,13 +196,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
195196
match cast_ty.kind {
196197
Int(_) | Uint(_) | RawPtr(_) => {
197198
let size = match cast_ty.kind {
198-
// FIXME: Isn't there a helper for this? The same pattern occurs below.
199-
Int(t) => {
200-
t.bit_width().map(Size::from_bits).unwrap_or_else(|| self.pointer_size())
201-
}
202-
Uint(t) => {
203-
t.bit_width().map(Size::from_bits).unwrap_or_else(|| self.pointer_size())
204-
}
199+
Int(t) => Integer::from_attr(self, attr::IntType::SignedInt(t)).size(),
200+
Uint(t) => Integer::from_attr(self, attr::IntType::UnsignedInt(t)).size(),
205201
RawPtr(_) => self.pointer_size(),
206202
_ => bug!(),
207203
};
@@ -232,20 +228,20 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
232228
match dest_ty.kind {
233229
// float -> uint
234230
Uint(t) => {
235-
let width = t.bit_width().unwrap_or_else(|| self.pointer_size().bits());
231+
let size = Integer::from_attr(self, attr::IntType::UnsignedInt(t)).size();
236232
// `to_u128` is a saturating cast, which is what we need
237233
// (https://doc.rust-lang.org/nightly/nightly-rustc/rustc_apfloat/trait.Float.html#method.to_i128_r).
238-
let v = f.to_u128(usize::try_from(width).unwrap()).value;
234+
let v = f.to_u128(size.bits_usize()).value;
239235
// This should already fit the bit width
240-
Scalar::from_uint(v, Size::from_bits(width))
236+
Scalar::from_uint(v, size)
241237
}
242238
// float -> int
243239
Int(t) => {
244-
let width = t.bit_width().unwrap_or_else(|| self.pointer_size().bits());
240+
let size = Integer::from_attr(self, attr::IntType::SignedInt(t)).size();
245241
// `to_i128` is a saturating cast, which is what we need
246242
// (https://doc.rust-lang.org/nightly/nightly-rustc/rustc_apfloat/trait.Float.html#method.to_i128_r).
247-
let v = f.to_i128(usize::try_from(width).unwrap()).value;
248-
Scalar::from_int(v, Size::from_bits(width))
243+
let v = f.to_i128(size.bits_usize()).value;
244+
Scalar::from_int(v, size)
249245
}
250246
// float -> f32
251247
Float(FloatTy::F32) => Scalar::from_f32(f.convert(&mut false).value),

0 commit comments

Comments
 (0)