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