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