@@ -46,7 +46,9 @@ crate fn lit_to_const<'tcx>(
4646 ( ast:: LitKind :: Int ( n, _) , ty:: Uint ( _) ) | ( ast:: LitKind :: Int ( n, _) , ty:: Int ( _) ) => {
4747 trunc ( if neg { ( * n as i128 ) . overflowing_neg ( ) . 0 as u128 } else { * n } ) ?
4848 }
49- ( ast:: LitKind :: Float ( n, _) , ty:: Float ( fty) ) => parse_float ( * n, * fty, neg) ,
49+ ( ast:: LitKind :: Float ( n, _) , ty:: Float ( fty) ) => {
50+ parse_float ( * n, * fty, neg) . ok_or ( LitToConstError :: Reported ) ?
51+ }
5052 ( ast:: LitKind :: Bool ( b) , ty:: Bool ) => ConstValue :: Scalar ( Scalar :: from_bool ( * b) ) ,
5153 ( ast:: LitKind :: Char ( c) , ty:: Char ) => ConstValue :: Scalar ( Scalar :: from_char ( * c) ) ,
5254 ( ast:: LitKind :: Err ( _) , _) => return Err ( LitToConstError :: Reported ) ,
@@ -55,14 +57,15 @@ crate fn lit_to_const<'tcx>(
5557 Ok ( ty:: Const :: from_value ( tcx, lit, ty) )
5658}
5759
58- fn parse_float < ' tcx > ( num : Symbol , fty : ty:: FloatTy , neg : bool ) -> ConstValue < ' tcx > {
60+ fn parse_float < ' tcx > ( num : Symbol , fty : ty:: FloatTy , neg : bool ) -> Option < ConstValue < ' tcx > > {
5961 let num = num. as_str ( ) ;
6062 use rustc_apfloat:: ieee:: { Double , Single } ;
6163 let scalar = match fty {
6264 ty:: FloatTy :: F32 => {
63- let rust_f = num
64- . parse :: < f32 > ( )
65- . unwrap_or_else ( |e| panic ! ( "f32 failed to parse `{}`: {:?}" , num, e) ) ;
65+ let rust_f = match num. parse :: < f32 > ( ) {
66+ Ok ( f) => f,
67+ Err ( _) => return None ,
68+ } ;
6669 let mut f = num. parse :: < Single > ( ) . unwrap_or_else ( |e| {
6770 panic ! ( "apfloat::ieee::Single failed to parse `{}`: {:?}" , num, e)
6871 } ) ;
@@ -82,9 +85,10 @@ fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> ConstValue<'tc
8285 Scalar :: from_f32 ( f)
8386 }
8487 ty:: FloatTy :: F64 => {
85- let rust_f = num
86- . parse :: < f64 > ( )
87- . unwrap_or_else ( |e| panic ! ( "f64 failed to parse `{}`: {:?}" , num, e) ) ;
88+ let rust_f = match num. parse :: < f64 > ( ) {
89+ Ok ( f) => f,
90+ Err ( _) => return None ,
91+ } ;
8892 let mut f = num. parse :: < Double > ( ) . unwrap_or_else ( |e| {
8993 panic ! ( "apfloat::ieee::Double failed to parse `{}`: {:?}" , num, e)
9094 } ) ;
@@ -105,5 +109,5 @@ fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> ConstValue<'tc
105109 }
106110 } ;
107111
108- ConstValue :: Scalar ( scalar)
112+ Some ( ConstValue :: Scalar ( scalar) )
109113}
0 commit comments