Fix consts.rs

This commit is contained in:
Manish Goregaokar 2019-05-10 19:37:37 -07:00
parent c9ed92ce20
commit 26ebc3e9a1

View file

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