11#![ allow( clippy:: float_cmp) ]
22
3- use crate :: utils:: { clip, sext, unsext} ;
3+ use crate :: utils:: { clip, higher , sext, unsext} ;
44use if_chain:: if_chain;
55use rustc:: hir:: def:: { DefKind , Res } ;
66use rustc:: hir:: * ;
@@ -15,7 +15,6 @@ use std::convert::TryFrom;
1515use std:: convert:: TryInto ;
1616use std:: hash:: { Hash , Hasher } ;
1717use syntax:: ast:: { FloatTy , LitKind } ;
18- use syntax:: ptr:: P ;
1918use syntax_pos:: symbol:: { LocalInternedString , Symbol } ;
2019
2120/// A `LitKind`-like enum to fold constant `Expr`s into.
@@ -222,10 +221,12 @@ pub struct ConstEvalLateContext<'a, 'tcx: 'a> {
222221impl < ' c , ' cc > ConstEvalLateContext < ' c , ' cc > {
223222 /// Simple constant folding: Insert an expression, get a constant or none.
224223 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+ }
225227 match e. node {
226228 ExprKind :: Path ( ref qpath) => self . fetch_path ( qpath, e. hir_id ) ,
227229 ExprKind :: Block ( ref block, _) => self . block ( block) ,
228- ExprKind :: If ( ref cond, ref then, ref otherwise) => self . ifthenelse ( cond, then, otherwise) ,
229230 ExprKind :: Lit ( ref lit) => Some ( lit_to_constant ( & lit. node , self . tables . expr_ty ( e) ) ) ,
230231 ExprKind :: Array ( ref vec) => self . multi ( vec) . map ( Constant :: Vec ) ,
231232 ExprKind :: Tup ( ref tup) => self . multi ( tup) . map ( Constant :: Tuple ) ,
@@ -358,10 +359,10 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
358359 }
359360 }
360361
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 > {
362363 if let Some ( Constant :: Bool ( b) ) = self . expr ( cond) {
363364 if b {
364- self . expr ( & * * then)
365+ self . expr ( & * then)
365366 } else {
366367 otherwise. as_ref ( ) . and_then ( |expr| self . expr ( expr) )
367368 }
0 commit comments