Skip to content

Commit ed885dc

Browse files
committed
Fix ICE for issues 2767, 2499, 1782
1 parent 6eb07cc commit ed885dc

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

clippy_lints/src/double_comparison.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<'a, 'tcx> DoubleComparisonPass {
5252
}
5353
_ => return,
5454
};
55-
let spanless_eq = SpanlessEq::new(cx).ignore_fn();
55+
let mut spanless_eq = SpanlessEq::new(cx).ignore_fn();
5656
if !(spanless_eq.eq_expr(&llhs, &rlhs) && spanless_eq.eq_expr(&lrhs, &rrhs)) {
5757
return;
5858
}

clippy_lints/src/utils/hir_utils.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
4141
}
4242

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

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

66-
pub fn eq_expr(&self, left: &Expr, right: &Expr) -> bool {
66+
pub fn eq_expr(&mut self, left: &Expr, right: &Expr) -> bool {
6767
if self.ignore_fn && differing_macro_contexts(left.span, right.span) {
6868
return false;
6969
}
@@ -144,20 +144,20 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
144144
}
145145
}
146146

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

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

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

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

187-
fn eq_qpath(&self, left: &QPath, right: &QPath) -> bool {
187+
fn eq_qpath(&mut self, left: &QPath, right: &QPath) -> bool {
188188
match (left, right) {
189189
(&QPath::Resolved(ref lty, ref lpath), &QPath::Resolved(ref rty, ref rpath)) => {
190190
both(lty, rty, |l, r| self.eq_ty(l, r)) && self.eq_path(lpath, rpath)
@@ -196,12 +196,12 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
196196
}
197197
}
198198

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

204-
fn eq_path_parameters(&self, left: &PathParameters, right: &PathParameters) -> bool {
204+
fn eq_path_parameters(&mut self, left: &PathParameters, right: &PathParameters) -> bool {
205205
if !(left.parenthesized || right.parenthesized) {
206206
over(&left.lifetimes, &right.lifetimes, |l, r| self.eq_lifetime(l, r))
207207
&& over(&left.types, &right.types, |l, r| self.eq_ty(l, r))
@@ -218,7 +218,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
218218
}
219219
}
220220

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

234-
fn eq_ty(&self, left: &Ty, right: &Ty) -> bool {
234+
fn eq_ty(&mut self, left: &Ty, right: &Ty) -> bool {
235235
match (&left.node, &right.node) {
236236
(&TySlice(ref l_vec), &TySlice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
237237
(&TyArray(ref lt, ll_id), &TyArray(ref rt, rl_id)) => {
238-
self.eq_ty(lt, rt)
239-
&& self.eq_expr(&self.cx.tcx.hir.body(ll_id).value, &self.cx.tcx.hir.body(rl_id).value)
238+
let full_table = self.tables;
239+
240+
let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(ll_id));
241+
self.tables = self.cx.tcx.body_tables(ll_id);
242+
let ll = celcx.expr(&self.cx.tcx.hir.body(ll_id).value);
243+
244+
let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(rl_id));
245+
self.tables = self.cx.tcx.body_tables(rl_id);
246+
let rl = celcx.expr(&self.cx.tcx.hir.body(rl_id).value);
247+
248+
let eq_ty = self.eq_ty(lt, rt);
249+
self.tables = full_table;
250+
eq_ty && ll == rl
240251
},
241252
(&TyPtr(ref l_mut), &TyPtr(ref r_mut)) => l_mut.mutbl == r_mut.mutbl && self.eq_ty(&*l_mut.ty, &*r_mut.ty),
242253
(&TyRptr(_, ref l_rmut), &TyRptr(_, ref r_rmut)) => {
@@ -249,7 +260,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
249260
}
250261
}
251262

252-
fn eq_type_binding(&self, left: &TypeBinding, right: &TypeBinding) -> bool {
263+
fn eq_type_binding(&mut self, left: &TypeBinding, right: &TypeBinding) -> bool {
253264
left.name == right.name && self.eq_ty(&left.ty, &right.ty)
254265
}
255266
}
@@ -467,8 +478,10 @@ impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
467478
let c: fn(_, _) -> _ = ExprRepeat;
468479
c.hash(&mut self.s);
469480
self.hash_expr(e);
481+
let full_table = self.tables;
470482
self.tables = self.cx.tcx.body_tables(l_id);
471483
self.hash_expr(&self.cx.tcx.hir.body(l_id).value);
484+
self.tables = full_table;
472485
},
473486
ExprRet(ref e) => {
474487
let c: fn(_) -> _ = ExprRet;

tests/run-pass/ice-2499.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(dead_code)]
1+
#![allow(dead_code, char_lit_as_u8, needless_bool)]
22

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

1010
match t {
1111
'E' | 'W' => {}
12-
'T' => if &s[0..(0 + 4)] != &['0' as u8; 4] {
12+
'T' => if s[0..4] != ['0' as u8; 4] {
1313
return false;
1414
} else {
1515
return true;

0 commit comments

Comments
 (0)