@@ -3,13 +3,14 @@ use std::convert::TryFrom;
3
3
use rustc_apfloat:: ieee:: { Double , Single } ;
4
4
use rustc_apfloat:: { Float , FloatConvert } ;
5
5
use rustc_ast:: ast:: FloatTy ;
6
+ use rustc_attr as attr;
6
7
use rustc_middle:: mir:: interpret:: { InterpResult , PointerArithmetic , Scalar } ;
7
8
use rustc_middle:: mir:: CastKind ;
8
9
use rustc_middle:: ty:: adjustment:: PointerCast ;
9
- use rustc_middle:: ty:: layout:: TyAndLayout ;
10
+ use rustc_middle:: ty:: layout:: { IntegerExt , TyAndLayout } ;
10
11
use rustc_middle:: ty:: { self , Ty , TypeAndMut , TypeFoldable } ;
11
12
use rustc_span:: symbol:: sym;
12
- use rustc_target:: abi:: { LayoutOf , Size , Variants } ;
13
+ use rustc_target:: abi:: { Integer , LayoutOf , Variants } ;
13
14
14
15
use super :: { truncate, FnVal , ImmTy , Immediate , InterpCx , Machine , OpTy , PlaceTy } ;
15
16
@@ -195,13 +196,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
195
196
match cast_ty. kind {
196
197
Int ( _) | Uint ( _) | RawPtr ( _) => {
197
198
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 ( ) ,
205
201
RawPtr ( _) => self . pointer_size ( ) ,
206
202
_ => bug ! ( ) ,
207
203
} ;
@@ -232,20 +228,20 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
232
228
match dest_ty. kind {
233
229
// float -> uint
234
230
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 ( ) ;
236
232
// `to_u128` is a saturating cast, which is what we need
237
233
// (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 ;
239
235
// This should already fit the bit width
240
- Scalar :: from_uint ( v, Size :: from_bits ( width ) )
236
+ Scalar :: from_uint ( v, size )
241
237
}
242
238
// float -> int
243
239
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 ( ) ;
245
241
// `to_i128` is a saturating cast, which is what we need
246
242
// (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 )
249
245
}
250
246
// float -> f32
251
247
Float ( FloatTy :: F32 ) => Scalar :: from_f32 ( f. convert ( & mut false ) . value ) ,
0 commit comments