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};
6 BinOpKind, Block, BlockCheckMode, BodyId, BorrowKind, CaptureBy, Expr, ExprKind, Field, FieldPat, FnRetTy,
7 GenericArg, GenericArgs, Guard, InlineAsmOperand, Lifetime, LifetimeName, ParamName, Pat, PatKind, Path,
8 PathSegment, QPath, Stmt, StmtKind, Ty, TyKind, TypeBinding,
10 use rustc_lint::LateContext;
11 use rustc_middle::ich::StableHashingContextProvider;
12 use rustc_middle::ty::TypeckResults;
13 use rustc_span::Symbol;
16 /// Type used to check whether two ast are the same. This is different from the
18 /// `==` on ast types as this operator would compare true equality with ID and
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_results: Option<&'tcx TypeckResults<'tcx>>,
26 allow_side_effects: bool,
29 impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
30 pub fn new(cx: &'a LateContext<'tcx>) -> Self {
33 maybe_typeck_results: cx.maybe_typeck_results(),
34 allow_side_effects: true,
38 /// Consider expressions containing potential side effects as not equal.
39 pub fn deny_side_effects(self) -> Self {
41 allow_side_effects: false,
46 /// Checks whether two statements are the same.
47 pub fn eq_stmt(&mut self, left: &Stmt<'_>, right: &Stmt<'_>) -> bool {
48 match (&left.kind, &right.kind) {
49 (&StmtKind::Local(ref l), &StmtKind::Local(ref r)) => {
50 self.eq_pat(&l.pat, &r.pat)
51 && both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r))
52 && both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
54 (&StmtKind::Expr(ref l), &StmtKind::Expr(ref r)) | (&StmtKind::Semi(ref l), &StmtKind::Semi(ref r)) => {
61 /// Checks whether two blocks are the same.
62 pub fn eq_block(&mut self, left: &Block<'_>, right: &Block<'_>) -> bool {
63 over(&left.stmts, &right.stmts, |l, r| self.eq_stmt(l, r))
64 && both(&left.expr, &right.expr, |l, r| self.eq_expr(l, r))
67 #[allow(clippy::similar_names)]
68 pub fn eq_expr(&mut self, left: &Expr<'_>, right: &Expr<'_>) -> bool {
69 if !self.allow_side_effects && differing_macro_contexts(left.span, right.span) {
73 if let Some(typeck_results) = self.maybe_typeck_results {
74 if let (Some(l), Some(r)) = (
75 constant_simple(self.cx, typeck_results, left),
76 constant_simple(self.cx, typeck_results, right),
84 match (&reduce_exprkind(&left.kind), &reduce_exprkind(&right.kind)) {
85 (&ExprKind::AddrOf(lb, l_mut, ref le), &ExprKind::AddrOf(rb, r_mut, ref re)) => {
86 lb == rb && l_mut == r_mut && self.eq_expr(le, re)
88 (&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
89 both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
91 (&ExprKind::Assign(ref ll, ref lr, _), &ExprKind::Assign(ref rl, ref rr, _)) => {
92 self.allow_side_effects && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
94 (&ExprKind::AssignOp(ref lo, ref ll, ref lr), &ExprKind::AssignOp(ref ro, ref rl, ref rr)) => {
95 self.allow_side_effects && lo.node == ro.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
97 (&ExprKind::Block(ref l, _), &ExprKind::Block(ref r, _)) => self.eq_block(l, r),
98 (&ExprKind::Binary(l_op, ref ll, ref lr), &ExprKind::Binary(r_op, ref rl, ref rr)) => {
99 l_op.node == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
100 || swap_binop(l_op.node, ll, lr).map_or(false, |(l_op, ll, lr)| {
101 l_op == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
104 (&ExprKind::Break(li, ref le), &ExprKind::Break(ri, ref re)) => {
105 both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
106 && both(le, re, |l, r| self.eq_expr(l, r))
108 (&ExprKind::Box(ref l), &ExprKind::Box(ref r)) => self.eq_expr(l, r),
109 (&ExprKind::Call(l_fun, l_args), &ExprKind::Call(r_fun, r_args)) => {
110 self.allow_side_effects && self.eq_expr(l_fun, r_fun) && self.eq_exprs(l_args, r_args)
112 (&ExprKind::Cast(ref lx, ref lt), &ExprKind::Cast(ref rx, ref rt))
113 | (&ExprKind::Type(ref lx, ref lt), &ExprKind::Type(ref rx, ref rt)) => {
114 self.eq_expr(lx, rx) && self.eq_ty(lt, rt)
116 (&ExprKind::Field(ref l_f_exp, ref l_f_ident), &ExprKind::Field(ref r_f_exp, ref r_f_ident)) => {
117 l_f_ident.name == r_f_ident.name && self.eq_expr(l_f_exp, r_f_exp)
119 (&ExprKind::Index(ref la, ref li), &ExprKind::Index(ref ra, ref ri)) => {
120 self.eq_expr(la, ra) && self.eq_expr(li, ri)
122 (&ExprKind::If(ref lc, ref lt, ref le), &ExprKind::If(ref rc, ref rt, ref re)) => {
123 self.eq_expr(lc, rc) && self.eq_expr(&**lt, &**rt) && both(le, re, |l, r| self.eq_expr(l, r))
125 (&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node,
126 (&ExprKind::Loop(ref lb, ref ll, ref lls, _), &ExprKind::Loop(ref rb, ref rl, ref rls, _)) => {
127 lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.name == r.ident.name)
129 (&ExprKind::Match(ref le, ref la, ref ls), &ExprKind::Match(ref re, ref ra, ref rs)) => {
131 && self.eq_expr(le, re)
132 && over(la, ra, |l, r| {
133 self.eq_expr(&l.body, &r.body)
134 && both(&l.guard, &r.guard, |l, r| self.eq_guard(l, r))
135 && self.eq_pat(&l.pat, &r.pat)
138 (&ExprKind::MethodCall(l_path, _, l_args, _), &ExprKind::MethodCall(r_path, _, r_args, _)) => {
139 self.allow_side_effects && self.eq_path_segment(l_path, r_path) && self.eq_exprs(l_args, r_args)
141 (&ExprKind::Repeat(ref le, ref ll_id), &ExprKind::Repeat(ref re, ref rl_id)) => {
142 let mut celcx = constant_context(self.cx, self.cx.tcx.typeck_body(ll_id.body));
143 let ll = celcx.expr(&self.cx.tcx.hir().body(ll_id.body).value);
144 let mut celcx = constant_context(self.cx, self.cx.tcx.typeck_body(rl_id.body));
145 let rl = celcx.expr(&self.cx.tcx.hir().body(rl_id.body).value);
147 self.eq_expr(le, re) && ll == rl
149 (&ExprKind::Ret(ref l), &ExprKind::Ret(ref r)) => both(l, r, |l, r| self.eq_expr(l, r)),
150 (&ExprKind::Path(ref l), &ExprKind::Path(ref r)) => self.eq_qpath(l, r),
151 (&ExprKind::Struct(ref l_path, ref lf, ref lo), &ExprKind::Struct(ref r_path, ref rf, ref ro)) => {
152 self.eq_qpath(l_path, r_path)
153 && both(lo, ro, |l, r| self.eq_expr(l, r))
154 && over(lf, rf, |l, r| self.eq_field(l, r))
156 (&ExprKind::Tup(l_tup), &ExprKind::Tup(r_tup)) => self.eq_exprs(l_tup, r_tup),
157 (&ExprKind::Unary(l_op, ref le), &ExprKind::Unary(r_op, ref re)) => l_op == r_op && self.eq_expr(le, re),
158 (&ExprKind::Array(l), &ExprKind::Array(r)) => self.eq_exprs(l, r),
159 (&ExprKind::DropTemps(ref le), &ExprKind::DropTemps(ref re)) => self.eq_expr(le, re),
164 fn eq_exprs(&mut self, left: &[Expr<'_>], right: &[Expr<'_>]) -> bool {
165 over(left, right, |l, r| self.eq_expr(l, r))
168 fn eq_field(&mut self, left: &Field<'_>, right: &Field<'_>) -> bool {
169 left.ident.name == right.ident.name && self.eq_expr(&left.expr, &right.expr)
172 fn eq_guard(&mut self, left: &Guard<'_>, right: &Guard<'_>) -> bool {
173 match (left, right) {
174 (Guard::If(l), Guard::If(r)) => self.eq_expr(l, r),
175 (Guard::IfLet(lp, le), Guard::IfLet(rp, re)) => self.eq_pat(lp, rp) && self.eq_expr(le, re),
180 fn eq_generic_arg(&mut self, left: &GenericArg<'_>, right: &GenericArg<'_>) -> bool {
181 match (left, right) {
182 (GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => Self::eq_lifetime(l_lt, r_lt),
183 (GenericArg::Type(l_ty), GenericArg::Type(r_ty)) => self.eq_ty(l_ty, r_ty),
188 fn eq_lifetime(left: &Lifetime, right: &Lifetime) -> bool {
189 left.name == right.name
192 pub fn eq_fieldpat(&mut self, left: &FieldPat<'_>, right: &FieldPat<'_>) -> bool {
193 let (FieldPat { ident: li, pat: lp, .. }, FieldPat { ident: ri, pat: rp, .. }) = (&left, &right);
194 li.name == ri.name && self.eq_pat(lp, rp)
197 /// Checks whether two patterns are the same.
198 pub fn eq_pat(&mut self, left: &Pat<'_>, right: &Pat<'_>) -> bool {
199 match (&left.kind, &right.kind) {
200 (&PatKind::Box(ref l), &PatKind::Box(ref r)) => self.eq_pat(l, r),
201 (&PatKind::Struct(ref lp, ref la, ..), &PatKind::Struct(ref rp, ref ra, ..)) => {
202 self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_fieldpat(l, r))
204 (&PatKind::TupleStruct(ref lp, ref la, ls), &PatKind::TupleStruct(ref rp, ref ra, rs)) => {
205 self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat(l, r)) && ls == rs
207 (&PatKind::Binding(ref lb, .., ref li, ref lp), &PatKind::Binding(ref rb, .., ref ri, ref rp)) => {
208 lb == rb && li.name == ri.name && both(lp, rp, |l, r| self.eq_pat(l, r))
210 (&PatKind::Path(ref l), &PatKind::Path(ref r)) => self.eq_qpath(l, r),
211 (&PatKind::Lit(ref l), &PatKind::Lit(ref r)) => self.eq_expr(l, r),
212 (&PatKind::Tuple(ref l, ls), &PatKind::Tuple(ref r, rs)) => {
213 ls == rs && over(l, r, |l, r| self.eq_pat(l, r))
215 (&PatKind::Range(ref ls, ref le, li), &PatKind::Range(ref rs, ref re, ri)) => {
216 both(ls, rs, |a, b| self.eq_expr(a, b)) && both(le, re, |a, b| self.eq_expr(a, b)) && (li == ri)
218 (&PatKind::Ref(ref le, ref lm), &PatKind::Ref(ref re, ref rm)) => lm == rm && self.eq_pat(le, re),
219 (&PatKind::Slice(ref ls, ref li, ref le), &PatKind::Slice(ref rs, ref ri, ref re)) => {
220 over(ls, rs, |l, r| self.eq_pat(l, r))
221 && over(le, re, |l, r| self.eq_pat(l, r))
222 && both(li, ri, |l, r| self.eq_pat(l, r))
224 (&PatKind::Wild, &PatKind::Wild) => true,
229 #[allow(clippy::similar_names)]
230 fn eq_qpath(&mut self, left: &QPath<'_>, right: &QPath<'_>) -> bool {
231 match (left, right) {
232 (&QPath::Resolved(ref lty, ref lpath), &QPath::Resolved(ref rty, ref rpath)) => {
233 both(lty, rty, |l, r| self.eq_ty(l, r)) && self.eq_path(lpath, rpath)
235 (&QPath::TypeRelative(ref lty, ref lseg), &QPath::TypeRelative(ref rty, ref rseg)) => {
236 self.eq_ty(lty, rty) && self.eq_path_segment(lseg, rseg)
238 (&QPath::LangItem(llang_item, _), &QPath::LangItem(rlang_item, _)) => llang_item == rlang_item,
243 fn eq_path(&mut self, left: &Path<'_>, right: &Path<'_>) -> bool {
244 left.is_global() == right.is_global()
245 && over(&left.segments, &right.segments, |l, r| self.eq_path_segment(l, r))
248 fn eq_path_parameters(&mut self, left: &GenericArgs<'_>, right: &GenericArgs<'_>) -> bool {
249 if !(left.parenthesized || right.parenthesized) {
250 over(&left.args, &right.args, |l, r| self.eq_generic_arg(l, r)) // FIXME(flip1995): may not work
251 && over(&left.bindings, &right.bindings, |l, r| self.eq_type_binding(l, r))
252 } else if left.parenthesized && right.parenthesized {
253 over(left.inputs(), right.inputs(), |l, r| self.eq_ty(l, r))
254 && both(&Some(&left.bindings[0].ty()), &Some(&right.bindings[0].ty()), |l, r| {
262 pub fn eq_path_segments(&mut self, left: &[PathSegment<'_>], right: &[PathSegment<'_>]) -> bool {
263 left.len() == right.len() && left.iter().zip(right).all(|(l, r)| self.eq_path_segment(l, r))
266 pub fn eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_>) -> bool {
267 // The == of idents doesn't work with different contexts,
268 // we have to be explicit about hygiene
269 left.ident.name == right.ident.name && both(&left.args, &right.args, |l, r| self.eq_path_parameters(l, r))
272 pub fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {
273 self.eq_ty_kind(&left.kind, &right.kind)
276 #[allow(clippy::similar_names)]
277 pub fn eq_ty_kind(&mut self, left: &TyKind<'_>, right: &TyKind<'_>) -> bool {
278 match (left, right) {
279 (&TyKind::Slice(ref l_vec), &TyKind::Slice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
280 (&TyKind::Array(ref lt, ref ll_id), &TyKind::Array(ref rt, ref rl_id)) => {
281 let old_maybe_typeck_results = self.maybe_typeck_results;
283 let mut celcx = constant_context(self.cx, self.cx.tcx.typeck_body(ll_id.body));
284 self.maybe_typeck_results = Some(self.cx.tcx.typeck_body(ll_id.body));
285 let ll = celcx.expr(&self.cx.tcx.hir().body(ll_id.body).value);
287 let mut celcx = constant_context(self.cx, self.cx.tcx.typeck_body(rl_id.body));
288 self.maybe_typeck_results = Some(self.cx.tcx.typeck_body(rl_id.body));
289 let rl = celcx.expr(&self.cx.tcx.hir().body(rl_id.body).value);
291 let eq_ty = self.eq_ty(lt, rt);
292 self.maybe_typeck_results = old_maybe_typeck_results;
295 (&TyKind::Ptr(ref l_mut), &TyKind::Ptr(ref r_mut)) => {
296 l_mut.mutbl == r_mut.mutbl && self.eq_ty(&*l_mut.ty, &*r_mut.ty)
298 (&TyKind::Rptr(_, ref l_rmut), &TyKind::Rptr(_, ref r_rmut)) => {
299 l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(&*l_rmut.ty, &*r_rmut.ty)
301 (&TyKind::Path(ref l), &TyKind::Path(ref r)) => self.eq_qpath(l, r),
302 (&TyKind::Tup(ref l), &TyKind::Tup(ref r)) => over(l, r, |l, r| self.eq_ty(l, r)),
303 (&TyKind::Infer, &TyKind::Infer) => true,
308 fn eq_type_binding(&mut self, left: &TypeBinding<'_>, right: &TypeBinding<'_>) -> bool {
309 left.ident.name == right.ident.name && self.eq_ty(&left.ty(), &right.ty())
313 /// Some simple reductions like `{ return }` => `return`
314 fn reduce_exprkind<'hir>(kind: &'hir ExprKind<'hir>) -> &ExprKind<'hir> {
315 if let ExprKind::Block(block, _) = kind {
316 match (block.stmts, block.expr) {
318 ([], None) => &ExprKind::Tup(&[]),
319 ([], Some(expr)) => match expr.kind {
320 // `{ return .. }` => `return ..`
321 ExprKind::Ret(..) => &expr.kind,
324 ([stmt], None) => match stmt.kind {
325 StmtKind::Expr(expr) | StmtKind::Semi(expr) => match expr.kind {
326 // `{ return ..; }` => `return ..`
327 ExprKind::Ret(..) => &expr.kind,
343 ) -> Option<(BinOpKind, &'a Expr<'a>, &'a Expr<'a>)> {
345 BinOpKind::Add | BinOpKind::Eq | BinOpKind::Ne | BinOpKind::BitAnd | BinOpKind::BitXor | BinOpKind::BitOr => {
346 Some((binop, rhs, lhs))
348 BinOpKind::Lt => Some((BinOpKind::Gt, rhs, lhs)),
349 BinOpKind::Le => Some((BinOpKind::Ge, rhs, lhs)),
350 BinOpKind::Ge => Some((BinOpKind::Le, rhs, lhs)),
351 BinOpKind::Gt => Some((BinOpKind::Lt, rhs, lhs)),
352 BinOpKind::Mul // Not always commutative, e.g. with matrices. See issue #5698
359 | BinOpKind::Or => None,
363 /// Checks if the two `Option`s are both `None` or some equal values as per
365 pub fn both<X>(l: &Option<X>, r: &Option<X>, mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
367 .map_or_else(|| r.is_none(), |x| r.as_ref().map_or(false, |y| eq_fn(x, y)))
370 /// Checks if two slices are equal as per `eq_fn`.
371 pub fn over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
372 left.len() == right.len() && left.iter().zip(right).all(|(x, y)| eq_fn(x, y))
375 /// Checks if two expressions evaluate to the same value, and don't contain any side effects.
376 pub fn eq_expr_value(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>) -> bool {
377 SpanlessEq::new(cx).deny_side_effects().eq_expr(left, right)
380 /// Type used to hash an ast element. This is different from the `Hash` trait
381 /// on ast types as this
382 /// trait would consider IDs and spans.
384 /// All expressions kind are hashed, but some might have a weaker hash.
385 pub struct SpanlessHash<'a, 'tcx> {
386 /// Context used to evaluate constant expressions.
387 cx: &'a LateContext<'tcx>,
388 maybe_typeck_results: Option<&'tcx TypeckResults<'tcx>>,
392 impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
393 pub fn new(cx: &'a LateContext<'tcx>) -> Self {
396 maybe_typeck_results: cx.maybe_typeck_results(),
397 s: StableHasher::new(),
401 pub fn finish(self) -> u64 {
405 pub fn hash_block(&mut self, b: &Block<'_>) {
410 if let Some(ref e) = b.expr {
415 BlockCheckMode::DefaultBlock => 0,
416 BlockCheckMode::UnsafeBlock(_) => 1,
417 BlockCheckMode::PushUnsafeBlock(_) => 2,
418 BlockCheckMode::PopUnsafeBlock(_) => 3,
423 #[allow(clippy::many_single_char_names, clippy::too_many_lines)]
424 pub fn hash_expr(&mut self, e: &Expr<'_>) {
425 let simple_const = self
426 .maybe_typeck_results
427 .and_then(|typeck_results| constant_simple(self.cx, typeck_results, e));
429 // const hashing may result in the same hash as some unrelated node, so add a sort of
430 // discriminant depending on which path we're choosing next
431 simple_const.is_some().hash(&mut self.s);
433 if let Some(e) = simple_const {
434 return e.hash(&mut self.s);
437 std::mem::discriminant(&e.kind).hash(&mut self.s);
440 ExprKind::AddrOf(kind, m, ref e) => {
442 BorrowKind::Ref => 0,
443 BorrowKind::Raw => 1,
449 ExprKind::Continue(i) => {
450 if let Some(i) = i.label {
451 self.hash_name(i.ident.name);
454 ExprKind::Assign(ref l, ref r, _) => {
458 ExprKind::AssignOp(ref o, ref l, ref r) => {
460 .hash_stable(&mut self.cx.tcx.get_stable_hashing_context(), &mut self.s);
464 ExprKind::Block(ref b, _) => {
467 ExprKind::Binary(op, ref l, ref r) => {
469 .hash_stable(&mut self.cx.tcx.get_stable_hashing_context(), &mut self.s);
473 ExprKind::Break(i, ref j) => {
474 if let Some(i) = i.label {
475 self.hash_name(i.ident.name);
477 if let Some(ref j) = *j {
481 ExprKind::Box(ref e) | ExprKind::DropTemps(ref e) | ExprKind::Yield(ref e, _) => {
484 ExprKind::Call(ref fun, args) => {
486 self.hash_exprs(args);
488 ExprKind::Cast(ref e, ref ty) | ExprKind::Type(ref e, ref ty) => {
492 ExprKind::Closure(cap, _, eid, _, _) => {
494 CaptureBy::Value => 0,
498 // closures inherit TypeckResults
499 self.hash_expr(&self.cx.tcx.hir().body(eid).value);
501 ExprKind::Field(ref e, ref f) => {
503 self.hash_name(f.name);
505 ExprKind::Index(ref a, ref i) => {
509 ExprKind::InlineAsm(ref asm) => {
510 for piece in asm.template {
512 InlineAsmTemplatePiece::String(s) => s.hash(&mut self.s),
513 InlineAsmTemplatePiece::Placeholder {
518 operand_idx.hash(&mut self.s);
519 modifier.hash(&mut self.s);
523 asm.options.hash(&mut self.s);
524 for (op, _op_sp) in asm.operands {
526 InlineAsmOperand::In { reg, expr } => {
527 reg.hash(&mut self.s);
528 self.hash_expr(expr);
530 InlineAsmOperand::Out { reg, late, expr } => {
531 reg.hash(&mut self.s);
532 late.hash(&mut self.s);
533 if let Some(expr) = expr {
534 self.hash_expr(expr);
537 InlineAsmOperand::InOut { reg, late, expr } => {
538 reg.hash(&mut self.s);
539 late.hash(&mut self.s);
540 self.hash_expr(expr);
542 InlineAsmOperand::SplitInOut {
548 reg.hash(&mut self.s);
549 late.hash(&mut self.s);
550 self.hash_expr(in_expr);
551 if let Some(out_expr) = out_expr {
552 self.hash_expr(out_expr);
555 InlineAsmOperand::Const { expr } | InlineAsmOperand::Sym { expr } => self.hash_expr(expr),
559 ExprKind::LlvmInlineAsm(..) | ExprKind::Err => {},
560 ExprKind::Lit(ref l) => {
561 l.node.hash(&mut self.s);
563 ExprKind::Loop(ref b, ref i, ..) => {
565 if let Some(i) = *i {
566 self.hash_name(i.ident.name);
569 ExprKind::If(ref cond, ref then, ref else_opt) => {
570 let c: fn(_, _, _) -> _ = ExprKind::If;
572 self.hash_expr(cond);
573 self.hash_expr(&**then);
574 if let Some(ref e) = *else_opt {
578 ExprKind::Match(ref e, arms, ref s) => {
583 if let Some(ref e) = arm.guard {
586 self.hash_expr(&arm.body);
591 ExprKind::MethodCall(ref path, ref _tys, args, ref _fn_span) => {
592 self.hash_name(path.ident.name);
593 self.hash_exprs(args);
595 ExprKind::ConstBlock(ref l_id) => {
596 self.hash_body(l_id.body);
598 ExprKind::Repeat(ref e, ref l_id) => {
600 self.hash_body(l_id.body);
602 ExprKind::Ret(ref e) => {
603 if let Some(ref e) = *e {
607 ExprKind::Path(ref qpath) => {
608 self.hash_qpath(qpath);
610 ExprKind::Struct(ref path, fields, ref expr) => {
611 self.hash_qpath(path);
614 self.hash_name(f.ident.name);
615 self.hash_expr(&f.expr);
618 if let Some(ref e) = *expr {
622 ExprKind::Tup(tup) => {
623 self.hash_exprs(tup);
625 ExprKind::Array(v) => {
628 ExprKind::Unary(lop, ref le) => {
629 lop.hash_stable(&mut self.cx.tcx.get_stable_hashing_context(), &mut self.s);
635 pub fn hash_exprs(&mut self, e: &[Expr<'_>]) {
641 pub fn hash_name(&mut self, n: Symbol) {
642 n.as_str().hash(&mut self.s);
645 pub fn hash_qpath(&mut self, p: &QPath<'_>) {
647 QPath::Resolved(_, ref path) => {
648 self.hash_path(path);
650 QPath::TypeRelative(_, ref path) => {
651 self.hash_name(path.ident.name);
653 QPath::LangItem(lang_item, ..) => {
654 lang_item.hash_stable(&mut self.cx.tcx.get_stable_hashing_context(), &mut self.s);
657 // self.maybe_typeck_results.unwrap().qpath_res(p, id).hash(&mut self.s);
660 pub fn hash_path(&mut self, p: &Path<'_>) {
661 p.is_global().hash(&mut self.s);
662 for p in p.segments {
663 self.hash_name(p.ident.name);
667 pub fn hash_stmt(&mut self, b: &Stmt<'_>) {
668 std::mem::discriminant(&b.kind).hash(&mut self.s);
671 StmtKind::Local(local) => {
672 if let Some(ref init) = local.init {
673 self.hash_expr(init);
676 StmtKind::Item(..) => {},
677 StmtKind::Expr(expr) | StmtKind::Semi(expr) => {
678 self.hash_expr(expr);
683 pub fn hash_guard(&mut self, g: &Guard<'_>) {
685 Guard::If(ref expr) | Guard::IfLet(_, ref expr) => {
686 self.hash_expr(expr);
691 pub fn hash_lifetime(&mut self, lifetime: &Lifetime) {
692 std::mem::discriminant(&lifetime.name).hash(&mut self.s);
693 if let LifetimeName::Param(ref name) = lifetime.name {
694 std::mem::discriminant(name).hash(&mut self.s);
696 ParamName::Plain(ref ident) => {
697 ident.name.hash(&mut self.s);
699 ParamName::Fresh(ref size) => {
700 size.hash(&mut self.s);
702 ParamName::Error => {},
707 pub fn hash_ty(&mut self, ty: &Ty<'_>) {
708 self.hash_tykind(&ty.kind);
711 pub fn hash_tykind(&mut self, ty: &TyKind<'_>) {
712 std::mem::discriminant(ty).hash(&mut self.s);
714 TyKind::Slice(ty) => {
717 TyKind::Array(ty, anon_const) => {
719 self.hash_body(anon_const.body);
721 TyKind::Ptr(mut_ty) => {
722 self.hash_ty(&mut_ty.ty);
723 mut_ty.mutbl.hash(&mut self.s);
725 TyKind::Rptr(lifetime, mut_ty) => {
726 self.hash_lifetime(lifetime);
727 self.hash_ty(&mut_ty.ty);
728 mut_ty.mutbl.hash(&mut self.s);
730 TyKind::BareFn(bfn) => {
731 bfn.unsafety.hash(&mut self.s);
732 bfn.abi.hash(&mut self.s);
733 for arg in bfn.decl.inputs {
736 match bfn.decl.output {
737 FnRetTy::DefaultReturn(_) => {
738 ().hash(&mut self.s);
740 FnRetTy::Return(ref ty) => {
744 bfn.decl.c_variadic.hash(&mut self.s);
746 TyKind::Tup(ty_list) => {
751 TyKind::Path(qpath) => match qpath {
752 QPath::Resolved(ref maybe_ty, ref path) => {
753 if let Some(ref ty) = maybe_ty {
756 for segment in path.segments {
757 segment.ident.name.hash(&mut self.s);
758 self.hash_generic_args(segment.args().args);
761 QPath::TypeRelative(ref ty, ref segment) => {
763 segment.ident.name.hash(&mut self.s);
765 QPath::LangItem(lang_item, ..) => {
766 lang_item.hash(&mut self.s);
769 TyKind::OpaqueDef(_, arg_list) => {
770 self.hash_generic_args(arg_list);
772 TyKind::TraitObject(_, lifetime) => {
773 self.hash_lifetime(lifetime);
775 TyKind::Typeof(anon_const) => {
776 self.hash_body(anon_const.body);
778 TyKind::Err | TyKind::Infer | TyKind::Never => {},
782 pub fn hash_body(&mut self, body_id: BodyId) {
783 // swap out TypeckResults when hashing a body
784 let old_maybe_typeck_results = self.maybe_typeck_results.replace(self.cx.tcx.typeck_body(body_id));
785 self.hash_expr(&self.cx.tcx.hir().body(body_id).value);
786 self.maybe_typeck_results = old_maybe_typeck_results;
789 fn hash_generic_args(&mut self, arg_list: &[GenericArg<'_>]) {
790 for arg in arg_list {
792 GenericArg::Lifetime(ref l) => self.hash_lifetime(l),
793 GenericArg::Type(ref ty) => self.hash_ty(&ty),
794 GenericArg::Const(ref ca) => self.hash_body(ca.value.body),