@@ -46,7 +46,9 @@ crate fn lit_to_const<'tcx>(
46
46
( ast:: LitKind :: Int ( n, _) , ty:: Uint ( _) ) | ( ast:: LitKind :: Int ( n, _) , ty:: Int ( _) ) => {
47
47
trunc ( if neg { ( * n as i128 ) . overflowing_neg ( ) . 0 as u128 } else { * n } ) ?
48
48
}
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
+ }
50
52
( ast:: LitKind :: Bool ( b) , ty:: Bool ) => ConstValue :: Scalar ( Scalar :: from_bool ( * b) ) ,
51
53
( ast:: LitKind :: Char ( c) , ty:: Char ) => ConstValue :: Scalar ( Scalar :: from_char ( * c) ) ,
52
54
( ast:: LitKind :: Err ( _) , _) => return Err ( LitToConstError :: Reported ) ,
@@ -55,14 +57,15 @@ crate fn lit_to_const<'tcx>(
55
57
Ok ( ty:: Const :: from_value ( tcx, lit, ty) )
56
58
}
57
59
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 > > {
59
61
let num = num. as_str ( ) ;
60
62
use rustc_apfloat:: ieee:: { Double , Single } ;
61
63
let scalar = match fty {
62
64
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
+ } ;
66
69
let mut f = num. parse :: < Single > ( ) . unwrap_or_else ( |e| {
67
70
panic ! ( "apfloat::ieee::Single failed to parse `{}`: {:?}" , num, e)
68
71
} ) ;
@@ -82,9 +85,10 @@ fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> ConstValue<'tc
82
85
Scalar :: from_f32 ( f)
83
86
}
84
87
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
+ } ;
88
92
let mut f = num. parse :: < Double > ( ) . unwrap_or_else ( |e| {
89
93
panic ! ( "apfloat::ieee::Double failed to parse `{}`: {:?}" , num, e)
90
94
} ) ;
@@ -105,5 +109,5 @@ fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> ConstValue<'tc
105
109
}
106
110
} ;
107
111
108
- ConstValue :: Scalar ( scalar)
112
+ Some ( ConstValue :: Scalar ( scalar) )
109
113
}
0 commit comments