2016-02-06 19:13:25 +00:00
|
|
|
use consts::constant;
|
|
|
|
use rustc::lint::*;
|
2016-04-07 15:46:48 +00:00
|
|
|
use rustc::hir::*;
|
2016-10-04 15:17:40 +00:00
|
|
|
use std::hash::{Hash, Hasher};
|
|
|
|
use std::collections::hash_map::DefaultHasher;
|
2016-12-02 21:23:24 +00:00
|
|
|
use syntax::ast::Name;
|
2016-02-06 19:13:25 +00:00
|
|
|
use syntax::ptr::P;
|
2016-02-13 14:36:57 +00:00
|
|
|
use utils::differing_macro_contexts;
|
2016-02-06 19:13:25 +00:00
|
|
|
|
2017-08-09 07:30:56 +00:00
|
|
|
/// Type used to check whether two ast are the same. This is different from the
|
|
|
|
/// operator
|
|
|
|
/// `==` on ast types as this operator would compare true equality with ID and
|
|
|
|
/// span.
|
2016-02-06 19:13:25 +00:00
|
|
|
///
|
|
|
|
/// Note that some expressions kinds are not considered but could be added.
|
|
|
|
pub struct SpanlessEq<'a, 'tcx: 'a> {
|
|
|
|
/// Context used to evaluate constant expressions.
|
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
2017-08-09 07:30:56 +00:00
|
|
|
/// If is true, never consider as equal expressions containing function
|
|
|
|
/// calls.
|
2016-02-06 19:13:25 +00:00
|
|
|
ignore_fn: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
|
|
|
|
pub fn new(cx: &'a LateContext<'a, 'tcx>) -> Self {
|
2017-08-21 11:32:12 +00:00
|
|
|
Self {
|
2016-02-24 16:38:57 +00:00
|
|
|
cx: cx,
|
|
|
|
ignore_fn: false,
|
|
|
|
}
|
2016-02-06 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ignore_fn(self) -> Self {
|
2017-08-21 11:32:12 +00:00
|
|
|
Self {
|
2016-02-24 16:38:57 +00:00
|
|
|
cx: self.cx,
|
|
|
|
ignore_fn: true,
|
|
|
|
}
|
2016-02-06 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check whether two statements are the same.
|
|
|
|
pub fn eq_stmt(&self, left: &Stmt, right: &Stmt) -> bool {
|
|
|
|
match (&left.node, &right.node) {
|
|
|
|
(&StmtDecl(ref l, _), &StmtDecl(ref r, _)) => {
|
|
|
|
if let (&DeclLocal(ref l), &DeclLocal(ref r)) = (&l.node, &r.node) {
|
2016-12-20 17:21:30 +00:00
|
|
|
both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
|
2016-02-29 11:19:32 +00:00
|
|
|
} else {
|
2016-02-06 19:13:25 +00:00
|
|
|
false
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2017-09-05 09:33:04 +00:00
|
|
|
(&StmtExpr(ref l, _), &StmtExpr(ref r, _)) | (&StmtSemi(ref l, _), &StmtSemi(ref r, _)) => {
|
|
|
|
self.eq_expr(l, r)
|
|
|
|
},
|
2016-02-06 19:13:25 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check whether two blocks are the same.
|
|
|
|
pub fn eq_block(&self, left: &Block, right: &Block) -> bool {
|
2017-11-04 19:55:56 +00:00
|
|
|
over(&left.stmts, &right.stmts, |l, r| self.eq_stmt(l, r))
|
|
|
|
&& both(&left.expr, &right.expr, |l, r| self.eq_expr(l, r))
|
2016-02-06 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn eq_expr(&self, left: &Expr, right: &Expr) -> bool {
|
2016-02-13 14:36:57 +00:00
|
|
|
if self.ignore_fn && differing_macro_contexts(left.span, right.span) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-06 19:13:25 +00:00
|
|
|
if let (Some(l), Some(r)) = (constant(self.cx, left), constant(self.cx, right)) {
|
|
|
|
if l == r {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match (&left.node, &right.node) {
|
2016-03-01 09:13:54 +00:00
|
|
|
(&ExprAddrOf(l_mut, ref le), &ExprAddrOf(r_mut, ref re)) => l_mut == r_mut && self.eq_expr(le, re),
|
2017-02-27 08:49:02 +00:00
|
|
|
(&ExprAgain(li), &ExprAgain(ri)) => {
|
|
|
|
both(&li.ident, &ri.ident, |l, r| l.node.name.as_str() == r.node.name.as_str())
|
|
|
|
},
|
2016-02-29 11:19:32 +00:00
|
|
|
(&ExprAssign(ref ll, ref lr), &ExprAssign(ref rl, ref rr)) => self.eq_expr(ll, rl) && self.eq_expr(lr, rr),
|
2016-02-06 19:13:25 +00:00
|
|
|
(&ExprAssignOp(ref lo, ref ll, ref lr), &ExprAssignOp(ref ro, ref rl, ref rr)) => {
|
|
|
|
lo.node == ro.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-24 16:38:57 +00:00
|
|
|
(&ExprBlock(ref l), &ExprBlock(ref r)) => self.eq_block(l, r),
|
2016-03-01 09:13:54 +00:00
|
|
|
(&ExprBinary(l_op, ref ll, ref lr), &ExprBinary(r_op, ref rl, ref rr)) => {
|
2017-11-04 19:55:56 +00:00
|
|
|
l_op.node == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
|
|
|
|
|| swap_binop(l_op.node, ll, lr).map_or(false, |(l_op, ll, lr)| {
|
2017-08-09 07:30:56 +00:00
|
|
|
l_op == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
|
|
|
|
})
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
|
|
|
(&ExprBreak(li, ref le), &ExprBreak(ri, ref re)) => {
|
2017-11-04 19:55:56 +00:00
|
|
|
both(&li.ident, &ri.ident, |l, r| l.node.name.as_str() == r.node.name.as_str())
|
|
|
|
&& both(le, re, |l, r| self.eq_expr(l, r))
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-29 11:19:32 +00:00
|
|
|
(&ExprBox(ref l), &ExprBox(ref r)) => self.eq_expr(l, r),
|
2016-03-01 09:13:54 +00:00
|
|
|
(&ExprCall(ref l_fun, ref l_args), &ExprCall(ref r_fun, ref r_args)) => {
|
|
|
|
!self.ignore_fn && self.eq_expr(l_fun, r_fun) && self.eq_exprs(l_args, r_args)
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-10-01 20:26:19 +00:00
|
|
|
(&ExprCast(ref lx, ref lt), &ExprCast(ref rx, ref rt)) |
|
2016-12-20 17:21:30 +00:00
|
|
|
(&ExprType(ref lx, ref lt), &ExprType(ref rx, ref rt)) => self.eq_expr(lx, rx) && self.eq_ty(lt, rt),
|
2016-03-01 09:13:54 +00:00
|
|
|
(&ExprField(ref l_f_exp, ref l_f_ident), &ExprField(ref r_f_exp, ref r_f_ident)) => {
|
|
|
|
l_f_ident.node == r_f_ident.node && self.eq_expr(l_f_exp, r_f_exp)
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-29 11:19:32 +00:00
|
|
|
(&ExprIndex(ref la, ref li), &ExprIndex(ref ra, ref ri)) => self.eq_expr(la, ra) && self.eq_expr(li, ri),
|
2016-02-06 19:13:25 +00:00
|
|
|
(&ExprIf(ref lc, ref lt, ref le), &ExprIf(ref rc, ref rt, ref re)) => {
|
2017-04-12 09:06:32 +00:00
|
|
|
self.eq_expr(lc, rc) && self.eq_expr(&**lt, &**rt) && both(le, re, |l, r| self.eq_expr(l, r))
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-06 19:13:25 +00:00
|
|
|
(&ExprLit(ref l), &ExprLit(ref r)) => l.node == r.node,
|
2016-12-02 21:23:24 +00:00
|
|
|
(&ExprLoop(ref lb, ref ll, ref lls), &ExprLoop(ref rb, ref rl, ref rls)) => {
|
|
|
|
lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.node.as_str() == r.node.as_str())
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-12-02 19:54:05 +00:00
|
|
|
(&ExprMatch(ref le, ref la, ref ls), &ExprMatch(ref re, ref ra, ref rs)) => {
|
2017-09-05 09:33:04 +00:00
|
|
|
ls == rs && self.eq_expr(le, re) && over(la, ra, |l, r| {
|
2017-11-04 19:55:56 +00:00
|
|
|
self.eq_expr(&l.body, &r.body) && both(&l.guard, &r.guard, |l, r| self.eq_expr(l, r))
|
|
|
|
&& over(&l.pats, &r.pats, |l, r| self.eq_pat(l, r))
|
2017-09-05 09:33:04 +00:00
|
|
|
})
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2017-07-10 08:21:21 +00:00
|
|
|
(&ExprMethodCall(ref l_path, _, ref l_args), &ExprMethodCall(ref r_path, _, ref r_args)) => {
|
2017-07-10 08:17:40 +00:00
|
|
|
!self.ignore_fn && l_path == r_path && self.eq_exprs(l_args, r_args)
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2017-01-04 23:53:16 +00:00
|
|
|
(&ExprRepeat(ref le, ll_id), &ExprRepeat(ref re, rl_id)) => {
|
2017-11-04 19:55:56 +00:00
|
|
|
self.eq_expr(le, re)
|
|
|
|
&& self.eq_expr(&self.cx.tcx.hir.body(ll_id).value, &self.cx.tcx.hir.body(rl_id).value)
|
2017-01-04 23:53:16 +00:00
|
|
|
},
|
2016-02-29 11:19:32 +00:00
|
|
|
(&ExprRet(ref l), &ExprRet(ref r)) => both(l, r, |l, r| self.eq_expr(l, r)),
|
2016-12-02 19:54:05 +00:00
|
|
|
(&ExprPath(ref l), &ExprPath(ref r)) => self.eq_qpath(l, r),
|
2016-03-01 09:13:54 +00:00
|
|
|
(&ExprStruct(ref l_path, ref lf, ref lo), &ExprStruct(ref r_path, ref rf, ref ro)) => {
|
2017-11-04 19:55:56 +00:00
|
|
|
self.eq_qpath(l_path, r_path) && both(lo, ro, |l, r| self.eq_expr(l, r))
|
|
|
|
&& over(lf, rf, |l, r| self.eq_field(l, r))
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-03-01 09:13:54 +00:00
|
|
|
(&ExprTup(ref l_tup), &ExprTup(ref r_tup)) => self.eq_exprs(l_tup, r_tup),
|
2016-02-29 11:19:32 +00:00
|
|
|
(&ExprTupField(ref le, li), &ExprTupField(ref re, ri)) => li.node == ri.node && self.eq_expr(le, re),
|
2016-03-01 09:13:54 +00:00
|
|
|
(&ExprUnary(l_op, ref le), &ExprUnary(r_op, ref re)) => l_op == r_op && self.eq_expr(le, re),
|
2016-09-30 13:35:24 +00:00
|
|
|
(&ExprArray(ref l), &ExprArray(ref r)) => self.eq_exprs(l, r),
|
2016-02-06 19:13:25 +00:00
|
|
|
(&ExprWhile(ref lc, ref lb, ref ll), &ExprWhile(ref rc, ref rb, ref rl)) => {
|
2016-05-27 12:24:28 +00:00
|
|
|
self.eq_expr(lc, rc) && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.node.as_str() == r.node.as_str())
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-06 19:13:25 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 21:44:00 +00:00
|
|
|
fn eq_exprs(&self, left: &P<[Expr]>, right: &P<[Expr]>) -> bool {
|
2016-02-06 19:13:25 +00:00
|
|
|
over(left, right, |l, r| self.eq_expr(l, r))
|
|
|
|
}
|
|
|
|
|
2016-03-07 15:30:02 +00:00
|
|
|
fn eq_field(&self, left: &Field, right: &Field) -> bool {
|
|
|
|
left.name.node == right.name.node && self.eq_expr(&left.expr, &right.expr)
|
|
|
|
}
|
|
|
|
|
2016-12-02 19:54:05 +00:00
|
|
|
fn eq_lifetime(&self, left: &Lifetime, right: &Lifetime) -> bool {
|
|
|
|
left.name == right.name
|
|
|
|
}
|
|
|
|
|
2016-02-06 19:13:25 +00:00
|
|
|
/// Check whether two patterns are the same.
|
|
|
|
pub fn eq_pat(&self, left: &Pat, right: &Pat) -> bool {
|
|
|
|
match (&left.node, &right.node) {
|
2016-02-29 11:19:32 +00:00
|
|
|
(&PatKind::Box(ref l), &PatKind::Box(ref r)) => self.eq_pat(l, r),
|
2016-05-27 12:24:28 +00:00
|
|
|
(&PatKind::TupleStruct(ref lp, ref la, ls), &PatKind::TupleStruct(ref rp, ref ra, rs)) => {
|
2016-12-02 19:54:05 +00:00
|
|
|
self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat(l, r)) && ls == rs
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-12-02 21:23:24 +00:00
|
|
|
(&PatKind::Binding(ref lb, _, ref li, ref lp), &PatKind::Binding(ref rb, _, ref ri, ref rp)) => {
|
|
|
|
lb == rb && li.node.as_str() == ri.node.as_str() && both(lp, rp, |l, r| self.eq_pat(l, r))
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-12-02 19:54:05 +00:00
|
|
|
(&PatKind::Path(ref l), &PatKind::Path(ref r)) => self.eq_qpath(l, r),
|
2016-07-10 13:42:02 +00:00
|
|
|
(&PatKind::Lit(ref l), &PatKind::Lit(ref r)) => self.eq_expr(l, r),
|
2016-06-05 23:42:39 +00:00
|
|
|
(&PatKind::Tuple(ref l, ls), &PatKind::Tuple(ref r, rs)) => {
|
|
|
|
ls == rs && over(l, r, |l, r| self.eq_pat(l, r))
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2017-01-26 22:32:34 +00:00
|
|
|
(&PatKind::Range(ref ls, ref le, ref li), &PatKind::Range(ref rs, ref re, ref ri)) => {
|
|
|
|
self.eq_expr(ls, rs) && self.eq_expr(le, re) && (*li == *ri)
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-29 11:19:32 +00:00
|
|
|
(&PatKind::Ref(ref le, ref lm), &PatKind::Ref(ref re, ref rm)) => lm == rm && self.eq_pat(le, re),
|
2016-09-30 13:35:24 +00:00
|
|
|
(&PatKind::Slice(ref ls, ref li, ref le), &PatKind::Slice(ref rs, ref ri, ref re)) => {
|
2017-11-04 19:55:56 +00:00
|
|
|
over(ls, rs, |l, r| self.eq_pat(l, r)) && over(le, re, |l, r| self.eq_pat(l, r))
|
|
|
|
&& both(li, ri, |l, r| self.eq_pat(l, r))
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-18 20:16:39 +00:00
|
|
|
(&PatKind::Wild, &PatKind::Wild) => true,
|
2016-02-06 19:13:25 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 19:54:05 +00:00
|
|
|
fn eq_qpath(&self, left: &QPath, right: &QPath) -> bool {
|
|
|
|
match (left, right) {
|
|
|
|
(&QPath::Resolved(ref lty, ref lpath), &QPath::Resolved(ref rty, ref rpath)) => {
|
|
|
|
both(lty, rty, |l, r| self.eq_ty(l, r)) && self.eq_path(lpath, rpath)
|
|
|
|
},
|
|
|
|
(&QPath::TypeRelative(ref lty, ref lseg), &QPath::TypeRelative(ref rty, ref rseg)) => {
|
|
|
|
self.eq_ty(lty, rty) && self.eq_path_segment(lseg, rseg)
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eq_path(&self, left: &Path, right: &Path) -> bool {
|
2017-11-04 19:55:56 +00:00
|
|
|
left.is_global() == right.is_global()
|
|
|
|
&& over(&left.segments, &right.segments, |l, r| self.eq_path_segment(l, r))
|
2016-12-02 19:54:05 +00:00
|
|
|
}
|
2016-10-01 20:18:17 +00:00
|
|
|
|
2016-12-02 19:54:05 +00:00
|
|
|
fn eq_path_parameters(&self, left: &PathParameters, right: &PathParameters) -> bool {
|
2017-08-25 07:30:21 +00:00
|
|
|
if !(left.parenthesized || right.parenthesized) {
|
2017-11-04 19:55:56 +00:00
|
|
|
over(&left.lifetimes, &right.lifetimes, |l, r| self.eq_lifetime(l, r))
|
|
|
|
&& over(&left.types, &right.types, |l, r| self.eq_ty(l, r))
|
|
|
|
&& over(&left.bindings, &right.bindings, |l, r| self.eq_type_binding(l, r))
|
2017-08-25 07:30:21 +00:00
|
|
|
} else if left.parenthesized && right.parenthesized {
|
2017-11-04 19:55:56 +00:00
|
|
|
over(left.inputs(), right.inputs(), |l, r| self.eq_ty(l, r))
|
|
|
|
&& both(
|
2017-09-03 21:15:15 +00:00
|
|
|
&Some(&left.bindings[0].ty),
|
|
|
|
&Some(&right.bindings[0].ty),
|
|
|
|
|l, r| self.eq_ty(l, r),
|
|
|
|
)
|
2017-08-25 07:30:21 +00:00
|
|
|
} else {
|
|
|
|
false
|
2016-10-01 20:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 19:54:05 +00:00
|
|
|
fn eq_path_segment(&self, left: &PathSegment, right: &PathSegment) -> bool {
|
|
|
|
// The == of idents doesn't work with different contexts,
|
|
|
|
// we have to be explicit about hygiene
|
2017-09-26 02:04:55 +00:00
|
|
|
if left.name.as_str() != right.name.as_str() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
match (&left.parameters, &right.parameters) {
|
|
|
|
(&None, &None) => true,
|
|
|
|
(&Some(ref l), &Some(ref r)) => self.eq_path_parameters(l, r),
|
2017-11-04 19:55:56 +00:00
|
|
|
_ => false,
|
2017-09-26 02:04:55 +00:00
|
|
|
}
|
2016-12-02 19:54:05 +00:00
|
|
|
}
|
|
|
|
|
2016-02-06 19:13:25 +00:00
|
|
|
fn eq_ty(&self, left: &Ty, right: &Ty) -> bool {
|
|
|
|
match (&left.node, &right.node) {
|
2016-09-30 13:35:24 +00:00
|
|
|
(&TySlice(ref l_vec), &TySlice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
|
2017-01-04 23:53:16 +00:00
|
|
|
(&TyArray(ref lt, ll_id), &TyArray(ref rt, rl_id)) => {
|
2017-11-04 19:55:56 +00:00
|
|
|
self.eq_ty(lt, rt)
|
|
|
|
&& self.eq_expr(&self.cx.tcx.hir.body(ll_id).value, &self.cx.tcx.hir.body(rl_id).value)
|
2017-01-04 23:53:16 +00:00
|
|
|
},
|
2016-03-01 09:13:54 +00:00
|
|
|
(&TyPtr(ref l_mut), &TyPtr(ref r_mut)) => l_mut.mutbl == r_mut.mutbl && self.eq_ty(&*l_mut.ty, &*r_mut.ty),
|
|
|
|
(&TyRptr(_, ref l_rmut), &TyRptr(_, ref r_rmut)) => {
|
|
|
|
l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(&*l_rmut.ty, &*r_rmut.ty)
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-12-02 19:54:05 +00:00
|
|
|
(&TyPath(ref l), &TyPath(ref r)) => self.eq_qpath(l, r),
|
2016-02-09 16:10:50 +00:00
|
|
|
(&TyTup(ref l), &TyTup(ref r)) => over(l, r, |l, r| self.eq_ty(l, r)),
|
2016-02-06 19:13:25 +00:00
|
|
|
(&TyInfer, &TyInfer) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2016-12-02 19:54:05 +00:00
|
|
|
|
|
|
|
fn eq_type_binding(&self, left: &TypeBinding, right: &TypeBinding) -> bool {
|
|
|
|
left.name == right.name && self.eq_ty(&left.ty, &right.ty)
|
|
|
|
}
|
2016-02-06 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 15:11:38 +00:00
|
|
|
fn swap_binop<'a>(binop: BinOp_, lhs: &'a Expr, rhs: &'a Expr) -> Option<(BinOp_, &'a Expr, &'a Expr)> {
|
|
|
|
match binop {
|
2016-12-20 17:21:30 +00:00
|
|
|
BiAdd | BiMul | BiBitXor | BiBitAnd | BiEq | BiNe | BiBitOr => Some((binop, rhs, lhs)),
|
2016-03-24 15:11:38 +00:00
|
|
|
BiLt => Some((BiGt, rhs, lhs)),
|
|
|
|
BiLe => Some((BiGe, rhs, lhs)),
|
|
|
|
BiGe => Some((BiLe, rhs, lhs)),
|
|
|
|
BiGt => Some((BiLt, rhs, lhs)),
|
|
|
|
BiShl | BiShr | BiRem | BiSub | BiDiv | BiAnd | BiOr => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-09 07:30:56 +00:00
|
|
|
/// Check if the two `Option`s are both `None` or some equal values as per
|
|
|
|
/// `eq_fn`.
|
2016-02-06 19:13:25 +00:00
|
|
|
fn both<X, F>(l: &Option<X>, r: &Option<X>, mut eq_fn: F) -> bool
|
2017-08-09 07:30:56 +00:00
|
|
|
where
|
|
|
|
F: FnMut(&X, &X) -> bool,
|
2016-02-06 19:13:25 +00:00
|
|
|
{
|
2017-09-05 09:33:04 +00:00
|
|
|
l.as_ref()
|
|
|
|
.map_or_else(|| r.is_none(), |x| r.as_ref().map_or(false, |y| eq_fn(x, y)))
|
2016-02-06 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if two slices are equal as per `eq_fn`.
|
|
|
|
fn over<X, F>(left: &[X], right: &[X], mut eq_fn: F) -> bool
|
2017-08-09 07:30:56 +00:00
|
|
|
where
|
|
|
|
F: FnMut(&X, &X) -> bool,
|
2016-02-06 19:13:25 +00:00
|
|
|
{
|
|
|
|
left.len() == right.len() && left.iter().zip(right).all(|(x, y)| eq_fn(x, y))
|
|
|
|
}
|
2016-02-09 14:18:27 +00:00
|
|
|
|
|
|
|
|
2017-08-09 07:30:56 +00:00
|
|
|
/// Type used to hash an ast element. This is different from the `Hash` trait
|
|
|
|
/// on ast types as this
|
2016-02-09 15:45:47 +00:00
|
|
|
/// trait would consider IDs and spans.
|
|
|
|
///
|
|
|
|
/// All expressions kind are hashed, but some might have a weaker hash.
|
2016-02-09 14:18:27 +00:00
|
|
|
pub struct SpanlessHash<'a, 'tcx: 'a> {
|
|
|
|
/// Context used to evaluate constant expressions.
|
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
2016-10-04 15:17:40 +00:00
|
|
|
s: DefaultHasher,
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
|
|
|
|
pub fn new(cx: &'a LateContext<'a, 'tcx>) -> Self {
|
2017-08-21 11:32:12 +00:00
|
|
|
Self {
|
2016-02-24 16:38:57 +00:00
|
|
|
cx: cx,
|
2016-10-04 15:17:40 +00:00
|
|
|
s: DefaultHasher::new(),
|
2016-02-24 16:38:57 +00:00
|
|
|
}
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn finish(&self) -> u64 {
|
|
|
|
self.s.finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hash_block(&mut self, b: &Block) {
|
|
|
|
for s in &b.stmts {
|
|
|
|
self.hash_stmt(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref e) = b.expr {
|
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
b.rules.hash(&mut self.s);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hash_expr(&mut self, e: &Expr) {
|
|
|
|
if let Some(e) = constant(self.cx, e) {
|
|
|
|
return e.hash(&mut self.s);
|
|
|
|
}
|
|
|
|
|
|
|
|
match e.node {
|
|
|
|
ExprAddrOf(m, ref e) => {
|
|
|
|
let c: fn(_, _) -> _ = ExprAddrOf;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
m.hash(&mut self.s);
|
|
|
|
self.hash_expr(e);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprAgain(i) => {
|
|
|
|
let c: fn(_) -> _ = ExprAgain;
|
|
|
|
c.hash(&mut self.s);
|
2017-02-27 08:45:02 +00:00
|
|
|
if let Some(i) = i.ident {
|
|
|
|
self.hash_name(&i.node.name);
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2017-08-30 08:54:24 +00:00
|
|
|
ExprYield(ref e) => {
|
|
|
|
let c: fn(_) -> _ = ExprYield;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
self.hash_expr(e);
|
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprAssign(ref l, ref r) => {
|
|
|
|
let c: fn(_, _) -> _ = ExprAssign;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
self.hash_expr(l);
|
|
|
|
self.hash_expr(r);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprAssignOp(ref o, ref l, ref r) => {
|
|
|
|
let c: fn(_, _, _) -> _ = ExprAssignOp;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
o.hash(&mut self.s);
|
|
|
|
self.hash_expr(l);
|
|
|
|
self.hash_expr(r);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprBlock(ref b) => {
|
|
|
|
let c: fn(_) -> _ = ExprBlock;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
self.hash_block(b);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprBinary(op, ref l, ref r) => {
|
|
|
|
let c: fn(_, _, _) -> _ = ExprBinary;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
op.node.hash(&mut self.s);
|
|
|
|
self.hash_expr(l);
|
|
|
|
self.hash_expr(r);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-11-25 18:24:55 +00:00
|
|
|
ExprBreak(i, ref j) => {
|
|
|
|
let c: fn(_, _) -> _ = ExprBreak;
|
2016-02-09 14:18:27 +00:00
|
|
|
c.hash(&mut self.s);
|
2017-02-27 08:45:02 +00:00
|
|
|
if let Some(i) = i.ident {
|
|
|
|
self.hash_name(&i.node.name);
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
2016-11-25 18:24:55 +00:00
|
|
|
if let Some(ref j) = *j {
|
|
|
|
self.hash_expr(&*j);
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprBox(ref e) => {
|
|
|
|
let c: fn(_) -> _ = ExprBox;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
self.hash_expr(e);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprCall(ref fun, ref args) => {
|
|
|
|
let c: fn(_, _) -> _ = ExprCall;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
self.hash_expr(fun);
|
|
|
|
self.hash_exprs(args);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprCast(ref e, ref _ty) => {
|
|
|
|
let c: fn(_, _) -> _ = ExprCast;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
self.hash_expr(e);
|
|
|
|
// TODO: _ty
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2017-08-30 08:54:24 +00:00
|
|
|
ExprClosure(cap, _, eid, _, _) => {
|
|
|
|
let c: fn(_, _, _, _, _) -> _ = ExprClosure;
|
2016-02-09 14:18:27 +00:00
|
|
|
c.hash(&mut self.s);
|
|
|
|
cap.hash(&mut self.s);
|
2017-02-02 16:53:28 +00:00
|
|
|
self.hash_expr(&self.cx.tcx.hir.body(eid).value);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprField(ref e, ref f) => {
|
|
|
|
let c: fn(_, _) -> _ = ExprField;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
self.hash_expr(e);
|
|
|
|
self.hash_name(&f.node);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprIndex(ref a, ref i) => {
|
|
|
|
let c: fn(_, _) -> _ = ExprIndex;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
self.hash_expr(a);
|
|
|
|
self.hash_expr(i);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-03-20 20:24:18 +00:00
|
|
|
ExprInlineAsm(..) => {
|
|
|
|
let c: fn(_, _, _) -> _ = ExprInlineAsm;
|
2016-02-09 14:18:27 +00:00
|
|
|
c.hash(&mut self.s);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprIf(ref cond, ref t, ref e) => {
|
|
|
|
let c: fn(_, _, _) -> _ = ExprIf;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
self.hash_expr(cond);
|
2017-03-31 17:23:35 +00:00
|
|
|
self.hash_expr(&**t);
|
2016-02-09 14:18:27 +00:00
|
|
|
if let Some(ref e) = *e {
|
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprLit(ref l) => {
|
|
|
|
let c: fn(_) -> _ = ExprLit;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
l.hash(&mut self.s);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-12-02 18:23:30 +00:00
|
|
|
ExprLoop(ref b, ref i, _) => {
|
2016-11-25 18:24:55 +00:00
|
|
|
let c: fn(_, _, _) -> _ = ExprLoop;
|
2016-02-09 14:18:27 +00:00
|
|
|
c.hash(&mut self.s);
|
|
|
|
self.hash_block(b);
|
|
|
|
if let Some(i) = *i {
|
2016-05-27 12:24:28 +00:00
|
|
|
self.hash_name(&i.node);
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-12-02 19:54:05 +00:00
|
|
|
ExprMatch(ref e, ref arms, ref s) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
let c: fn(_, _, _) -> _ = ExprMatch;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
self.hash_expr(e);
|
|
|
|
|
|
|
|
for arm in arms {
|
|
|
|
// TODO: arm.pat?
|
|
|
|
if let Some(ref e) = arm.guard {
|
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
|
|
|
self.hash_expr(&arm.body);
|
|
|
|
}
|
2016-12-02 19:54:05 +00:00
|
|
|
|
|
|
|
s.hash(&mut self.s);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2017-07-10 08:17:40 +00:00
|
|
|
ExprMethodCall(ref path, ref _tys, ref args) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
let c: fn(_, _, _) -> _ = ExprMethodCall;
|
|
|
|
c.hash(&mut self.s);
|
2017-07-10 08:17:40 +00:00
|
|
|
self.hash_name(&path.name);
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_exprs(args);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2017-01-04 22:18:11 +00:00
|
|
|
ExprRepeat(ref e, l_id) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
let c: fn(_, _) -> _ = ExprRepeat;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
self.hash_expr(e);
|
2017-02-02 16:53:28 +00:00
|
|
|
self.hash_expr(&self.cx.tcx.hir.body(l_id).value);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprRet(ref e) => {
|
|
|
|
let c: fn(_) -> _ = ExprRet;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
if let Some(ref e) = *e {
|
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-12-01 21:31:56 +00:00
|
|
|
ExprPath(ref qpath) => {
|
|
|
|
let c: fn(_) -> _ = ExprPath;
|
2016-02-09 14:18:27 +00:00
|
|
|
c.hash(&mut self.s);
|
2016-12-02 21:23:24 +00:00
|
|
|
self.hash_qpath(qpath);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprStruct(ref path, ref fields, ref expr) => {
|
|
|
|
let c: fn(_, _, _) -> _ = ExprStruct;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
|
2016-12-02 21:23:24 +00:00
|
|
|
self.hash_qpath(path);
|
2016-02-09 14:18:27 +00:00
|
|
|
|
|
|
|
for f in fields {
|
|
|
|
self.hash_name(&f.name.node);
|
|
|
|
self.hash_expr(&f.expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref e) = *expr {
|
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprTup(ref tup) => {
|
|
|
|
let c: fn(_) -> _ = ExprTup;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
self.hash_exprs(tup);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprTupField(ref le, li) => {
|
|
|
|
let c: fn(_, _) -> _ = ExprTupField;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
|
|
|
|
self.hash_expr(le);
|
|
|
|
li.node.hash(&mut self.s);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-10-01 20:26:19 +00:00
|
|
|
ExprType(ref e, ref _ty) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
let c: fn(_, _) -> _ = ExprType;
|
|
|
|
c.hash(&mut self.s);
|
2016-10-01 20:26:19 +00:00
|
|
|
self.hash_expr(e);
|
|
|
|
// TODO: _ty
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprUnary(lop, ref le) => {
|
|
|
|
let c: fn(_, _) -> _ = ExprUnary;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
|
|
|
|
lop.hash(&mut self.s);
|
|
|
|
self.hash_expr(le);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-09-30 13:35:24 +00:00
|
|
|
ExprArray(ref v) => {
|
|
|
|
let c: fn(_) -> _ = ExprArray;
|
2016-02-09 14:18:27 +00:00
|
|
|
c.hash(&mut self.s);
|
|
|
|
|
|
|
|
self.hash_exprs(v);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
ExprWhile(ref cond, ref b, l) => {
|
|
|
|
let c: fn(_, _, _) -> _ = ExprWhile;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
|
|
|
|
self.hash_expr(cond);
|
|
|
|
self.hash_block(b);
|
|
|
|
if let Some(l) = l {
|
2016-05-27 12:24:28 +00:00
|
|
|
self.hash_name(&l.node);
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 21:44:00 +00:00
|
|
|
pub fn hash_exprs(&mut self, e: &P<[Expr]>) {
|
2016-02-09 14:18:27 +00:00
|
|
|
for e in e {
|
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hash_name(&mut self, n: &Name) {
|
|
|
|
n.as_str().hash(&mut self.s);
|
|
|
|
}
|
|
|
|
|
2016-12-02 21:23:24 +00:00
|
|
|
pub fn hash_qpath(&mut self, p: &QPath) {
|
|
|
|
match *p {
|
|
|
|
QPath::Resolved(_, ref path) => {
|
|
|
|
self.hash_path(path);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-12-02 21:23:24 +00:00
|
|
|
QPath::TypeRelative(_, ref path) => {
|
|
|
|
self.hash_name(&path.name);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-12-02 21:23:24 +00:00
|
|
|
}
|
2017-01-13 16:04:56 +00:00
|
|
|
// self.cx.tables.qpath_def(p, id).hash(&mut self.s);
|
2016-12-01 21:31:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-09 14:18:27 +00:00
|
|
|
pub fn hash_path(&mut self, p: &Path) {
|
2017-01-04 04:40:42 +00:00
|
|
|
p.is_global().hash(&mut self.s);
|
2016-02-09 14:18:27 +00:00
|
|
|
for p in &p.segments {
|
2016-05-19 21:14:34 +00:00
|
|
|
self.hash_name(&p.name);
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hash_stmt(&mut self, b: &Stmt) {
|
|
|
|
match b.node {
|
2016-10-02 00:17:04 +00:00
|
|
|
StmtDecl(ref decl, _) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
let c: fn(_, _) -> _ = StmtDecl;
|
|
|
|
c.hash(&mut self.s);
|
2016-10-02 00:17:04 +00:00
|
|
|
|
|
|
|
if let DeclLocal(ref local) = decl.node {
|
|
|
|
if let Some(ref init) = local.init {
|
|
|
|
self.hash_expr(init);
|
|
|
|
}
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
StmtExpr(ref expr, _) => {
|
|
|
|
let c: fn(_, _) -> _ = StmtExpr;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
self.hash_expr(expr);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
StmtSemi(ref expr, _) => {
|
|
|
|
let c: fn(_, _) -> _ = StmtSemi;
|
|
|
|
c.hash(&mut self.s);
|
|
|
|
self.hash_expr(expr);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|