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