]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/hir_utils.rs
Check pattern equality while checking declaration equality
[rust.git] / clippy_lints / src / utils / hir_utils.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 use crate::consts::{constant_context, constant_simple};
11 use crate::utils::differing_macro_contexts;
12 use rustc::hir::*;
13 use rustc::lint::LateContext;
14 use rustc::ty::TypeckTables;
15 use std::collections::hash_map::DefaultHasher;
16 use std::hash::{Hash, Hasher};
17 use syntax::ast::Name;
18 use syntax::ptr::P;
19
20 /// Type used to check whether two ast are the same. This is different from the
21 /// operator
22 /// `==` on ast types as this operator would compare true equality with ID and
23 /// span.
24 ///
25 /// Note that some expressions kinds are not considered but could be added.
26 pub struct SpanlessEq<'a, 'tcx: 'a> {
27     /// Context used to evaluate constant expressions.
28     cx: &'a LateContext<'a, 'tcx>,
29     tables: &'a TypeckTables<'tcx>,
30     /// If is true, never consider as equal expressions containing function
31     /// calls.
32     ignore_fn: bool,
33 }
34
35 impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
36     pub fn new(cx: &'a LateContext<'a, 'tcx>) -> Self {
37         Self {
38             cx,
39             tables: cx.tables,
40             ignore_fn: false,
41         }
42     }
43
44     pub fn ignore_fn(self) -> Self {
45         Self {
46             cx: self.cx,
47             tables: self.cx.tables,
48             ignore_fn: true,
49         }
50     }
51
52     /// Check whether two statements are the same.
53     pub fn eq_stmt(&mut self, left: &Stmt, right: &Stmt) -> bool {
54         match (&left.node, &right.node) {
55             (&StmtKind::Decl(ref l, _), &StmtKind::Decl(ref r, _)) => {
56                 if let (&DeclKind::Local(ref l), &DeclKind::Local(ref r)) = (&l.node, &r.node) {
57                     self.eq_pat(&l.pat, &r.pat)
58                         && both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r))
59                         && both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
60                 } else {
61                     false
62                 }
63             },
64             (&StmtKind::Expr(ref l, _), &StmtKind::Expr(ref r, _))
65             | (&StmtKind::Semi(ref l, _), &StmtKind::Semi(ref r, _)) => self.eq_expr(l, r),
66             _ => false,
67         }
68     }
69
70     /// Check whether two blocks are the same.
71     pub fn eq_block(&mut self, left: &Block, right: &Block) -> bool {
72         over(&left.stmts, &right.stmts, |l, r| self.eq_stmt(l, r))
73             && both(&left.expr, &right.expr, |l, r| self.eq_expr(l, r))
74     }
75
76     #[allow(clippy::similar_names)]
77     pub fn eq_expr(&mut self, left: &Expr, right: &Expr) -> bool {
78         if self.ignore_fn && differing_macro_contexts(left.span, right.span) {
79             return false;
80         }
81
82         if let (Some(l), Some(r)) = (
83             constant_simple(self.cx, self.tables, left),
84             constant_simple(self.cx, self.tables, right),
85         ) {
86             if l == r {
87                 return true;
88             }
89         }
90
91         match (&left.node, &right.node) {
92             (&ExprKind::AddrOf(l_mut, ref le), &ExprKind::AddrOf(r_mut, ref re)) => {
93                 l_mut == r_mut && self.eq_expr(le, re)
94             },
95             (&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
96                 both(&li.label, &ri.label, |l, r| l.ident.as_str() == r.ident.as_str())
97             },
98             (&ExprKind::Assign(ref ll, ref lr), &ExprKind::Assign(ref rl, ref rr)) => {
99                 self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
100             },
101             (&ExprKind::AssignOp(ref lo, ref ll, ref lr), &ExprKind::AssignOp(ref ro, ref rl, ref rr)) => {
102                 lo.node == ro.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
103             },
104             (&ExprKind::Block(ref l, _), &ExprKind::Block(ref r, _)) => self.eq_block(l, r),
105             (&ExprKind::Binary(l_op, ref ll, ref lr), &ExprKind::Binary(r_op, ref rl, ref rr)) => {
106                 l_op.node == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
107                     || swap_binop(l_op.node, ll, lr).map_or(false, |(l_op, ll, lr)| {
108                         l_op == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
109                     })
110             },
111             (&ExprKind::Break(li, ref le), &ExprKind::Break(ri, ref re)) => {
112                 both(&li.label, &ri.label, |l, r| l.ident.as_str() == r.ident.as_str())
113                     && both(le, re, |l, r| self.eq_expr(l, r))
114             },
115             (&ExprKind::Box(ref l), &ExprKind::Box(ref r)) => self.eq_expr(l, r),
116             (&ExprKind::Call(ref l_fun, ref l_args), &ExprKind::Call(ref r_fun, ref r_args)) => {
117                 !self.ignore_fn && self.eq_expr(l_fun, r_fun) && self.eq_exprs(l_args, r_args)
118             },
119             (&ExprKind::Cast(ref lx, ref lt), &ExprKind::Cast(ref rx, ref rt))
120             | (&ExprKind::Type(ref lx, ref lt), &ExprKind::Type(ref rx, ref rt)) => {
121                 self.eq_expr(lx, rx) && self.eq_ty(lt, rt)
122             },
123             (&ExprKind::Field(ref l_f_exp, ref l_f_ident), &ExprKind::Field(ref r_f_exp, ref r_f_ident)) => {
124                 l_f_ident.name == r_f_ident.name && self.eq_expr(l_f_exp, r_f_exp)
125             },
126             (&ExprKind::Index(ref la, ref li), &ExprKind::Index(ref ra, ref ri)) => {
127                 self.eq_expr(la, ra) && self.eq_expr(li, ri)
128             },
129             (&ExprKind::If(ref lc, ref lt, ref le), &ExprKind::If(ref rc, ref rt, ref re)) => {
130                 self.eq_expr(lc, rc) && self.eq_expr(&**lt, &**rt) && both(le, re, |l, r| self.eq_expr(l, r))
131             },
132             (&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node,
133             (&ExprKind::Loop(ref lb, ref ll, ref lls), &ExprKind::Loop(ref rb, ref rl, ref rls)) => {
134                 lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.as_str() == r.ident.as_str())
135             },
136             (&ExprKind::Match(ref le, ref la, ref ls), &ExprKind::Match(ref re, ref ra, ref rs)) => {
137                 ls == rs
138                     && self.eq_expr(le, re)
139                     && over(la, ra, |l, r| {
140                         self.eq_expr(&l.body, &r.body)
141                             && both(&l.guard, &r.guard, |l, r| self.eq_guard(l, r))
142                             && over(&l.pats, &r.pats, |l, r| self.eq_pat(l, r))
143                     })
144             },
145             (&ExprKind::MethodCall(ref l_path, _, ref l_args), &ExprKind::MethodCall(ref r_path, _, ref r_args)) => {
146                 !self.ignore_fn && self.eq_path_segment(l_path, r_path) && self.eq_exprs(l_args, r_args)
147             },
148             (&ExprKind::Repeat(ref le, ref ll_id), &ExprKind::Repeat(ref re, ref rl_id)) => {
149                 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(ll_id.body));
150                 let ll = celcx.expr(&self.cx.tcx.hir().body(ll_id.body).value);
151                 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(rl_id.body));
152                 let rl = celcx.expr(&self.cx.tcx.hir().body(rl_id.body).value);
153
154                 self.eq_expr(le, re) && ll == rl
155             },
156             (&ExprKind::Ret(ref l), &ExprKind::Ret(ref r)) => both(l, r, |l, r| self.eq_expr(l, r)),
157             (&ExprKind::Path(ref l), &ExprKind::Path(ref r)) => self.eq_qpath(l, r),
158             (&ExprKind::Struct(ref l_path, ref lf, ref lo), &ExprKind::Struct(ref r_path, ref rf, ref ro)) => {
159                 self.eq_qpath(l_path, r_path)
160                     && both(lo, ro, |l, r| self.eq_expr(l, r))
161                     && over(lf, rf, |l, r| self.eq_field(l, r))
162             },
163             (&ExprKind::Tup(ref l_tup), &ExprKind::Tup(ref r_tup)) => self.eq_exprs(l_tup, r_tup),
164             (&ExprKind::Unary(l_op, ref le), &ExprKind::Unary(r_op, ref re)) => l_op == r_op && self.eq_expr(le, re),
165             (&ExprKind::Array(ref l), &ExprKind::Array(ref r)) => self.eq_exprs(l, r),
166             (&ExprKind::While(ref lc, ref lb, ref ll), &ExprKind::While(ref rc, ref rb, ref rl)) => {
167                 self.eq_expr(lc, rc)
168                     && self.eq_block(lb, rb)
169                     && both(ll, rl, |l, r| l.ident.as_str() == r.ident.as_str())
170             },
171             _ => false,
172         }
173     }
174
175     fn eq_exprs(&mut self, left: &P<[Expr]>, right: &P<[Expr]>) -> bool {
176         over(left, right, |l, r| self.eq_expr(l, r))
177     }
178
179     fn eq_field(&mut self, left: &Field, right: &Field) -> bool {
180         left.ident.name == right.ident.name && self.eq_expr(&left.expr, &right.expr)
181     }
182
183     fn eq_guard(&mut self, left: &Guard, right: &Guard) -> bool {
184         match (left, right) {
185             (Guard::If(l), Guard::If(r)) => self.eq_expr(l, r),
186         }
187     }
188
189     fn eq_generic_arg(&mut self, left: &GenericArg, right: &GenericArg) -> bool {
190         match (left, right) {
191             (GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => self.eq_lifetime(l_lt, r_lt),
192             (GenericArg::Type(l_ty), GenericArg::Type(r_ty)) => self.eq_ty(l_ty, r_ty),
193             _ => false,
194         }
195     }
196
197     fn eq_lifetime(&mut self, left: &Lifetime, right: &Lifetime) -> bool {
198         left.name == right.name
199     }
200
201     /// Check whether two patterns are the same.
202     pub fn eq_pat(&mut self, left: &Pat, right: &Pat) -> bool {
203         match (&left.node, &right.node) {
204             (&PatKind::Box(ref l), &PatKind::Box(ref r)) => self.eq_pat(l, r),
205             (&PatKind::TupleStruct(ref lp, ref la, ls), &PatKind::TupleStruct(ref rp, ref ra, rs)) => {
206                 self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat(l, r)) && ls == rs
207             },
208             (&PatKind::Binding(ref lb, _, ref li, ref lp), &PatKind::Binding(ref rb, _, ref ri, ref rp)) => {
209                 lb == rb && li.name.as_str() == ri.name.as_str() && both(lp, rp, |l, r| self.eq_pat(l, r))
210             },
211             (&PatKind::Path(ref l), &PatKind::Path(ref r)) => self.eq_qpath(l, r),
212             (&PatKind::Lit(ref l), &PatKind::Lit(ref r)) => self.eq_expr(l, r),
213             (&PatKind::Tuple(ref l, ls), &PatKind::Tuple(ref r, rs)) => {
214                 ls == rs && over(l, r, |l, r| self.eq_pat(l, r))
215             },
216             (&PatKind::Range(ref ls, ref le, ref li), &PatKind::Range(ref rs, ref re, ref ri)) => {
217                 self.eq_expr(ls, rs) && self.eq_expr(le, re) && (*li == *ri)
218             },
219             (&PatKind::Ref(ref le, ref lm), &PatKind::Ref(ref re, ref rm)) => lm == rm && self.eq_pat(le, re),
220             (&PatKind::Slice(ref ls, ref li, ref le), &PatKind::Slice(ref rs, ref ri, ref re)) => {
221                 over(ls, rs, |l, r| self.eq_pat(l, r))
222                     && over(le, re, |l, r| self.eq_pat(l, r))
223                     && both(li, ri, |l, r| self.eq_pat(l, r))
224             },
225             (&PatKind::Wild, &PatKind::Wild) => true,
226             _ => false,
227         }
228     }
229
230     #[allow(clippy::similar_names)]
231     fn eq_qpath(&mut self, left: &QPath, right: &QPath) -> bool {
232         match (left, right) {
233             (&QPath::Resolved(ref lty, ref lpath), &QPath::Resolved(ref rty, ref rpath)) => {
234                 both(lty, rty, |l, r| self.eq_ty(l, r)) && self.eq_path(lpath, rpath)
235             },
236             (&QPath::TypeRelative(ref lty, ref lseg), &QPath::TypeRelative(ref rty, ref rseg)) => {
237                 self.eq_ty(lty, rty) && self.eq_path_segment(lseg, rseg)
238             },
239             _ => false,
240         }
241     }
242
243     fn eq_path(&mut self, left: &Path, right: &Path) -> bool {
244         left.is_global() == right.is_global()
245             && over(&left.segments, &right.segments, |l, r| self.eq_path_segment(l, r))
246     }
247
248     fn eq_path_parameters(&mut self, left: &GenericArgs, right: &GenericArgs) -> bool {
249         if !(left.parenthesized || right.parenthesized) {
250             over(&left.args, &right.args, |l, r| self.eq_generic_arg(l, r)) // FIXME(flip1995): may not work
251                 && over(&left.bindings, &right.bindings, |l, r| self.eq_type_binding(l, r))
252         } else if left.parenthesized && right.parenthesized {
253             over(left.inputs(), right.inputs(), |l, r| self.eq_ty(l, r))
254                 && both(&Some(&left.bindings[0].ty), &Some(&right.bindings[0].ty), |l, r| {
255                     self.eq_ty(l, r)
256                 })
257         } else {
258             false
259         }
260     }
261
262     pub fn eq_path_segments(&mut self, left: &[PathSegment], right: &[PathSegment]) -> bool {
263         left.len() == right.len() && left.iter().zip(right).all(|(l, r)| self.eq_path_segment(l, r))
264     }
265
266     pub fn eq_path_segment(&mut self, left: &PathSegment, right: &PathSegment) -> bool {
267         // The == of idents doesn't work with different contexts,
268         // we have to be explicit about hygiene
269         if left.ident.as_str() != right.ident.as_str() {
270             return false;
271         }
272         match (&left.args, &right.args) {
273             (&None, &None) => true,
274             (&Some(ref l), &Some(ref r)) => self.eq_path_parameters(l, r),
275             _ => false,
276         }
277     }
278
279     pub fn eq_ty(&mut self, left: &Ty, right: &Ty) -> bool {
280         self.eq_ty_kind(&left.node, &right.node)
281     }
282
283     #[allow(clippy::similar_names)]
284     pub fn eq_ty_kind(&mut self, left: &TyKind, right: &TyKind) -> bool {
285         match (left, right) {
286             (&TyKind::Slice(ref l_vec), &TyKind::Slice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
287             (&TyKind::Array(ref lt, ref ll_id), &TyKind::Array(ref rt, ref rl_id)) => {
288                 let full_table = self.tables;
289
290                 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(ll_id.body));
291                 self.tables = self.cx.tcx.body_tables(ll_id.body);
292                 let ll = celcx.expr(&self.cx.tcx.hir().body(ll_id.body).value);
293
294                 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(rl_id.body));
295                 self.tables = self.cx.tcx.body_tables(rl_id.body);
296                 let rl = celcx.expr(&self.cx.tcx.hir().body(rl_id.body).value);
297
298                 let eq_ty = self.eq_ty(lt, rt);
299                 self.tables = full_table;
300                 eq_ty && ll == rl
301             },
302             (&TyKind::Ptr(ref l_mut), &TyKind::Ptr(ref r_mut)) => {
303                 l_mut.mutbl == r_mut.mutbl && self.eq_ty(&*l_mut.ty, &*r_mut.ty)
304             },
305             (&TyKind::Rptr(_, ref l_rmut), &TyKind::Rptr(_, ref r_rmut)) => {
306                 l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(&*l_rmut.ty, &*r_rmut.ty)
307             },
308             (&TyKind::Path(ref l), &TyKind::Path(ref r)) => self.eq_qpath(l, r),
309             (&TyKind::Tup(ref l), &TyKind::Tup(ref r)) => over(l, r, |l, r| self.eq_ty(l, r)),
310             (&TyKind::Infer, &TyKind::Infer) => true,
311             _ => false,
312         }
313     }
314
315     fn eq_type_binding(&mut self, left: &TypeBinding, right: &TypeBinding) -> bool {
316         left.ident.name == right.ident.name && self.eq_ty(&left.ty, &right.ty)
317     }
318 }
319
320 fn swap_binop<'a>(binop: BinOpKind, lhs: &'a Expr, rhs: &'a Expr) -> Option<(BinOpKind, &'a Expr, &'a Expr)> {
321     match binop {
322         BinOpKind::Add
323         | BinOpKind::Mul
324         | BinOpKind::Eq
325         | BinOpKind::Ne
326         | BinOpKind::BitAnd
327         | BinOpKind::BitXor
328         | BinOpKind::BitOr => Some((binop, rhs, lhs)),
329         BinOpKind::Lt => Some((BinOpKind::Gt, rhs, lhs)),
330         BinOpKind::Le => Some((BinOpKind::Ge, rhs, lhs)),
331         BinOpKind::Ge => Some((BinOpKind::Le, rhs, lhs)),
332         BinOpKind::Gt => Some((BinOpKind::Lt, rhs, lhs)),
333         BinOpKind::Shl
334         | BinOpKind::Shr
335         | BinOpKind::Rem
336         | BinOpKind::Sub
337         | BinOpKind::Div
338         | BinOpKind::And
339         | BinOpKind::Or => None,
340     }
341 }
342
343 /// Check if the two `Option`s are both `None` or some equal values as per
344 /// `eq_fn`.
345 fn both<X, F>(l: &Option<X>, r: &Option<X>, mut eq_fn: F) -> bool
346 where
347     F: FnMut(&X, &X) -> bool,
348 {
349     l.as_ref()
350         .map_or_else(|| r.is_none(), |x| r.as_ref().map_or(false, |y| eq_fn(x, y)))
351 }
352
353 /// Check if two slices are equal as per `eq_fn`.
354 fn over<X, F>(left: &[X], right: &[X], mut eq_fn: F) -> bool
355 where
356     F: FnMut(&X, &X) -> bool,
357 {
358     left.len() == right.len() && left.iter().zip(right).all(|(x, y)| eq_fn(x, y))
359 }
360
361 /// Type used to hash an ast element. This is different from the `Hash` trait
362 /// on ast types as this
363 /// trait would consider IDs and spans.
364 ///
365 /// All expressions kind are hashed, but some might have a weaker hash.
366 pub struct SpanlessHash<'a, 'tcx: 'a> {
367     /// Context used to evaluate constant expressions.
368     cx: &'a LateContext<'a, 'tcx>,
369     tables: &'a TypeckTables<'tcx>,
370     s: DefaultHasher,
371 }
372
373 impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
374     pub fn new(cx: &'a LateContext<'a, 'tcx>, tables: &'a TypeckTables<'tcx>) -> Self {
375         Self {
376             cx,
377             tables,
378             s: DefaultHasher::new(),
379         }
380     }
381
382     pub fn finish(&self) -> u64 {
383         self.s.finish()
384     }
385
386     pub fn hash_block(&mut self, b: &Block) {
387         for s in &b.stmts {
388             self.hash_stmt(s);
389         }
390
391         if let Some(ref e) = b.expr {
392             self.hash_expr(e);
393         }
394
395         match b.rules {
396             BlockCheckMode::DefaultBlock => 0,
397             BlockCheckMode::UnsafeBlock(_) => 1,
398             BlockCheckMode::PushUnsafeBlock(_) => 2,
399             BlockCheckMode::PopUnsafeBlock(_) => 3,
400         }
401         .hash(&mut self.s);
402     }
403
404     #[allow(clippy::many_single_char_names)]
405     pub fn hash_expr(&mut self, e: &Expr) {
406         if let Some(e) = constant_simple(self.cx, self.tables, e) {
407             return e.hash(&mut self.s);
408         }
409
410         match e.node {
411             ExprKind::AddrOf(m, ref e) => {
412                 let c: fn(_, _) -> _ = ExprKind::AddrOf;
413                 c.hash(&mut self.s);
414                 m.hash(&mut self.s);
415                 self.hash_expr(e);
416             },
417             ExprKind::Continue(i) => {
418                 let c: fn(_) -> _ = ExprKind::Continue;
419                 c.hash(&mut self.s);
420                 if let Some(i) = i.label {
421                     self.hash_name(i.ident.name);
422                 }
423             },
424             ExprKind::Yield(ref e) => {
425                 let c: fn(_) -> _ = ExprKind::Yield;
426                 c.hash(&mut self.s);
427                 self.hash_expr(e);
428             },
429             ExprKind::Assign(ref l, ref r) => {
430                 let c: fn(_, _) -> _ = ExprKind::Assign;
431                 c.hash(&mut self.s);
432                 self.hash_expr(l);
433                 self.hash_expr(r);
434             },
435             ExprKind::AssignOp(ref o, ref l, ref r) => {
436                 let c: fn(_, _, _) -> _ = ExprKind::AssignOp;
437                 c.hash(&mut self.s);
438                 o.hash(&mut self.s);
439                 self.hash_expr(l);
440                 self.hash_expr(r);
441             },
442             ExprKind::Block(ref b, _) => {
443                 let c: fn(_, _) -> _ = ExprKind::Block;
444                 c.hash(&mut self.s);
445                 self.hash_block(b);
446             },
447             ExprKind::Binary(op, ref l, ref r) => {
448                 let c: fn(_, _, _) -> _ = ExprKind::Binary;
449                 c.hash(&mut self.s);
450                 op.node.hash(&mut self.s);
451                 self.hash_expr(l);
452                 self.hash_expr(r);
453             },
454             ExprKind::Break(i, ref j) => {
455                 let c: fn(_, _) -> _ = ExprKind::Break;
456                 c.hash(&mut self.s);
457                 if let Some(i) = i.label {
458                     self.hash_name(i.ident.name);
459                 }
460                 if let Some(ref j) = *j {
461                     self.hash_expr(&*j);
462                 }
463             },
464             ExprKind::Box(ref e) => {
465                 let c: fn(_) -> _ = ExprKind::Box;
466                 c.hash(&mut self.s);
467                 self.hash_expr(e);
468             },
469             ExprKind::Call(ref fun, ref args) => {
470                 let c: fn(_, _) -> _ = ExprKind::Call;
471                 c.hash(&mut self.s);
472                 self.hash_expr(fun);
473                 self.hash_exprs(args);
474             },
475             ExprKind::Cast(ref e, ref _ty) => {
476                 let c: fn(_, _) -> _ = ExprKind::Cast;
477                 c.hash(&mut self.s);
478                 self.hash_expr(e);
479                 // TODO: _ty
480             },
481             ExprKind::Closure(cap, _, eid, _, _) => {
482                 let c: fn(_, _, _, _, _) -> _ = ExprKind::Closure;
483                 c.hash(&mut self.s);
484                 match cap {
485                     CaptureClause::CaptureByValue => 0,
486                     CaptureClause::CaptureByRef => 1,
487                 }
488                 .hash(&mut self.s);
489                 self.hash_expr(&self.cx.tcx.hir().body(eid).value);
490             },
491             ExprKind::Field(ref e, ref f) => {
492                 let c: fn(_, _) -> _ = ExprKind::Field;
493                 c.hash(&mut self.s);
494                 self.hash_expr(e);
495                 self.hash_name(f.name);
496             },
497             ExprKind::Index(ref a, ref i) => {
498                 let c: fn(_, _) -> _ = ExprKind::Index;
499                 c.hash(&mut self.s);
500                 self.hash_expr(a);
501                 self.hash_expr(i);
502             },
503             ExprKind::InlineAsm(..) => {
504                 let c: fn(_, _, _) -> _ = ExprKind::InlineAsm;
505                 c.hash(&mut self.s);
506             },
507             ExprKind::If(ref cond, ref t, ref e) => {
508                 let c: fn(_, _, _) -> _ = ExprKind::If;
509                 c.hash(&mut self.s);
510                 self.hash_expr(cond);
511                 self.hash_expr(&**t);
512                 if let Some(ref e) = *e {
513                     self.hash_expr(e);
514                 }
515             },
516             ExprKind::Lit(ref l) => {
517                 let c: fn(_) -> _ = ExprKind::Lit;
518                 c.hash(&mut self.s);
519                 l.hash(&mut self.s);
520             },
521             ExprKind::Loop(ref b, ref i, _) => {
522                 let c: fn(_, _, _) -> _ = ExprKind::Loop;
523                 c.hash(&mut self.s);
524                 self.hash_block(b);
525                 if let Some(i) = *i {
526                     self.hash_name(i.ident.name);
527                 }
528             },
529             ExprKind::Match(ref e, ref arms, ref s) => {
530                 let c: fn(_, _, _) -> _ = ExprKind::Match;
531                 c.hash(&mut self.s);
532                 self.hash_expr(e);
533
534                 for arm in arms {
535                     // TODO: arm.pat?
536                     if let Some(ref e) = arm.guard {
537                         self.hash_guard(e);
538                     }
539                     self.hash_expr(&arm.body);
540                 }
541
542                 s.hash(&mut self.s);
543             },
544             ExprKind::MethodCall(ref path, ref _tys, ref args) => {
545                 let c: fn(_, _, _) -> _ = ExprKind::MethodCall;
546                 c.hash(&mut self.s);
547                 self.hash_name(path.ident.name);
548                 self.hash_exprs(args);
549             },
550             ExprKind::Repeat(ref e, ref l_id) => {
551                 let c: fn(_, _) -> _ = ExprKind::Repeat;
552                 c.hash(&mut self.s);
553                 self.hash_expr(e);
554                 let full_table = self.tables;
555                 self.tables = self.cx.tcx.body_tables(l_id.body);
556                 self.hash_expr(&self.cx.tcx.hir().body(l_id.body).value);
557                 self.tables = full_table;
558             },
559             ExprKind::Ret(ref e) => {
560                 let c: fn(_) -> _ = ExprKind::Ret;
561                 c.hash(&mut self.s);
562                 if let Some(ref e) = *e {
563                     self.hash_expr(e);
564                 }
565             },
566             ExprKind::Path(ref qpath) => {
567                 let c: fn(_) -> _ = ExprKind::Path;
568                 c.hash(&mut self.s);
569                 self.hash_qpath(qpath);
570             },
571             ExprKind::Struct(ref path, ref fields, ref expr) => {
572                 let c: fn(_, _, _) -> _ = ExprKind::Struct;
573                 c.hash(&mut self.s);
574
575                 self.hash_qpath(path);
576
577                 for f in fields {
578                     self.hash_name(f.ident.name);
579                     self.hash_expr(&f.expr);
580                 }
581
582                 if let Some(ref e) = *expr {
583                     self.hash_expr(e);
584                 }
585             },
586             ExprKind::Tup(ref tup) => {
587                 let c: fn(_) -> _ = ExprKind::Tup;
588                 c.hash(&mut self.s);
589                 self.hash_exprs(tup);
590             },
591             ExprKind::Type(ref e, ref _ty) => {
592                 let c: fn(_, _) -> _ = ExprKind::Type;
593                 c.hash(&mut self.s);
594                 self.hash_expr(e);
595                 // TODO: _ty
596             },
597             ExprKind::Unary(lop, ref le) => {
598                 let c: fn(_, _) -> _ = ExprKind::Unary;
599                 c.hash(&mut self.s);
600
601                 lop.hash(&mut self.s);
602                 self.hash_expr(le);
603             },
604             ExprKind::Array(ref v) => {
605                 let c: fn(_) -> _ = ExprKind::Array;
606                 c.hash(&mut self.s);
607
608                 self.hash_exprs(v);
609             },
610             ExprKind::While(ref cond, ref b, l) => {
611                 let c: fn(_, _, _) -> _ = ExprKind::While;
612                 c.hash(&mut self.s);
613
614                 self.hash_expr(cond);
615                 self.hash_block(b);
616                 if let Some(l) = l {
617                     self.hash_name(l.ident.name);
618                 }
619             },
620             ExprKind::Err => {},
621         }
622     }
623
624     pub fn hash_exprs(&mut self, e: &P<[Expr]>) {
625         for e in e {
626             self.hash_expr(e);
627         }
628     }
629
630     pub fn hash_name(&mut self, n: Name) {
631         n.as_str().hash(&mut self.s);
632     }
633
634     pub fn hash_qpath(&mut self, p: &QPath) {
635         match *p {
636             QPath::Resolved(_, ref path) => {
637                 self.hash_path(path);
638             },
639             QPath::TypeRelative(_, ref path) => {
640                 self.hash_name(path.ident.name);
641             },
642         }
643         // self.cx.tables.qpath_def(p, id).hash(&mut self.s);
644     }
645
646     pub fn hash_path(&mut self, p: &Path) {
647         p.is_global().hash(&mut self.s);
648         for p in &p.segments {
649             self.hash_name(p.ident.name);
650         }
651     }
652
653     pub fn hash_stmt(&mut self, b: &Stmt) {
654         match b.node {
655             StmtKind::Decl(ref decl, _) => {
656                 let c: fn(_, _) -> _ = StmtKind::Decl;
657                 c.hash(&mut self.s);
658
659                 if let DeclKind::Local(ref local) = decl.node {
660                     if let Some(ref init) = local.init {
661                         self.hash_expr(init);
662                     }
663                 }
664             },
665             StmtKind::Expr(ref expr, _) => {
666                 let c: fn(_, _) -> _ = StmtKind::Expr;
667                 c.hash(&mut self.s);
668                 self.hash_expr(expr);
669             },
670             StmtKind::Semi(ref expr, _) => {
671                 let c: fn(_, _) -> _ = StmtKind::Semi;
672                 c.hash(&mut self.s);
673                 self.hash_expr(expr);
674             },
675         }
676     }
677
678     pub fn hash_guard(&mut self, g: &Guard) {
679         match g {
680             Guard::If(ref expr) => {
681                 let c: fn(_) -> _ = Guard::If;
682                 c.hash(&mut self.s);
683                 self.hash_expr(expr);
684             },
685         }
686     }
687 }