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