Fix ICE comparing ExprRange

`eq_expr` on hir::utils was throwing an ICE due to an invalid
LateContext being used. Due to this missusage, it was generating an ICE
with the code on the following issue:
https://github.com/rust-lang-nursery/rust-clippy/issues/2423
This commit is contained in:
Guillem Nieto 2018-02-06 00:31:06 +01:00
parent 503a63390d
commit bcf2e41421
3 changed files with 35 additions and 5 deletions

View file

@ -229,7 +229,18 @@ pub fn constant_simple(lcx: &LateContext, e: &Expr) -> Option<Constant> {
constant(lcx, e).and_then(|(cst, res)| if res { None } else { Some(cst) })
}
struct ConstEvalLateContext<'a, 'tcx: 'a> {
/// Creates a ConstEvalLateContext from the given LateContext and TypeckTables
pub fn constant_context<'c, 'cc>(lcx: &LateContext<'c, 'cc>, tables: &'cc ty::TypeckTables<'cc>) -> ConstEvalLateContext<'c, 'cc> {
ConstEvalLateContext {
tcx: lcx.tcx,
tables,
param_env: lcx.param_env,
needed_resolution: false,
substs: lcx.tcx.intern_substs(&[]),
}
}
pub struct ConstEvalLateContext<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
tables: &'a ty::TypeckTables<'tcx>,
param_env: ty::ParamEnv<'tcx>,
@ -239,7 +250,7 @@ struct ConstEvalLateContext<'a, 'tcx: 'a> {
impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
/// simple constant folding: Insert an expression, get a constant or none.
fn expr(&mut self, e: &Expr) -> Option<Constant> {
pub fn expr(&mut self, e: &Expr) -> Option<Constant> {
match e.node {
ExprPath(ref qpath) => self.fetch_path(qpath, e.hir_id),
ExprBlock(ref block) => self.block(block),

View file

@ -1,4 +1,4 @@
use consts::constant;
use consts::{constant, constant_context};
use rustc::lint::*;
use rustc::hir::*;
use std::hash::{Hash, Hasher};
@ -117,8 +117,12 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
!self.ignore_fn && l_path == r_path && self.eq_exprs(l_args, r_args)
},
(&ExprRepeat(ref le, ll_id), &ExprRepeat(ref re, rl_id)) => {
self.eq_expr(le, re)
&& self.eq_expr(&self.cx.tcx.hir.body(ll_id).value, &self.cx.tcx.hir.body(rl_id).value)
let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(ll_id));
let ll = celcx.expr(&self.cx.tcx.hir.body(ll_id).value);
let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(rl_id));
let rl = celcx.expr(&self.cx.tcx.hir.body(rl_id).value);
self.eq_expr(le, re) && ll == rl
},
(&ExprRet(ref l), &ExprRet(ref r)) => both(l, r, |l, r| self.eq_expr(l, r)),
(&ExprPath(ref l), &ExprPath(ref r)) => self.eq_qpath(l, r),

View file

@ -396,3 +396,18 @@ fn ifs_same_cond() {
}
fn main() {}
// Issue #2423. This was causing an ICE
fn func() {
if true {
f(&[0; 62]);
f(&[0; 4]);
f(&[0; 3]);
} else {
f(&[0; 62]);
f(&[0; 6]);
f(&[0; 6]);
}
}
fn f(val: &[u8]) {}