2018-11-27 14:14:15 -06:00
|
|
|
use crate::consts::{constant_context, constant_simple};
|
2018-05-30 03:15:50 -05:00
|
|
|
use crate::utils::differing_macro_contexts;
|
2020-05-06 09:03:53 -05:00
|
|
|
use rustc_ast::ast::InlineAsmTemplatePiece;
|
2019-10-22 04:18:18 -05:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2020-02-21 02:39:38 -06:00
|
|
|
use rustc_hir::{
|
2020-08-28 09:10:16 -05:00
|
|
|
BinOpKind, Block, BlockCheckMode, BodyId, BorrowKind, CaptureBy, Expr, ExprKind, Field, FieldPat, FnRetTy,
|
|
|
|
GenericArg, GenericArgs, Guard, InlineAsmOperand, Lifetime, LifetimeName, ParamName, Pat, PatKind, Path,
|
|
|
|
PathSegment, QPath, Stmt, StmtKind, Ty, TyKind, TypeBinding,
|
2020-02-21 02:39:38 -06:00
|
|
|
};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::LateContext;
|
2020-03-30 04:02:14 -05:00
|
|
|
use rustc_middle::ich::StableHashingContextProvider;
|
2020-07-17 03:47:04 -05:00
|
|
|
use rustc_middle::ty::TypeckResults;
|
2020-05-08 06:57:01 -05:00
|
|
|
use rustc_span::Symbol;
|
2019-10-22 04:18:18 -05:00
|
|
|
use std::hash::Hash;
|
2016-02-06 13:13:25 -06:00
|
|
|
|
2017-08-09 02:30:56 -05: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 13:13:25 -06:00
|
|
|
///
|
|
|
|
/// Note that some expressions kinds are not considered but could be added.
|
2019-06-19 13:36:23 -05:00
|
|
|
pub struct SpanlessEq<'a, 'tcx> {
|
2016-02-06 13:13:25 -06:00
|
|
|
/// Context used to evaluate constant expressions.
|
2020-06-25 15:41:36 -05:00
|
|
|
cx: &'a LateContext<'tcx>,
|
2020-07-17 03:47:04 -05:00
|
|
|
maybe_typeck_results: Option<&'tcx TypeckResults<'tcx>>,
|
2020-08-28 09:10:16 -05:00
|
|
|
allow_side_effects: bool,
|
2016-02-06 13:13:25 -06:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:36:23 -05:00
|
|
|
impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
|
2020-06-25 15:41:36 -05:00
|
|
|
pub fn new(cx: &'a LateContext<'tcx>) -> Self {
|
2017-08-21 06:32:12 -05:00
|
|
|
Self {
|
2018-03-15 10:07:15 -05:00
|
|
|
cx,
|
2020-07-17 03:47:04 -05:00
|
|
|
maybe_typeck_results: cx.maybe_typeck_results(),
|
2020-08-28 09:10:16 -05:00
|
|
|
allow_side_effects: true,
|
2016-02-24 10:38:57 -06:00
|
|
|
}
|
2016-02-06 13:13:25 -06:00
|
|
|
}
|
|
|
|
|
2020-08-28 09:10:16 -05:00
|
|
|
/// Consider expressions containing potential side effects as not equal.
|
|
|
|
pub fn deny_side_effects(self) -> Self {
|
2017-08-21 06:32:12 -05:00
|
|
|
Self {
|
2020-08-28 09:10:16 -05:00
|
|
|
allow_side_effects: false,
|
2020-06-25 21:55:23 -05:00
|
|
|
..self
|
2016-02-24 10:38:57 -06:00
|
|
|
}
|
2016-02-06 13:13:25 -06:00
|
|
|
}
|
|
|
|
|
2019-01-30 19:15:29 -06:00
|
|
|
/// Checks whether two statements are the same.
|
2019-12-27 01:12:26 -06:00
|
|
|
pub fn eq_stmt(&mut self, left: &Stmt<'_>, right: &Stmt<'_>) -> bool {
|
2019-09-27 10:16:06 -05:00
|
|
|
match (&left.kind, &right.kind) {
|
2019-01-20 04:49:45 -06:00
|
|
|
(&StmtKind::Local(ref l), &StmtKind::Local(ref r)) => {
|
2019-01-20 04:21:30 -06:00
|
|
|
self.eq_pat(&l.pat, &r.pat)
|
2019-07-24 16:59:32 -05:00
|
|
|
&& both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r))
|
2019-01-20 04:21:30 -06:00
|
|
|
&& both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
|
2019-01-20 04:49:45 -06:00
|
|
|
},
|
|
|
|
(&StmtKind::Expr(ref l), &StmtKind::Expr(ref r)) | (&StmtKind::Semi(ref l), &StmtKind::Semi(ref r)) => {
|
|
|
|
self.eq_expr(l, r)
|
|
|
|
},
|
2016-02-06 13:13:25 -06:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 19:15:29 -06:00
|
|
|
/// Checks whether two blocks are the same.
|
2019-12-27 01:12:26 -06:00
|
|
|
pub fn eq_block(&mut self, left: &Block<'_>, right: &Block<'_>) -> bool {
|
2017-11-04 14:55:56 -05: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 13:13:25 -06:00
|
|
|
}
|
|
|
|
|
2018-10-11 17:18:58 -05:00
|
|
|
#[allow(clippy::similar_names)]
|
2019-12-27 01:12:26 -06:00
|
|
|
pub fn eq_expr(&mut self, left: &Expr<'_>, right: &Expr<'_>) -> bool {
|
2020-08-28 09:10:16 -05:00
|
|
|
if !self.allow_side_effects && differing_macro_contexts(left.span, right.span) {
|
2016-02-13 08:36:57 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-17 03:47:04 -05:00
|
|
|
if let Some(typeck_results) = self.maybe_typeck_results {
|
2020-06-25 21:55:23 -05:00
|
|
|
if let (Some(l), Some(r)) = (
|
2020-07-17 03:47:04 -05:00
|
|
|
constant_simple(self.cx, typeck_results, left),
|
|
|
|
constant_simple(self.cx, typeck_results, right),
|
2020-06-25 21:55:23 -05:00
|
|
|
) {
|
|
|
|
if l == r {
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-06 13:13:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:16:06 -05:00
|
|
|
match (&left.kind, &right.kind) {
|
2019-11-27 16:34:32 -06:00
|
|
|
(&ExprKind::AddrOf(lb, l_mut, ref le), &ExprKind::AddrOf(rb, r_mut, ref re)) => {
|
|
|
|
lb == rb && l_mut == r_mut && self.eq_expr(le, re)
|
2018-11-27 14:14:15 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
(&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
|
2018-06-28 08:46:58 -05:00
|
|
|
both(&li.label, &ri.label, |l, r| l.ident.as_str() == r.ident.as_str())
|
2017-02-27 02:49:02 -06:00
|
|
|
},
|
2019-12-23 22:16:04 -06:00
|
|
|
(&ExprKind::Assign(ref ll, ref lr, _), &ExprKind::Assign(ref rl, ref rr, _)) => {
|
2020-08-28 09:10:16 -05:00
|
|
|
self.allow_side_effects && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
|
2018-11-27 14:14:15 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
(&ExprKind::AssignOp(ref lo, ref ll, ref lr), &ExprKind::AssignOp(ref ro, ref rl, ref rr)) => {
|
2020-08-28 09:10:16 -05:00
|
|
|
self.allow_side_effects && lo.node == ro.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
(&ExprKind::Block(ref l, _), &ExprKind::Block(ref r, _)) => self.eq_block(l, r),
|
|
|
|
(&ExprKind::Binary(l_op, ref ll, ref lr), &ExprKind::Binary(r_op, ref rl, ref rr)) => {
|
2017-11-04 14:55:56 -05: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 02:30:56 -05:00
|
|
|
l_op == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
|
|
|
|
})
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
(&ExprKind::Break(li, ref le), &ExprKind::Break(ri, ref re)) => {
|
2018-06-28 08:46:58 -05:00
|
|
|
both(&li.label, &ri.label, |l, r| l.ident.as_str() == r.ident.as_str())
|
2017-11-04 14:55:56 -05:00
|
|
|
&& both(le, re, |l, r| self.eq_expr(l, r))
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
(&ExprKind::Box(ref l), &ExprKind::Box(ref r)) => self.eq_expr(l, r),
|
2019-12-27 01:12:26 -06:00
|
|
|
(&ExprKind::Call(l_fun, l_args), &ExprKind::Call(r_fun, r_args)) => {
|
2020-08-28 09:10:16 -05:00
|
|
|
self.allow_side_effects && self.eq_expr(l_fun, r_fun) && self.eq_exprs(l_args, r_args)
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-11-27 14:14:15 -06:00
|
|
|
(&ExprKind::Cast(ref lx, ref lt), &ExprKind::Cast(ref rx, ref rt))
|
|
|
|
| (&ExprKind::Type(ref lx, ref lt), &ExprKind::Type(ref rx, ref rt)) => {
|
|
|
|
self.eq_expr(lx, rx) && self.eq_ty(lt, rt)
|
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
(&ExprKind::Field(ref l_f_exp, ref l_f_ident), &ExprKind::Field(ref r_f_exp, ref r_f_ident)) => {
|
2018-05-29 03:56:58 -05:00
|
|
|
l_f_ident.name == r_f_ident.name && self.eq_expr(l_f_exp, r_f_exp)
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-11-27 14:14:15 -06:00
|
|
|
(&ExprKind::Index(ref la, ref li), &ExprKind::Index(ref ra, ref ri)) => {
|
|
|
|
self.eq_expr(la, ra) && self.eq_expr(li, ri)
|
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
(&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node,
|
|
|
|
(&ExprKind::Loop(ref lb, ref ll, ref lls), &ExprKind::Loop(ref rb, ref rl, ref rls)) => {
|
2018-06-28 08:46:58 -05:00
|
|
|
lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.as_str() == r.ident.as_str())
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
(&ExprKind::Match(ref le, ref la, ref ls), &ExprKind::Match(ref re, ref ra, ref rs)) => {
|
2018-11-27 14:14:15 -06:00
|
|
|
ls == rs
|
|
|
|
&& self.eq_expr(le, re)
|
|
|
|
&& over(la, ra, |l, r| {
|
|
|
|
self.eq_expr(&l.body, &r.body)
|
|
|
|
&& both(&l.guard, &r.guard, |l, r| self.eq_guard(l, r))
|
2019-09-25 14:00:17 -05:00
|
|
|
&& self.eq_pat(&l.pat, &r.pat)
|
2018-11-27 14:14:15 -06:00
|
|
|
})
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2020-06-09 16:44:04 -05:00
|
|
|
(&ExprKind::MethodCall(l_path, _, l_args, _), &ExprKind::MethodCall(r_path, _, r_args, _)) => {
|
2020-08-28 09:10:16 -05:00
|
|
|
self.allow_side_effects && self.eq_path_segment(l_path, r_path) && self.eq_exprs(l_args, r_args)
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
(&ExprKind::Repeat(ref le, ref ll_id), &ExprKind::Repeat(ref re, ref rl_id)) => {
|
2020-07-17 03:47:04 -05:00
|
|
|
let mut celcx = constant_context(self.cx, self.cx.tcx.typeck_body(ll_id.body));
|
2018-12-07 18:56:03 -06:00
|
|
|
let ll = celcx.expr(&self.cx.tcx.hir().body(ll_id.body).value);
|
2020-07-17 03:47:04 -05:00
|
|
|
let mut celcx = constant_context(self.cx, self.cx.tcx.typeck_body(rl_id.body));
|
2018-12-07 18:56:03 -06:00
|
|
|
let rl = celcx.expr(&self.cx.tcx.hir().body(rl_id.body).value);
|
2018-02-05 17:31:06 -06:00
|
|
|
|
|
|
|
self.eq_expr(le, re) && ll == rl
|
2017-01-04 17:53:16 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
(&ExprKind::Ret(ref l), &ExprKind::Ret(ref r)) => both(l, r, |l, r| self.eq_expr(l, r)),
|
|
|
|
(&ExprKind::Path(ref l), &ExprKind::Path(ref r)) => self.eq_qpath(l, r),
|
|
|
|
(&ExprKind::Struct(ref l_path, ref lf, ref lo), &ExprKind::Struct(ref r_path, ref rf, ref ro)) => {
|
2018-11-27 14:14:15 -06:00
|
|
|
self.eq_qpath(l_path, r_path)
|
|
|
|
&& both(lo, ro, |l, r| self.eq_expr(l, r))
|
2017-11-04 14:55:56 -05:00
|
|
|
&& over(lf, rf, |l, r| self.eq_field(l, r))
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2019-12-27 01:12:26 -06:00
|
|
|
(&ExprKind::Tup(l_tup), &ExprKind::Tup(r_tup)) => self.eq_exprs(l_tup, r_tup),
|
2018-07-12 02:30:57 -05:00
|
|
|
(&ExprKind::Unary(l_op, ref le), &ExprKind::Unary(r_op, ref re)) => l_op == r_op && self.eq_expr(le, re),
|
2019-12-27 01:12:26 -06:00
|
|
|
(&ExprKind::Array(l), &ExprKind::Array(r)) => self.eq_exprs(l, r),
|
2019-05-01 15:18:05 -05:00
|
|
|
(&ExprKind::DropTemps(ref le), &ExprKind::DropTemps(ref re)) => self.eq_expr(le, re),
|
2016-02-06 13:13:25 -06:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 01:12:26 -06:00
|
|
|
fn eq_exprs(&mut self, left: &[Expr<'_>], right: &[Expr<'_>]) -> bool {
|
2016-02-06 13:13:25 -06:00
|
|
|
over(left, right, |l, r| self.eq_expr(l, r))
|
|
|
|
}
|
|
|
|
|
2019-12-27 01:12:26 -06:00
|
|
|
fn eq_field(&mut self, left: &Field<'_>, right: &Field<'_>) -> bool {
|
2018-05-29 03:56:58 -05:00
|
|
|
left.ident.name == right.ident.name && self.eq_expr(&left.expr, &right.expr)
|
2016-03-07 09:30:02 -06:00
|
|
|
}
|
|
|
|
|
2019-12-27 01:12:26 -06:00
|
|
|
fn eq_guard(&mut self, left: &Guard<'_>, right: &Guard<'_>) -> bool {
|
2018-09-02 02:38:25 -05:00
|
|
|
match (left, right) {
|
|
|
|
(Guard::If(l), Guard::If(r)) => self.eq_expr(l, r),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 22:02:10 -06:00
|
|
|
fn eq_generic_arg(&mut self, left: &GenericArg<'_>, right: &GenericArg<'_>) -> bool {
|
2018-06-24 16:42:52 -05:00
|
|
|
match (left, right) {
|
2019-10-03 14:09:32 -05:00
|
|
|
(GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => Self::eq_lifetime(l_lt, r_lt),
|
2018-06-24 16:42:52 -05:00
|
|
|
(GenericArg::Type(l_ty), GenericArg::Type(r_ty)) => self.eq_ty(l_ty, r_ty),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 14:09:32 -05:00
|
|
|
fn eq_lifetime(left: &Lifetime, right: &Lifetime) -> bool {
|
2016-12-02 13:54:05 -06:00
|
|
|
left.name == right.name
|
|
|
|
}
|
|
|
|
|
2020-08-04 08:24:13 -05:00
|
|
|
pub fn eq_fieldpat(&mut self, left: &FieldPat<'_>, right: &FieldPat<'_>) -> bool {
|
2020-08-28 09:10:16 -05:00
|
|
|
let (FieldPat { ident: li, pat: lp, .. }, FieldPat { ident: ri, pat: rp, .. }) = (&left, &right);
|
|
|
|
li.name.as_str() == ri.name.as_str() && self.eq_pat(lp, rp)
|
2020-08-04 08:24:13 -05:00
|
|
|
}
|
|
|
|
|
2019-01-30 19:15:29 -06:00
|
|
|
/// Checks whether two patterns are the same.
|
2019-12-27 01:12:26 -06:00
|
|
|
pub fn eq_pat(&mut self, left: &Pat<'_>, right: &Pat<'_>) -> bool {
|
2019-09-27 10:16:06 -05:00
|
|
|
match (&left.kind, &right.kind) {
|
2016-02-29 05:19:32 -06:00
|
|
|
(&PatKind::Box(ref l), &PatKind::Box(ref r)) => self.eq_pat(l, r),
|
2020-08-04 08:24:13 -05:00
|
|
|
(&PatKind::Struct(ref lp, ref la, ..), &PatKind::Struct(ref rp, ref ra, ..)) => {
|
|
|
|
self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_fieldpat(l, r))
|
|
|
|
},
|
2016-05-27 07:24:28 -05:00
|
|
|
(&PatKind::TupleStruct(ref lp, ref la, ls), &PatKind::TupleStruct(ref rp, ref ra, rs)) => {
|
2016-12-02 13:54:05 -06:00
|
|
|
self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat(l, r)) && ls == rs
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2019-02-03 01:12:07 -06:00
|
|
|
(&PatKind::Binding(ref lb, .., ref li, ref lp), &PatKind::Binding(ref rb, .., ref ri, ref rp)) => {
|
2018-06-28 08:46:58 -05:00
|
|
|
lb == rb && li.name.as_str() == ri.name.as_str() && both(lp, rp, |l, r| self.eq_pat(l, r))
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-12-02 13:54:05 -06:00
|
|
|
(&PatKind::Path(ref l), &PatKind::Path(ref r)) => self.eq_qpath(l, r),
|
2016-07-10 08:42:02 -05:00
|
|
|
(&PatKind::Lit(ref l), &PatKind::Lit(ref r)) => self.eq_expr(l, r),
|
2016-06-05 18:42:39 -05: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 11:21:30 -06:00
|
|
|
},
|
2020-01-11 04:39:43 -06:00
|
|
|
(&PatKind::Range(ref ls, ref le, li), &PatKind::Range(ref rs, ref re, ri)) => {
|
|
|
|
both(ls, rs, |a, b| self.eq_expr(a, b)) && both(le, re, |a, b| self.eq_expr(a, b)) && (li == ri)
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-02-29 05:19:32 -06:00
|
|
|
(&PatKind::Ref(ref le, ref lm), &PatKind::Ref(ref re, ref rm)) => lm == rm && self.eq_pat(le, re),
|
2016-09-30 08:35:24 -05:00
|
|
|
(&PatKind::Slice(ref ls, ref li, ref le), &PatKind::Slice(ref rs, ref ri, ref re)) => {
|
2018-11-27 14:14:15 -06:00
|
|
|
over(ls, rs, |l, r| self.eq_pat(l, r))
|
|
|
|
&& over(le, re, |l, r| self.eq_pat(l, r))
|
2017-11-04 14:55:56 -05:00
|
|
|
&& both(li, ri, |l, r| self.eq_pat(l, r))
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-02-18 14:16:39 -06:00
|
|
|
(&PatKind::Wild, &PatKind::Wild) => true,
|
2016-02-06 13:13:25 -06:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 17:18:58 -05:00
|
|
|
#[allow(clippy::similar_names)]
|
2019-12-29 22:02:10 -06:00
|
|
|
fn eq_qpath(&mut self, left: &QPath<'_>, right: &QPath<'_>) -> bool {
|
2016-12-02 13:54:05 -06:00
|
|
|
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)
|
|
|
|
},
|
2020-08-28 09:10:16 -05:00
|
|
|
(&QPath::LangItem(llang_item, _), &QPath::LangItem(rlang_item, _)) => llang_item == rlang_item,
|
2016-12-02 13:54:05 -06:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 22:02:10 -06:00
|
|
|
fn eq_path(&mut self, left: &Path<'_>, right: &Path<'_>) -> bool {
|
2017-11-04 14:55:56 -05:00
|
|
|
left.is_global() == right.is_global()
|
|
|
|
&& over(&left.segments, &right.segments, |l, r| self.eq_path_segment(l, r))
|
2016-12-02 13:54:05 -06:00
|
|
|
}
|
2016-10-01 15:18:17 -05:00
|
|
|
|
2019-12-29 22:02:10 -06:00
|
|
|
fn eq_path_parameters(&mut self, left: &GenericArgs<'_>, right: &GenericArgs<'_>) -> bool {
|
2017-08-25 02:30:21 -05:00
|
|
|
if !(left.parenthesized || right.parenthesized) {
|
2018-06-24 16:42:52 -05:00
|
|
|
over(&left.args, &right.args, |l, r| self.eq_generic_arg(l, r)) // FIXME(flip1995): may not work
|
2017-11-04 14:55:56 -05:00
|
|
|
&& over(&left.bindings, &right.bindings, |l, r| self.eq_type_binding(l, r))
|
2017-08-25 02:30:21 -05:00
|
|
|
} else if left.parenthesized && right.parenthesized {
|
2017-11-04 14:55:56 -05:00
|
|
|
over(left.inputs(), right.inputs(), |l, r| self.eq_ty(l, r))
|
2019-06-06 02:21:19 -05:00
|
|
|
&& both(&Some(&left.bindings[0].ty()), &Some(&right.bindings[0].ty()), |l, r| {
|
2018-11-27 14:14:15 -06:00
|
|
|
self.eq_ty(l, r)
|
|
|
|
})
|
2017-08-25 02:30:21 -05:00
|
|
|
} else {
|
|
|
|
false
|
2016-10-01 15:18:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 22:02:10 -06:00
|
|
|
pub fn eq_path_segments(&mut self, left: &[PathSegment<'_>], right: &[PathSegment<'_>]) -> bool {
|
2018-07-14 17:00:27 -05:00
|
|
|
left.len() == right.len() && left.iter().zip(right).all(|(l, r)| self.eq_path_segment(l, r))
|
|
|
|
}
|
|
|
|
|
2019-12-29 22:02:10 -06:00
|
|
|
pub fn eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_>) -> bool {
|
2016-12-02 13:54:05 -06:00
|
|
|
// The == of idents doesn't work with different contexts,
|
|
|
|
// we have to be explicit about hygiene
|
2020-10-28 17:36:07 -05:00
|
|
|
left.ident.as_str() == right.ident.as_str()
|
|
|
|
&& both(&left.args, &right.args, |l, r| self.eq_path_parameters(l, r))
|
2016-12-02 13:54:05 -06:00
|
|
|
}
|
|
|
|
|
2019-12-29 22:02:10 -06:00
|
|
|
pub fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {
|
2019-09-27 10:16:06 -05:00
|
|
|
self.eq_ty_kind(&left.kind, &right.kind)
|
2018-07-14 17:00:27 -05:00
|
|
|
}
|
|
|
|
|
2018-10-11 17:18:58 -05:00
|
|
|
#[allow(clippy::similar_names)]
|
2019-12-29 22:02:10 -06:00
|
|
|
pub fn eq_ty_kind(&mut self, left: &TyKind<'_>, right: &TyKind<'_>) -> bool {
|
2018-07-14 17:00:27 -05:00
|
|
|
match (left, right) {
|
2018-07-12 03:03:06 -05:00
|
|
|
(&TyKind::Slice(ref l_vec), &TyKind::Slice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
|
|
|
|
(&TyKind::Array(ref lt, ref ll_id), &TyKind::Array(ref rt, ref rl_id)) => {
|
2020-07-17 03:47:04 -05:00
|
|
|
let old_maybe_typeck_results = self.maybe_typeck_results;
|
2018-05-17 13:17:21 -05:00
|
|
|
|
2020-07-17 03:47:04 -05:00
|
|
|
let mut celcx = constant_context(self.cx, self.cx.tcx.typeck_body(ll_id.body));
|
|
|
|
self.maybe_typeck_results = Some(self.cx.tcx.typeck_body(ll_id.body));
|
2018-12-07 18:56:03 -06:00
|
|
|
let ll = celcx.expr(&self.cx.tcx.hir().body(ll_id.body).value);
|
2018-05-17 13:17:21 -05:00
|
|
|
|
2020-07-17 03:47:04 -05:00
|
|
|
let mut celcx = constant_context(self.cx, self.cx.tcx.typeck_body(rl_id.body));
|
|
|
|
self.maybe_typeck_results = Some(self.cx.tcx.typeck_body(rl_id.body));
|
2018-12-07 18:56:03 -06:00
|
|
|
let rl = celcx.expr(&self.cx.tcx.hir().body(rl_id.body).value);
|
2018-05-17 13:17:21 -05:00
|
|
|
|
|
|
|
let eq_ty = self.eq_ty(lt, rt);
|
2020-07-17 03:47:04 -05:00
|
|
|
self.maybe_typeck_results = old_maybe_typeck_results;
|
2018-05-17 13:17:21 -05:00
|
|
|
eq_ty && ll == rl
|
2017-01-04 17:53:16 -06:00
|
|
|
},
|
2018-11-27 14:14:15 -06:00
|
|
|
(&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)
|
|
|
|
},
|
2018-07-12 03:03:06 -05:00
|
|
|
(&TyKind::Rptr(_, ref l_rmut), &TyKind::Rptr(_, ref r_rmut)) => {
|
2016-03-01 03:13:54 -06:00
|
|
|
l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(&*l_rmut.ty, &*r_rmut.ty)
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 03:03:06 -05:00
|
|
|
(&TyKind::Path(ref l), &TyKind::Path(ref r)) => self.eq_qpath(l, r),
|
|
|
|
(&TyKind::Tup(ref l), &TyKind::Tup(ref r)) => over(l, r, |l, r| self.eq_ty(l, r)),
|
|
|
|
(&TyKind::Infer, &TyKind::Infer) => true,
|
2016-02-06 13:13:25 -06:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2016-12-02 13:54:05 -06:00
|
|
|
|
2019-12-29 22:02:10 -06:00
|
|
|
fn eq_type_binding(&mut self, left: &TypeBinding<'_>, right: &TypeBinding<'_>) -> bool {
|
2019-06-06 02:21:19 -05:00
|
|
|
left.ident.name == right.ident.name && self.eq_ty(&left.ty(), &right.ty())
|
2016-12-02 13:54:05 -06:00
|
|
|
}
|
2016-02-06 13:13:25 -06:00
|
|
|
}
|
|
|
|
|
2019-12-27 01:12:26 -06:00
|
|
|
fn swap_binop<'a>(
|
|
|
|
binop: BinOpKind,
|
|
|
|
lhs: &'a Expr<'a>,
|
|
|
|
rhs: &'a Expr<'a>,
|
|
|
|
) -> Option<(BinOpKind, &'a Expr<'a>, &'a Expr<'a>)> {
|
2016-03-24 10:11:38 -05:00
|
|
|
match binop {
|
2020-06-23 10:05:22 -05:00
|
|
|
BinOpKind::Add | BinOpKind::Eq | BinOpKind::Ne | BinOpKind::BitAnd | BinOpKind::BitXor | BinOpKind::BitOr => {
|
|
|
|
Some((binop, rhs, lhs))
|
|
|
|
},
|
2018-07-12 02:50:09 -05:00
|
|
|
BinOpKind::Lt => Some((BinOpKind::Gt, rhs, lhs)),
|
|
|
|
BinOpKind::Le => Some((BinOpKind::Ge, rhs, lhs)),
|
|
|
|
BinOpKind::Ge => Some((BinOpKind::Le, rhs, lhs)),
|
|
|
|
BinOpKind::Gt => Some((BinOpKind::Lt, rhs, lhs)),
|
2020-06-23 10:05:22 -05:00
|
|
|
BinOpKind::Mul // Not always commutative, e.g. with matrices. See issue #5698
|
|
|
|
| BinOpKind::Shl
|
2018-11-27 14:14:15 -06:00
|
|
|
| BinOpKind::Shr
|
|
|
|
| BinOpKind::Rem
|
|
|
|
| BinOpKind::Sub
|
|
|
|
| BinOpKind::Div
|
|
|
|
| BinOpKind::And
|
|
|
|
| BinOpKind::Or => None,
|
2016-03-24 10:11:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 19:15:29 -06:00
|
|
|
/// Checks if the two `Option`s are both `None` or some equal values as per
|
2017-08-09 02:30:56 -05:00
|
|
|
/// `eq_fn`.
|
2020-06-09 09:36:01 -05:00
|
|
|
pub fn both<X>(l: &Option<X>, r: &Option<X>, mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
|
2017-09-05 04:33:04 -05: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 13:13:25 -06:00
|
|
|
}
|
|
|
|
|
2019-01-30 19:15:29 -06:00
|
|
|
/// Checks if two slices are equal as per `eq_fn`.
|
2020-06-09 09:36:01 -05:00
|
|
|
pub fn over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
|
2016-02-06 13:13:25 -06:00
|
|
|
left.len() == right.len() && left.iter().zip(right).all(|(x, y)| eq_fn(x, y))
|
|
|
|
}
|
2016-02-09 08:18:27 -06:00
|
|
|
|
2020-08-28 09:10:16 -05:00
|
|
|
/// Checks if two expressions evaluate to the same value, and don't contain any side effects.
|
|
|
|
pub fn eq_expr_value(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>) -> bool {
|
|
|
|
SpanlessEq::new(cx).deny_side_effects().eq_expr(left, right)
|
|
|
|
}
|
|
|
|
|
2017-08-09 02:30:56 -05:00
|
|
|
/// Type used to hash an ast element. This is different from the `Hash` trait
|
|
|
|
/// on ast types as this
|
2016-02-09 09:45:47 -06:00
|
|
|
/// trait would consider IDs and spans.
|
|
|
|
///
|
|
|
|
/// All expressions kind are hashed, but some might have a weaker hash.
|
2019-06-19 13:36:23 -05:00
|
|
|
pub struct SpanlessHash<'a, 'tcx> {
|
2016-02-09 08:18:27 -06:00
|
|
|
/// Context used to evaluate constant expressions.
|
2020-06-25 15:41:36 -05:00
|
|
|
cx: &'a LateContext<'tcx>,
|
2020-07-17 03:47:04 -05:00
|
|
|
maybe_typeck_results: Option<&'tcx TypeckResults<'tcx>>,
|
2019-10-22 04:18:18 -05:00
|
|
|
s: StableHasher,
|
2016-02-09 08:18:27 -06:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:36:23 -05:00
|
|
|
impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
|
2020-06-25 15:41:36 -05:00
|
|
|
pub fn new(cx: &'a LateContext<'tcx>) -> Self {
|
2017-08-21 06:32:12 -05:00
|
|
|
Self {
|
2018-03-15 10:07:15 -05:00
|
|
|
cx,
|
2020-07-17 03:47:04 -05:00
|
|
|
maybe_typeck_results: cx.maybe_typeck_results(),
|
2019-10-22 04:18:18 -05:00
|
|
|
s: StableHasher::new(),
|
2016-02-24 10:38:57 -06:00
|
|
|
}
|
2016-02-09 08:18:27 -06:00
|
|
|
}
|
|
|
|
|
2019-10-22 04:18:18 -05:00
|
|
|
pub fn finish(self) -> u64 {
|
2016-02-09 08:18:27 -06:00
|
|
|
self.s.finish()
|
|
|
|
}
|
|
|
|
|
2019-12-27 01:12:26 -06:00
|
|
|
pub fn hash_block(&mut self, b: &Block<'_>) {
|
|
|
|
for s in b.stmts {
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_stmt(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref e) = b.expr {
|
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
|
|
|
|
2018-07-14 17:00:27 -05:00
|
|
|
match b.rules {
|
|
|
|
BlockCheckMode::DefaultBlock => 0,
|
|
|
|
BlockCheckMode::UnsafeBlock(_) => 1,
|
|
|
|
BlockCheckMode::PushUnsafeBlock(_) => 2,
|
|
|
|
BlockCheckMode::PopUnsafeBlock(_) => 3,
|
2018-11-27 14:14:15 -06:00
|
|
|
}
|
|
|
|
.hash(&mut self.s);
|
2016-02-09 08:18:27 -06:00
|
|
|
}
|
|
|
|
|
2019-01-13 09:19:02 -06:00
|
|
|
#[allow(clippy::many_single_char_names, clippy::too_many_lines)]
|
2019-12-27 01:12:26 -06:00
|
|
|
pub fn hash_expr(&mut self, e: &Expr<'_>) {
|
2020-06-25 21:55:23 -05:00
|
|
|
let simple_const = self
|
2020-07-17 03:47:04 -05:00
|
|
|
.maybe_typeck_results
|
|
|
|
.and_then(|typeck_results| constant_simple(self.cx, typeck_results, e));
|
2019-05-07 07:48:00 -05:00
|
|
|
|
|
|
|
// const hashing may result in the same hash as some unrelated node, so add a sort of
|
|
|
|
// discriminant depending on which path we're choosing next
|
|
|
|
simple_const.is_some().hash(&mut self.s);
|
|
|
|
|
|
|
|
if let Some(e) = simple_const {
|
2016-02-09 08:18:27 -06:00
|
|
|
return e.hash(&mut self.s);
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:16:06 -05:00
|
|
|
std::mem::discriminant(&e.kind).hash(&mut self.s);
|
2019-05-07 07:48:00 -05:00
|
|
|
|
2019-09-27 10:16:06 -05:00
|
|
|
match e.kind {
|
2019-11-27 16:34:32 -06:00
|
|
|
ExprKind::AddrOf(kind, m, ref e) => {
|
|
|
|
match kind {
|
|
|
|
BorrowKind::Ref => 0,
|
|
|
|
BorrowKind::Raw => 1,
|
|
|
|
}
|
|
|
|
.hash(&mut self.s);
|
2016-02-09 08:18:27 -06:00
|
|
|
m.hash(&mut self.s);
|
|
|
|
self.hash_expr(e);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Continue(i) => {
|
2018-01-27 06:57:31 -06:00
|
|
|
if let Some(i) = i.label {
|
2018-06-28 08:46:58 -05:00
|
|
|
self.hash_name(i.ident.name);
|
2016-02-09 08:18:27 -06:00
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2019-12-23 22:16:04 -06:00
|
|
|
ExprKind::Assign(ref l, ref r, _) => {
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_expr(l);
|
|
|
|
self.hash_expr(r);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::AssignOp(ref o, ref l, ref r) => {
|
2019-10-22 04:18:18 -05:00
|
|
|
o.node
|
|
|
|
.hash_stable(&mut self.cx.tcx.get_stable_hashing_context(), &mut self.s);
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_expr(l);
|
|
|
|
self.hash_expr(r);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Block(ref b, _) => {
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_block(b);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Binary(op, ref l, ref r) => {
|
2019-10-22 04:18:18 -05:00
|
|
|
op.node
|
|
|
|
.hash_stable(&mut self.cx.tcx.get_stable_hashing_context(), &mut self.s);
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_expr(l);
|
|
|
|
self.hash_expr(r);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Break(i, ref j) => {
|
2018-01-27 06:57:31 -06:00
|
|
|
if let Some(i) = i.label {
|
2018-06-28 08:46:58 -05:00
|
|
|
self.hash_name(i.ident.name);
|
2016-02-09 08:18:27 -06:00
|
|
|
}
|
2016-11-25 12:24:55 -06:00
|
|
|
if let Some(ref j) = *j {
|
|
|
|
self.hash_expr(&*j);
|
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2019-06-19 04:59:25 -05:00
|
|
|
ExprKind::Box(ref e) | ExprKind::DropTemps(ref e) | ExprKind::Yield(ref e, _) => {
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_expr(e);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2019-12-27 01:12:26 -06:00
|
|
|
ExprKind::Call(ref fun, args) => {
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_expr(fun);
|
|
|
|
self.hash_exprs(args);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2019-07-28 03:31:05 -05:00
|
|
|
ExprKind::Cast(ref e, ref ty) | ExprKind::Type(ref e, ref ty) => {
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_expr(e);
|
2019-02-15 14:21:13 -06:00
|
|
|
self.hash_ty(ty);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Closure(cap, _, eid, _, _) => {
|
2018-07-14 17:00:27 -05:00
|
|
|
match cap {
|
2019-11-11 11:24:12 -06:00
|
|
|
CaptureBy::Value => 0,
|
|
|
|
CaptureBy::Ref => 1,
|
2018-11-27 14:14:15 -06:00
|
|
|
}
|
|
|
|
.hash(&mut self.s);
|
2020-07-17 03:47:04 -05:00
|
|
|
// closures inherit TypeckResults
|
2018-12-07 18:56:03 -06:00
|
|
|
self.hash_expr(&self.cx.tcx.hir().body(eid).value);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Field(ref e, ref f) => {
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_expr(e);
|
2018-05-31 13:15:48 -05:00
|
|
|
self.hash_name(f.name);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Index(ref a, ref i) => {
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_expr(a);
|
|
|
|
self.hash_expr(i);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2020-05-06 09:03:53 -05:00
|
|
|
ExprKind::InlineAsm(ref asm) => {
|
|
|
|
for piece in asm.template {
|
|
|
|
match piece {
|
|
|
|
InlineAsmTemplatePiece::String(s) => s.hash(&mut self.s),
|
|
|
|
InlineAsmTemplatePiece::Placeholder {
|
|
|
|
operand_idx,
|
|
|
|
modifier,
|
|
|
|
span: _,
|
|
|
|
} => {
|
|
|
|
operand_idx.hash(&mut self.s);
|
|
|
|
modifier.hash(&mut self.s);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
asm.options.hash(&mut self.s);
|
|
|
|
for op in asm.operands {
|
|
|
|
match op {
|
|
|
|
InlineAsmOperand::In { reg, expr } => {
|
|
|
|
reg.hash(&mut self.s);
|
|
|
|
self.hash_expr(expr);
|
|
|
|
},
|
|
|
|
InlineAsmOperand::Out { reg, late, expr } => {
|
|
|
|
reg.hash(&mut self.s);
|
|
|
|
late.hash(&mut self.s);
|
|
|
|
if let Some(expr) = expr {
|
|
|
|
self.hash_expr(expr);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
InlineAsmOperand::InOut { reg, late, expr } => {
|
|
|
|
reg.hash(&mut self.s);
|
|
|
|
late.hash(&mut self.s);
|
|
|
|
self.hash_expr(expr);
|
|
|
|
},
|
|
|
|
InlineAsmOperand::SplitInOut {
|
|
|
|
reg,
|
|
|
|
late,
|
|
|
|
in_expr,
|
|
|
|
out_expr,
|
|
|
|
} => {
|
|
|
|
reg.hash(&mut self.s);
|
|
|
|
late.hash(&mut self.s);
|
|
|
|
self.hash_expr(in_expr);
|
|
|
|
if let Some(out_expr) = out_expr {
|
|
|
|
self.hash_expr(out_expr);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
InlineAsmOperand::Const { expr } | InlineAsmOperand::Sym { expr } => self.hash_expr(expr),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-03-27 09:41:56 -05:00
|
|
|
ExprKind::LlvmInlineAsm(..) | ExprKind::Err => {},
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Lit(ref l) => {
|
2019-10-22 04:17:16 -05:00
|
|
|
l.node.hash(&mut self.s);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Loop(ref b, ref i, _) => {
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_block(b);
|
|
|
|
if let Some(i) = *i {
|
2018-06-28 08:46:58 -05:00
|
|
|
self.hash_name(i.ident.name);
|
2016-02-09 08:18:27 -06:00
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2019-12-27 01:12:26 -06:00
|
|
|
ExprKind::Match(ref e, arms, ref s) => {
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_expr(e);
|
|
|
|
|
|
|
|
for arm in arms {
|
|
|
|
// TODO: arm.pat?
|
|
|
|
if let Some(ref e) = arm.guard {
|
2018-09-02 02:38:25 -05:00
|
|
|
self.hash_guard(e);
|
2016-02-09 08:18:27 -06:00
|
|
|
}
|
|
|
|
self.hash_expr(&arm.body);
|
|
|
|
}
|
2016-12-02 13:54:05 -06:00
|
|
|
|
|
|
|
s.hash(&mut self.s);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2020-06-09 16:44:04 -05:00
|
|
|
ExprKind::MethodCall(ref path, ref _tys, args, ref _fn_span) => {
|
2018-06-28 08:46:58 -05:00
|
|
|
self.hash_name(path.ident.name);
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_exprs(args);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2020-10-06 16:51:31 -05:00
|
|
|
ExprKind::ConstBlock(ref l_id) => {
|
|
|
|
self.hash_body(l_id.body);
|
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Repeat(ref e, ref l_id) => {
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_expr(e);
|
2019-11-04 19:03:03 -06:00
|
|
|
self.hash_body(l_id.body);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Ret(ref e) => {
|
2016-02-09 08:18:27 -06:00
|
|
|
if let Some(ref e) = *e {
|
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Path(ref qpath) => {
|
2016-12-02 15:23:24 -06:00
|
|
|
self.hash_qpath(qpath);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2019-12-27 01:12:26 -06:00
|
|
|
ExprKind::Struct(ref path, fields, ref expr) => {
|
2016-12-02 15:23:24 -06:00
|
|
|
self.hash_qpath(path);
|
2016-02-09 08:18:27 -06:00
|
|
|
|
|
|
|
for f in fields {
|
2018-05-31 13:15:48 -05:00
|
|
|
self.hash_name(f.ident.name);
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_expr(&f.expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref e) = *expr {
|
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2019-12-27 01:12:26 -06:00
|
|
|
ExprKind::Tup(tup) => {
|
2019-02-15 14:21:13 -06:00
|
|
|
self.hash_exprs(tup);
|
|
|
|
},
|
2019-12-27 01:12:26 -06:00
|
|
|
ExprKind::Array(v) => {
|
2019-05-14 08:08:29 -05:00
|
|
|
self.hash_exprs(v);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Unary(lop, ref le) => {
|
2019-10-22 04:18:18 -05:00
|
|
|
lop.hash_stable(&mut self.cx.tcx.get_stable_hashing_context(), &mut self.s);
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_expr(le);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-02-09 08:18:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 01:12:26 -06:00
|
|
|
pub fn hash_exprs(&mut self, e: &[Expr<'_>]) {
|
2016-02-09 08:18:27 -06:00
|
|
|
for e in e {
|
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 06:57:01 -05:00
|
|
|
pub fn hash_name(&mut self, n: Symbol) {
|
2016-02-09 08:18:27 -06:00
|
|
|
n.as_str().hash(&mut self.s);
|
|
|
|
}
|
|
|
|
|
2019-12-29 22:02:10 -06:00
|
|
|
pub fn hash_qpath(&mut self, p: &QPath<'_>) {
|
2016-12-02 15:23:24 -06:00
|
|
|
match *p {
|
|
|
|
QPath::Resolved(_, ref path) => {
|
|
|
|
self.hash_path(path);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-12-02 15:23:24 -06:00
|
|
|
QPath::TypeRelative(_, ref path) => {
|
2018-06-28 08:46:58 -05:00
|
|
|
self.hash_name(path.ident.name);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2020-08-04 08:24:13 -05:00
|
|
|
QPath::LangItem(lang_item, ..) => {
|
|
|
|
lang_item.hash_stable(&mut self.cx.tcx.get_stable_hashing_context(), &mut self.s);
|
2020-08-28 09:10:16 -05:00
|
|
|
},
|
2016-12-02 15:23:24 -06:00
|
|
|
}
|
2020-07-17 03:47:04 -05:00
|
|
|
// self.maybe_typeck_results.unwrap().qpath_res(p, id).hash(&mut self.s);
|
2016-12-01 15:31:56 -06:00
|
|
|
}
|
|
|
|
|
2019-12-29 22:02:10 -06:00
|
|
|
pub fn hash_path(&mut self, p: &Path<'_>) {
|
2017-01-03 22:40:42 -06:00
|
|
|
p.is_global().hash(&mut self.s);
|
2019-12-29 22:02:10 -06:00
|
|
|
for p in p.segments {
|
2018-06-28 08:46:58 -05:00
|
|
|
self.hash_name(p.ident.name);
|
2016-02-09 08:18:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 01:12:26 -06:00
|
|
|
pub fn hash_stmt(&mut self, b: &Stmt<'_>) {
|
2019-09-27 10:16:06 -05:00
|
|
|
std::mem::discriminant(&b.kind).hash(&mut self.s);
|
2019-05-14 07:13:23 -05:00
|
|
|
|
2019-09-27 10:16:06 -05:00
|
|
|
match &b.kind {
|
2019-05-14 08:08:29 -05:00
|
|
|
StmtKind::Local(local) => {
|
2019-01-20 04:21:30 -06:00
|
|
|
if let Some(ref init) = local.init {
|
|
|
|
self.hash_expr(init);
|
2016-10-01 19:17:04 -05:00
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2019-05-08 04:34:35 -05:00
|
|
|
StmtKind::Item(..) => {},
|
2019-05-14 08:08:29 -05:00
|
|
|
StmtKind::Expr(expr) | StmtKind::Semi(expr) => {
|
2016-02-09 08:18:27 -06:00
|
|
|
self.hash_expr(expr);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-02-09 08:18:27 -06:00
|
|
|
}
|
|
|
|
}
|
2018-09-02 02:38:25 -05:00
|
|
|
|
2019-12-27 01:12:26 -06:00
|
|
|
pub fn hash_guard(&mut self, g: &Guard<'_>) {
|
2018-09-02 02:38:25 -05:00
|
|
|
match g {
|
|
|
|
Guard::If(ref expr) => {
|
|
|
|
self.hash_expr(expr);
|
2018-11-27 14:14:15 -06:00
|
|
|
},
|
2018-09-02 02:38:25 -05:00
|
|
|
}
|
|
|
|
}
|
2019-02-15 14:21:13 -06:00
|
|
|
|
|
|
|
pub fn hash_lifetime(&mut self, lifetime: &Lifetime) {
|
2019-07-27 17:04:36 -05:00
|
|
|
std::mem::discriminant(&lifetime.name).hash(&mut self.s);
|
2019-02-15 14:21:13 -06:00
|
|
|
if let LifetimeName::Param(ref name) = lifetime.name {
|
2019-07-28 03:31:05 -05:00
|
|
|
std::mem::discriminant(name).hash(&mut self.s);
|
2019-02-15 14:21:13 -06:00
|
|
|
match name {
|
|
|
|
ParamName::Plain(ref ident) => {
|
|
|
|
ident.name.hash(&mut self.s);
|
|
|
|
},
|
|
|
|
ParamName::Fresh(ref size) => {
|
|
|
|
size.hash(&mut self.s);
|
|
|
|
},
|
2019-07-27 16:59:46 -05:00
|
|
|
ParamName::Error => {},
|
2019-02-15 14:21:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 22:02:10 -06:00
|
|
|
pub fn hash_ty(&mut self, ty: &Ty<'_>) {
|
2019-09-27 10:16:06 -05:00
|
|
|
self.hash_tykind(&ty.kind);
|
2019-07-24 16:59:32 -05:00
|
|
|
}
|
|
|
|
|
2019-12-29 22:02:10 -06:00
|
|
|
pub fn hash_tykind(&mut self, ty: &TyKind<'_>) {
|
2019-07-28 03:31:05 -05:00
|
|
|
std::mem::discriminant(ty).hash(&mut self.s);
|
2019-07-24 16:27:12 -05:00
|
|
|
match ty {
|
|
|
|
TyKind::Slice(ty) => {
|
2019-02-15 14:21:13 -06:00
|
|
|
self.hash_ty(ty);
|
|
|
|
},
|
2019-07-24 16:27:12 -05:00
|
|
|
TyKind::Array(ty, anon_const) => {
|
2019-02-15 14:21:13 -06:00
|
|
|
self.hash_ty(ty);
|
2019-11-04 19:03:03 -06:00
|
|
|
self.hash_body(anon_const.body);
|
2019-02-15 14:21:13 -06:00
|
|
|
},
|
2019-07-24 16:27:12 -05:00
|
|
|
TyKind::Ptr(mut_ty) => {
|
2019-02-15 14:21:13 -06:00
|
|
|
self.hash_ty(&mut_ty.ty);
|
|
|
|
mut_ty.mutbl.hash(&mut self.s);
|
|
|
|
},
|
2019-07-24 16:27:12 -05:00
|
|
|
TyKind::Rptr(lifetime, mut_ty) => {
|
2019-02-15 14:21:13 -06:00
|
|
|
self.hash_lifetime(lifetime);
|
|
|
|
self.hash_ty(&mut_ty.ty);
|
|
|
|
mut_ty.mutbl.hash(&mut self.s);
|
|
|
|
},
|
2019-07-24 16:27:12 -05:00
|
|
|
TyKind::BareFn(bfn) => {
|
2019-02-15 14:21:13 -06:00
|
|
|
bfn.unsafety.hash(&mut self.s);
|
|
|
|
bfn.abi.hash(&mut self.s);
|
2019-12-29 22:02:10 -06:00
|
|
|
for arg in bfn.decl.inputs {
|
2019-02-15 14:21:13 -06:00
|
|
|
self.hash_ty(&arg);
|
|
|
|
}
|
|
|
|
match bfn.decl.output {
|
2020-02-17 03:36:17 -06:00
|
|
|
FnRetTy::DefaultReturn(_) => {
|
2019-02-15 14:21:13 -06:00
|
|
|
().hash(&mut self.s);
|
|
|
|
},
|
2020-02-17 03:36:17 -06:00
|
|
|
FnRetTy::Return(ref ty) => {
|
2019-02-15 14:21:13 -06:00
|
|
|
self.hash_ty(ty);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
bfn.decl.c_variadic.hash(&mut self.s);
|
|
|
|
},
|
2019-07-24 16:27:12 -05:00
|
|
|
TyKind::Tup(ty_list) => {
|
2019-12-29 22:02:10 -06:00
|
|
|
for ty in *ty_list {
|
2019-02-15 14:21:13 -06:00
|
|
|
self.hash_ty(ty);
|
|
|
|
}
|
|
|
|
},
|
2019-07-27 15:58:29 -05:00
|
|
|
TyKind::Path(qpath) => match qpath {
|
|
|
|
QPath::Resolved(ref maybe_ty, ref path) => {
|
|
|
|
if let Some(ref ty) = maybe_ty {
|
2019-02-15 14:21:13 -06:00
|
|
|
self.hash_ty(ty);
|
2019-07-27 15:58:29 -05:00
|
|
|
}
|
2019-12-29 22:02:10 -06:00
|
|
|
for segment in path.segments {
|
2019-02-15 14:21:13 -06:00
|
|
|
segment.ident.name.hash(&mut self.s);
|
2020-07-14 07:59:59 -05:00
|
|
|
self.hash_generic_args(segment.generic_args().args);
|
2019-07-27 15:58:29 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
QPath::TypeRelative(ref ty, ref segment) => {
|
|
|
|
self.hash_ty(ty);
|
|
|
|
segment.ident.name.hash(&mut self.s);
|
|
|
|
},
|
2020-08-04 08:24:13 -05:00
|
|
|
QPath::LangItem(lang_item, ..) => {
|
|
|
|
lang_item.hash(&mut self.s);
|
2020-08-28 09:10:16 -05:00
|
|
|
},
|
2019-02-15 14:21:13 -06:00
|
|
|
},
|
2020-05-10 06:15:51 -05:00
|
|
|
TyKind::OpaqueDef(_, arg_list) => {
|
2020-07-14 07:59:59 -05:00
|
|
|
self.hash_generic_args(arg_list);
|
2019-02-15 14:21:13 -06:00
|
|
|
},
|
2019-07-24 16:27:12 -05:00
|
|
|
TyKind::TraitObject(_, lifetime) => {
|
2019-02-15 14:21:13 -06:00
|
|
|
self.hash_lifetime(lifetime);
|
|
|
|
},
|
2019-07-24 16:27:12 -05:00
|
|
|
TyKind::Typeof(anon_const) => {
|
2019-11-04 19:03:03 -06:00
|
|
|
self.hash_body(anon_const.body);
|
2019-02-15 14:21:13 -06:00
|
|
|
},
|
2019-07-24 16:27:12 -05:00
|
|
|
TyKind::Err | TyKind::Infer | TyKind::Never => {},
|
2019-02-15 14:21:13 -06:00
|
|
|
}
|
|
|
|
}
|
2019-11-04 19:03:03 -06:00
|
|
|
|
|
|
|
pub fn hash_body(&mut self, body_id: BodyId) {
|
2020-07-17 03:47:04 -05:00
|
|
|
// swap out TypeckResults when hashing a body
|
|
|
|
let old_maybe_typeck_results = self.maybe_typeck_results.replace(self.cx.tcx.typeck_body(body_id));
|
2019-11-04 19:03:03 -06:00
|
|
|
self.hash_expr(&self.cx.tcx.hir().body(body_id).value);
|
2020-07-17 03:47:04 -05:00
|
|
|
self.maybe_typeck_results = old_maybe_typeck_results;
|
2019-11-04 19:03:03 -06:00
|
|
|
}
|
2020-07-14 07:59:59 -05:00
|
|
|
|
|
|
|
fn hash_generic_args(&mut self, arg_list: &[GenericArg<'_>]) {
|
|
|
|
for arg in arg_list {
|
|
|
|
match arg {
|
|
|
|
GenericArg::Lifetime(ref l) => self.hash_lifetime(l),
|
|
|
|
GenericArg::Type(ref ty) => self.hash_ty(&ty),
|
|
|
|
GenericArg::Const(ref ca) => self.hash_body(ca.value.body),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-09 08:18:27 -06:00
|
|
|
}
|