@@ -106,14 +106,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
106
106
match src. layout . ty . kind {
107
107
// Floating point
108
108
Float ( FloatTy :: F32 ) => {
109
- return Ok ( self
110
- . cast_from_float ( src. to_scalar ( ) ?. to_f32 ( ) ?, dest_layout. ty ) ?
111
- . into ( ) ) ;
109
+ return Ok ( self . cast_from_float ( src. to_scalar ( ) ?. to_f32 ( ) ?, dest_layout. ty ) . into ( ) ) ;
112
110
}
113
111
Float ( FloatTy :: F64 ) => {
114
- return Ok ( self
115
- . cast_from_float ( src. to_scalar ( ) ?. to_f64 ( ) ?, dest_layout. ty ) ?
116
- . into ( ) ) ;
112
+ return Ok ( self . cast_from_float ( src. to_scalar ( ) ?. to_f64 ( ) ?, dest_layout. ty ) . into ( ) ) ;
117
113
}
118
114
// The rest is integer/pointer-"like", including fn ptr casts and casts from enums that
119
115
// are represented as integers.
@@ -135,7 +131,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
135
131
assert ! ( src. layout. is_zst( ) ) ;
136
132
let discr_layout = self . layout_of ( discr. ty ) ?;
137
133
return Ok ( self
138
- . cast_from_int_like ( discr. val , discr_layout, dest_layout) ?
134
+ . cast_from_int_like ( discr. val , discr_layout, dest_layout)
139
135
. into ( ) ) ;
140
136
}
141
137
}
@@ -173,15 +169,15 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
173
169
// (b) cast from an integer-like (including bool, char, enums).
174
170
// In both cases we want the bits.
175
171
let bits = self . force_bits ( src. to_scalar ( ) ?, src. layout . size ) ?;
176
- Ok ( self . cast_from_int_like ( bits, src. layout , dest_layout) ? . into ( ) )
172
+ Ok ( self . cast_from_int_like ( bits, src. layout , dest_layout) . into ( ) )
177
173
}
178
174
179
- fn cast_from_int_like (
175
+ pub ( super ) fn cast_from_int_like (
180
176
& self ,
181
177
v : u128 , // raw bits
182
178
src_layout : TyAndLayout < ' tcx > ,
183
179
dest_layout : TyAndLayout < ' tcx > ,
184
- ) -> InterpResult < ' tcx , Scalar < M :: PointerTag > > {
180
+ ) -> Scalar < M :: PointerTag > {
185
181
// Let's make sure v is sign-extended *if* it has a signed type.
186
182
let signed = src_layout. abi . is_signed ( ) ;
187
183
let v = if signed { self . sign_extend ( v, src_layout) } else { v } ;
@@ -190,33 +186,25 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
190
186
match dest_layout. ty . kind {
191
187
Int ( _) | Uint ( _) | RawPtr ( _) => {
192
188
let v = self . truncate ( v, dest_layout) ;
193
- Ok ( Scalar :: from_uint ( v, dest_layout. size ) )
189
+ Scalar :: from_uint ( v, dest_layout. size )
194
190
}
195
191
196
- Float ( FloatTy :: F32 ) if signed => {
197
- Ok ( Scalar :: from_f32 ( Single :: from_i128 ( v as i128 ) . value ) )
198
- }
199
- Float ( FloatTy :: F64 ) if signed => {
200
- Ok ( Scalar :: from_f64 ( Double :: from_i128 ( v as i128 ) . value ) )
201
- }
202
- Float ( FloatTy :: F32 ) => Ok ( Scalar :: from_f32 ( Single :: from_u128 ( v) . value ) ) ,
203
- Float ( FloatTy :: F64 ) => Ok ( Scalar :: from_f64 ( Double :: from_u128 ( v) . value ) ) ,
192
+ Float ( FloatTy :: F32 ) if signed => Scalar :: from_f32 ( Single :: from_i128 ( v as i128 ) . value ) ,
193
+ Float ( FloatTy :: F64 ) if signed => Scalar :: from_f64 ( Double :: from_i128 ( v as i128 ) . value ) ,
194
+ Float ( FloatTy :: F32 ) => Scalar :: from_f32 ( Single :: from_u128 ( v) . value ) ,
195
+ Float ( FloatTy :: F64 ) => Scalar :: from_f64 ( Double :: from_u128 ( v) . value ) ,
204
196
205
197
Char => {
206
198
// `u8` to `char` cast
207
- Ok ( Scalar :: from_u32 ( u8:: try_from ( v) . unwrap ( ) . into ( ) ) )
199
+ Scalar :: from_u32 ( u8:: try_from ( v) . unwrap ( ) . into ( ) )
208
200
}
209
201
210
202
// Casts to bool are not permitted by rustc, no need to handle them here.
211
203
_ => bug ! ( "invalid int to {:?} cast" , dest_layout. ty) ,
212
204
}
213
205
}
214
206
215
- fn cast_from_float < F > (
216
- & self ,
217
- f : F ,
218
- dest_ty : Ty < ' tcx > ,
219
- ) -> InterpResult < ' tcx , Scalar < M :: PointerTag > >
207
+ fn cast_from_float < F > ( & self , f : F , dest_ty : Ty < ' tcx > ) -> Scalar < M :: PointerTag >
220
208
where
221
209
F : Float + Into < Scalar < M :: PointerTag > > + FloatConvert < Single > + FloatConvert < Double > ,
222
210
{
@@ -229,20 +217,20 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
229
217
// (https://doc.rust-lang.org/nightly/nightly-rustc/rustc_apfloat/trait.Float.html#method.to_i128_r).
230
218
let v = f. to_u128 ( usize:: try_from ( width) . unwrap ( ) ) . value ;
231
219
// This should already fit the bit width
232
- Ok ( Scalar :: from_uint ( v, Size :: from_bits ( width) ) )
220
+ Scalar :: from_uint ( v, Size :: from_bits ( width) )
233
221
}
234
222
// float -> int
235
223
Int ( t) => {
236
224
let width = t. bit_width ( ) . unwrap_or_else ( || self . pointer_size ( ) . bits ( ) ) ;
237
225
// `to_i128` is a saturating cast, which is what we need
238
226
// (https://doc.rust-lang.org/nightly/nightly-rustc/rustc_apfloat/trait.Float.html#method.to_i128_r).
239
227
let v = f. to_i128 ( usize:: try_from ( width) . unwrap ( ) ) . value ;
240
- Ok ( Scalar :: from_int ( v, Size :: from_bits ( width) ) )
228
+ Scalar :: from_int ( v, Size :: from_bits ( width) )
241
229
}
242
230
// float -> f32
243
- Float ( FloatTy :: F32 ) => Ok ( Scalar :: from_f32 ( f. convert ( & mut false ) . value ) ) ,
231
+ Float ( FloatTy :: F32 ) => Scalar :: from_f32 ( f. convert ( & mut false ) . value ) ,
244
232
// float -> f64
245
- Float ( FloatTy :: F64 ) => Ok ( Scalar :: from_f64 ( f. convert ( & mut false ) . value ) ) ,
233
+ Float ( FloatTy :: F64 ) => Scalar :: from_f64 ( f. convert ( & mut false ) . value ) ,
246
234
// That's it.
247
235
_ => bug ! ( "invalid float to {:?} cast" , dest_ty) ,
248
236
}
0 commit comments