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,
27 expr_fallback: Option<Box<dyn Fn(&Expr<'_>, &Expr<'_>) -> bool + 'a>>,
30 impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
31 pub fn new(cx: &'a LateContext<'tcx>) -> Self {
34 maybe_typeck_results: cx.maybe_typeck_results(),
35 allow_side_effects: true,
40 /// Consider expressions containing potential side effects as not equal.
41 pub fn deny_side_effects(self) -> Self {
43 allow_side_effects: false,
48 pub fn expr_fallback(self, expr_fallback: impl Fn(&Expr<'_>, &Expr<'_>) -> bool + 'a) -> Self {
50 expr_fallback: Some(Box::new(expr_fallback)),
55 /// Checks whether two statements are the same.
56 pub fn eq_stmt(&mut self, left: &Stmt<'_>, right: &Stmt<'_>) -> bool {
57 match (&left.kind, &right.kind) {
58 (&StmtKind::Local(ref l), &StmtKind::Local(ref r)) => {
59 self.eq_pat(&l.pat, &r.pat)
60 && both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r))
61 && both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
63 (&StmtKind::Expr(ref l), &StmtKind::Expr(ref r)) | (&StmtKind::Semi(ref l), &StmtKind::Semi(ref r)) => {
70 /// Checks whether two blocks are the same.
71 pub fn eq_block(&mut self, left: &Block<'_>, right: &Block<'_>) -> bool {
72 over(&left.stmts, &right.stmts, |l, r| self.eq_stmt(l, r))
73 && both(&left.expr, &right.expr, |l, r| self.eq_expr(l, r))
76 #[allow(clippy::similar_names)]
77 pub fn eq_expr(&mut self, left: &Expr<'_>, right: &Expr<'_>) -> bool {
78 if !self.allow_side_effects && differing_macro_contexts(left.span, right.span) {
82 if let Some(typeck_results) = self.maybe_typeck_results {
83 if let (Some(l), Some(r)) = (
84 constant_simple(self.cx, typeck_results, left),
85 constant_simple(self.cx, typeck_results, right),
93 let is_eq = match (&reduce_exprkind(&left.kind), &reduce_exprkind(&right.kind)) {
94 (&ExprKind::AddrOf(lb, l_mut, ref le), &ExprKind::AddrOf(rb, r_mut, ref re)) => {
95 lb == rb && l_mut == r_mut && self.eq_expr(le, re)
97 (&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
98 both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
100 (&ExprKind::Assign(ref ll, ref lr, _), &ExprKind::Assign(ref rl, ref rr, _)) => {
101 self.allow_side_effects && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
103 (&ExprKind::AssignOp(ref lo, ref ll, ref lr), &ExprKind::AssignOp(ref ro, ref rl, ref rr)) => {
104 self.allow_side_effects && lo.node == ro.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
106 (&ExprKind::Block(ref l, _), &ExprKind::Block(ref r, _)) => self.eq_block(l, r),
107 (&ExprKind::Binary(l_op, ref ll, ref lr), &ExprKind::Binary(r_op, ref rl, ref rr)) => {
108 l_op.node == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
109 || swap_binop(l_op.node, ll, lr).map_or(false, |(l_op, ll, lr)| {
110 l_op == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
113 (&ExprKind::Break(li, ref le), &ExprKind::Break(ri, ref re)) => {
114 both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
115 && both(le, re, |l, r| self.eq_expr(l, r))
117 (&ExprKind::Box(ref l), &ExprKind::Box(ref r)) => self.eq_expr(l, r),
118 (&ExprKind::Call(l_fun, l_args), &ExprKind::Call(r_fun, r_args)) => {
119 self.allow_side_effects && self.eq_expr(l_fun, r_fun) && self.eq_exprs(l_args, r_args)
121 (&ExprKind::Cast(ref lx, ref lt), &ExprKind::Cast(ref rx, ref rt))
122 | (&ExprKind::Type(ref lx, ref lt), &ExprKind::Type(ref rx, ref rt)) => {
123 self.eq_expr(lx, rx) && self.eq_ty(lt, rt)
125 (&ExprKind::Field(ref l_f_exp, ref l_f_ident), &ExprKind::Field(ref r_f_exp, ref r_f_ident)) => {
126 l_f_ident.name == r_f_ident.name && self.eq_expr(l_f_exp, r_f_exp)
128 (&ExprKind::Index(ref la, ref li), &ExprKind::Index(ref ra, ref ri)) => {
129 self.eq_expr(la, ra) && self.eq_expr(li, ri)
131 (&ExprKind::If(ref lc, ref lt, ref le), &ExprKind::If(ref rc, ref rt, ref re)) => {
132 self.eq_expr(lc, rc) && self.eq_expr(&**lt, &**rt) && both(le, re, |l, r| self.eq_expr(l, r))
134 (&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node,
135 (&ExprKind::Loop(ref lb, ref ll, ref lls, _), &ExprKind::Loop(ref rb, ref rl, ref rls, _)) => {
136 lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.name == r.ident.name)
138 (&ExprKind::Match(ref le, ref la, ref ls), &ExprKind::Match(ref re, ref ra, ref rs)) => {
140 && self.eq_expr(le, re)
141 && over(la, ra, |l, r| {
142 self.eq_expr(&l.body, &r.body)
143 && both(&l.guard, &r.guard, |l, r| self.eq_guard(l, r))
144 && self.eq_pat(&l.pat, &r.pat)
147 (&ExprKind::MethodCall(l_path, _, l_args, _), &ExprKind::MethodCall(r_path, _, r_args, _)) => {
148 self.allow_side_effects && self.eq_path_segment(l_path, r_path) && self.eq_exprs(l_args, r_args)
150 (&ExprKind::Repeat(ref le, ref ll_id), &ExprKind::Repeat(ref re, ref rl_id)) => {
151 let mut celcx = constant_context(self.cx, self.cx.tcx.typeck_body(ll_id.body));
152 let ll = celcx.expr(&self.cx.tcx.hir().body(ll_id.body).value);
153 let mut celcx = constant_context(self.cx, self.cx.tcx.typeck_body(rl_id.body));
154 let rl = celcx.expr(&self.cx.tcx.hir().body(rl_id.body).value);
156 self.eq_expr(le, re) && ll == rl
158 (&ExprKind::Ret(ref l), &ExprKind::Ret(ref r)) => both(l, r, |l, r| self.eq_expr(l, r)),
159 (&ExprKind::Path(ref l), &ExprKind::Path(ref r)) => self.eq_qpath(l, r),
160 (&ExprKind::Struct(ref l_path, ref lf, ref lo), &ExprKind::Struct(ref r_path, ref rf, ref ro)) => {
161 self.eq_qpath(l_path, r_path)
162 && both(lo, ro, |l, r| self.eq_expr(l, r))
163 && over(lf, rf, |l, r| self.eq_field(l, r))
165 (&ExprKind::Tup(l_tup), &ExprKind::Tup(r_tup)) => self.eq_exprs(l_tup, r_tup),
166 (&ExprKind::Unary(l_op, ref le), &ExprKind::Unary(r_op, ref re)) => l_op == r_op && self.eq_expr(le, re),
167 (&ExprKind::Array(l), &ExprKind::Array(r)) => self.eq_exprs(l, r),
168 (&ExprKind::DropTemps(ref le), &ExprKind::DropTemps(ref re)) => self.eq_expr(le, re),
171 is_eq || self.expr_fallback.as_ref().map_or(false, |f| f(left, right))
174 fn eq_exprs(&mut self, left: &[Expr<'_>], right: &[Expr<'_>]) -> bool {
175 over(left, right, |l, r| self.eq_expr(l, r))
178 fn eq_field(&mut self, left: &Field<'_>, right: &Field<'_>) -> bool {
179 left.ident.name == right.ident.name && self.eq_expr(&left.expr, &right.expr)
182 fn eq_guard(&mut self, left: &Guard<'_>, right: &Guard<'_>) -> bool {
183 match (left, right) {
184 (Guard::If(l), Guard::If(r)) => self.eq_expr(l, r),
185 (Guard::IfLet(lp, le), Guard::IfLet(rp, re)) => self.eq_pat(lp, rp) && self.eq_expr(le, re),
190 fn eq_generic_arg(&mut self, left: &GenericArg<'_>, right: &GenericArg<'_>) -> bool {
191 match (left, right) {
192 (GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => Self::eq_lifetime(l_lt, r_lt),
193 (GenericArg::Type(l_ty), GenericArg::Type(r_ty)) => self.eq_ty(l_ty, r_ty),
198 fn eq_lifetime(left: &Lifetime, right: &Lifetime) -> bool {
199 left.name == right.name
202 pub fn eq_fieldpat(&mut self, left: &FieldPat<'_>, right: &FieldPat<'_>) -> bool {
203 let (FieldPat { ident: li, pat: lp, .. }, FieldPat { ident: ri, pat: rp, .. }) = (&left, &right);
204 li.name == ri.name && self.eq_pat(lp, rp)
207 /// Checks whether two patterns are the same.
208 pub fn eq_pat(&mut self, left: &Pat<'_>, right: &Pat<'_>) -> bool {
209 match (&left.kind, &right.kind) {
210 (&PatKind::Box(ref l), &PatKind::Box(ref r)) => self.eq_pat(l, r),
211 (&PatKind::Struct(ref lp, ref la, ..), &PatKind::Struct(ref rp, ref ra, ..)) => {
212 self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_fieldpat(l, r))
214 (&PatKind::TupleStruct(ref lp, ref la, ls), &PatKind::TupleStruct(ref rp, ref ra, rs)) => {
215 self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat(l, r)) && ls == rs
217 (&PatKind::Binding(ref lb, .., ref li, ref lp), &PatKind::Binding(ref rb, .., ref ri, ref rp)) => {
218 lb == rb && li.name == ri.name && both(lp, rp, |l, r| self.eq_pat(l, r))
220 (&PatKind::Path(ref l), &PatKind::Path(ref r)) => self.eq_qpath(l, r),
221 (&PatKind::Lit(ref l), &PatKind::Lit(ref r)) => self.eq_expr(l, r),
222 (&PatKind::Tuple(ref l, ls), &PatKind::Tuple(ref r, rs)) => {
223 ls == rs && over(l, r, |l, r| self.eq_pat(l, r))
225 (&PatKind::Range(ref ls, ref le, li), &PatKind::Range(ref rs, ref re, ri)) => {
226 both(ls, rs, |a, b| self.eq_expr(a, b)) && both(le, re, |a, b| self.eq_expr(a, b)) && (li == ri)
228 (&PatKind::Ref(ref le, ref lm), &PatKind::Ref(ref re, ref rm)) => lm == rm && self.eq_pat(le, re),
229 (&PatKind::Slice(ref ls, ref li, ref le), &PatKind::Slice(ref rs, ref ri, ref re)) => {
230 over(ls, rs, |l, r| self.eq_pat(l, r))
231 && over(le, re, |l, r| self.eq_pat(l, r))
232 && both(li, ri, |l, r| self.eq_pat(l, r))
234 (&PatKind::Wild, &PatKind::Wild) => true,
239 #[allow(clippy::similar_names)]
240 fn eq_qpath(&mut self, left: &QPath<'_>, right: &QPath<'_>) -> bool {
241 match (left, right) {
242 (&QPath::Resolved(ref lty, ref lpath), &QPath::Resolved(ref rty, ref rpath)) => {
243 both(lty, rty, |l, r| self.eq_ty(l, r)) && self.eq_path(lpath, rpath)
245 (&QPath::TypeRelative(ref lty, ref lseg), &QPath::TypeRelative(ref rty, ref rseg)) => {
246 self.eq_ty(lty, rty) && self.eq_path_segment(lseg, rseg)
248 (&QPath::LangItem(llang_item, _), &QPath::LangItem(rlang_item, _)) => llang_item == rlang_item,
253 fn eq_path(&mut self, left: &Path<'_>, right: &Path<'_>) -> bool {
254 left.is_global() == right.is_global()
255 && over(&left.segments, &right.segments, |l, r| self.eq_path_segment(l, r))
258 fn eq_path_parameters(&mut self, left: &GenericArgs<'_>, right: &GenericArgs<'_>) -> bool {
259 if !(left.parenthesized || right.parenthesized) {
260 over(&left.args, &right.args, |l, r| self.eq_generic_arg(l, r)) // FIXME(flip1995): may not work
261 && over(&left.bindings, &right.bindings, |l, r| self.eq_type_binding(l, r))
262 } else if left.parenthesized && right.parenthesized {
263 over(left.inputs(), right.inputs(), |l, r| self.eq_ty(l, r))
264 && both(&Some(&left.bindings[0].ty()), &Some(&right.bindings[0].ty()), |l, r| {
272 pub fn eq_path_segments(&mut self, left: &[PathSegment<'_>], right: &[PathSegment<'_>]) -> bool {
273 left.len() == right.len() && left.iter().zip(right).all(|(l, r)| self.eq_path_segment(l, r))
276 pub fn eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_>) -> bool {
277 // The == of idents doesn't work with different contexts,
278 // we have to be explicit about hygiene
279 left.ident.name == right.ident.name && both(&left.args, &right.args, |l, r| self.eq_path_parameters(l, r))
282 pub fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {
283 self.eq_ty_kind(&left.kind, &right.kind)
286 #[allow(clippy::similar_names)]
287 pub fn eq_ty_kind(&mut self, left: &TyKind<'_>, right: &TyKind<'_>) -> bool {
288 match (left, right) {
289 (&TyKind::Slice(ref l_vec), &TyKind::Slice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
290 (&TyKind::Array(ref lt, ref ll_id), &TyKind::Array(ref rt, ref rl_id)) => {
291 let old_maybe_typeck_results = self.maybe_typeck_results;
293 let mut celcx = constant_context(self.cx, self.cx.tcx.typeck_body(ll_id.body));
294 self.maybe_typeck_results = Some(self.cx.tcx.typeck_body(ll_id.body));
295 let ll = celcx.expr(&self.cx.tcx.hir().body(ll_id.body).value);
297 let mut celcx = constant_context(self.cx, self.cx.tcx.typeck_body(rl_id.body));
298 self.maybe_typeck_results = Some(self.cx.tcx.typeck_body(rl_id.body));
299 let rl = celcx.expr(&self.cx.tcx.hir().body(rl_id.body).value);
301 let eq_ty = self.eq_ty(lt, rt);
302 self.maybe_typeck_results = old_maybe_typeck_results;
305 (&TyKind::Ptr(ref l_mut), &TyKind::Ptr(ref r_mut)) => {
306 l_mut.mutbl == r_mut.mutbl && self.eq_ty(&*l_mut.ty, &*r_mut.ty)
308 (&TyKind::Rptr(_, ref l_rmut), &TyKind::Rptr(_, ref r_rmut)) => {
309 l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(&*l_rmut.ty, &*r_rmut.ty)
311 (&TyKind::Path(ref l), &TyKind::Path(ref r)) => self.eq_qpath(l, r),
312 (&TyKind::Tup(ref l), &TyKind::Tup(ref r)) => over(l, r, |l, r| self.eq_ty(l, r)),
313 (&TyKind::Infer, &TyKind::Infer) => true,
318 fn eq_type_binding(&mut self, left: &TypeBinding<'_>, right: &TypeBinding<'_>) -> bool {
319 left.ident.name == right.ident.name && self.eq_ty(&left.ty(), &right.ty())
323 /// Some simple reductions like `{ return }` => `return`
324 fn reduce_exprkind<'hir>(kind: &'hir ExprKind<'hir>) -> &ExprKind<'hir> {
325 if let ExprKind::Block(block, _) = kind {
326 match (block.stmts, block.expr) {
328 ([], None) => &ExprKind::Tup(&[]),
329 ([], Some(expr)) => match expr.kind {
330 // `{ return .. }` => `return ..`
331 ExprKind::Ret(..) => &expr.kind,
334 ([stmt], None) => match stmt.kind {
335 StmtKind::Expr(expr) | StmtKind::Semi(expr) => match expr.kind {
336 // `{ return ..; }` => `return ..`
337 ExprKind::Ret(..) => &expr.kind,
353 ) -> Option<(BinOpKind, &'a Expr<'a>, &'a Expr<'a>)> {
355 BinOpKind::Add | BinOpKind::Eq | BinOpKind::Ne | BinOpKind::BitAnd | BinOpKind::BitXor | BinOpKind::BitOr => {
356 Some((binop, rhs, lhs))
358 BinOpKind::Lt => Some((BinOpKind::Gt, rhs, lhs)),
359 BinOpKind::Le => Some((BinOpKind::Ge, rhs, lhs)),
360 BinOpKind::Ge => Some((BinOpKind::Le, rhs, lhs)),
361 BinOpKind::Gt => Some((BinOpKind::Lt, rhs, lhs)),
362 BinOpKind::Mul // Not always commutative, e.g. with matrices. See issue #5698
369 | BinOpKind::Or => None,
373 /// Checks if the two `Option`s are both `None` or some equal values as per
375 pub fn both<X>(l: &Option<X>, r: &Option<X>, mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
377 .map_or_else(|| r.is_none(), |x| r.as_ref().map_or(false, |y| eq_fn(x, y)))
380 /// Checks if two slices are equal as per `eq_fn`.
381 pub fn over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
382 left.len() == right.len() && left.iter().zip(right).all(|(x, y)| eq_fn(x, y))
385 /// Checks if two expressions evaluate to the same value, and don't contain any side effects.
386 pub fn eq_expr_value(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>) -> bool {
387 SpanlessEq::new(cx).deny_side_effects().eq_expr(left, right)
390 /// Type used to hash an ast element. This is different from the `Hash` trait
391 /// on ast types as this
392 /// trait would consider IDs and spans.
394 /// All expressions kind are hashed, but some might have a weaker hash.
395 pub struct SpanlessHash<'a, 'tcx> {
396 /// Context used to evaluate constant expressions.
397 cx: &'a LateContext<'tcx>,
398 maybe_typeck_results: Option<&'tcx TypeckResults<'tcx>>,
402 impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
403 pub fn new(cx: &'a LateContext<'tcx>) -> Self {
406 maybe_typeck_results: cx.maybe_typeck_results(),
407 s: StableHasher::new(),
411 pub fn finish(self) -> u64 {
415 pub fn hash_block(&mut self, b: &Block<'_>) {
420 if let Some(ref e) = b.expr {
425 BlockCheckMode::DefaultBlock => 0,
426 BlockCheckMode::UnsafeBlock(_) => 1,
427 BlockCheckMode::PushUnsafeBlock(_) => 2,
428 BlockCheckMode::PopUnsafeBlock(_) => 3,
433 #[allow(clippy::many_single_char_names, clippy::too_many_lines)]
434 pub fn hash_expr(&mut self, e: &Expr<'_>) {
435 let simple_const = self
436 .maybe_typeck_results
437 .and_then(|typeck_results| constant_simple(self.cx, typeck_results, e));
439 // const hashing may result in the same hash as some unrelated node, so add a sort of
440 // discriminant depending on which path we're choosing next
441 simple_const.is_some().hash(&mut self.s);
443 if let Some(e) = simple_const {
444 return e.hash(&mut self.s);
447 std::mem::discriminant(&e.kind).hash(&mut self.s);
450 ExprKind::AddrOf(kind, m, ref e) => {
452 BorrowKind::Ref => 0,
453 BorrowKind::Raw => 1,
459 ExprKind::Continue(i) => {
460 if let Some(i) = i.label {
461 self.hash_name(i.ident.name);
464 ExprKind::Assign(ref l, ref r, _) => {
468 ExprKind::AssignOp(ref o, ref l, ref r) => {
470 .hash_stable(&mut self.cx.tcx.get_stable_hashing_context(), &mut self.s);
474 ExprKind::Block(ref b, _) => {
477 ExprKind::Binary(op, ref l, ref r) => {
479 .hash_stable(&mut self.cx.tcx.get_stable_hashing_context(), &mut self.s);
483 ExprKind::Break(i, ref j) => {
484 if let Some(i) = i.label {
485 self.hash_name(i.ident.name);
487 if let Some(ref j) = *j {
491 ExprKind::Box(ref e) | ExprKind::DropTemps(ref e) | ExprKind::Yield(ref e, _) => {
494 ExprKind::Call(ref fun, args) => {
496 self.hash_exprs(args);
498 ExprKind::Cast(ref e, ref ty) | ExprKind::Type(ref e, ref ty) => {
502 ExprKind::Closure(cap, _, eid, _, _) => {
504 CaptureBy::Value => 0,
508 // closures inherit TypeckResults
509 self.hash_expr(&self.cx.tcx.hir().body(eid).value);
511 ExprKind::Field(ref e, ref f) => {
513 self.hash_name(f.name);
515 ExprKind::Index(ref a, ref i) => {
519 ExprKind::InlineAsm(ref asm) => {
520 for piece in asm.template {
522 InlineAsmTemplatePiece::String(s) => s.hash(&mut self.s),
523 InlineAsmTemplatePiece::Placeholder {
528 operand_idx.hash(&mut self.s);
529 modifier.hash(&mut self.s);
533 asm.options.hash(&mut self.s);
534 for (op, _op_sp) in asm.operands {
536 InlineAsmOperand::In { reg, expr } => {
537 reg.hash(&mut self.s);
538 self.hash_expr(expr);
540 InlineAsmOperand::Out { reg, late, expr } => {
541 reg.hash(&mut self.s);
542 late.hash(&mut self.s);
543 if let Some(expr) = expr {
544 self.hash_expr(expr);
547 InlineAsmOperand::InOut { reg, late, expr } => {
548 reg.hash(&mut self.s);
549 late.hash(&mut self.s);
550 self.hash_expr(expr);
552 InlineAsmOperand::SplitInOut {
558 reg.hash(&mut self.s);
559 late.hash(&mut self.s);
560 self.hash_expr(in_expr);
561 if let Some(out_expr) = out_expr {
562 self.hash_expr(out_expr);
565 InlineAsmOperand::Const { expr } | InlineAsmOperand::Sym { expr } => self.hash_expr(expr),
569 ExprKind::LlvmInlineAsm(..) | ExprKind::Err => {},
570 ExprKind::Lit(ref l) => {
571 l.node.hash(&mut self.s);
573 ExprKind::Loop(ref b, ref i, ..) => {
575 if let Some(i) = *i {
576 self.hash_name(i.ident.name);
579 ExprKind::If(ref cond, ref then, ref else_opt) => {
580 let c: fn(_, _, _) -> _ = ExprKind::If;
582 self.hash_expr(cond);
583 self.hash_expr(&**then);
584 if let Some(ref e) = *else_opt {
588 ExprKind::Match(ref e, arms, ref s) => {
593 if let Some(ref e) = arm.guard {
596 self.hash_expr(&arm.body);
601 ExprKind::MethodCall(ref path, ref _tys, args, ref _fn_span) => {
602 self.hash_name(path.ident.name);
603 self.hash_exprs(args);
605 ExprKind::ConstBlock(ref l_id) => {
606 self.hash_body(l_id.body);
608 ExprKind::Repeat(ref e, ref l_id) => {
610 self.hash_body(l_id.body);
612 ExprKind::Ret(ref e) => {
613 if let Some(ref e) = *e {
617 ExprKind::Path(ref qpath) => {
618 self.hash_qpath(qpath);
620 ExprKind::Struct(ref path, fields, ref expr) => {
621 self.hash_qpath(path);
624 self.hash_name(f.ident.name);
625 self.hash_expr(&f.expr);
628 if let Some(ref e) = *expr {
632 ExprKind::Tup(tup) => {
633 self.hash_exprs(tup);
635 ExprKind::Array(v) => {
638 ExprKind::Unary(lop, ref le) => {
639 lop.hash_stable(&mut self.cx.tcx.get_stable_hashing_context(), &mut self.s);
645 pub fn hash_exprs(&mut self, e: &[Expr<'_>]) {
651 pub fn hash_name(&mut self, n: Symbol) {
652 n.as_str().hash(&mut self.s);
655 pub fn hash_qpath(&mut self, p: &QPath<'_>) {
657 QPath::Resolved(_, ref path) => {
658 self.hash_path(path);
660 QPath::TypeRelative(_, ref path) => {
661 self.hash_name(path.ident.name);
663 QPath::LangItem(lang_item, ..) => {
664 lang_item.hash_stable(&mut self.cx.tcx.get_stable_hashing_context(), &mut self.s);
667 // self.maybe_typeck_results.unwrap().qpath_res(p, id).hash(&mut self.s);
670 pub fn hash_path(&mut self, p: &Path<'_>) {
671 p.is_global().hash(&mut self.s);
672 for p in p.segments {
673 self.hash_name(p.ident.name);
677 pub fn hash_stmt(&mut self, b: &Stmt<'_>) {
678 std::mem::discriminant(&b.kind).hash(&mut self.s);
681 StmtKind::Local(local) => {
682 if let Some(ref init) = local.init {
683 self.hash_expr(init);
686 StmtKind::Item(..) => {},
687 StmtKind::Expr(expr) | StmtKind::Semi(expr) => {
688 self.hash_expr(expr);
693 pub fn hash_guard(&mut self, g: &Guard<'_>) {
695 Guard::If(ref expr) | Guard::IfLet(_, ref expr) => {
696 self.hash_expr(expr);
701 pub fn hash_lifetime(&mut self, lifetime: &Lifetime) {
702 std::mem::discriminant(&lifetime.name).hash(&mut self.s);
703 if let LifetimeName::Param(ref name) = lifetime.name {
704 std::mem::discriminant(name).hash(&mut self.s);
706 ParamName::Plain(ref ident) => {
707 ident.name.hash(&mut self.s);
709 ParamName::Fresh(ref size) => {
710 size.hash(&mut self.s);
712 ParamName::Error => {},
717 pub fn hash_ty(&mut self, ty: &Ty<'_>) {
718 self.hash_tykind(&ty.kind);
721 pub fn hash_tykind(&mut self, ty: &TyKind<'_>) {
722 std::mem::discriminant(ty).hash(&mut self.s);
724 TyKind::Slice(ty) => {
727 TyKind::Array(ty, anon_const) => {
729 self.hash_body(anon_const.body);
731 TyKind::Ptr(mut_ty) => {
732 self.hash_ty(&mut_ty.ty);
733 mut_ty.mutbl.hash(&mut self.s);
735 TyKind::Rptr(lifetime, mut_ty) => {
736 self.hash_lifetime(lifetime);
737 self.hash_ty(&mut_ty.ty);
738 mut_ty.mutbl.hash(&mut self.s);
740 TyKind::BareFn(bfn) => {
741 bfn.unsafety.hash(&mut self.s);
742 bfn.abi.hash(&mut self.s);
743 for arg in bfn.decl.inputs {
746 match bfn.decl.output {
747 FnRetTy::DefaultReturn(_) => {
748 ().hash(&mut self.s);
750 FnRetTy::Return(ref ty) => {
754 bfn.decl.c_variadic.hash(&mut self.s);
756 TyKind::Tup(ty_list) => {
761 TyKind::Path(qpath) => match qpath {
762 QPath::Resolved(ref maybe_ty, ref path) => {
763 if let Some(ref ty) = maybe_ty {
766 for segment in path.segments {
767 segment.ident.name.hash(&mut self.s);
768 self.hash_generic_args(segment.args().args);
771 QPath::TypeRelative(ref ty, ref segment) => {
773 segment.ident.name.hash(&mut self.s);
775 QPath::LangItem(lang_item, ..) => {
776 lang_item.hash(&mut self.s);
779 TyKind::OpaqueDef(_, arg_list) => {
780 self.hash_generic_args(arg_list);
782 TyKind::TraitObject(_, lifetime) => {
783 self.hash_lifetime(lifetime);
785 TyKind::Typeof(anon_const) => {
786 self.hash_body(anon_const.body);
788 TyKind::Err | TyKind::Infer | TyKind::Never => {},
792 pub fn hash_body(&mut self, body_id: BodyId) {
793 // swap out TypeckResults when hashing a body
794 let old_maybe_typeck_results = self.maybe_typeck_results.replace(self.cx.tcx.typeck_body(body_id));
795 self.hash_expr(&self.cx.tcx.hir().body(body_id).value);
796 self.maybe_typeck_results = old_maybe_typeck_results;
799 fn hash_generic_args(&mut self, arg_list: &[GenericArg<'_>]) {
800 for arg in arg_list {
802 GenericArg::Lifetime(ref l) => self.hash_lifetime(l),
803 GenericArg::Type(ref ty) => self.hash_ty(&ty),
804 GenericArg::Const(ref ca) => self.hash_body(ca.value.body),