@@ -501,35 +501,22 @@ fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
501
501
Ok ( & OsStr :: new ( s) )
502
502
}
503
503
504
- // FIXME: change `ImmTy::from_int` so it returns an `InterpResult` instead and remove this
505
- // function.
506
504
pub fn immty_from_int_checked < ' tcx > (
507
505
int : impl Into < i128 > ,
508
506
layout : TyLayout < ' tcx > ,
509
507
) -> InterpResult < ' tcx , ImmTy < ' tcx , Tag > > {
510
508
let int = int. into ( ) ;
511
- // If `int` does not fit in `size` bits, we error instead of letting
512
- // `ImmTy::from_int` panic.
513
- let size = layout. size ;
514
- let truncated = truncate ( int as u128 , size) ;
515
- if sign_extend ( truncated, size) as i128 != int {
516
- throw_unsup_format ! ( "Signed value {:#x} does not fit in {} bits" , int, size. bits( ) )
517
- }
518
- Ok ( ImmTy :: from_int ( int, layout) )
509
+ Ok ( ImmTy :: try_from_int ( int, layout) . ok_or_else ( ||
510
+ err_unsup_format ! ( "Signed value {:#x} does not fit in {} bits" , int, layout. size. bits( ) )
511
+ ) ?)
519
512
}
520
513
521
- // FIXME: change `ImmTy::from_uint` so it returns an `InterpResult` instead and remove this
522
- // function.
523
514
pub fn immty_from_uint_checked < ' tcx > (
524
515
int : impl Into < u128 > ,
525
516
layout : TyLayout < ' tcx > ,
526
517
) -> InterpResult < ' tcx , ImmTy < ' tcx , Tag > > {
527
518
let int = int. into ( ) ;
528
- // If `int` does not fit in `size` bits, we error instead of letting
529
- // `ImmTy::from_int` panic.
530
- let size = layout. size ;
531
- if truncate ( int, size) != int {
532
- throw_unsup_format ! ( "Unsigned value {:#x} does not fit in {} bits" , int, size. bits( ) )
533
- }
534
- Ok ( ImmTy :: from_uint ( int, layout) )
519
+ Ok ( ImmTy :: try_from_uint ( int, layout) . ok_or_else ( ||
520
+ err_unsup_format ! ( "Signed value {:#x} does not fit in {} bits" , int, layout. size. bits( ) )
521
+ ) ?)
535
522
}
0 commit comments