1- use crate :: mir:: interpret:: { sign_extend, truncate, InterpErrorInfo , InterpResult } ;
2- use crate :: throw_ub;
1+ use crate :: mir:: interpret:: { sign_extend, truncate, InterpResult } ;
32use rustc_apfloat:: ieee:: { Double , Single } ;
43use rustc_apfloat:: Float ;
54use rustc_serialize:: { Decodable , Decoder , Encodable , Encoder } ;
@@ -233,16 +232,14 @@ impl ScalarInt {
233232 }
234233
235234 #[ 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 > {
237236 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 ( ) )
243242 }
244- self . check_data ( ) ;
245- Ok ( self . data )
246243 }
247244}
248245
@@ -266,9 +263,9 @@ macro_rules! try_from {
266263 ( $( $ty: ty) ,* ) => {
267264 $(
268265 impl TryFrom <ScalarInt > for $ty {
269- type Error = InterpErrorInfo < ' static > ;
266+ type Error = Size ;
270267 #[ inline]
271- fn try_from( int: ScalarInt ) -> InterpResult < ' static , Self > {
268+ fn try_from( int: ScalarInt ) -> Result < Self , Size > {
272269 int. to_bits( Size :: from_bytes( std:: mem:: size_of:: <$ty>( ) ) ) . map( |u| u. try_into( ) . unwrap( ) )
273270 }
274271 }
@@ -287,9 +284,9 @@ impl From<char> for ScalarInt {
287284}
288285
289286impl TryFrom < ScalarInt > for char {
290- type Error = InterpErrorInfo < ' static > ;
287+ type Error = Size ;
291288 #[ inline]
292- fn try_from ( int : ScalarInt ) -> InterpResult < ' static , Self > {
289+ fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
293290 int. to_bits ( Size :: from_bytes ( std:: mem:: size_of :: < char > ( ) ) )
294291 . map ( |u| char:: from_u32 ( u. try_into ( ) . unwrap ( ) ) . unwrap ( ) )
295292 }
@@ -304,9 +301,9 @@ impl From<Single> for ScalarInt {
304301}
305302
306303impl TryFrom < ScalarInt > for Single {
307- type Error = InterpErrorInfo < ' static > ;
304+ type Error = Size ;
308305 #[ inline]
309- fn try_from ( int : ScalarInt ) -> InterpResult < ' static , Self > {
306+ fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
310307 int. to_bits ( Size :: from_bytes ( 4 ) ) . map ( Self :: from_bits)
311308 }
312309}
@@ -320,9 +317,9 @@ impl From<Double> for ScalarInt {
320317}
321318
322319impl TryFrom < ScalarInt > for Double {
323- type Error = InterpErrorInfo < ' static > ;
320+ type Error = Size ;
324321 #[ inline]
325- fn try_from ( int : ScalarInt ) -> InterpResult < ' static , Self > {
322+ fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
326323 int. to_bits ( Size :: from_bytes ( 8 ) ) . map ( Self :: from_bits)
327324 }
328325}
0 commit comments