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