1 use crate::consts::{constant_simple, constant_context};
4 use rustc::ty::{TypeckTables};
5 use std::hash::{Hash, Hasher};
6 use std::collections::hash_map::DefaultHasher;
9 use crate::utils::differing_macro_contexts;
11 /// Type used to check whether two ast are the same. This is different from the
13 /// `==` on ast types as this operator would compare true equality with ID and
16 /// Note that some expressions kinds are not considered but could be added.
17 pub struct SpanlessEq<'a, 'tcx: 'a> {
18 /// Context used to evaluate constant expressions.
19 cx: &'a LateContext<'a, 'tcx>,
20 tables: &'a TypeckTables<'tcx>,
21 /// If is true, never consider as equal expressions containing function
26 impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
27 pub fn new(cx: &'a LateContext<'a, 'tcx>) -> Self {
35 pub fn ignore_fn(self) -> Self {
38 tables: self.cx.tables,
43 /// Check whether two statements are the same.
44 pub fn eq_stmt(&mut self, left: &Stmt, right: &Stmt) -> bool {
45 match (&left.node, &right.node) {
46 (&StmtKind::Decl(ref l, _), &StmtKind::Decl(ref r, _)) => {
47 if let (&DeclKind::Local(ref l), &DeclKind::Local(ref r)) = (&l.node, &r.node) {
48 both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
53 (&StmtKind::Expr(ref l, _), &StmtKind::Expr(ref r, _)) | (&StmtKind::Semi(ref l, _), &StmtKind::Semi(ref r, _)) => {
60 /// Check whether two blocks are the same.
61 pub fn eq_block(&mut self, left: &Block, right: &Block) -> bool {
62 over(&left.stmts, &right.stmts, |l, r| self.eq_stmt(l, r))
63 && both(&left.expr, &right.expr, |l, r| self.eq_expr(l, r))
66 pub fn eq_expr(&mut self, left: &Expr, right: &Expr) -> bool {
67 if self.ignore_fn && differing_macro_contexts(left.span, right.span) {
71 if let (Some(l), Some(r)) = (constant_simple(self.cx, self.tables, left), constant_simple(self.cx, self.tables, right)) {
77 match (&left.node, &right.node) {
78 (&ExprKind::AddrOf(l_mut, ref le), &ExprKind::AddrOf(r_mut, ref re)) => l_mut == r_mut && self.eq_expr(le, re),
79 (&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
80 both(&li.label, &ri.label, |l, r| l.ident.as_str() == r.ident.as_str())
82 (&ExprKind::Assign(ref ll, ref lr), &ExprKind::Assign(ref rl, ref rr)) => self.eq_expr(ll, rl) && self.eq_expr(lr, rr),
83 (&ExprKind::AssignOp(ref lo, ref ll, ref lr), &ExprKind::AssignOp(ref ro, ref rl, ref rr)) => {
84 lo.node == ro.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
86 (&ExprKind::Block(ref l, _), &ExprKind::Block(ref r, _)) => self.eq_block(l, r),
87 (&ExprKind::Binary(l_op, ref ll, ref lr), &ExprKind::Binary(r_op, ref rl, ref rr)) => {
88 l_op.node == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
89 || swap_binop(l_op.node, ll, lr).map_or(false, |(l_op, ll, lr)| {
90 l_op == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
93 (&ExprKind::Break(li, ref le), &ExprKind::Break(ri, ref re)) => {
94 both(&li.label, &ri.label, |l, r| l.ident.as_str() == r.ident.as_str())
95 && both(le, re, |l, r| self.eq_expr(l, r))
97 (&ExprKind::Box(ref l), &ExprKind::Box(ref r)) => self.eq_expr(l, r),
98 (&ExprKind::Call(ref l_fun, ref l_args), &ExprKind::Call(ref r_fun, ref r_args)) => {
99 !self.ignore_fn && self.eq_expr(l_fun, r_fun) && self.eq_exprs(l_args, r_args)
101 (&ExprKind::Cast(ref lx, ref lt), &ExprKind::Cast(ref rx, ref rt)) |
102 (&ExprKind::Type(ref lx, ref lt), &ExprKind::Type(ref rx, ref rt)) => self.eq_expr(lx, rx) && self.eq_ty(lt, rt),
103 (&ExprKind::Field(ref l_f_exp, ref l_f_ident), &ExprKind::Field(ref r_f_exp, ref r_f_ident)) => {
104 l_f_ident.name == r_f_ident.name && self.eq_expr(l_f_exp, r_f_exp)
106 (&ExprKind::Index(ref la, ref li), &ExprKind::Index(ref ra, ref ri)) => self.eq_expr(la, ra) && self.eq_expr(li, ri),
107 (&ExprKind::If(ref lc, ref lt, ref le), &ExprKind::If(ref rc, ref rt, ref re)) => {
108 self.eq_expr(lc, rc) && self.eq_expr(&**lt, &**rt) && both(le, re, |l, r| self.eq_expr(l, r))
110 (&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node,
111 (&ExprKind::Loop(ref lb, ref ll, ref lls), &ExprKind::Loop(ref rb, ref rl, ref rls)) => {
112 lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.as_str() == r.ident.as_str())
114 (&ExprKind::Match(ref le, ref la, ref ls), &ExprKind::Match(ref re, ref ra, ref rs)) => {
115 ls == rs && self.eq_expr(le, re) && over(la, ra, |l, r| {
116 self.eq_expr(&l.body, &r.body) && both(&l.guard, &r.guard, |l, r| self.eq_expr(l, r))
117 && over(&l.pats, &r.pats, |l, r| self.eq_pat(l, r))
120 (&ExprKind::MethodCall(ref l_path, _, ref l_args), &ExprKind::MethodCall(ref r_path, _, ref r_args)) => {
121 !self.ignore_fn && self.eq_path_segment(l_path, r_path) && self.eq_exprs(l_args, r_args)
123 (&ExprKind::Repeat(ref le, ref ll_id), &ExprKind::Repeat(ref re, ref rl_id)) => {
124 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(ll_id.body));
125 let ll = celcx.expr(&self.cx.tcx.hir.body(ll_id.body).value);
126 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(rl_id.body));
127 let rl = celcx.expr(&self.cx.tcx.hir.body(rl_id.body).value);
129 self.eq_expr(le, re) && ll == rl
131 (&ExprKind::Ret(ref l), &ExprKind::Ret(ref r)) => both(l, r, |l, r| self.eq_expr(l, r)),
132 (&ExprKind::Path(ref l), &ExprKind::Path(ref r)) => self.eq_qpath(l, r),
133 (&ExprKind::Struct(ref l_path, ref lf, ref lo), &ExprKind::Struct(ref r_path, ref rf, ref ro)) => {
134 self.eq_qpath(l_path, r_path) && both(lo, ro, |l, r| self.eq_expr(l, r))
135 && over(lf, rf, |l, r| self.eq_field(l, r))
137 (&ExprKind::Tup(ref l_tup), &ExprKind::Tup(ref r_tup)) => self.eq_exprs(l_tup, r_tup),
138 (&ExprKind::Unary(l_op, ref le), &ExprKind::Unary(r_op, ref re)) => l_op == r_op && self.eq_expr(le, re),
139 (&ExprKind::Array(ref l), &ExprKind::Array(ref r)) => self.eq_exprs(l, r),
140 (&ExprKind::While(ref lc, ref lb, ref ll), &ExprKind::While(ref rc, ref rb, ref rl)) => {
141 self.eq_expr(lc, rc) && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.as_str() == r.ident.as_str())
147 fn eq_exprs(&mut self, left: &P<[Expr]>, right: &P<[Expr]>) -> bool {
148 over(left, right, |l, r| self.eq_expr(l, r))
151 fn eq_field(&mut self, left: &Field, right: &Field) -> bool {
152 left.ident.name == right.ident.name && self.eq_expr(&left.expr, &right.expr)
155 fn eq_generic_arg(&mut self, left: &GenericArg, right: &GenericArg) -> bool {
156 match (left, right) {
157 (GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => self.eq_lifetime(l_lt, r_lt),
158 (GenericArg::Type(l_ty), GenericArg::Type(r_ty)) => self.eq_ty(l_ty, r_ty),
163 fn eq_lifetime(&mut self, left: &Lifetime, right: &Lifetime) -> bool {
164 left.name == right.name
167 /// Check whether two patterns are the same.
168 pub fn eq_pat(&mut self, left: &Pat, right: &Pat) -> bool {
169 match (&left.node, &right.node) {
170 (&PatKind::Box(ref l), &PatKind::Box(ref r)) => self.eq_pat(l, r),
171 (&PatKind::TupleStruct(ref lp, ref la, ls), &PatKind::TupleStruct(ref rp, ref ra, rs)) => {
172 self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat(l, r)) && ls == rs
174 (&PatKind::Binding(ref lb, _, ref li, ref lp), &PatKind::Binding(ref rb, _, ref ri, ref rp)) => {
175 lb == rb && li.name.as_str() == ri.name.as_str() && both(lp, rp, |l, r| self.eq_pat(l, r))
177 (&PatKind::Path(ref l), &PatKind::Path(ref r)) => self.eq_qpath(l, r),
178 (&PatKind::Lit(ref l), &PatKind::Lit(ref r)) => self.eq_expr(l, r),
179 (&PatKind::Tuple(ref l, ls), &PatKind::Tuple(ref r, rs)) => {
180 ls == rs && over(l, r, |l, r| self.eq_pat(l, r))
182 (&PatKind::Range(ref ls, ref le, ref li), &PatKind::Range(ref rs, ref re, ref ri)) => {
183 self.eq_expr(ls, rs) && self.eq_expr(le, re) && (*li == *ri)
185 (&PatKind::Ref(ref le, ref lm), &PatKind::Ref(ref re, ref rm)) => lm == rm && self.eq_pat(le, re),
186 (&PatKind::Slice(ref ls, ref li, ref le), &PatKind::Slice(ref rs, ref ri, ref re)) => {
187 over(ls, rs, |l, r| self.eq_pat(l, r)) && over(le, re, |l, r| self.eq_pat(l, r))
188 && both(li, ri, |l, r| self.eq_pat(l, r))
190 (&PatKind::Wild, &PatKind::Wild) => true,
195 fn eq_qpath(&mut self, left: &QPath, right: &QPath) -> bool {
196 match (left, right) {
197 (&QPath::Resolved(ref lty, ref lpath), &QPath::Resolved(ref rty, ref rpath)) => {
198 both(lty, rty, |l, r| self.eq_ty(l, r)) && self.eq_path(lpath, rpath)
200 (&QPath::TypeRelative(ref lty, ref lseg), &QPath::TypeRelative(ref rty, ref rseg)) => {
201 self.eq_ty(lty, rty) && self.eq_path_segment(lseg, rseg)
207 fn eq_path(&mut self, left: &Path, right: &Path) -> bool {
208 left.is_global() == right.is_global()
209 && over(&left.segments, &right.segments, |l, r| self.eq_path_segment(l, r))
212 fn eq_path_parameters(&mut self, left: &GenericArgs, right: &GenericArgs) -> bool {
213 if !(left.parenthesized || right.parenthesized) {
214 over(&left.args, &right.args, |l, r| self.eq_generic_arg(l, r)) // FIXME(flip1995): may not work
215 && over(&left.bindings, &right.bindings, |l, r| self.eq_type_binding(l, r))
216 } else if left.parenthesized && right.parenthesized {
217 over(left.inputs(), right.inputs(), |l, r| self.eq_ty(l, r))
219 &Some(&left.bindings[0].ty),
220 &Some(&right.bindings[0].ty),
221 |l, r| self.eq_ty(l, r),
228 pub fn eq_path_segments(&mut self, left: &[PathSegment], right: &[PathSegment]) -> bool {
229 left.len() == right.len() && left.iter().zip(right).all(|(l, r)| self.eq_path_segment(l, r))
232 pub fn eq_path_segment(&mut self, left: &PathSegment, right: &PathSegment) -> bool {
233 // The == of idents doesn't work with different contexts,
234 // we have to be explicit about hygiene
235 if left.ident.as_str() != right.ident.as_str() {
238 match (&left.args, &right.args) {
239 (&None, &None) => true,
240 (&Some(ref l), &Some(ref r)) => self.eq_path_parameters(l, r),
245 pub fn eq_ty(&mut self, left: &Ty, right: &Ty) -> bool {
246 self.eq_ty_kind(&left.node, &right.node)
249 pub fn eq_ty_kind(&mut self, left: &TyKind, right: &TyKind) -> bool {
250 match (left, right) {
251 (&TyKind::Slice(ref l_vec), &TyKind::Slice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
252 (&TyKind::Array(ref lt, ref ll_id), &TyKind::Array(ref rt, ref rl_id)) => {
253 let full_table = self.tables;
255 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(ll_id.body));
256 self.tables = self.cx.tcx.body_tables(ll_id.body);
257 let ll = celcx.expr(&self.cx.tcx.hir.body(ll_id.body).value);
259 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(rl_id.body));
260 self.tables = self.cx.tcx.body_tables(rl_id.body);
261 let rl = celcx.expr(&self.cx.tcx.hir.body(rl_id.body).value);
263 let eq_ty = self.eq_ty(lt, rt);
264 self.tables = full_table;
267 (&TyKind::Ptr(ref l_mut), &TyKind::Ptr(ref r_mut)) => l_mut.mutbl == r_mut.mutbl && self.eq_ty(&*l_mut.ty, &*r_mut.ty),
268 (&TyKind::Rptr(_, ref l_rmut), &TyKind::Rptr(_, ref r_rmut)) => {
269 l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(&*l_rmut.ty, &*r_rmut.ty)
271 (&TyKind::Path(ref l), &TyKind::Path(ref r)) => self.eq_qpath(l, r),
272 (&TyKind::Tup(ref l), &TyKind::Tup(ref r)) => over(l, r, |l, r| self.eq_ty(l, r)),
273 (&TyKind::Infer, &TyKind::Infer) => true,
278 fn eq_type_binding(&mut self, left: &TypeBinding, right: &TypeBinding) -> bool {
279 left.ident.name == right.ident.name && self.eq_ty(&left.ty, &right.ty)
283 fn swap_binop<'a>(binop: BinOpKind, lhs: &'a Expr, rhs: &'a Expr) -> Option<(BinOpKind, &'a Expr, &'a Expr)> {
291 BinOpKind::BitOr => Some((binop, rhs, lhs)),
292 BinOpKind::Lt => Some((BinOpKind::Gt, rhs, lhs)),
293 BinOpKind::Le => Some((BinOpKind::Ge, rhs, lhs)),
294 BinOpKind::Ge => Some((BinOpKind::Le, rhs, lhs)),
295 BinOpKind::Gt => Some((BinOpKind::Lt, rhs, lhs)),
302 BinOpKind::Or => None,
306 /// Check if the two `Option`s are both `None` or some equal values as per
308 fn both<X, F>(l: &Option<X>, r: &Option<X>, mut eq_fn: F) -> bool
310 F: FnMut(&X, &X) -> bool,
313 .map_or_else(|| r.is_none(), |x| r.as_ref().map_or(false, |y| eq_fn(x, y)))
316 /// Check if two slices are equal as per `eq_fn`.
317 fn over<X, F>(left: &[X], right: &[X], mut eq_fn: F) -> bool
319 F: FnMut(&X, &X) -> bool,
321 left.len() == right.len() && left.iter().zip(right).all(|(x, y)| eq_fn(x, y))
325 /// Type used to hash an ast element. This is different from the `Hash` trait
326 /// on ast types as this
327 /// trait would consider IDs and spans.
329 /// All expressions kind are hashed, but some might have a weaker hash.
330 pub struct SpanlessHash<'a, 'tcx: 'a> {
331 /// Context used to evaluate constant expressions.
332 cx: &'a LateContext<'a, 'tcx>,
333 tables: &'a TypeckTables<'tcx>,
337 impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
338 pub fn new(cx: &'a LateContext<'a, 'tcx>, tables: &'a TypeckTables<'tcx>) -> Self {
342 s: DefaultHasher::new(),
346 pub fn finish(&self) -> u64 {
350 pub fn hash_block(&mut self, b: &Block) {
355 if let Some(ref e) = b.expr {
360 BlockCheckMode::DefaultBlock => 0,
361 BlockCheckMode::UnsafeBlock(_) => 1,
362 BlockCheckMode::PushUnsafeBlock(_) => 2,
363 BlockCheckMode::PopUnsafeBlock(_) => 3,
367 #[allow(many_single_char_names)]
368 pub fn hash_expr(&mut self, e: &Expr) {
369 if let Some(e) = constant_simple(self.cx, self.tables, e) {
370 return e.hash(&mut self.s);
374 ExprKind::AddrOf(m, ref e) => {
375 let c: fn(_, _) -> _ = ExprKind::AddrOf;
380 ExprKind::Continue(i) => {
381 let c: fn(_) -> _ = ExprKind::Continue;
383 if let Some(i) = i.label {
384 self.hash_name(i.ident.name);
387 ExprKind::Yield(ref e) => {
388 let c: fn(_) -> _ = ExprKind::Yield;
392 ExprKind::Assign(ref l, ref r) => {
393 let c: fn(_, _) -> _ = ExprKind::Assign;
398 ExprKind::AssignOp(ref o, ref l, ref r) => {
399 let c: fn(_, _, _) -> _ = ExprKind::AssignOp;
405 ExprKind::Block(ref b, _) => {
406 let c: fn(_, _) -> _ = ExprKind::Block;
410 ExprKind::Binary(op, ref l, ref r) => {
411 let c: fn(_, _, _) -> _ = ExprKind::Binary;
413 op.node.hash(&mut self.s);
417 ExprKind::Break(i, ref j) => {
418 let c: fn(_, _) -> _ = ExprKind::Break;
420 if let Some(i) = i.label {
421 self.hash_name(i.ident.name);
423 if let Some(ref j) = *j {
427 ExprKind::Box(ref e) => {
428 let c: fn(_) -> _ = ExprKind::Box;
432 ExprKind::Call(ref fun, ref args) => {
433 let c: fn(_, _) -> _ = ExprKind::Call;
436 self.hash_exprs(args);
438 ExprKind::Cast(ref e, ref _ty) => {
439 let c: fn(_, _) -> _ = ExprKind::Cast;
444 ExprKind::Closure(cap, _, eid, _, _) => {
445 let c: fn(_, _, _, _, _) -> _ = ExprKind::Closure;
448 CaptureClause::CaptureByValue => 0,
449 CaptureClause::CaptureByRef => 1,
451 self.hash_expr(&self.cx.tcx.hir.body(eid).value);
453 ExprKind::Field(ref e, ref f) => {
454 let c: fn(_, _) -> _ = ExprKind::Field;
457 self.hash_name(f.name);
459 ExprKind::Index(ref a, ref i) => {
460 let c: fn(_, _) -> _ = ExprKind::Index;
465 ExprKind::InlineAsm(..) => {
466 let c: fn(_, _, _) -> _ = ExprKind::InlineAsm;
469 ExprKind::If(ref cond, ref t, ref e) => {
470 let c: fn(_, _, _) -> _ = ExprKind::If;
472 self.hash_expr(cond);
473 self.hash_expr(&**t);
474 if let Some(ref e) = *e {
478 ExprKind::Lit(ref l) => {
479 let c: fn(_) -> _ = ExprKind::Lit;
483 ExprKind::Loop(ref b, ref i, _) => {
484 let c: fn(_, _, _) -> _ = ExprKind::Loop;
487 if let Some(i) = *i {
488 self.hash_name(i.ident.name);
491 ExprKind::Match(ref e, ref arms, ref s) => {
492 let c: fn(_, _, _) -> _ = ExprKind::Match;
498 if let Some(ref e) = arm.guard {
501 self.hash_expr(&arm.body);
506 ExprKind::MethodCall(ref path, ref _tys, ref args) => {
507 let c: fn(_, _, _) -> _ = ExprKind::MethodCall;
509 self.hash_name(path.ident.name);
510 self.hash_exprs(args);
512 ExprKind::Repeat(ref e, ref l_id) => {
513 let c: fn(_, _) -> _ = ExprKind::Repeat;
516 let full_table = self.tables;
517 self.tables = self.cx.tcx.body_tables(l_id.body);
518 self.hash_expr(&self.cx.tcx.hir.body(l_id.body).value);
519 self.tables = full_table;
521 ExprKind::Ret(ref e) => {
522 let c: fn(_) -> _ = ExprKind::Ret;
524 if let Some(ref e) = *e {
528 ExprKind::Path(ref qpath) => {
529 let c: fn(_) -> _ = ExprKind::Path;
531 self.hash_qpath(qpath);
533 ExprKind::Struct(ref path, ref fields, ref expr) => {
534 let c: fn(_, _, _) -> _ = ExprKind::Struct;
537 self.hash_qpath(path);
540 self.hash_name(f.ident.name);
541 self.hash_expr(&f.expr);
544 if let Some(ref e) = *expr {
548 ExprKind::Tup(ref tup) => {
549 let c: fn(_) -> _ = ExprKind::Tup;
551 self.hash_exprs(tup);
553 ExprKind::Type(ref e, ref _ty) => {
554 let c: fn(_, _) -> _ = ExprKind::Type;
559 ExprKind::Unary(lop, ref le) => {
560 let c: fn(_, _) -> _ = ExprKind::Unary;
563 lop.hash(&mut self.s);
566 ExprKind::Array(ref v) => {
567 let c: fn(_) -> _ = ExprKind::Array;
572 ExprKind::While(ref cond, ref b, l) => {
573 let c: fn(_, _, _) -> _ = ExprKind::While;
576 self.hash_expr(cond);
579 self.hash_name(l.ident.name);
585 pub fn hash_exprs(&mut self, e: &P<[Expr]>) {
591 pub fn hash_name(&mut self, n: Name) {
592 n.as_str().hash(&mut self.s);
595 pub fn hash_qpath(&mut self, p: &QPath) {
597 QPath::Resolved(_, ref path) => {
598 self.hash_path(path);
600 QPath::TypeRelative(_, ref path) => {
601 self.hash_name(path.ident.name);
604 // self.cx.tables.qpath_def(p, id).hash(&mut self.s);
607 pub fn hash_path(&mut self, p: &Path) {
608 p.is_global().hash(&mut self.s);
609 for p in &p.segments {
610 self.hash_name(p.ident.name);
614 pub fn hash_stmt(&mut self, b: &Stmt) {
616 StmtKind::Decl(ref decl, _) => {
617 let c: fn(_, _) -> _ = StmtKind::Decl;
620 if let DeclKind::Local(ref local) = decl.node {
621 if let Some(ref init) = local.init {
622 self.hash_expr(init);
626 StmtKind::Expr(ref expr, _) => {
627 let c: fn(_, _) -> _ = StmtKind::Expr;
629 self.hash_expr(expr);
631 StmtKind::Semi(ref expr, _) => {
632 let c: fn(_, _) -> _ = StmtKind::Semi;
634 self.hash_expr(expr);