4 use std::hash::{Hash, Hasher};
5 use std::collections::hash_map::DefaultHasher;
8 use utils::differing_macro_contexts;
10 /// Type used to check whether two ast are the same. This is different from the
12 /// `==` on ast types as this operator would compare true equality with ID and
15 /// Note that some expressions kinds are not considered but could be added.
16 pub struct SpanlessEq<'a, 'tcx: 'a> {
17 /// Context used to evaluate constant expressions.
18 cx: &'a LateContext<'a, 'tcx>,
19 /// If is true, never consider as equal expressions containing function
24 impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
25 pub fn new(cx: &'a LateContext<'a, 'tcx>) -> Self {
32 pub fn ignore_fn(self) -> Self {
39 /// Check whether two statements are the same.
40 pub fn eq_stmt(&self, left: &Stmt, right: &Stmt) -> bool {
41 match (&left.node, &right.node) {
42 (&StmtDecl(ref l, _), &StmtDecl(ref r, _)) => {
43 if let (&DeclLocal(ref l), &DeclLocal(ref r)) = (&l.node, &r.node) {
44 both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
49 (&StmtExpr(ref l, _), &StmtExpr(ref r, _)) | (&StmtSemi(ref l, _), &StmtSemi(ref r, _)) => {
56 /// Check whether two blocks are the same.
57 pub fn eq_block(&self, left: &Block, right: &Block) -> bool {
58 over(&left.stmts, &right.stmts, |l, r| self.eq_stmt(l, r))
59 && both(&left.expr, &right.expr, |l, r| self.eq_expr(l, r))
62 pub fn eq_expr(&self, left: &Expr, right: &Expr) -> bool {
63 if self.ignore_fn && differing_macro_contexts(left.span, right.span) {
67 if let (Some(l), Some(r)) = (constant(self.cx, left), constant(self.cx, right)) {
73 match (&left.node, &right.node) {
74 (&ExprAddrOf(l_mut, ref le), &ExprAddrOf(r_mut, ref re)) => l_mut == r_mut && self.eq_expr(le, re),
75 (&ExprAgain(li), &ExprAgain(ri)) => {
76 both(&li.ident, &ri.ident, |l, r| l.node.name.as_str() == r.node.name.as_str())
78 (&ExprAssign(ref ll, ref lr), &ExprAssign(ref rl, ref rr)) => self.eq_expr(ll, rl) && self.eq_expr(lr, rr),
79 (&ExprAssignOp(ref lo, ref ll, ref lr), &ExprAssignOp(ref ro, ref rl, ref rr)) => {
80 lo.node == ro.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
82 (&ExprBlock(ref l), &ExprBlock(ref r)) => self.eq_block(l, r),
83 (&ExprBinary(l_op, ref ll, ref lr), &ExprBinary(r_op, ref rl, ref rr)) => {
84 l_op.node == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
85 || swap_binop(l_op.node, ll, lr).map_or(false, |(l_op, ll, lr)| {
86 l_op == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
89 (&ExprBreak(li, ref le), &ExprBreak(ri, ref re)) => {
90 both(&li.ident, &ri.ident, |l, r| l.node.name.as_str() == r.node.name.as_str())
91 && both(le, re, |l, r| self.eq_expr(l, r))
93 (&ExprBox(ref l), &ExprBox(ref r)) => self.eq_expr(l, r),
94 (&ExprCall(ref l_fun, ref l_args), &ExprCall(ref r_fun, ref r_args)) => {
95 !self.ignore_fn && self.eq_expr(l_fun, r_fun) && self.eq_exprs(l_args, r_args)
97 (&ExprCast(ref lx, ref lt), &ExprCast(ref rx, ref rt)) |
98 (&ExprType(ref lx, ref lt), &ExprType(ref rx, ref rt)) => self.eq_expr(lx, rx) && self.eq_ty(lt, rt),
99 (&ExprField(ref l_f_exp, ref l_f_ident), &ExprField(ref r_f_exp, ref r_f_ident)) => {
100 l_f_ident.node == r_f_ident.node && self.eq_expr(l_f_exp, r_f_exp)
102 (&ExprIndex(ref la, ref li), &ExprIndex(ref ra, ref ri)) => self.eq_expr(la, ra) && self.eq_expr(li, ri),
103 (&ExprIf(ref lc, ref lt, ref le), &ExprIf(ref rc, ref rt, ref re)) => {
104 self.eq_expr(lc, rc) && self.eq_expr(&**lt, &**rt) && both(le, re, |l, r| self.eq_expr(l, r))
106 (&ExprLit(ref l), &ExprLit(ref r)) => l.node == r.node,
107 (&ExprLoop(ref lb, ref ll, ref lls), &ExprLoop(ref rb, ref rl, ref rls)) => {
108 lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.node.as_str() == r.node.as_str())
110 (&ExprMatch(ref le, ref la, ref ls), &ExprMatch(ref re, ref ra, ref rs)) => {
111 ls == rs && self.eq_expr(le, re) && over(la, ra, |l, r| {
112 self.eq_expr(&l.body, &r.body) && both(&l.guard, &r.guard, |l, r| self.eq_expr(l, r))
113 && over(&l.pats, &r.pats, |l, r| self.eq_pat(l, r))
116 (&ExprMethodCall(ref l_path, _, ref l_args), &ExprMethodCall(ref r_path, _, ref r_args)) => {
117 !self.ignore_fn && l_path == r_path && self.eq_exprs(l_args, r_args)
119 (&ExprRepeat(ref le, ll_id), &ExprRepeat(ref re, rl_id)) => {
121 && self.eq_expr(&self.cx.tcx.hir.body(ll_id).value, &self.cx.tcx.hir.body(rl_id).value)
123 (&ExprRet(ref l), &ExprRet(ref r)) => both(l, r, |l, r| self.eq_expr(l, r)),
124 (&ExprPath(ref l), &ExprPath(ref r)) => self.eq_qpath(l, r),
125 (&ExprStruct(ref l_path, ref lf, ref lo), &ExprStruct(ref r_path, ref rf, ref ro)) => {
126 self.eq_qpath(l_path, r_path) && both(lo, ro, |l, r| self.eq_expr(l, r))
127 && over(lf, rf, |l, r| self.eq_field(l, r))
129 (&ExprTup(ref l_tup), &ExprTup(ref r_tup)) => self.eq_exprs(l_tup, r_tup),
130 (&ExprTupField(ref le, li), &ExprTupField(ref re, ri)) => li.node == ri.node && self.eq_expr(le, re),
131 (&ExprUnary(l_op, ref le), &ExprUnary(r_op, ref re)) => l_op == r_op && self.eq_expr(le, re),
132 (&ExprArray(ref l), &ExprArray(ref r)) => self.eq_exprs(l, r),
133 (&ExprWhile(ref lc, ref lb, ref ll), &ExprWhile(ref rc, ref rb, ref rl)) => {
134 self.eq_expr(lc, rc) && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.node.as_str() == r.node.as_str())
140 fn eq_exprs(&self, left: &P<[Expr]>, right: &P<[Expr]>) -> bool {
141 over(left, right, |l, r| self.eq_expr(l, r))
144 fn eq_field(&self, left: &Field, right: &Field) -> bool {
145 left.name.node == right.name.node && self.eq_expr(&left.expr, &right.expr)
148 fn eq_lifetime(&self, left: &Lifetime, right: &Lifetime) -> bool {
149 left.name == right.name
152 /// Check whether two patterns are the same.
153 pub fn eq_pat(&self, left: &Pat, right: &Pat) -> bool {
154 match (&left.node, &right.node) {
155 (&PatKind::Box(ref l), &PatKind::Box(ref r)) => self.eq_pat(l, r),
156 (&PatKind::TupleStruct(ref lp, ref la, ls), &PatKind::TupleStruct(ref rp, ref ra, rs)) => {
157 self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat(l, r)) && ls == rs
159 (&PatKind::Binding(ref lb, _, ref li, ref lp), &PatKind::Binding(ref rb, _, ref ri, ref rp)) => {
160 lb == rb && li.node.as_str() == ri.node.as_str() && both(lp, rp, |l, r| self.eq_pat(l, r))
162 (&PatKind::Path(ref l), &PatKind::Path(ref r)) => self.eq_qpath(l, r),
163 (&PatKind::Lit(ref l), &PatKind::Lit(ref r)) => self.eq_expr(l, r),
164 (&PatKind::Tuple(ref l, ls), &PatKind::Tuple(ref r, rs)) => {
165 ls == rs && over(l, r, |l, r| self.eq_pat(l, r))
167 (&PatKind::Range(ref ls, ref le, ref li), &PatKind::Range(ref rs, ref re, ref ri)) => {
168 self.eq_expr(ls, rs) && self.eq_expr(le, re) && (*li == *ri)
170 (&PatKind::Ref(ref le, ref lm), &PatKind::Ref(ref re, ref rm)) => lm == rm && self.eq_pat(le, re),
171 (&PatKind::Slice(ref ls, ref li, ref le), &PatKind::Slice(ref rs, ref ri, ref re)) => {
172 over(ls, rs, |l, r| self.eq_pat(l, r)) && over(le, re, |l, r| self.eq_pat(l, r))
173 && both(li, ri, |l, r| self.eq_pat(l, r))
175 (&PatKind::Wild, &PatKind::Wild) => true,
180 fn eq_qpath(&self, left: &QPath, right: &QPath) -> bool {
181 match (left, right) {
182 (&QPath::Resolved(ref lty, ref lpath), &QPath::Resolved(ref rty, ref rpath)) => {
183 both(lty, rty, |l, r| self.eq_ty(l, r)) && self.eq_path(lpath, rpath)
185 (&QPath::TypeRelative(ref lty, ref lseg), &QPath::TypeRelative(ref rty, ref rseg)) => {
186 self.eq_ty(lty, rty) && self.eq_path_segment(lseg, rseg)
192 fn eq_path(&self, left: &Path, right: &Path) -> bool {
193 left.is_global() == right.is_global()
194 && over(&left.segments, &right.segments, |l, r| self.eq_path_segment(l, r))
197 fn eq_path_parameters(&self, left: &PathParameters, right: &PathParameters) -> bool {
198 if !(left.parenthesized || right.parenthesized) {
199 over(&left.lifetimes, &right.lifetimes, |l, r| self.eq_lifetime(l, r))
200 && over(&left.types, &right.types, |l, r| self.eq_ty(l, r))
201 && over(&left.bindings, &right.bindings, |l, r| self.eq_type_binding(l, r))
202 } else if left.parenthesized && right.parenthesized {
203 over(left.inputs(), right.inputs(), |l, r| self.eq_ty(l, r))
205 &Some(&left.bindings[0].ty),
206 &Some(&right.bindings[0].ty),
207 |l, r| self.eq_ty(l, r),
214 fn eq_path_segment(&self, left: &PathSegment, right: &PathSegment) -> bool {
215 // The == of idents doesn't work with different contexts,
216 // we have to be explicit about hygiene
217 if left.name.as_str() != right.name.as_str() {
220 match (&left.parameters, &right.parameters) {
221 (&None, &None) => true,
222 (&Some(ref l), &Some(ref r)) => self.eq_path_parameters(l, r),
227 fn eq_ty(&self, left: &Ty, right: &Ty) -> bool {
228 match (&left.node, &right.node) {
229 (&TySlice(ref l_vec), &TySlice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
230 (&TyArray(ref lt, ll_id), &TyArray(ref rt, rl_id)) => {
232 && self.eq_expr(&self.cx.tcx.hir.body(ll_id).value, &self.cx.tcx.hir.body(rl_id).value)
234 (&TyPtr(ref l_mut), &TyPtr(ref r_mut)) => l_mut.mutbl == r_mut.mutbl && self.eq_ty(&*l_mut.ty, &*r_mut.ty),
235 (&TyRptr(_, ref l_rmut), &TyRptr(_, ref r_rmut)) => {
236 l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(&*l_rmut.ty, &*r_rmut.ty)
238 (&TyPath(ref l), &TyPath(ref r)) => self.eq_qpath(l, r),
239 (&TyTup(ref l), &TyTup(ref r)) => over(l, r, |l, r| self.eq_ty(l, r)),
240 (&TyInfer, &TyInfer) => true,
245 fn eq_type_binding(&self, left: &TypeBinding, right: &TypeBinding) -> bool {
246 left.name == right.name && self.eq_ty(&left.ty, &right.ty)
250 fn swap_binop<'a>(binop: BinOp_, lhs: &'a Expr, rhs: &'a Expr) -> Option<(BinOp_, &'a Expr, &'a Expr)> {
252 BiAdd | BiMul | BiBitXor | BiBitAnd | BiEq | BiNe | BiBitOr => Some((binop, rhs, lhs)),
253 BiLt => Some((BiGt, rhs, lhs)),
254 BiLe => Some((BiGe, rhs, lhs)),
255 BiGe => Some((BiLe, rhs, lhs)),
256 BiGt => Some((BiLt, rhs, lhs)),
257 BiShl | BiShr | BiRem | BiSub | BiDiv | BiAnd | BiOr => None,
261 /// Check if the two `Option`s are both `None` or some equal values as per
263 fn both<X, F>(l: &Option<X>, r: &Option<X>, mut eq_fn: F) -> bool
265 F: FnMut(&X, &X) -> bool,
268 .map_or_else(|| r.is_none(), |x| r.as_ref().map_or(false, |y| eq_fn(x, y)))
271 /// Check if two slices are equal as per `eq_fn`.
272 fn over<X, F>(left: &[X], right: &[X], mut eq_fn: F) -> bool
274 F: FnMut(&X, &X) -> bool,
276 left.len() == right.len() && left.iter().zip(right).all(|(x, y)| eq_fn(x, y))
280 /// Type used to hash an ast element. This is different from the `Hash` trait
281 /// on ast types as this
282 /// trait would consider IDs and spans.
284 /// All expressions kind are hashed, but some might have a weaker hash.
285 pub struct SpanlessHash<'a, 'tcx: 'a> {
286 /// Context used to evaluate constant expressions.
287 cx: &'a LateContext<'a, 'tcx>,
291 impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
292 pub fn new(cx: &'a LateContext<'a, 'tcx>) -> Self {
295 s: DefaultHasher::new(),
299 pub fn finish(&self) -> u64 {
303 pub fn hash_block(&mut self, b: &Block) {
308 if let Some(ref e) = b.expr {
312 b.rules.hash(&mut self.s);
315 pub fn hash_expr(&mut self, e: &Expr) {
316 if let Some(e) = constant(self.cx, e) {
317 return e.hash(&mut self.s);
321 ExprAddrOf(m, ref e) => {
322 let c: fn(_, _) -> _ = ExprAddrOf;
328 let c: fn(_) -> _ = ExprAgain;
330 if let Some(i) = i.ident {
331 self.hash_name(&i.node.name);
334 ExprYield(ref e) => {
335 let c: fn(_) -> _ = ExprYield;
339 ExprAssign(ref l, ref r) => {
340 let c: fn(_, _) -> _ = ExprAssign;
345 ExprAssignOp(ref o, ref l, ref r) => {
346 let c: fn(_, _, _) -> _ = ExprAssignOp;
352 ExprBlock(ref b) => {
353 let c: fn(_) -> _ = ExprBlock;
357 ExprBinary(op, ref l, ref r) => {
358 let c: fn(_, _, _) -> _ = ExprBinary;
360 op.node.hash(&mut self.s);
364 ExprBreak(i, ref j) => {
365 let c: fn(_, _) -> _ = ExprBreak;
367 if let Some(i) = i.ident {
368 self.hash_name(&i.node.name);
370 if let Some(ref j) = *j {
375 let c: fn(_) -> _ = ExprBox;
379 ExprCall(ref fun, ref args) => {
380 let c: fn(_, _) -> _ = ExprCall;
383 self.hash_exprs(args);
385 ExprCast(ref e, ref _ty) => {
386 let c: fn(_, _) -> _ = ExprCast;
391 ExprClosure(cap, _, eid, _, _) => {
392 let c: fn(_, _, _, _, _) -> _ = ExprClosure;
394 cap.hash(&mut self.s);
395 self.hash_expr(&self.cx.tcx.hir.body(eid).value);
397 ExprField(ref e, ref f) => {
398 let c: fn(_, _) -> _ = ExprField;
401 self.hash_name(&f.node);
403 ExprIndex(ref a, ref i) => {
404 let c: fn(_, _) -> _ = ExprIndex;
409 ExprInlineAsm(..) => {
410 let c: fn(_, _, _) -> _ = ExprInlineAsm;
413 ExprIf(ref cond, ref t, ref e) => {
414 let c: fn(_, _, _) -> _ = ExprIf;
416 self.hash_expr(cond);
417 self.hash_expr(&**t);
418 if let Some(ref e) = *e {
423 let c: fn(_) -> _ = ExprLit;
427 ExprLoop(ref b, ref i, _) => {
428 let c: fn(_, _, _) -> _ = ExprLoop;
431 if let Some(i) = *i {
432 self.hash_name(&i.node);
435 ExprMatch(ref e, ref arms, ref s) => {
436 let c: fn(_, _, _) -> _ = ExprMatch;
442 if let Some(ref e) = arm.guard {
445 self.hash_expr(&arm.body);
450 ExprMethodCall(ref path, ref _tys, ref args) => {
451 let c: fn(_, _, _) -> _ = ExprMethodCall;
453 self.hash_name(&path.name);
454 self.hash_exprs(args);
456 ExprRepeat(ref e, l_id) => {
457 let c: fn(_, _) -> _ = ExprRepeat;
460 self.hash_expr(&self.cx.tcx.hir.body(l_id).value);
463 let c: fn(_) -> _ = ExprRet;
465 if let Some(ref e) = *e {
469 ExprPath(ref qpath) => {
470 let c: fn(_) -> _ = ExprPath;
472 self.hash_qpath(qpath);
474 ExprStruct(ref path, ref fields, ref expr) => {
475 let c: fn(_, _, _) -> _ = ExprStruct;
478 self.hash_qpath(path);
481 self.hash_name(&f.name.node);
482 self.hash_expr(&f.expr);
485 if let Some(ref e) = *expr {
489 ExprTup(ref tup) => {
490 let c: fn(_) -> _ = ExprTup;
492 self.hash_exprs(tup);
494 ExprTupField(ref le, li) => {
495 let c: fn(_, _) -> _ = ExprTupField;
499 li.node.hash(&mut self.s);
501 ExprType(ref e, ref _ty) => {
502 let c: fn(_, _) -> _ = ExprType;
507 ExprUnary(lop, ref le) => {
508 let c: fn(_, _) -> _ = ExprUnary;
511 lop.hash(&mut self.s);
514 ExprArray(ref v) => {
515 let c: fn(_) -> _ = ExprArray;
520 ExprWhile(ref cond, ref b, l) => {
521 let c: fn(_, _, _) -> _ = ExprWhile;
524 self.hash_expr(cond);
527 self.hash_name(&l.node);
533 pub fn hash_exprs(&mut self, e: &P<[Expr]>) {
539 pub fn hash_name(&mut self, n: &Name) {
540 n.as_str().hash(&mut self.s);
543 pub fn hash_qpath(&mut self, p: &QPath) {
545 QPath::Resolved(_, ref path) => {
546 self.hash_path(path);
548 QPath::TypeRelative(_, ref path) => {
549 self.hash_name(&path.name);
552 // self.cx.tables.qpath_def(p, id).hash(&mut self.s);
555 pub fn hash_path(&mut self, p: &Path) {
556 p.is_global().hash(&mut self.s);
557 for p in &p.segments {
558 self.hash_name(&p.name);
562 pub fn hash_stmt(&mut self, b: &Stmt) {
564 StmtDecl(ref decl, _) => {
565 let c: fn(_, _) -> _ = StmtDecl;
568 if let DeclLocal(ref local) = decl.node {
569 if let Some(ref init) = local.init {
570 self.hash_expr(init);
574 StmtExpr(ref expr, _) => {
575 let c: fn(_, _) -> _ = StmtExpr;
577 self.hash_expr(expr);
579 StmtSemi(ref expr, _) => {
580 let c: fn(_, _) -> _ = StmtSemi;
582 self.hash_expr(expr);