1
1
#![ allow( clippy:: float_cmp) ]
2
2
3
- use crate :: utils:: { clip, sext, unsext} ;
3
+ use crate :: utils:: { higher , clip, sext, unsext} ;
4
4
use if_chain:: if_chain;
5
5
use rustc:: hir:: def:: { DefKind , Res } ;
6
6
use rustc:: hir:: * ;
@@ -15,7 +15,6 @@ use std::convert::TryFrom;
15
15
use std:: convert:: TryInto ;
16
16
use std:: hash:: { Hash , Hasher } ;
17
17
use syntax:: ast:: { FloatTy , LitKind } ;
18
- use syntax:: ptr:: P ;
19
18
use syntax_pos:: symbol:: { LocalInternedString , Symbol } ;
20
19
21
20
/// A `LitKind`-like enum to fold constant `Expr`s into.
@@ -222,10 +221,12 @@ pub struct ConstEvalLateContext<'a, 'tcx: 'a> {
222
221
impl < ' c , ' cc > ConstEvalLateContext < ' c , ' cc > {
223
222
/// Simple constant folding: Insert an expression, get a constant or none.
224
223
pub fn expr ( & mut self , e : & Expr ) -> Option < Constant > {
224
+ if let Some ( ( ref cond, ref then, otherwise) ) = higher:: if_block ( & e) {
225
+ return self . ifthenelse ( cond, then, otherwise) ;
226
+ }
225
227
match e. node {
226
228
ExprKind :: Path ( ref qpath) => self . fetch_path ( qpath, e. hir_id ) ,
227
229
ExprKind :: Block ( ref block, _) => self . block ( block) ,
228
- ExprKind :: If ( ref cond, ref then, ref otherwise) => self . ifthenelse ( cond, then, otherwise) ,
229
230
ExprKind :: Lit ( ref lit) => Some ( lit_to_constant ( & lit. node , self . tables . expr_ty ( e) ) ) ,
230
231
ExprKind :: Array ( ref vec) => self . multi ( vec) . map ( Constant :: Vec ) ,
231
232
ExprKind :: Tup ( ref tup) => self . multi ( tup) . map ( Constant :: Tuple ) ,
@@ -358,10 +359,10 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
358
359
}
359
360
}
360
361
361
- fn ifthenelse ( & mut self , cond : & Expr , then : & P < Expr > , otherwise : & Option < P < Expr > > ) -> Option < Constant > {
362
+ fn ifthenelse ( & mut self , cond : & Expr , then : & Expr , otherwise : Option < & Expr > ) -> Option < Constant > {
362
363
if let Some ( Constant :: Bool ( b) ) = self . expr ( cond) {
363
364
if b {
364
- self . expr ( & * * then)
365
+ self . expr ( & * then)
365
366
} else {
366
367
otherwise. as_ref ( ) . and_then ( |expr| self . expr ( expr) )
367
368
}
0 commit comments