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