]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/hir_utils.rs
Fix ICE for issues 2767, 2499, 1782
[rust.git] / clippy_lints / src / utils / hir_utils.rs
1 use consts::{constant_simple, constant_context};
2 use rustc::lint::*;
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 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             (&StmtDecl(ref l, _), &StmtDecl(ref r, _)) => {
47                 if let (&DeclLocal(ref l), &DeclLocal(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             (&StmtExpr(ref l, _), &StmtExpr(ref r, _)) | (&StmtSemi(ref l, _), &StmtSemi(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             (&ExprAddrOf(l_mut, ref le), &ExprAddrOf(r_mut, ref re)) => l_mut == r_mut && self.eq_expr(le, re),
79             (&ExprAgain(li), &ExprAgain(ri)) => {
80                 both(&li.label, &ri.label, |l, r| l.name.as_str() == r.name.as_str())
81             },
82             (&ExprAssign(ref ll, ref lr), &ExprAssign(ref rl, ref rr)) => self.eq_expr(ll, rl) && self.eq_expr(lr, rr),
83             (&ExprAssignOp(ref lo, ref ll, ref lr), &ExprAssignOp(ref ro, ref rl, ref rr)) => {
84                 lo.node == ro.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
85             },
86             (&ExprBlock(ref l, _), &ExprBlock(ref r, _)) => self.eq_block(l, r),
87             (&ExprBinary(l_op, ref ll, ref lr), &ExprBinary(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             (&ExprBreak(li, ref le), &ExprBreak(ri, ref re)) => {
94                 both(&li.label, &ri.label, |l, r| l.name.as_str() == r.name.as_str())
95                     && both(le, re, |l, r| self.eq_expr(l, r))
96             },
97             (&ExprBox(ref l), &ExprBox(ref r)) => self.eq_expr(l, r),
98             (&ExprCall(ref l_fun, ref l_args), &ExprCall(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             (&ExprCast(ref lx, ref lt), &ExprCast(ref rx, ref rt)) |
102             (&ExprType(ref lx, ref lt), &ExprType(ref rx, ref rt)) => self.eq_expr(lx, rx) && self.eq_ty(lt, rt),
103             (&ExprField(ref l_f_exp, ref l_f_ident), &ExprField(ref r_f_exp, ref r_f_ident)) => {
104                 l_f_ident.node == r_f_ident.node && self.eq_expr(l_f_exp, r_f_exp)
105             },
106             (&ExprIndex(ref la, ref li), &ExprIndex(ref ra, ref ri)) => self.eq_expr(la, ra) && self.eq_expr(li, ri),
107             (&ExprIf(ref lc, ref lt, ref le), &ExprIf(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             (&ExprLit(ref l), &ExprLit(ref r)) => l.node == r.node,
111             (&ExprLoop(ref lb, ref ll, ref lls), &ExprLoop(ref rb, ref rl, ref rls)) => {
112                 lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.name.as_str() == r.name.as_str())
113             },
114             (&ExprMatch(ref le, ref la, ref ls), &ExprMatch(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             (&ExprMethodCall(ref l_path, _, ref l_args), &ExprMethodCall(ref r_path, _, ref r_args)) => {
121                 !self.ignore_fn && l_path == r_path && self.eq_exprs(l_args, r_args)
122             },
123             (&ExprRepeat(ref le, ll_id), &ExprRepeat(ref re, rl_id)) => {
124                 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(ll_id));
125                 let ll = celcx.expr(&self.cx.tcx.hir.body(ll_id).value);
126                 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(rl_id));
127                 let rl = celcx.expr(&self.cx.tcx.hir.body(rl_id).value);
128
129                 self.eq_expr(le, re) && ll == rl
130             },
131             (&ExprRet(ref l), &ExprRet(ref r)) => both(l, r, |l, r| self.eq_expr(l, r)),
132             (&ExprPath(ref l), &ExprPath(ref r)) => self.eq_qpath(l, r),
133             (&ExprStruct(ref l_path, ref lf, ref lo), &ExprStruct(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             (&ExprTup(ref l_tup), &ExprTup(ref r_tup)) => self.eq_exprs(l_tup, r_tup),
138             (&ExprUnary(l_op, ref le), &ExprUnary(r_op, ref re)) => l_op == r_op && self.eq_expr(le, re),
139             (&ExprArray(ref l), &ExprArray(ref r)) => self.eq_exprs(l, r),
140             (&ExprWhile(ref lc, ref lb, ref ll), &ExprWhile(ref rc, ref rb, ref rl)) => {
141                 self.eq_expr(lc, rc) && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.name.as_str() == r.name.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.name.node == right.name.node && self.eq_expr(&left.expr, &right.expr)
153     }
154
155     fn eq_lifetime(&mut self, left: &Lifetime, right: &Lifetime) -> bool {
156         left.name == right.name
157     }
158
159     /// Check whether two patterns are the same.
160     pub fn eq_pat(&mut self, left: &Pat, right: &Pat) -> bool {
161         match (&left.node, &right.node) {
162             (&PatKind::Box(ref l), &PatKind::Box(ref r)) => self.eq_pat(l, r),
163             (&PatKind::TupleStruct(ref lp, ref la, ls), &PatKind::TupleStruct(ref rp, ref ra, rs)) => {
164                 self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat(l, r)) && ls == rs
165             },
166             (&PatKind::Binding(ref lb, _, ref li, ref lp), &PatKind::Binding(ref rb, _, ref ri, ref rp)) => {
167                 lb == rb && li.node.as_str() == ri.node.as_str() && both(lp, rp, |l, r| self.eq_pat(l, r))
168             },
169             (&PatKind::Path(ref l), &PatKind::Path(ref r)) => self.eq_qpath(l, r),
170             (&PatKind::Lit(ref l), &PatKind::Lit(ref r)) => self.eq_expr(l, r),
171             (&PatKind::Tuple(ref l, ls), &PatKind::Tuple(ref r, rs)) => {
172                 ls == rs && over(l, r, |l, r| self.eq_pat(l, r))
173             },
174             (&PatKind::Range(ref ls, ref le, ref li), &PatKind::Range(ref rs, ref re, ref ri)) => {
175                 self.eq_expr(ls, rs) && self.eq_expr(le, re) && (*li == *ri)
176             },
177             (&PatKind::Ref(ref le, ref lm), &PatKind::Ref(ref re, ref rm)) => lm == rm && self.eq_pat(le, re),
178             (&PatKind::Slice(ref ls, ref li, ref le), &PatKind::Slice(ref rs, ref ri, ref re)) => {
179                 over(ls, rs, |l, r| self.eq_pat(l, r)) && over(le, re, |l, r| self.eq_pat(l, r))
180                     && both(li, ri, |l, r| self.eq_pat(l, r))
181             },
182             (&PatKind::Wild, &PatKind::Wild) => true,
183             _ => false,
184         }
185     }
186
187     fn eq_qpath(&mut self, left: &QPath, right: &QPath) -> bool {
188         match (left, right) {
189             (&QPath::Resolved(ref lty, ref lpath), &QPath::Resolved(ref rty, ref rpath)) => {
190                 both(lty, rty, |l, r| self.eq_ty(l, r)) && self.eq_path(lpath, rpath)
191             },
192             (&QPath::TypeRelative(ref lty, ref lseg), &QPath::TypeRelative(ref rty, ref rseg)) => {
193                 self.eq_ty(lty, rty) && self.eq_path_segment(lseg, rseg)
194             },
195             _ => false,
196         }
197     }
198
199     fn eq_path(&mut self, left: &Path, right: &Path) -> bool {
200         left.is_global() == right.is_global()
201             && over(&left.segments, &right.segments, |l, r| self.eq_path_segment(l, r))
202     }
203
204     fn eq_path_parameters(&mut self, left: &PathParameters, right: &PathParameters) -> bool {
205         if !(left.parenthesized || right.parenthesized) {
206             over(&left.lifetimes, &right.lifetimes, |l, r| self.eq_lifetime(l, r))
207                 && over(&left.types, &right.types, |l, r| self.eq_ty(l, r))
208                 && over(&left.bindings, &right.bindings, |l, r| self.eq_type_binding(l, r))
209         } else if left.parenthesized && right.parenthesized {
210             over(left.inputs(), right.inputs(), |l, r| self.eq_ty(l, r))
211                 && both(
212                     &Some(&left.bindings[0].ty),
213                     &Some(&right.bindings[0].ty),
214                     |l, r| self.eq_ty(l, r),
215                 )
216         } else {
217             false
218         }
219     }
220
221     fn eq_path_segment(&mut self, left: &PathSegment, right: &PathSegment) -> bool {
222         // The == of idents doesn't work with different contexts,
223         // we have to be explicit about hygiene
224         if left.name.as_str() != right.name.as_str() {
225             return false;
226         }
227         match (&left.parameters, &right.parameters) {
228             (&None, &None) => true,
229             (&Some(ref l), &Some(ref r)) => self.eq_path_parameters(l, r),
230             _ => false,
231         }
232     }
233
234     fn eq_ty(&mut self, left: &Ty, right: &Ty) -> bool {
235         match (&left.node, &right.node) {
236             (&TySlice(ref l_vec), &TySlice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
237             (&TyArray(ref lt, ll_id), &TyArray(ref rt, rl_id)) => {
238                 let full_table = self.tables;
239
240                 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(ll_id));
241                 self.tables = self.cx.tcx.body_tables(ll_id);
242                 let ll = celcx.expr(&self.cx.tcx.hir.body(ll_id).value);
243
244                 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(rl_id));
245                 self.tables = self.cx.tcx.body_tables(rl_id);
246                 let rl = celcx.expr(&self.cx.tcx.hir.body(rl_id).value);
247
248                 let eq_ty = self.eq_ty(lt, rt);
249                 self.tables = full_table;
250                 eq_ty && ll == rl
251             },
252             (&TyPtr(ref l_mut), &TyPtr(ref r_mut)) => l_mut.mutbl == r_mut.mutbl && self.eq_ty(&*l_mut.ty, &*r_mut.ty),
253             (&TyRptr(_, ref l_rmut), &TyRptr(_, ref r_rmut)) => {
254                 l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(&*l_rmut.ty, &*r_rmut.ty)
255             },
256             (&TyPath(ref l), &TyPath(ref r)) => self.eq_qpath(l, r),
257             (&TyTup(ref l), &TyTup(ref r)) => over(l, r, |l, r| self.eq_ty(l, r)),
258             (&TyInfer, &TyInfer) => true,
259             _ => false,
260         }
261     }
262
263     fn eq_type_binding(&mut self, left: &TypeBinding, right: &TypeBinding) -> bool {
264         left.name == right.name && self.eq_ty(&left.ty, &right.ty)
265     }
266 }
267
268 fn swap_binop<'a>(binop: BinOp_, lhs: &'a Expr, rhs: &'a Expr) -> Option<(BinOp_, &'a Expr, &'a Expr)> {
269     match binop {
270         BiAdd | BiMul | BiBitXor | BiBitAnd | BiEq | BiNe | BiBitOr => Some((binop, rhs, lhs)),
271         BiLt => Some((BiGt, rhs, lhs)),
272         BiLe => Some((BiGe, rhs, lhs)),
273         BiGe => Some((BiLe, rhs, lhs)),
274         BiGt => Some((BiLt, rhs, lhs)),
275         BiShl | BiShr | BiRem | BiSub | BiDiv | BiAnd | BiOr => None,
276     }
277 }
278
279 /// Check if the two `Option`s are both `None` or some equal values as per
280 /// `eq_fn`.
281 fn both<X, F>(l: &Option<X>, r: &Option<X>, mut eq_fn: F) -> bool
282 where
283     F: FnMut(&X, &X) -> bool,
284 {
285     l.as_ref()
286         .map_or_else(|| r.is_none(), |x| r.as_ref().map_or(false, |y| eq_fn(x, y)))
287 }
288
289 /// Check if two slices are equal as per `eq_fn`.
290 fn over<X, F>(left: &[X], right: &[X], mut eq_fn: F) -> bool
291 where
292     F: FnMut(&X, &X) -> bool,
293 {
294     left.len() == right.len() && left.iter().zip(right).all(|(x, y)| eq_fn(x, y))
295 }
296
297
298 /// Type used to hash an ast element. This is different from the `Hash` trait
299 /// on ast types as this
300 /// trait would consider IDs and spans.
301 ///
302 /// All expressions kind are hashed, but some might have a weaker hash.
303 pub struct SpanlessHash<'a, 'tcx: 'a> {
304     /// Context used to evaluate constant expressions.
305     cx: &'a LateContext<'a, 'tcx>,
306     tables: &'a TypeckTables<'tcx>,
307     s: DefaultHasher,
308 }
309
310 impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
311     pub fn new(cx: &'a LateContext<'a, 'tcx>, tables: &'a TypeckTables<'tcx>) -> Self {
312         Self {
313             cx,
314             tables,
315             s: DefaultHasher::new(),
316         }
317     }
318
319     pub fn finish(&self) -> u64 {
320         self.s.finish()
321     }
322
323     pub fn hash_block(&mut self, b: &Block) {
324         for s in &b.stmts {
325             self.hash_stmt(s);
326         }
327
328         if let Some(ref e) = b.expr {
329             self.hash_expr(e);
330         }
331
332         b.rules.hash(&mut self.s);
333     }
334
335     #[allow(many_single_char_names)]
336     pub fn hash_expr(&mut self, e: &Expr) {
337         if let Some(e) = constant_simple(self.cx, self.tables, e) {
338             return e.hash(&mut self.s);
339         }
340
341         match e.node {
342             ExprAddrOf(m, ref e) => {
343                 let c: fn(_, _) -> _ = ExprAddrOf;
344                 c.hash(&mut self.s);
345                 m.hash(&mut self.s);
346                 self.hash_expr(e);
347             },
348             ExprAgain(i) => {
349                 let c: fn(_) -> _ = ExprAgain;
350                 c.hash(&mut self.s);
351                 if let Some(i) = i.label {
352                     self.hash_name(&i.name);
353                 }
354             },
355             ExprYield(ref e) => {
356                 let c: fn(_) -> _ = ExprYield;
357                 c.hash(&mut self.s);
358                 self.hash_expr(e);
359             },
360             ExprAssign(ref l, ref r) => {
361                 let c: fn(_, _) -> _ = ExprAssign;
362                 c.hash(&mut self.s);
363                 self.hash_expr(l);
364                 self.hash_expr(r);
365             },
366             ExprAssignOp(ref o, ref l, ref r) => {
367                 let c: fn(_, _, _) -> _ = ExprAssignOp;
368                 c.hash(&mut self.s);
369                 o.hash(&mut self.s);
370                 self.hash_expr(l);
371                 self.hash_expr(r);
372             },
373             ExprBlock(ref b, _) => {
374                 let c: fn(_, _) -> _ = ExprBlock;
375                 c.hash(&mut self.s);
376                 self.hash_block(b);
377             },
378             ExprBinary(op, ref l, ref r) => {
379                 let c: fn(_, _, _) -> _ = ExprBinary;
380                 c.hash(&mut self.s);
381                 op.node.hash(&mut self.s);
382                 self.hash_expr(l);
383                 self.hash_expr(r);
384             },
385             ExprBreak(i, ref j) => {
386                 let c: fn(_, _) -> _ = ExprBreak;
387                 c.hash(&mut self.s);
388                 if let Some(i) = i.label {
389                     self.hash_name(&i.name);
390                 }
391                 if let Some(ref j) = *j {
392                     self.hash_expr(&*j);
393                 }
394             },
395             ExprBox(ref e) => {
396                 let c: fn(_) -> _ = ExprBox;
397                 c.hash(&mut self.s);
398                 self.hash_expr(e);
399             },
400             ExprCall(ref fun, ref args) => {
401                 let c: fn(_, _) -> _ = ExprCall;
402                 c.hash(&mut self.s);
403                 self.hash_expr(fun);
404                 self.hash_exprs(args);
405             },
406             ExprCast(ref e, ref _ty) => {
407                 let c: fn(_, _) -> _ = ExprCast;
408                 c.hash(&mut self.s);
409                 self.hash_expr(e);
410                 // TODO: _ty
411             },
412             ExprClosure(cap, _, eid, _, _) => {
413                 let c: fn(_, _, _, _, _) -> _ = ExprClosure;
414                 c.hash(&mut self.s);
415                 cap.hash(&mut self.s);
416                 self.hash_expr(&self.cx.tcx.hir.body(eid).value);
417             },
418             ExprField(ref e, ref f) => {
419                 let c: fn(_, _) -> _ = ExprField;
420                 c.hash(&mut self.s);
421                 self.hash_expr(e);
422                 self.hash_name(&f.node);
423             },
424             ExprIndex(ref a, ref i) => {
425                 let c: fn(_, _) -> _ = ExprIndex;
426                 c.hash(&mut self.s);
427                 self.hash_expr(a);
428                 self.hash_expr(i);
429             },
430             ExprInlineAsm(..) => {
431                 let c: fn(_, _, _) -> _ = ExprInlineAsm;
432                 c.hash(&mut self.s);
433             },
434             ExprIf(ref cond, ref t, ref e) => {
435                 let c: fn(_, _, _) -> _ = ExprIf;
436                 c.hash(&mut self.s);
437                 self.hash_expr(cond);
438                 self.hash_expr(&**t);
439                 if let Some(ref e) = *e {
440                     self.hash_expr(e);
441                 }
442             },
443             ExprLit(ref l) => {
444                 let c: fn(_) -> _ = ExprLit;
445                 c.hash(&mut self.s);
446                 l.hash(&mut self.s);
447             },
448             ExprLoop(ref b, ref i, _) => {
449                 let c: fn(_, _, _) -> _ = ExprLoop;
450                 c.hash(&mut self.s);
451                 self.hash_block(b);
452                 if let Some(i) = *i {
453                     self.hash_name(&i.name);
454                 }
455             },
456             ExprMatch(ref e, ref arms, ref s) => {
457                 let c: fn(_, _, _) -> _ = ExprMatch;
458                 c.hash(&mut self.s);
459                 self.hash_expr(e);
460
461                 for arm in arms {
462                     // TODO: arm.pat?
463                     if let Some(ref e) = arm.guard {
464                         self.hash_expr(e);
465                     }
466                     self.hash_expr(&arm.body);
467                 }
468
469                 s.hash(&mut self.s);
470             },
471             ExprMethodCall(ref path, ref _tys, ref args) => {
472                 let c: fn(_, _, _) -> _ = ExprMethodCall;
473                 c.hash(&mut self.s);
474                 self.hash_name(&path.name);
475                 self.hash_exprs(args);
476             },
477             ExprRepeat(ref e, l_id) => {
478                 let c: fn(_, _) -> _ = ExprRepeat;
479                 c.hash(&mut self.s);
480                 self.hash_expr(e);
481                 let full_table = self.tables;
482                 self.tables = self.cx.tcx.body_tables(l_id);
483                 self.hash_expr(&self.cx.tcx.hir.body(l_id).value);
484                 self.tables = full_table;
485             },
486             ExprRet(ref e) => {
487                 let c: fn(_) -> _ = ExprRet;
488                 c.hash(&mut self.s);
489                 if let Some(ref e) = *e {
490                     self.hash_expr(e);
491                 }
492             },
493             ExprPath(ref qpath) => {
494                 let c: fn(_) -> _ = ExprPath;
495                 c.hash(&mut self.s);
496                 self.hash_qpath(qpath);
497             },
498             ExprStruct(ref path, ref fields, ref expr) => {
499                 let c: fn(_, _, _) -> _ = ExprStruct;
500                 c.hash(&mut self.s);
501
502                 self.hash_qpath(path);
503
504                 for f in fields {
505                     self.hash_name(&f.name.node);
506                     self.hash_expr(&f.expr);
507                 }
508
509                 if let Some(ref e) = *expr {
510                     self.hash_expr(e);
511                 }
512             },
513             ExprTup(ref tup) => {
514                 let c: fn(_) -> _ = ExprTup;
515                 c.hash(&mut self.s);
516                 self.hash_exprs(tup);
517             },
518             ExprType(ref e, ref _ty) => {
519                 let c: fn(_, _) -> _ = ExprType;
520                 c.hash(&mut self.s);
521                 self.hash_expr(e);
522                 // TODO: _ty
523             },
524             ExprUnary(lop, ref le) => {
525                 let c: fn(_, _) -> _ = ExprUnary;
526                 c.hash(&mut self.s);
527
528                 lop.hash(&mut self.s);
529                 self.hash_expr(le);
530             },
531             ExprArray(ref v) => {
532                 let c: fn(_) -> _ = ExprArray;
533                 c.hash(&mut self.s);
534
535                 self.hash_exprs(v);
536             },
537             ExprWhile(ref cond, ref b, l) => {
538                 let c: fn(_, _, _) -> _ = ExprWhile;
539                 c.hash(&mut self.s);
540
541                 self.hash_expr(cond);
542                 self.hash_block(b);
543                 if let Some(l) = l {
544                     self.hash_name(&l.name);
545                 }
546             },
547         }
548     }
549
550     pub fn hash_exprs(&mut self, e: &P<[Expr]>) {
551         for e in e {
552             self.hash_expr(e);
553         }
554     }
555
556     pub fn hash_name(&mut self, n: &Name) {
557         n.as_str().hash(&mut self.s);
558     }
559
560     pub fn hash_qpath(&mut self, p: &QPath) {
561         match *p {
562             QPath::Resolved(_, ref path) => {
563                 self.hash_path(path);
564             },
565             QPath::TypeRelative(_, ref path) => {
566                 self.hash_name(&path.name);
567             },
568         }
569         // self.cx.tables.qpath_def(p, id).hash(&mut self.s);
570     }
571
572     pub fn hash_path(&mut self, p: &Path) {
573         p.is_global().hash(&mut self.s);
574         for p in &p.segments {
575             self.hash_name(&p.name);
576         }
577     }
578
579     pub fn hash_stmt(&mut self, b: &Stmt) {
580         match b.node {
581             StmtDecl(ref decl, _) => {
582                 let c: fn(_, _) -> _ = StmtDecl;
583                 c.hash(&mut self.s);
584
585                 if let DeclLocal(ref local) = decl.node {
586                     if let Some(ref init) = local.init {
587                         self.hash_expr(init);
588                     }
589                 }
590             },
591             StmtExpr(ref expr, _) => {
592                 let c: fn(_, _) -> _ = StmtExpr;
593                 c.hash(&mut self.s);
594                 self.hash_expr(expr);
595             },
596             StmtSemi(ref expr, _) => {
597                 let c: fn(_, _) -> _ = StmtSemi;
598                 c.hash(&mut self.s);
599                 self.hash_expr(expr);
600             },
601         }
602     }
603 }