@@ -501,35 +501,22 @@ fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
501501 Ok ( & OsStr :: new ( s) )
502502}
503503
504- // FIXME: change `ImmTy::from_int` so it returns an `InterpResult` instead and remove this
505- // function.
506504pub fn immty_from_int_checked < ' tcx > (
507505 int : impl Into < i128 > ,
508506 layout : TyLayout < ' tcx > ,
509507) -> InterpResult < ' tcx , ImmTy < ' tcx , Tag > > {
510508 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+ ) ?)
519512}
520513
521- // FIXME: change `ImmTy::from_uint` so it returns an `InterpResult` instead and remove this
522- // function.
523514pub fn immty_from_uint_checked < ' tcx > (
524515 int : impl Into < u128 > ,
525516 layout : TyLayout < ' tcx > ,
526517) -> InterpResult < ' tcx , ImmTy < ' tcx , Tag > > {
527518 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+ ) ?)
535522}
0 commit comments