1
- use crate :: mir:: interpret:: { sign_extend, truncate, InterpErrorInfo , InterpResult } ;
2
- use crate :: throw_ub;
1
+ use crate :: mir:: interpret:: { sign_extend, truncate, InterpResult } ;
3
2
use rustc_apfloat:: ieee:: { Double , Single } ;
4
3
use rustc_apfloat:: Float ;
5
4
use rustc_serialize:: { Decodable , Decoder , Encodable , Encoder } ;
@@ -233,16 +232,14 @@ impl ScalarInt {
233
232
}
234
233
235
234
#[ inline]
236
- pub fn to_bits ( self , target_size : Size ) -> InterpResult < ' static , u128 > {
235
+ pub fn to_bits ( self , target_size : Size ) -> Result < u128 , Size > {
237
236
assert_ne ! ( target_size. bytes( ) , 0 , "you should never look at the bits of a ZST" ) ;
238
- if target_size. bytes ( ) ! = u64:: from ( self . size ) {
239
- throw_ub ! ( ScalarSizeMismatch {
240
- target_size : target_size . bytes ( ) ,
241
- data_size : u64 :: from ( self . size ) ,
242
- } ) ;
237
+ if target_size. bytes ( ) = = u64:: from ( self . size ) {
238
+ self . check_data ( ) ;
239
+ Ok ( self . data )
240
+ } else {
241
+ Err ( self . size ( ) )
243
242
}
244
- self . check_data ( ) ;
245
- Ok ( self . data )
246
243
}
247
244
}
248
245
@@ -266,9 +263,9 @@ macro_rules! try_from {
266
263
( $( $ty: ty) ,* ) => {
267
264
$(
268
265
impl TryFrom <ScalarInt > for $ty {
269
- type Error = InterpErrorInfo < ' static > ;
266
+ type Error = Size ;
270
267
#[ inline]
271
- fn try_from( int: ScalarInt ) -> InterpResult < ' static , Self > {
268
+ fn try_from( int: ScalarInt ) -> Result < Self , Size > {
272
269
int. to_bits( Size :: from_bytes( std:: mem:: size_of:: <$ty>( ) ) ) . map( |u| u. try_into( ) . unwrap( ) )
273
270
}
274
271
}
@@ -287,9 +284,9 @@ impl From<char> for ScalarInt {
287
284
}
288
285
289
286
impl TryFrom < ScalarInt > for char {
290
- type Error = InterpErrorInfo < ' static > ;
287
+ type Error = Size ;
291
288
#[ inline]
292
- fn try_from ( int : ScalarInt ) -> InterpResult < ' static , Self > {
289
+ fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
293
290
int. to_bits ( Size :: from_bytes ( std:: mem:: size_of :: < char > ( ) ) )
294
291
. map ( |u| char:: from_u32 ( u. try_into ( ) . unwrap ( ) ) . unwrap ( ) )
295
292
}
@@ -304,9 +301,9 @@ impl From<Single> for ScalarInt {
304
301
}
305
302
306
303
impl TryFrom < ScalarInt > for Single {
307
- type Error = InterpErrorInfo < ' static > ;
304
+ type Error = Size ;
308
305
#[ inline]
309
- fn try_from ( int : ScalarInt ) -> InterpResult < ' static , Self > {
306
+ fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
310
307
int. to_bits ( Size :: from_bytes ( 4 ) ) . map ( Self :: from_bits)
311
308
}
312
309
}
@@ -320,9 +317,9 @@ impl From<Double> for ScalarInt {
320
317
}
321
318
322
319
impl TryFrom < ScalarInt > for Double {
323
- type Error = InterpErrorInfo < ' static > ;
320
+ type Error = Size ;
324
321
#[ inline]
325
- fn try_from ( int : ScalarInt ) -> InterpResult < ' static , Self > {
322
+ fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
326
323
int. to_bits ( Size :: from_bytes ( 8 ) ) . map ( Self :: from_bits)
327
324
}
328
325
}
0 commit comments