]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/hir_utils.rs
Merge branch 'master' into never_loop
[rust.git] / clippy_lints / src / utils / hir_utils.rs
1 use consts::constant;
2 use rustc::lint::*;
3 use rustc::hir::*;
4 use std::hash::{Hash, Hasher};
5 use std::collections::hash_map::DefaultHasher;
6 use syntax::ast::Name;
7 use syntax::ptr::P;
8 use utils::differing_macro_contexts;
9
10 /// Type used to check whether two ast are the same. This is different from the operator
11 /// `==` on ast types as this operator would compare true equality with ID and span.
12 ///
13 /// Note that some expressions kinds are not considered but could be added.
14 pub struct SpanlessEq<'a, 'tcx: 'a> {
15     /// Context used to evaluate constant expressions.
16     cx: &'a LateContext<'a, 'tcx>,
17     /// If is true, never consider as equal expressions containing function calls.
18     ignore_fn: bool,
19 }
20
21 impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
22     pub fn new(cx: &'a LateContext<'a, 'tcx>) -> Self {
23         SpanlessEq {
24             cx: cx,
25             ignore_fn: false,
26         }
27     }
28
29     pub fn ignore_fn(self) -> Self {
30         SpanlessEq {
31             cx: self.cx,
32             ignore_fn: true,
33         }
34     }
35
36     /// Check whether two statements are the same.
37     pub fn eq_stmt(&self, left: &Stmt, right: &Stmt) -> bool {
38         match (&left.node, &right.node) {
39             (&StmtDecl(ref l, _), &StmtDecl(ref r, _)) => {
40                 if let (&DeclLocal(ref l), &DeclLocal(ref r)) = (&l.node, &r.node) {
41                     both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
42                 } else {
43                     false
44                 }
45             },
46             (&StmtExpr(ref l, _), &StmtExpr(ref r, _)) |
47             (&StmtSemi(ref l, _), &StmtSemi(ref r, _)) => self.eq_expr(l, r),
48             _ => false,
49         }
50     }
51
52     /// Check whether two blocks are the same.
53     pub fn eq_block(&self, left: &Block, right: &Block) -> bool {
54         over(&left.stmts, &right.stmts, |l, r| self.eq_stmt(l, r)) &&
55         both(&left.expr, &right.expr, |l, r| self.eq_expr(l, r))
56     }
57
58     pub fn eq_expr(&self, left: &Expr, right: &Expr) -> bool {
59         if self.ignore_fn && differing_macro_contexts(left.span, right.span) {
60             return false;
61         }
62
63         if let (Some(l), Some(r)) = (constant(self.cx, left), constant(self.cx, right)) {
64             if l == r {
65                 return true;
66             }
67         }
68
69         match (&left.node, &right.node) {
70             (&ExprAddrOf(l_mut, ref le), &ExprAddrOf(r_mut, ref re)) => l_mut == r_mut && self.eq_expr(le, re),
71             (&ExprAgain(li), &ExprAgain(ri)) => {
72                 both(&li.ident, &ri.ident, |l, r| l.node.name.as_str() == r.node.name.as_str())
73             },
74             (&ExprAssign(ref ll, ref lr), &ExprAssign(ref rl, ref rr)) => self.eq_expr(ll, rl) && self.eq_expr(lr, rr),
75             (&ExprAssignOp(ref lo, ref ll, ref lr), &ExprAssignOp(ref ro, ref rl, ref rr)) => {
76                 lo.node == ro.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
77             },
78             (&ExprBlock(ref l), &ExprBlock(ref r)) => self.eq_block(l, r),
79             (&ExprBinary(l_op, ref ll, ref lr), &ExprBinary(r_op, ref rl, ref rr)) => {
80                 l_op.node == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr) ||
81                 swap_binop(l_op.node, ll, lr).map_or(false, |(l_op, ll, lr)| {
82                     l_op == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
83                 })
84             },
85             (&ExprBreak(li, ref le), &ExprBreak(ri, ref re)) => {
86                 both(&li.ident, &ri.ident, |l, r| l.node.name.as_str() == r.node.name.as_str()) &&
87                 both(le, re, |l, r| self.eq_expr(l, r))
88             },
89             (&ExprBox(ref l), &ExprBox(ref r)) => self.eq_expr(l, r),
90             (&ExprCall(ref l_fun, ref l_args), &ExprCall(ref r_fun, ref r_args)) => {
91                 !self.ignore_fn && self.eq_expr(l_fun, r_fun) && self.eq_exprs(l_args, r_args)
92             },
93             (&ExprCast(ref lx, ref lt), &ExprCast(ref rx, ref rt)) |
94             (&ExprType(ref lx, ref lt), &ExprType(ref rx, ref rt)) => self.eq_expr(lx, rx) && self.eq_ty(lt, rt),
95             (&ExprField(ref l_f_exp, ref l_f_ident), &ExprField(ref r_f_exp, ref r_f_ident)) => {
96                 l_f_ident.node == r_f_ident.node && self.eq_expr(l_f_exp, r_f_exp)
97             },
98             (&ExprIndex(ref la, ref li), &ExprIndex(ref ra, ref ri)) => self.eq_expr(la, ra) && self.eq_expr(li, ri),
99             (&ExprIf(ref lc, ref lt, ref le), &ExprIf(ref rc, ref rt, ref re)) => {
100                 self.eq_expr(lc, rc) && self.eq_expr(&**lt, &**rt) && both(le, re, |l, r| self.eq_expr(l, r))
101             },
102             (&ExprLit(ref l), &ExprLit(ref r)) => l.node == r.node,
103             (&ExprLoop(ref lb, ref ll, ref lls), &ExprLoop(ref rb, ref rl, ref rls)) => {
104                 lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.node.as_str() == r.node.as_str())
105             },
106             (&ExprMatch(ref le, ref la, ref ls), &ExprMatch(ref re, ref ra, ref rs)) => {
107                 ls == rs && self.eq_expr(le, re) &&
108                 over(la, ra, |l, r| {
109                     self.eq_expr(&l.body, &r.body) && both(&l.guard, &r.guard, |l, r| self.eq_expr(l, r)) &&
110                     over(&l.pats, &r.pats, |l, r| self.eq_pat(l, r))
111                 })
112             },
113             (&ExprMethodCall(ref l_name, ref l_tys, ref l_args),
114              &ExprMethodCall(ref r_name, ref r_tys, ref r_args)) => {
115                 !self.ignore_fn && l_name.node == r_name.node && over(l_tys, r_tys, |l, r| self.eq_ty(l, r)) &&
116                 self.eq_exprs(l_args, r_args)
117             },
118             (&ExprRepeat(ref le, ll_id), &ExprRepeat(ref re, rl_id)) => {
119                 self.eq_expr(le, re) &&
120                 self.eq_expr(&self.cx.tcx.hir.body(ll_id).value, &self.cx.tcx.hir.body(rl_id).value)
121             },
122             (&ExprRet(ref l), &ExprRet(ref r)) => both(l, r, |l, r| self.eq_expr(l, r)),
123             (&ExprPath(ref l), &ExprPath(ref r)) => self.eq_qpath(l, r),
124             (&ExprStruct(ref l_path, ref lf, ref lo), &ExprStruct(ref r_path, ref rf, ref ro)) => {
125                 self.eq_qpath(l_path, r_path) && both(lo, ro, |l, r| self.eq_expr(l, r)) &&
126                 over(lf, rf, |l, r| self.eq_field(l, r))
127             },
128             (&ExprTup(ref l_tup), &ExprTup(ref r_tup)) => self.eq_exprs(l_tup, r_tup),
129             (&ExprTupField(ref le, li), &ExprTupField(ref re, ri)) => li.node == ri.node && self.eq_expr(le, re),
130             (&ExprUnary(l_op, ref le), &ExprUnary(r_op, ref re)) => l_op == r_op && self.eq_expr(le, re),
131             (&ExprArray(ref l), &ExprArray(ref r)) => self.eq_exprs(l, r),
132             (&ExprWhile(ref lc, ref lb, ref ll), &ExprWhile(ref rc, ref rb, ref rl)) => {
133                 self.eq_expr(lc, rc) && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.node.as_str() == r.node.as_str())
134             },
135             _ => false,
136         }
137     }
138
139     fn eq_exprs(&self, left: &P<[Expr]>, right: &P<[Expr]>) -> bool {
140         over(left, right, |l, r| self.eq_expr(l, r))
141     }
142
143     fn eq_field(&self, left: &Field, right: &Field) -> bool {
144         left.name.node == right.name.node && self.eq_expr(&left.expr, &right.expr)
145     }
146
147     fn eq_lifetime(&self, left: &Lifetime, right: &Lifetime) -> bool {
148         left.name == right.name
149     }
150
151     /// Check whether two patterns are the same.
152     pub fn eq_pat(&self, left: &Pat, right: &Pat) -> bool {
153         match (&left.node, &right.node) {
154             (&PatKind::Box(ref l), &PatKind::Box(ref r)) => self.eq_pat(l, r),
155             (&PatKind::TupleStruct(ref lp, ref la, ls), &PatKind::TupleStruct(ref rp, ref ra, rs)) => {
156                 self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat(l, r)) && ls == rs
157             },
158             (&PatKind::Binding(ref lb, _, ref li, ref lp), &PatKind::Binding(ref rb, _, ref ri, ref rp)) => {
159                 lb == rb && li.node.as_str() == ri.node.as_str() && both(lp, rp, |l, r| self.eq_pat(l, r))
160             },
161             (&PatKind::Path(ref l), &PatKind::Path(ref r)) => self.eq_qpath(l, r),
162             (&PatKind::Lit(ref l), &PatKind::Lit(ref r)) => self.eq_expr(l, r),
163             (&PatKind::Tuple(ref l, ls), &PatKind::Tuple(ref r, rs)) => {
164                 ls == rs && over(l, r, |l, r| self.eq_pat(l, r))
165             },
166             (&PatKind::Range(ref ls, ref le, ref li), &PatKind::Range(ref rs, ref re, ref ri)) => {
167                 self.eq_expr(ls, rs) && self.eq_expr(le, re) && (*li == *ri)
168             },
169             (&PatKind::Ref(ref le, ref lm), &PatKind::Ref(ref re, ref rm)) => lm == rm && self.eq_pat(le, re),
170             (&PatKind::Slice(ref ls, ref li, ref le), &PatKind::Slice(ref rs, ref ri, ref re)) => {
171                 over(ls, rs, |l, r| self.eq_pat(l, r)) && over(le, re, |l, r| self.eq_pat(l, r)) &&
172                 both(li, ri, |l, r| self.eq_pat(l, r))
173             },
174             (&PatKind::Wild, &PatKind::Wild) => true,
175             _ => false,
176         }
177     }
178
179     fn eq_qpath(&self, left: &QPath, right: &QPath) -> bool {
180         match (left, right) {
181             (&QPath::Resolved(ref lty, ref lpath), &QPath::Resolved(ref rty, ref rpath)) => {
182                 both(lty, rty, |l, r| self.eq_ty(l, r)) && self.eq_path(lpath, rpath)
183             },
184             (&QPath::TypeRelative(ref lty, ref lseg), &QPath::TypeRelative(ref rty, ref rseg)) => {
185                 self.eq_ty(lty, rty) && self.eq_path_segment(lseg, rseg)
186             },
187             _ => false,
188         }
189     }
190
191     fn eq_path(&self, left: &Path, right: &Path) -> bool {
192         left.is_global() == right.is_global() &&
193         over(&left.segments, &right.segments, |l, r| self.eq_path_segment(l, r))
194     }
195
196     fn eq_path_parameters(&self, left: &PathParameters, right: &PathParameters) -> bool {
197         match (left, right) {
198             (&AngleBracketedParameters(ref left), &AngleBracketedParameters(ref right)) => {
199                 over(&left.lifetimes, &right.lifetimes, |l, r| self.eq_lifetime(l, r)) &&
200                 over(&left.types, &right.types, |l, r| self.eq_ty(l, r)) &&
201                 over(&left.bindings, &right.bindings, |l, r| self.eq_type_binding(l, r))
202             },
203             (&ParenthesizedParameters(ref left), &ParenthesizedParameters(ref right)) => {
204                 over(&left.inputs, &right.inputs, |l, r| self.eq_ty(l, r)) &&
205                 both(&left.output, &right.output, |l, r| self.eq_ty(l, r))
206             },
207             (&AngleBracketedParameters(_), &ParenthesizedParameters(_)) |
208             (&ParenthesizedParameters(_), &AngleBracketedParameters(_)) => false,
209         }
210     }
211
212     fn eq_path_segment(&self, left: &PathSegment, right: &PathSegment) -> bool {
213         // The == of idents doesn't work with different contexts,
214         // we have to be explicit about hygiene
215         left.name.as_str() == right.name.as_str() && self.eq_path_parameters(&left.parameters, &right.parameters)
216     }
217
218     fn eq_ty(&self, left: &Ty, right: &Ty) -> bool {
219         match (&left.node, &right.node) {
220             (&TySlice(ref l_vec), &TySlice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
221             (&TyArray(ref lt, ll_id), &TyArray(ref rt, rl_id)) => {
222                 self.eq_ty(lt, rt) &&
223                 self.eq_expr(&self.cx.tcx.hir.body(ll_id).value, &self.cx.tcx.hir.body(rl_id).value)
224             },
225             (&TyPtr(ref l_mut), &TyPtr(ref r_mut)) => l_mut.mutbl == r_mut.mutbl && self.eq_ty(&*l_mut.ty, &*r_mut.ty),
226             (&TyRptr(_, ref l_rmut), &TyRptr(_, ref r_rmut)) => {
227                 l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(&*l_rmut.ty, &*r_rmut.ty)
228             },
229             (&TyPath(ref l), &TyPath(ref r)) => self.eq_qpath(l, r),
230             (&TyTup(ref l), &TyTup(ref r)) => over(l, r, |l, r| self.eq_ty(l, r)),
231             (&TyInfer, &TyInfer) => true,
232             _ => false,
233         }
234     }
235
236     fn eq_type_binding(&self, left: &TypeBinding, right: &TypeBinding) -> bool {
237         left.name == right.name && self.eq_ty(&left.ty, &right.ty)
238     }
239 }
240
241 fn swap_binop<'a>(binop: BinOp_, lhs: &'a Expr, rhs: &'a Expr) -> Option<(BinOp_, &'a Expr, &'a Expr)> {
242     match binop {
243         BiAdd | BiMul | BiBitXor | BiBitAnd | BiEq | BiNe | BiBitOr => Some((binop, rhs, lhs)),
244         BiLt => Some((BiGt, rhs, lhs)),
245         BiLe => Some((BiGe, rhs, lhs)),
246         BiGe => Some((BiLe, rhs, lhs)),
247         BiGt => Some((BiLt, rhs, lhs)),
248         BiShl | BiShr | BiRem | BiSub | BiDiv | BiAnd | BiOr => None,
249     }
250 }
251
252 /// Check if the two `Option`s are both `None` or some equal values as per `eq_fn`.
253 fn both<X, F>(l: &Option<X>, r: &Option<X>, mut eq_fn: F) -> bool
254     where F: FnMut(&X, &X) -> bool
255 {
256     l.as_ref().map_or_else(|| r.is_none(), |x| r.as_ref().map_or(false, |y| eq_fn(x, y)))
257 }
258
259 /// Check if two slices are equal as per `eq_fn`.
260 fn over<X, F>(left: &[X], right: &[X], mut eq_fn: F) -> bool
261     where F: FnMut(&X, &X) -> bool
262 {
263     left.len() == right.len() && left.iter().zip(right).all(|(x, y)| eq_fn(x, y))
264 }
265
266
267 /// Type used to hash an ast element. This is different from the `Hash` trait on ast types as this
268 /// trait would consider IDs and spans.
269 ///
270 /// All expressions kind are hashed, but some might have a weaker hash.
271 pub struct SpanlessHash<'a, 'tcx: 'a> {
272     /// Context used to evaluate constant expressions.
273     cx: &'a LateContext<'a, 'tcx>,
274     s: DefaultHasher,
275 }
276
277 impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
278     pub fn new(cx: &'a LateContext<'a, 'tcx>) -> Self {
279         SpanlessHash {
280             cx: cx,
281             s: DefaultHasher::new(),
282         }
283     }
284
285     pub fn finish(&self) -> u64 {
286         self.s.finish()
287     }
288
289     pub fn hash_block(&mut self, b: &Block) {
290         for s in &b.stmts {
291             self.hash_stmt(s);
292         }
293
294         if let Some(ref e) = b.expr {
295             self.hash_expr(e);
296         }
297
298         b.rules.hash(&mut self.s);
299     }
300
301     pub fn hash_expr(&mut self, e: &Expr) {
302         if let Some(e) = constant(self.cx, e) {
303             return e.hash(&mut self.s);
304         }
305
306         match e.node {
307             ExprAddrOf(m, ref e) => {
308                 let c: fn(_, _) -> _ = ExprAddrOf;
309                 c.hash(&mut self.s);
310                 m.hash(&mut self.s);
311                 self.hash_expr(e);
312             },
313             ExprAgain(i) => {
314                 let c: fn(_) -> _ = ExprAgain;
315                 c.hash(&mut self.s);
316                 if let Some(i) = i.ident {
317                     self.hash_name(&i.node.name);
318                 }
319             },
320             ExprAssign(ref l, ref r) => {
321                 let c: fn(_, _) -> _ = ExprAssign;
322                 c.hash(&mut self.s);
323                 self.hash_expr(l);
324                 self.hash_expr(r);
325             },
326             ExprAssignOp(ref o, ref l, ref r) => {
327                 let c: fn(_, _, _) -> _ = ExprAssignOp;
328                 c.hash(&mut self.s);
329                 o.hash(&mut self.s);
330                 self.hash_expr(l);
331                 self.hash_expr(r);
332             },
333             ExprBlock(ref b) => {
334                 let c: fn(_) -> _ = ExprBlock;
335                 c.hash(&mut self.s);
336                 self.hash_block(b);
337             },
338             ExprBinary(op, ref l, ref r) => {
339                 let c: fn(_, _, _) -> _ = ExprBinary;
340                 c.hash(&mut self.s);
341                 op.node.hash(&mut self.s);
342                 self.hash_expr(l);
343                 self.hash_expr(r);
344             },
345             ExprBreak(i, ref j) => {
346                 let c: fn(_, _) -> _ = ExprBreak;
347                 c.hash(&mut self.s);
348                 if let Some(i) = i.ident {
349                     self.hash_name(&i.node.name);
350                 }
351                 if let Some(ref j) = *j {
352                     self.hash_expr(&*j);
353                 }
354             },
355             ExprBox(ref e) => {
356                 let c: fn(_) -> _ = ExprBox;
357                 c.hash(&mut self.s);
358                 self.hash_expr(e);
359             },
360             ExprCall(ref fun, ref args) => {
361                 let c: fn(_, _) -> _ = ExprCall;
362                 c.hash(&mut self.s);
363                 self.hash_expr(fun);
364                 self.hash_exprs(args);
365             },
366             ExprCast(ref e, ref _ty) => {
367                 let c: fn(_, _) -> _ = ExprCast;
368                 c.hash(&mut self.s);
369                 self.hash_expr(e);
370                 // TODO: _ty
371             },
372             ExprClosure(cap, _, eid, _) => {
373                 let c: fn(_, _, _, _) -> _ = ExprClosure;
374                 c.hash(&mut self.s);
375                 cap.hash(&mut self.s);
376                 self.hash_expr(&self.cx.tcx.hir.body(eid).value);
377             },
378             ExprField(ref e, ref f) => {
379                 let c: fn(_, _) -> _ = ExprField;
380                 c.hash(&mut self.s);
381                 self.hash_expr(e);
382                 self.hash_name(&f.node);
383             },
384             ExprIndex(ref a, ref i) => {
385                 let c: fn(_, _) -> _ = ExprIndex;
386                 c.hash(&mut self.s);
387                 self.hash_expr(a);
388                 self.hash_expr(i);
389             },
390             ExprInlineAsm(..) => {
391                 let c: fn(_, _, _) -> _ = ExprInlineAsm;
392                 c.hash(&mut self.s);
393             },
394             ExprIf(ref cond, ref t, ref e) => {
395                 let c: fn(_, _, _) -> _ = ExprIf;
396                 c.hash(&mut self.s);
397                 self.hash_expr(cond);
398                 self.hash_expr(&**t);
399                 if let Some(ref e) = *e {
400                     self.hash_expr(e);
401                 }
402             },
403             ExprLit(ref l) => {
404                 let c: fn(_) -> _ = ExprLit;
405                 c.hash(&mut self.s);
406                 l.hash(&mut self.s);
407             },
408             ExprLoop(ref b, ref i, _) => {
409                 let c: fn(_, _, _) -> _ = ExprLoop;
410                 c.hash(&mut self.s);
411                 self.hash_block(b);
412                 if let Some(i) = *i {
413                     self.hash_name(&i.node);
414                 }
415             },
416             ExprMatch(ref e, ref arms, ref s) => {
417                 let c: fn(_, _, _) -> _ = ExprMatch;
418                 c.hash(&mut self.s);
419                 self.hash_expr(e);
420
421                 for arm in arms {
422                     // TODO: arm.pat?
423                     if let Some(ref e) = arm.guard {
424                         self.hash_expr(e);
425                     }
426                     self.hash_expr(&arm.body);
427                 }
428
429                 s.hash(&mut self.s);
430             },
431             ExprMethodCall(ref name, ref _tys, ref args) => {
432                 let c: fn(_, _, _) -> _ = ExprMethodCall;
433                 c.hash(&mut self.s);
434                 self.hash_name(&name.node);
435                 self.hash_exprs(args);
436             },
437             ExprRepeat(ref e, l_id) => {
438                 let c: fn(_, _) -> _ = ExprRepeat;
439                 c.hash(&mut self.s);
440                 self.hash_expr(e);
441                 self.hash_expr(&self.cx.tcx.hir.body(l_id).value);
442             },
443             ExprRet(ref e) => {
444                 let c: fn(_) -> _ = ExprRet;
445                 c.hash(&mut self.s);
446                 if let Some(ref e) = *e {
447                     self.hash_expr(e);
448                 }
449             },
450             ExprPath(ref qpath) => {
451                 let c: fn(_) -> _ = ExprPath;
452                 c.hash(&mut self.s);
453                 self.hash_qpath(qpath);
454             },
455             ExprStruct(ref path, ref fields, ref expr) => {
456                 let c: fn(_, _, _) -> _ = ExprStruct;
457                 c.hash(&mut self.s);
458
459                 self.hash_qpath(path);
460
461                 for f in fields {
462                     self.hash_name(&f.name.node);
463                     self.hash_expr(&f.expr);
464                 }
465
466                 if let Some(ref e) = *expr {
467                     self.hash_expr(e);
468                 }
469             },
470             ExprTup(ref tup) => {
471                 let c: fn(_) -> _ = ExprTup;
472                 c.hash(&mut self.s);
473                 self.hash_exprs(tup);
474             },
475             ExprTupField(ref le, li) => {
476                 let c: fn(_, _) -> _ = ExprTupField;
477                 c.hash(&mut self.s);
478
479                 self.hash_expr(le);
480                 li.node.hash(&mut self.s);
481             },
482             ExprType(ref e, ref _ty) => {
483                 let c: fn(_, _) -> _ = ExprType;
484                 c.hash(&mut self.s);
485                 self.hash_expr(e);
486                 // TODO: _ty
487             },
488             ExprUnary(lop, ref le) => {
489                 let c: fn(_, _) -> _ = ExprUnary;
490                 c.hash(&mut self.s);
491
492                 lop.hash(&mut self.s);
493                 self.hash_expr(le);
494             },
495             ExprArray(ref v) => {
496                 let c: fn(_) -> _ = ExprArray;
497                 c.hash(&mut self.s);
498
499                 self.hash_exprs(v);
500             },
501             ExprWhile(ref cond, ref b, l) => {
502                 let c: fn(_, _, _) -> _ = ExprWhile;
503                 c.hash(&mut self.s);
504
505                 self.hash_expr(cond);
506                 self.hash_block(b);
507                 if let Some(l) = l {
508                     self.hash_name(&l.node);
509                 }
510             },
511         }
512     }
513
514     pub fn hash_exprs(&mut self, e: &P<[Expr]>) {
515         for e in e {
516             self.hash_expr(e);
517         }
518     }
519
520     pub fn hash_name(&mut self, n: &Name) {
521         n.as_str().hash(&mut self.s);
522     }
523
524     pub fn hash_qpath(&mut self, p: &QPath) {
525         match *p {
526             QPath::Resolved(_, ref path) => {
527                 self.hash_path(path);
528             },
529             QPath::TypeRelative(_, ref path) => {
530                 self.hash_name(&path.name);
531             },
532         }
533         // self.cx.tables.qpath_def(p, id).hash(&mut self.s);
534     }
535
536     pub fn hash_path(&mut self, p: &Path) {
537         p.is_global().hash(&mut self.s);
538         for p in &p.segments {
539             self.hash_name(&p.name);
540         }
541     }
542
543     pub fn hash_stmt(&mut self, b: &Stmt) {
544         match b.node {
545             StmtDecl(ref decl, _) => {
546                 let c: fn(_, _) -> _ = StmtDecl;
547                 c.hash(&mut self.s);
548
549                 if let DeclLocal(ref local) = decl.node {
550                     if let Some(ref init) = local.init {
551                         self.hash_expr(init);
552                     }
553                 }
554             },
555             StmtExpr(ref expr, _) => {
556                 let c: fn(_, _) -> _ = StmtExpr;
557                 c.hash(&mut self.s);
558                 self.hash_expr(expr);
559             },
560             StmtSemi(ref expr, _) => {
561                 let c: fn(_, _) -> _ = StmtSemi;
562                 c.hash(&mut self.s);
563                 self.hash_expr(expr);
564             },
565         }
566     }
567 }