Skip to content

Commit

Permalink
Fix ICE for issues 2767, 2499, 1782
Browse files Browse the repository at this point in the history
  • Loading branch information
phansch committed May 17, 2018
1 parent d275d11 commit 373033e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/double_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<'a, 'tcx> DoubleComparisonPass {
}
_ => return,
};
let spanless_eq = SpanlessEq::new(cx).ignore_fn();
let mut spanless_eq = SpanlessEq::new(cx).ignore_fn();
if !(spanless_eq.eq_expr(&llhs, &rlhs) && spanless_eq.eq_expr(&lrhs, &rrhs)) {
return;
}
Expand Down
43 changes: 28 additions & 15 deletions clippy_lints/src/utils/hir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
}

/// Check whether two statements are the same.
pub fn eq_stmt(&self, left: &Stmt, right: &Stmt) -> bool {
pub fn eq_stmt(&mut self, left: &Stmt, right: &Stmt) -> bool {
match (&left.node, &right.node) {
(&StmtDecl(ref l, _), &StmtDecl(ref r, _)) => {
if let (&DeclLocal(ref l), &DeclLocal(ref r)) = (&l.node, &r.node) {
Expand All @@ -70,12 +70,12 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
}

/// Check whether two blocks are the same.
pub fn eq_block(&self, left: &Block, right: &Block) -> bool {
pub fn eq_block(&mut self, left: &Block, right: &Block) -> bool {
over(&left.stmts, &right.stmts, |l, r| self.eq_stmt(l, r))
&& both(&left.expr, &right.expr, |l, r| self.eq_expr(l, r))
}

pub fn eq_expr(&self, left: &Expr, right: &Expr) -> bool {
pub fn eq_expr(&mut self, left: &Expr, right: &Expr) -> bool {
if self.ignore_fn && differing_macro_contexts(left.span, right.span) {
return false;
}
Expand Down Expand Up @@ -156,20 +156,20 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
}
}

fn eq_exprs(&self, left: &P<[Expr]>, right: &P<[Expr]>) -> bool {
fn eq_exprs(&mut self, left: &P<[Expr]>, right: &P<[Expr]>) -> bool {
over(left, right, |l, r| self.eq_expr(l, r))
}

fn eq_field(&self, left: &Field, right: &Field) -> bool {
fn eq_field(&mut self, left: &Field, right: &Field) -> bool {
left.name.node == right.name.node && self.eq_expr(&left.expr, &right.expr)
}

fn eq_lifetime(&self, left: &Lifetime, right: &Lifetime) -> bool {
fn eq_lifetime(&mut self, left: &Lifetime, right: &Lifetime) -> bool {
left.name == right.name
}

/// Check whether two patterns are the same.
pub fn eq_pat(&self, left: &Pat, right: &Pat) -> bool {
pub fn eq_pat(&mut self, left: &Pat, right: &Pat) -> bool {
match (&left.node, &right.node) {
(&PatKind::Box(ref l), &PatKind::Box(ref r)) => self.eq_pat(l, r),
(&PatKind::TupleStruct(ref lp, ref la, ls), &PatKind::TupleStruct(ref rp, ref ra, rs)) => {
Expand All @@ -196,7 +196,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
}
}

fn eq_qpath(&self, left: &QPath, right: &QPath) -> bool {
fn eq_qpath(&mut self, left: &QPath, right: &QPath) -> bool {
match (left, right) {
(&QPath::Resolved(ref lty, ref lpath), &QPath::Resolved(ref rty, ref rpath)) => {
both(lty, rty, |l, r| self.eq_ty(l, r)) && self.eq_path(lpath, rpath)
Expand All @@ -208,12 +208,12 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
}
}

fn eq_path(&self, left: &Path, right: &Path) -> bool {
fn eq_path(&mut self, left: &Path, right: &Path) -> bool {
left.is_global() == right.is_global()
&& over(&left.segments, &right.segments, |l, r| self.eq_path_segment(l, r))
}

fn eq_path_parameters(&self, left: &PathParameters, right: &PathParameters) -> bool {
fn eq_path_parameters(&mut self, left: &PathParameters, right: &PathParameters) -> bool {
if !(left.parenthesized || right.parenthesized) {
over(&left.lifetimes, &right.lifetimes, |l, r| self.eq_lifetime(l, r))
&& over(&left.types, &right.types, |l, r| self.eq_ty(l, r))
Expand All @@ -230,7 +230,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
}
}

fn eq_path_segment(&self, left: &PathSegment, right: &PathSegment) -> bool {
fn eq_path_segment(&mut self, left: &PathSegment, right: &PathSegment) -> bool {
// The == of idents doesn't work with different contexts,
// we have to be explicit about hygiene
if left.name.as_str() != right.name.as_str() {
Expand All @@ -243,12 +243,23 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
}
}

fn eq_ty(&self, left: &Ty, right: &Ty) -> bool {
fn eq_ty(&mut self, left: &Ty, right: &Ty) -> bool {
match (&left.node, &right.node) {
(&TySlice(ref l_vec), &TySlice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
(&TyArray(ref lt, ll_id), &TyArray(ref rt, rl_id)) => {
self.eq_ty(lt, rt)
&& self.eq_expr(&self.cx.tcx.hir.body(ll_id).value, &self.cx.tcx.hir.body(rl_id).value)
let full_table = self.tables;

let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(ll_id));
self.tables = 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));
self.tables = self.cx.tcx.body_tables(rl_id);
let rl = celcx.expr(&self.cx.tcx.hir.body(rl_id).value);

let eq_ty = self.eq_ty(lt, rt);
self.tables = full_table;
eq_ty && ll == rl
},
(&TyPtr(ref l_mut), &TyPtr(ref r_mut)) => l_mut.mutbl == r_mut.mutbl && self.eq_ty(&*l_mut.ty, &*r_mut.ty),
(&TyRptr(_, ref l_rmut), &TyRptr(_, ref r_rmut)) => {
Expand All @@ -261,7 +272,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
}
}

fn eq_type_binding(&self, left: &TypeBinding, right: &TypeBinding) -> bool {
fn eq_type_binding(&mut self, left: &TypeBinding, right: &TypeBinding) -> bool {
left.name == right.name && self.eq_ty(&left.ty, &right.ty)
}
}
Expand Down Expand Up @@ -490,8 +501,10 @@ impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
let c: fn(_, _) -> _ = ExprRepeat;
c.hash(&mut self.s);
self.hash_expr(e);
let full_table = self.tables;
self.tables = self.cx.tcx.body_tables(l_id);
self.hash_expr(&self.cx.tcx.hir.body(l_id).value);
self.tables = full_table;
},
ExprRet(ref e) => {
let c: fn(_) -> _ = ExprRet;
Expand Down
4 changes: 2 additions & 2 deletions tests/run-pass/ice-2499.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(dead_code)]
#![allow(dead_code, char_lit_as_u8, needless_bool)]

/// Should not trigger an ICE in `SpanlessHash` / `consts::constant`
///
Expand All @@ -9,7 +9,7 @@ fn f(s: &[u8]) -> bool {

match t {
'E' | 'W' => {}
'T' => if &s[0..(0 + 4)] != &['0' as u8; 4] {
'T' => if s[0..4] != ['0' as u8; 4] {
return false;
} else {
return true;
Expand Down

0 comments on commit 373033e

Please sign in to comment.