Skip to content

Commit c322a74

Browse files
authored
Merge pull request #2439 from gnieto/fix/cterror
Fix ICE comparing `ExprRange` equality
2 parents 503a633 + bcf2e41 commit c322a74

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

clippy_lints/src/consts.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,18 @@ pub fn constant_simple(lcx: &LateContext, e: &Expr) -> Option<Constant> {
229229
constant(lcx, e).and_then(|(cst, res)| if res { None } else { Some(cst) })
230230
}
231231

232-
struct ConstEvalLateContext<'a, 'tcx: 'a> {
232+
/// Creates a ConstEvalLateContext from the given LateContext and TypeckTables
233+
pub fn constant_context<'c, 'cc>(lcx: &LateContext<'c, 'cc>, tables: &'cc ty::TypeckTables<'cc>) -> ConstEvalLateContext<'c, 'cc> {
234+
ConstEvalLateContext {
235+
tcx: lcx.tcx,
236+
tables,
237+
param_env: lcx.param_env,
238+
needed_resolution: false,
239+
substs: lcx.tcx.intern_substs(&[]),
240+
}
241+
}
242+
243+
pub struct ConstEvalLateContext<'a, 'tcx: 'a> {
233244
tcx: TyCtxt<'a, 'tcx, 'tcx>,
234245
tables: &'a ty::TypeckTables<'tcx>,
235246
param_env: ty::ParamEnv<'tcx>,
@@ -239,7 +250,7 @@ struct ConstEvalLateContext<'a, 'tcx: 'a> {
239250

240251
impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
241252
/// simple constant folding: Insert an expression, get a constant or none.
242-
fn expr(&mut self, e: &Expr) -> Option<Constant> {
253+
pub fn expr(&mut self, e: &Expr) -> Option<Constant> {
243254
match e.node {
244255
ExprPath(ref qpath) => self.fetch_path(qpath, e.hir_id),
245256
ExprBlock(ref block) => self.block(block),

clippy_lints/src/utils/hir_utils.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use consts::constant;
1+
use consts::{constant, constant_context};
22
use rustc::lint::*;
33
use rustc::hir::*;
44
use std::hash::{Hash, Hasher};
@@ -117,8 +117,12 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
117117
!self.ignore_fn && l_path == r_path && self.eq_exprs(l_args, r_args)
118118
},
119119
(&ExprRepeat(ref le, ll_id), &ExprRepeat(ref re, rl_id)) => {
120-
self.eq_expr(le, re)
121-
&& self.eq_expr(&self.cx.tcx.hir.body(ll_id).value, &self.cx.tcx.hir.body(rl_id).value)
120+
let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(ll_id));
121+
let ll = celcx.expr(&self.cx.tcx.hir.body(ll_id).value);
122+
let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(rl_id));
123+
let rl = celcx.expr(&self.cx.tcx.hir.body(rl_id).value);
124+
125+
self.eq_expr(le, re) && ll == rl
122126
},
123127
(&ExprRet(ref l), &ExprRet(ref r)) => both(l, r, |l, r| self.eq_expr(l, r)),
124128
(&ExprPath(ref l), &ExprPath(ref r)) => self.eq_qpath(l, r),

tests/ui/copies.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,18 @@ fn ifs_same_cond() {
396396
}
397397

398398
fn main() {}
399+
400+
// Issue #2423. This was causing an ICE
401+
fn func() {
402+
if true {
403+
f(&[0; 62]);
404+
f(&[0; 4]);
405+
f(&[0; 3]);
406+
} else {
407+
f(&[0; 62]);
408+
f(&[0; 6]);
409+
f(&[0; 6]);
410+
}
411+
}
412+
413+
fn f(val: &[u8]) {}

0 commit comments

Comments
 (0)