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