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