2018-11-27 20:14:15 +00:00
|
|
|
use crate::consts::{constant_context, constant_simple};
|
2021-03-25 18:29:11 +00:00
|
|
|
use crate::source::snippet_opt;
|
2020-05-06 14:03:53 +00:00
|
|
|
use rustc_ast::ast::InlineAsmTemplatePiece;
|
2021-06-03 06:41:37 +00:00
|
|
|
use rustc_data_structures::fx::FxHasher;
|
2021-02-25 10:25:22 +00:00
|
|
|
use rustc_hir::def::Res;
|
2021-04-08 15:50:13 +00:00
|
|
|
use rustc_hir::HirIdMap;
|
2020-02-21 08:39:38 +00:00
|
|
|
use rustc_hir::{
|
2022-01-13 12:18:19 +00:00
|
|
|
ArrayLen, BinOpKind, Block, BodyId, Expr, ExprField, ExprKind, FnRetTy, GenericArg, GenericArgs, Guard, HirId,
|
2021-10-13 07:44:47 +00:00
|
|
|
InlineAsmOperand, Let, Lifetime, LifetimeName, ParamName, Pat, PatField, PatKind, Path, PathSegment, QPath, Stmt,
|
2022-01-13 12:18:19 +00:00
|
|
|
StmtKind, Ty, TyKind, TypeBinding,
|
2020-02-21 08:39:38 +00:00
|
|
|
};
|
2021-03-12 14:30:50 +00:00
|
|
|
use rustc_lexer::{tokenize, TokenKind};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::LateContext;
|
2020-07-17 08:47:04 +00:00
|
|
|
use rustc_middle::ty::TypeckResults;
|
2020-05-08 11:57:01 +00:00
|
|
|
use rustc_span::Symbol;
|
2021-06-03 06:41:37 +00:00
|
|
|
use std::hash::{Hash, Hasher};
|
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.
|
2019-06-19 18:36:23 +00:00
|
|
|
pub struct SpanlessEq<'a, 'tcx> {
|
2016-02-06 19:13:25 +00:00
|
|
|
/// Context used to evaluate constant expressions.
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &'a LateContext<'tcx>,
|
2020-07-17 08:47:04 +00:00
|
|
|
maybe_typeck_results: Option<&'tcx TypeckResults<'tcx>>,
|
2020-08-28 14:10:16 +00:00
|
|
|
allow_side_effects: bool,
|
2021-02-25 10:25:22 +00:00
|
|
|
expr_fallback: Option<Box<dyn FnMut(&Expr<'_>, &Expr<'_>) -> bool + 'a>>,
|
2016-02-06 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 18:36:23 +00:00
|
|
|
impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
|
2020-06-25 20:41:36 +00:00
|
|
|
pub fn new(cx: &'a LateContext<'tcx>) -> Self {
|
2017-08-21 11:32:12 +00:00
|
|
|
Self {
|
2018-03-15 15:07:15 +00:00
|
|
|
cx,
|
2020-07-17 08:47:04 +00:00
|
|
|
maybe_typeck_results: cx.maybe_typeck_results(),
|
2020-08-28 14:10:16 +00:00
|
|
|
allow_side_effects: true,
|
2021-01-30 17:06:34 +00:00
|
|
|
expr_fallback: None,
|
2016-02-24 16:38:57 +00:00
|
|
|
}
|
2016-02-06 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 14:10:16 +00:00
|
|
|
/// Consider expressions containing potential side effects as not equal.
|
2021-12-17 12:40:22 +00:00
|
|
|
#[must_use]
|
2020-08-28 14:10:16 +00:00
|
|
|
pub fn deny_side_effects(self) -> Self {
|
2017-08-21 11:32:12 +00:00
|
|
|
Self {
|
2020-08-28 14:10:16 +00:00
|
|
|
allow_side_effects: false,
|
2020-06-26 02:55:23 +00:00
|
|
|
..self
|
2016-02-24 16:38:57 +00:00
|
|
|
}
|
2016-02-06 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 12:40:22 +00:00
|
|
|
#[must_use]
|
2021-02-25 10:25:22 +00:00
|
|
|
pub fn expr_fallback(self, expr_fallback: impl FnMut(&Expr<'_>, &Expr<'_>) -> bool + 'a) -> Self {
|
2021-01-30 17:06:34 +00:00
|
|
|
Self {
|
|
|
|
expr_fallback: Some(Box::new(expr_fallback)),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 10:25:22 +00:00
|
|
|
/// Use this method to wrap comparisons that may involve inter-expression context.
|
|
|
|
/// See `self.locals`.
|
2021-04-08 15:50:13 +00:00
|
|
|
pub fn inter_expr(&mut self) -> HirEqInterExpr<'_, 'a, 'tcx> {
|
2021-02-25 10:25:22 +00:00
|
|
|
HirEqInterExpr {
|
|
|
|
inner: self,
|
2021-04-08 15:50:13 +00:00
|
|
|
locals: HirIdMap::default(),
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 15:50:13 +00:00
|
|
|
#[allow(dead_code)]
|
2021-02-25 10:25:22 +00:00
|
|
|
pub fn eq_block(&mut self, left: &Block<'_>, right: &Block<'_>) -> bool {
|
|
|
|
self.inter_expr().eq_block(left, right)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn eq_expr(&mut self, left: &Expr<'_>, right: &Expr<'_>) -> bool {
|
|
|
|
self.inter_expr().eq_expr(left, right)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_>) -> bool {
|
|
|
|
self.inter_expr().eq_path_segment(left, right)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn eq_path_segments(&mut self, left: &[PathSegment<'_>], right: &[PathSegment<'_>]) -> bool {
|
|
|
|
self.inter_expr().eq_path_segments(left, right)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 15:50:13 +00:00
|
|
|
pub struct HirEqInterExpr<'a, 'b, 'tcx> {
|
2021-02-25 10:25:22 +00:00
|
|
|
inner: &'a mut SpanlessEq<'b, 'tcx>,
|
|
|
|
|
|
|
|
// When binding are declared, the binding ID in the left expression is mapped to the one on the
|
|
|
|
// right. For example, when comparing `{ let x = 1; x + 2 }` and `{ let y = 1; y + 2 }`,
|
|
|
|
// these blocks are considered equal since `x` is mapped to `y`.
|
2021-04-08 15:50:13 +00:00
|
|
|
locals: HirIdMap<HirId>,
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HirEqInterExpr<'_, '_, '_> {
|
2021-04-08 15:50:13 +00:00
|
|
|
pub fn eq_stmt(&mut self, left: &Stmt<'_>, right: &Stmt<'_>) -> bool {
|
2019-09-27 15:16:06 +00:00
|
|
|
match (&left.kind, &right.kind) {
|
2021-06-03 06:41:37 +00:00
|
|
|
(&StmtKind::Local(l), &StmtKind::Local(r)) => {
|
2021-04-22 09:31:13 +00:00
|
|
|
// This additional check ensures that the type of the locals are equivalent even if the init
|
|
|
|
// expression or type have some inferred parts.
|
|
|
|
if let Some(typeck) = self.inner.maybe_typeck_results {
|
2021-06-03 06:41:37 +00:00
|
|
|
let l_ty = typeck.pat_ty(l.pat);
|
|
|
|
let r_ty = typeck.pat_ty(r.pat);
|
2021-04-22 09:31:13 +00:00
|
|
|
if !rustc_middle::ty::TyS::same_type(l_ty, r_ty) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 15:50:13 +00:00
|
|
|
// eq_pat adds the HirIds to the locals map. We therefor call it last to make sure that
|
|
|
|
// these only get added if the init and type is equal.
|
|
|
|
both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
|
2019-07-24 21:59:32 +00:00
|
|
|
&& both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r))
|
2021-06-03 06:41:37 +00:00
|
|
|
&& self.eq_pat(l.pat, r.pat)
|
2019-01-20 10:49:45 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
(&StmtKind::Expr(l), &StmtKind::Expr(r)) | (&StmtKind::Semi(l), &StmtKind::Semi(r)) => self.eq_expr(l, r),
|
2016-02-06 19:13:25 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
/// Checks whether two blocks are the same.
|
2021-02-25 10:25:22 +00:00
|
|
|
fn eq_block(&mut self, left: &Block<'_>, right: &Block<'_>) -> bool {
|
2021-03-12 14:30:50 +00:00
|
|
|
match (left.stmts, left.expr, right.stmts, right.expr) {
|
|
|
|
([], None, [], None) => {
|
|
|
|
// For empty blocks, check to see if the tokens are equal. This will catch the case where a macro
|
|
|
|
// expanded to nothing, or the cfg attribute was used.
|
|
|
|
let (left, right) = match (
|
|
|
|
snippet_opt(self.inner.cx, left.span),
|
|
|
|
snippet_opt(self.inner.cx, right.span),
|
|
|
|
) {
|
|
|
|
(Some(left), Some(right)) => (left, right),
|
|
|
|
_ => return true,
|
|
|
|
};
|
|
|
|
let mut left_pos = 0;
|
|
|
|
let left = tokenize(&left)
|
|
|
|
.map(|t| {
|
|
|
|
let end = left_pos + t.len;
|
|
|
|
let s = &left[left_pos..end];
|
|
|
|
left_pos = end;
|
|
|
|
(t, s)
|
|
|
|
})
|
|
|
|
.filter(|(t, _)| {
|
|
|
|
!matches!(
|
|
|
|
t.kind,
|
|
|
|
TokenKind::LineComment { .. } | TokenKind::BlockComment { .. } | TokenKind::Whitespace
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.map(|(_, s)| s);
|
|
|
|
let mut right_pos = 0;
|
|
|
|
let right = tokenize(&right)
|
|
|
|
.map(|t| {
|
|
|
|
let end = right_pos + t.len;
|
|
|
|
let s = &right[right_pos..end];
|
|
|
|
right_pos = end;
|
|
|
|
(t, s)
|
|
|
|
})
|
|
|
|
.filter(|(t, _)| {
|
|
|
|
!matches!(
|
|
|
|
t.kind,
|
|
|
|
TokenKind::LineComment { .. } | TokenKind::BlockComment { .. } | TokenKind::Whitespace
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.map(|(_, s)| s);
|
|
|
|
left.eq(right)
|
|
|
|
},
|
|
|
|
_ => {
|
2021-06-03 06:41:37 +00:00
|
|
|
over(left.stmts, right.stmts, |l, r| self.eq_stmt(l, r))
|
2021-03-12 14:30:50 +00:00
|
|
|
&& both(&left.expr, &right.expr, |l, r| self.eq_expr(l, r))
|
|
|
|
},
|
|
|
|
}
|
2016-02-06 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 09:02:05 +00:00
|
|
|
pub fn eq_array_length(&mut self, left: ArrayLen, right: ArrayLen) -> bool {
|
2022-01-13 12:18:19 +00:00
|
|
|
match (left, right) {
|
|
|
|
(ArrayLen::Infer(..), ArrayLen::Infer(..)) => true,
|
|
|
|
(ArrayLen::Body(l_ct), ArrayLen::Body(r_ct)) => self.eq_body(l_ct.body, r_ct.body),
|
|
|
|
(_, _) => false,
|
|
|
|
}
|
2021-12-23 09:02:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 06:41:37 +00:00
|
|
|
pub fn eq_body(&mut self, left: BodyId, right: BodyId) -> bool {
|
|
|
|
let cx = self.inner.cx;
|
|
|
|
let eval_const = |body| constant_context(cx, cx.tcx.typeck_body(body)).expr(&cx.tcx.hir().body(body).value);
|
|
|
|
eval_const(left) == eval_const(right)
|
|
|
|
}
|
|
|
|
|
2018-10-11 22:18:58 +00:00
|
|
|
#[allow(clippy::similar_names)]
|
2021-04-08 15:50:13 +00:00
|
|
|
pub fn eq_expr(&mut self, left: &Expr<'_>, right: &Expr<'_>) -> bool {
|
2021-11-02 14:43:31 +00:00
|
|
|
if !self.inner.allow_side_effects && left.span.ctxt() != right.span.ctxt() {
|
2016-02-13 14:36:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-25 10:25:22 +00:00
|
|
|
if let Some(typeck_results) = self.inner.maybe_typeck_results {
|
2020-06-26 02:55:23 +00:00
|
|
|
if let (Some(l), Some(r)) = (
|
2021-02-25 10:25:22 +00:00
|
|
|
constant_simple(self.inner.cx, typeck_results, left),
|
|
|
|
constant_simple(self.inner.cx, typeck_results, right),
|
2020-06-26 02:55:23 +00:00
|
|
|
) {
|
|
|
|
if l == r {
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-06 19:13:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:30:50 +00:00
|
|
|
let is_eq = match (
|
2021-12-23 09:02:05 +00:00
|
|
|
reduce_exprkind(self.inner.cx, &left.kind),
|
|
|
|
reduce_exprkind(self.inner.cx, &right.kind),
|
2021-03-12 14:30:50 +00:00
|
|
|
) {
|
2021-06-03 06:41:37 +00:00
|
|
|
(&ExprKind::AddrOf(lb, l_mut, le), &ExprKind::AddrOf(rb, r_mut, re)) => {
|
2019-11-27 22:34:32 +00:00
|
|
|
lb == rb && l_mut == r_mut && self.eq_expr(le, re)
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
(&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
|
2021-01-15 09:56:44 +00:00
|
|
|
both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
|
2017-02-27 08:49:02 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
(&ExprKind::Assign(ll, lr, _), &ExprKind::Assign(rl, rr, _)) => {
|
2021-02-25 10:25:22 +00:00
|
|
|
self.inner.allow_side_effects && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
(&ExprKind::AssignOp(ref lo, ll, lr), &ExprKind::AssignOp(ref ro, rl, rr)) => {
|
2021-02-25 10:25:22 +00:00
|
|
|
self.inner.allow_side_effects && lo.node == ro.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
(&ExprKind::Block(l, _), &ExprKind::Block(r, _)) => self.eq_block(l, r),
|
|
|
|
(&ExprKind::Binary(l_op, ll, lr), &ExprKind::Binary(r_op, rl, 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
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
(&ExprKind::Break(li, ref le), &ExprKind::Break(ri, ref re)) => {
|
2021-01-15 09:56:44 +00:00
|
|
|
both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
|
2017-11-04 19:55:56 +00:00
|
|
|
&& both(le, re, |l, r| self.eq_expr(l, r))
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
(&ExprKind::Box(l), &ExprKind::Box(r)) => self.eq_expr(l, r),
|
2019-12-27 07:12:26 +00:00
|
|
|
(&ExprKind::Call(l_fun, l_args), &ExprKind::Call(r_fun, r_args)) => {
|
2021-02-25 10:25:22 +00:00
|
|
|
self.inner.allow_side_effects && self.eq_expr(l_fun, r_fun) && self.eq_exprs(l_args, r_args)
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
(&ExprKind::Cast(lx, lt), &ExprKind::Cast(rx, rt)) | (&ExprKind::Type(lx, lt), &ExprKind::Type(rx, rt)) => {
|
2018-11-27 20:14:15 +00:00
|
|
|
self.eq_expr(lx, rx) && self.eq_ty(lt, rt)
|
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
(&ExprKind::Field(l_f_exp, ref l_f_ident), &ExprKind::Field(r_f_exp, ref r_f_ident)) => {
|
2018-05-29 08:56:58 +00:00
|
|
|
l_f_ident.name == r_f_ident.name && self.eq_expr(l_f_exp, r_f_exp)
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
(&ExprKind::Index(la, li), &ExprKind::Index(ra, ri)) => self.eq_expr(la, ra) && self.eq_expr(li, ri),
|
|
|
|
(&ExprKind::If(lc, lt, ref le), &ExprKind::If(rc, rt, ref re)) => {
|
2021-12-23 09:02:05 +00:00
|
|
|
self.eq_expr(lc, rc) && self.eq_expr(lt, rt) && both(le, re, |l, r| self.eq_expr(l, r))
|
2021-01-01 18:38:11 +00:00
|
|
|
},
|
2021-10-13 07:44:47 +00:00
|
|
|
(&ExprKind::Let(l), &ExprKind::Let(r)) => {
|
|
|
|
self.eq_pat(l.pat, r.pat) && both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && self.eq_expr(l.init, r.init)
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
(&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node,
|
2021-06-03 06:41:37 +00:00
|
|
|
(&ExprKind::Loop(lb, ref ll, ref lls, _), &ExprKind::Loop(rb, ref rl, ref rls, _)) => {
|
2021-01-15 09:56:44 +00:00
|
|
|
lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.name == r.ident.name)
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
(&ExprKind::Match(le, la, ref ls), &ExprKind::Match(re, ra, ref rs)) => {
|
2018-11-27 20:14:15 +00:00
|
|
|
ls == rs
|
|
|
|
&& self.eq_expr(le, re)
|
|
|
|
&& over(la, ra, |l, r| {
|
2021-06-03 06:41:37 +00:00
|
|
|
self.eq_pat(l.pat, r.pat)
|
2018-11-27 20:14:15 +00:00
|
|
|
&& both(&l.guard, &r.guard, |l, r| self.eq_guard(l, r))
|
2021-06-03 06:41:37 +00:00
|
|
|
&& self.eq_expr(l.body, r.body)
|
2018-11-27 20:14:15 +00:00
|
|
|
})
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-12-01 17:17:50 +00:00
|
|
|
(&ExprKind::MethodCall(l_path, l_args, _), &ExprKind::MethodCall(r_path, r_args, _)) => {
|
2021-02-25 10:25:22 +00:00
|
|
|
self.inner.allow_side_effects && self.eq_path_segment(l_path, r_path) && self.eq_exprs(l_args, r_args)
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-12-23 09:02:05 +00:00
|
|
|
(&ExprKind::Repeat(le, ll), &ExprKind::Repeat(re, rl)) => {
|
|
|
|
self.eq_expr(le, re) && self.eq_array_length(ll, rl)
|
2017-01-04 23:53:16 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00: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),
|
2021-06-03 06:41:37 +00:00
|
|
|
(&ExprKind::Struct(l_path, lf, ref lo), &ExprKind::Struct(r_path, rf, ref ro)) => {
|
2018-11-27 20:14:15 +00:00
|
|
|
self.eq_qpath(l_path, r_path)
|
|
|
|
&& both(lo, ro, |l, r| self.eq_expr(l, r))
|
2021-03-25 18:29:11 +00:00
|
|
|
&& over(lf, rf, |l, r| self.eq_expr_field(l, r))
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2019-12-27 07:12:26 +00:00
|
|
|
(&ExprKind::Tup(l_tup), &ExprKind::Tup(r_tup)) => self.eq_exprs(l_tup, r_tup),
|
2021-06-03 06:41:37 +00:00
|
|
|
(&ExprKind::Unary(l_op, le), &ExprKind::Unary(r_op, re)) => l_op == r_op && self.eq_expr(le, re),
|
2019-12-27 07:12:26 +00:00
|
|
|
(&ExprKind::Array(l), &ExprKind::Array(r)) => self.eq_exprs(l, r),
|
2021-06-03 06:41:37 +00:00
|
|
|
(&ExprKind::DropTemps(le), &ExprKind::DropTemps(re)) => self.eq_expr(le, re),
|
2016-02-06 19:13:25 +00:00
|
|
|
_ => false,
|
2021-01-30 17:06:34 +00:00
|
|
|
};
|
2021-02-25 10:25:22 +00:00
|
|
|
is_eq || self.inner.expr_fallback.as_mut().map_or(false, |f| f(left, right))
|
2016-02-06 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn eq_exprs(&mut self, left: &[Expr<'_>], right: &[Expr<'_>]) -> bool {
|
2016-02-06 19:13:25 +00:00
|
|
|
over(left, right, |l, r| self.eq_expr(l, r))
|
|
|
|
}
|
|
|
|
|
2021-03-25 18:29:11 +00:00
|
|
|
fn eq_expr_field(&mut self, left: &ExprField<'_>, right: &ExprField<'_>) -> bool {
|
2021-06-03 06:41:37 +00:00
|
|
|
left.ident.name == right.ident.name && self.eq_expr(left.expr, right.expr)
|
2016-03-07 15:30:02 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn eq_guard(&mut self, left: &Guard<'_>, right: &Guard<'_>) -> bool {
|
2018-09-02 07:38:25 +00:00
|
|
|
match (left, right) {
|
|
|
|
(Guard::If(l), Guard::If(r)) => self.eq_expr(l, r),
|
2020-11-15 21:51:02 +00:00
|
|
|
(Guard::IfLet(lp, le), Guard::IfLet(rp, re)) => self.eq_pat(lp, rp) && self.eq_expr(le, re),
|
|
|
|
_ => false,
|
2018-09-02 07:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 04:02:10 +00:00
|
|
|
fn eq_generic_arg(&mut self, left: &GenericArg<'_>, right: &GenericArg<'_>) -> bool {
|
2018-06-24 21:42:52 +00:00
|
|
|
match (left, right) {
|
2021-06-03 06:41:37 +00:00
|
|
|
(GenericArg::Const(l), GenericArg::Const(r)) => self.eq_body(l.value.body, r.value.body),
|
2019-10-03 19:09:32 +00:00
|
|
|
(GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => Self::eq_lifetime(l_lt, r_lt),
|
2018-06-24 21:42:52 +00:00
|
|
|
(GenericArg::Type(l_ty), GenericArg::Type(r_ty)) => self.eq_ty(l_ty, r_ty),
|
2021-07-29 10:16:06 +00:00
|
|
|
(GenericArg::Infer(l_inf), GenericArg::Infer(r_inf)) => self.eq_ty(&l_inf.to_ty(), &r_inf.to_ty()),
|
2018-06-24 21:42:52 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 19:09:32 +00:00
|
|
|
fn eq_lifetime(left: &Lifetime, right: &Lifetime) -> bool {
|
2016-12-02 19:54:05 +00:00
|
|
|
left.name == right.name
|
|
|
|
}
|
|
|
|
|
2021-03-25 18:29:11 +00:00
|
|
|
fn eq_pat_field(&mut self, left: &PatField<'_>, right: &PatField<'_>) -> bool {
|
2021-03-15 21:36:07 +00:00
|
|
|
let (PatField { ident: li, pat: lp, .. }, PatField { ident: ri, pat: rp, .. }) = (&left, &right);
|
2021-01-15 09:56:44 +00:00
|
|
|
li.name == ri.name && self.eq_pat(lp, rp)
|
2020-08-04 13:24:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
/// Checks whether two patterns are the same.
|
2021-02-25 10:25:22 +00:00
|
|
|
fn eq_pat(&mut self, left: &Pat<'_>, right: &Pat<'_>) -> bool {
|
2019-09-27 15:16:06 +00:00
|
|
|
match (&left.kind, &right.kind) {
|
2021-06-03 06:41:37 +00:00
|
|
|
(&PatKind::Box(l), &PatKind::Box(r)) => self.eq_pat(l, r),
|
|
|
|
(&PatKind::Struct(ref lp, la, ..), &PatKind::Struct(ref rp, ra, ..)) => {
|
2021-03-25 18:29:11 +00:00
|
|
|
self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat_field(l, r))
|
2020-08-04 13:24:13 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
(&PatKind::TupleStruct(ref lp, la, ls), &PatKind::TupleStruct(ref rp, 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
|
|
|
},
|
2021-02-25 10:25:22 +00:00
|
|
|
(&PatKind::Binding(lb, li, _, ref lp), &PatKind::Binding(rb, ri, _, ref rp)) => {
|
|
|
|
let eq = lb == rb && both(lp, rp, |l, r| self.eq_pat(l, r));
|
|
|
|
if eq {
|
|
|
|
self.locals.insert(li, ri);
|
|
|
|
}
|
|
|
|
eq
|
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),
|
2021-06-03 06:41:37 +00:00
|
|
|
(&PatKind::Lit(l), &PatKind::Lit(r)) => self.eq_expr(l, r),
|
|
|
|
(&PatKind::Tuple(l, ls), &PatKind::Tuple(r, rs)) => ls == rs && over(l, r, |l, r| self.eq_pat(l, r)),
|
2020-01-11 10:39:43 +00: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 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
(&PatKind::Ref(le, ref lm), &PatKind::Ref(re, ref rm)) => lm == rm && self.eq_pat(le, re),
|
|
|
|
(&PatKind::Slice(ls, ref li, le), &PatKind::Slice(rs, ref ri, re)) => {
|
2018-11-27 20:14:15 +00:00
|
|
|
over(ls, rs, |l, r| self.eq_pat(l, r))
|
|
|
|
&& over(le, re, |l, r| self.eq_pat(l, r))
|
2017-11-04 19:55:56 +00:00
|
|
|
&& 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 22:18:58 +00:00
|
|
|
#[allow(clippy::similar_names)]
|
2019-12-30 04:02:10 +00:00
|
|
|
fn eq_qpath(&mut self, left: &QPath<'_>, right: &QPath<'_>) -> bool {
|
2016-12-02 19:54:05 +00:00
|
|
|
match (left, right) {
|
2021-06-03 06:41:37 +00:00
|
|
|
(&QPath::Resolved(ref lty, lpath), &QPath::Resolved(ref rty, rpath)) => {
|
2016-12-02 19:54:05 +00:00
|
|
|
both(lty, rty, |l, r| self.eq_ty(l, r)) && self.eq_path(lpath, rpath)
|
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
(&QPath::TypeRelative(lty, lseg), &QPath::TypeRelative(rty, rseg)) => {
|
2016-12-02 19:54:05 +00:00
|
|
|
self.eq_ty(lty, rty) && self.eq_path_segment(lseg, rseg)
|
|
|
|
},
|
2021-11-16 20:44:25 +00:00
|
|
|
(&QPath::LangItem(llang_item, ..), &QPath::LangItem(rlang_item, ..)) => llang_item == rlang_item,
|
2016-12-02 19:54:05 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 04:02:10 +00:00
|
|
|
fn eq_path(&mut self, left: &Path<'_>, right: &Path<'_>) -> bool {
|
2021-02-25 10:25:22 +00:00
|
|
|
match (left.res, right.res) {
|
|
|
|
(Res::Local(l), Res::Local(r)) => l == r || self.locals.get(&l) == Some(&r),
|
|
|
|
(Res::Local(_), _) | (_, Res::Local(_)) => false,
|
2021-06-03 06:41:37 +00:00
|
|
|
_ => over(left.segments, right.segments, |l, r| self.eq_path_segment(l, r)),
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
2016-12-02 19:54:05 +00:00
|
|
|
}
|
2016-10-01 20:18:17 +00:00
|
|
|
|
2019-12-30 04:02:10 +00:00
|
|
|
fn eq_path_parameters(&mut self, left: &GenericArgs<'_>, right: &GenericArgs<'_>) -> bool {
|
2017-08-25 07:30:21 +00:00
|
|
|
if !(left.parenthesized || right.parenthesized) {
|
2021-06-03 06:41:37 +00:00
|
|
|
over(left.args, right.args, |l, r| self.eq_generic_arg(l, r)) // FIXME(flip1995): may not work
|
|
|
|
&& 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))
|
2019-06-06 07:21:19 +00:00
|
|
|
&& both(&Some(&left.bindings[0].ty()), &Some(&right.bindings[0].ty()), |l, r| {
|
2018-11-27 20:14:15 +00:00
|
|
|
self.eq_ty(l, r)
|
|
|
|
})
|
2017-08-25 07:30:21 +00:00
|
|
|
} else {
|
|
|
|
false
|
2016-10-01 20:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 04:02:10 +00:00
|
|
|
pub fn eq_path_segments(&mut self, left: &[PathSegment<'_>], right: &[PathSegment<'_>]) -> bool {
|
2018-07-14 22:00:27 +00:00
|
|
|
left.len() == right.len() && left.iter().zip(right).all(|(l, r)| self.eq_path_segment(l, r))
|
|
|
|
}
|
|
|
|
|
2019-12-30 04:02:10 +00:00
|
|
|
pub fn eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_>) -> bool {
|
2016-12-02 19:54:05 +00:00
|
|
|
// The == of idents doesn't work with different contexts,
|
|
|
|
// we have to be explicit about hygiene
|
2021-01-15 09:56:44 +00:00
|
|
|
left.ident.name == right.ident.name && both(&left.args, &right.args, |l, r| self.eq_path_parameters(l, r))
|
2016-12-02 19:54:05 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 22:18:58 +00:00
|
|
|
#[allow(clippy::similar_names)]
|
2022-01-13 12:18:19 +00:00
|
|
|
pub fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {
|
2021-03-25 18:29:11 +00:00
|
|
|
match (&left.kind, &right.kind) {
|
2021-06-03 06:41:37 +00:00
|
|
|
(&TyKind::Slice(l_vec), &TyKind::Slice(r_vec)) => self.eq_ty(l_vec, r_vec),
|
2022-01-13 12:18:19 +00:00
|
|
|
(&TyKind::Array(lt, ll), &TyKind::Array(rt, rl)) => self.eq_ty(lt, rt) && self.eq_array_length(ll, rl),
|
2018-11-27 20:14:15 +00: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 08:03:06 +00:00
|
|
|
(&TyKind::Rptr(_, ref l_rmut), &TyKind::Rptr(_, ref r_rmut)) => {
|
2016-03-01 09:13:54 +00:00
|
|
|
l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(&*l_rmut.ty, &*r_rmut.ty)
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2018-07-12 08:03:06 +00:00
|
|
|
(&TyKind::Path(ref l), &TyKind::Path(ref r)) => self.eq_qpath(l, r),
|
2021-06-03 06:41:37 +00:00
|
|
|
(&TyKind::Tup(l), &TyKind::Tup(r)) => over(l, r, |l, r| self.eq_ty(l, r)),
|
2018-07-12 08:03:06 +00:00
|
|
|
(&TyKind::Infer, &TyKind::Infer) => true,
|
2016-02-06 19:13:25 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2016-12-02 19:54:05 +00:00
|
|
|
|
2019-12-30 04:02:10 +00:00
|
|
|
fn eq_type_binding(&mut self, left: &TypeBinding<'_>, right: &TypeBinding<'_>) -> bool {
|
2021-06-03 06:41:37 +00:00
|
|
|
left.ident.name == right.ident.name && self.eq_ty(left.ty(), right.ty())
|
2016-12-02 19:54:05 +00:00
|
|
|
}
|
2016-02-06 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 14:01:03 +00:00
|
|
|
/// Some simple reductions like `{ return }` => `return`
|
2021-03-12 14:30:50 +00:00
|
|
|
fn reduce_exprkind<'hir>(cx: &LateContext<'_>, kind: &'hir ExprKind<'hir>) -> &'hir ExprKind<'hir> {
|
2020-12-06 14:01:03 +00:00
|
|
|
if let ExprKind::Block(block, _) = kind {
|
|
|
|
match (block.stmts, block.expr) {
|
2021-03-12 14:30:50 +00:00
|
|
|
// From an `if let` expression without an `else` block. The arm for the implicit wild pattern is an empty
|
|
|
|
// block with an empty span.
|
|
|
|
([], None) if block.span.is_empty() => &ExprKind::Tup(&[]),
|
2020-12-06 14:01:03 +00:00
|
|
|
// `{}` => `()`
|
2021-03-12 14:30:50 +00:00
|
|
|
([], None) => match snippet_opt(cx, block.span) {
|
|
|
|
// Don't reduce if there are any tokens contained in the braces
|
|
|
|
Some(snip)
|
|
|
|
if tokenize(&snip)
|
|
|
|
.map(|t| t.kind)
|
|
|
|
.filter(|t| {
|
|
|
|
!matches!(
|
|
|
|
t,
|
|
|
|
TokenKind::LineComment { .. } | TokenKind::BlockComment { .. } | TokenKind::Whitespace
|
|
|
|
)
|
|
|
|
})
|
2021-04-22 09:31:13 +00:00
|
|
|
.ne([TokenKind::OpenBrace, TokenKind::CloseBrace].iter().copied()) =>
|
2021-03-12 14:30:50 +00:00
|
|
|
{
|
|
|
|
kind
|
|
|
|
},
|
|
|
|
_ => &ExprKind::Tup(&[]),
|
|
|
|
},
|
2020-12-06 14:01:03 +00:00
|
|
|
([], Some(expr)) => match expr.kind {
|
|
|
|
// `{ return .. }` => `return ..`
|
|
|
|
ExprKind::Ret(..) => &expr.kind,
|
|
|
|
_ => kind,
|
|
|
|
},
|
|
|
|
([stmt], None) => match stmt.kind {
|
|
|
|
StmtKind::Expr(expr) | StmtKind::Semi(expr) => match expr.kind {
|
|
|
|
// `{ return ..; }` => `return ..`
|
|
|
|
ExprKind::Ret(..) => &expr.kind,
|
|
|
|
_ => kind,
|
|
|
|
},
|
|
|
|
_ => kind,
|
|
|
|
},
|
|
|
|
_ => kind,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
kind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 07:12:26 +00: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 15:11:38 +00:00
|
|
|
match binop {
|
2020-06-23 15:05:22 +00:00
|
|
|
BinOpKind::Add | BinOpKind::Eq | BinOpKind::Ne | BinOpKind::BitAnd | BinOpKind::BitXor | BinOpKind::BitOr => {
|
|
|
|
Some((binop, rhs, lhs))
|
|
|
|
},
|
2018-07-12 07:50:09 +00: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 15:05:22 +00:00
|
|
|
BinOpKind::Mul // Not always commutative, e.g. with matrices. See issue #5698
|
|
|
|
| BinOpKind::Shl
|
2018-11-27 20:14:15 +00:00
|
|
|
| BinOpKind::Shr
|
|
|
|
| BinOpKind::Rem
|
|
|
|
| BinOpKind::Sub
|
|
|
|
| BinOpKind::Div
|
|
|
|
| BinOpKind::And
|
|
|
|
| BinOpKind::Or => None,
|
2016-03-24 15:11:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
/// Checks if the two `Option`s are both `None` or some equal values as per
|
2017-08-09 07:30:56 +00:00
|
|
|
/// `eq_fn`.
|
2020-06-09 14:36:01 +00:00
|
|
|
pub fn both<X>(l: &Option<X>, r: &Option<X>, mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
|
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
|
|
|
}
|
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
/// Checks if two slices are equal as per `eq_fn`.
|
2020-06-09 14:36:01 +00:00
|
|
|
pub fn over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> 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
|
|
|
|
2021-04-08 15:50:13 +00:00
|
|
|
/// Counts how many elements of the slices are equal as per `eq_fn`.
|
|
|
|
pub fn count_eq<X: Sized>(
|
|
|
|
left: &mut dyn Iterator<Item = X>,
|
|
|
|
right: &mut dyn Iterator<Item = X>,
|
|
|
|
mut eq_fn: impl FnMut(&X, &X) -> bool,
|
|
|
|
) -> usize {
|
|
|
|
left.zip(right).take_while(|(l, r)| eq_fn(l, r)).count()
|
|
|
|
}
|
|
|
|
|
2020-08-28 14:10:16 +00: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 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.
|
2019-06-19 18:36:23 +00:00
|
|
|
pub struct SpanlessHash<'a, 'tcx> {
|
2016-02-09 14:18:27 +00:00
|
|
|
/// Context used to evaluate constant expressions.
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &'a LateContext<'tcx>,
|
2020-07-17 08:47:04 +00:00
|
|
|
maybe_typeck_results: Option<&'tcx TypeckResults<'tcx>>,
|
2021-06-03 06:41:37 +00:00
|
|
|
s: FxHasher,
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 18:36:23 +00:00
|
|
|
impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
|
2020-06-25 20:41:36 +00:00
|
|
|
pub fn new(cx: &'a LateContext<'tcx>) -> Self {
|
2017-08-21 11:32:12 +00:00
|
|
|
Self {
|
2018-03-15 15:07:15 +00:00
|
|
|
cx,
|
2020-07-17 08:47:04 +00:00
|
|
|
maybe_typeck_results: cx.maybe_typeck_results(),
|
2021-06-03 06:41:37 +00:00
|
|
|
s: FxHasher::default(),
|
2016-02-24 16:38:57 +00:00
|
|
|
}
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 09:18:18 +00:00
|
|
|
pub fn finish(self) -> u64 {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.s.finish()
|
|
|
|
}
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
pub fn hash_block(&mut self, b: &Block<'_>) {
|
|
|
|
for s in b.stmts {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_stmt(s);
|
|
|
|
}
|
|
|
|
|
2021-06-03 06:41:37 +00:00
|
|
|
if let Some(e) = b.expr {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
|
|
|
|
2021-06-03 06:41:37 +00:00
|
|
|
std::mem::discriminant(&b.rules).hash(&mut self.s);
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 17:03:12 +00:00
|
|
|
#[allow(clippy::too_many_lines)]
|
2019-12-27 07:12:26 +00:00
|
|
|
pub fn hash_expr(&mut self, e: &Expr<'_>) {
|
2020-06-26 02:55:23 +00:00
|
|
|
let simple_const = self
|
2020-07-17 08:47:04 +00:00
|
|
|
.maybe_typeck_results
|
|
|
|
.and_then(|typeck_results| constant_simple(self.cx, typeck_results, e));
|
2019-05-07 12:48:00 +00: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
|
2021-06-03 06:41:37 +00:00
|
|
|
simple_const.hash(&mut self.s);
|
|
|
|
if simple_const.is_some() {
|
|
|
|
return;
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 15:16:06 +00:00
|
|
|
std::mem::discriminant(&e.kind).hash(&mut self.s);
|
2019-05-07 12:48:00 +00:00
|
|
|
|
2019-09-27 15:16:06 +00:00
|
|
|
match e.kind {
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::AddrOf(kind, m, e) => {
|
|
|
|
std::mem::discriminant(&kind).hash(&mut self.s);
|
2016-02-09 14:18:27 +00:00
|
|
|
m.hash(&mut self.s);
|
|
|
|
self.hash_expr(e);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Continue(i) => {
|
2018-01-27 12:57:31 +00:00
|
|
|
if let Some(i) = i.label {
|
2018-06-28 13:46:58 +00:00
|
|
|
self.hash_name(i.ident.name);
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::Assign(l, r, _) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_expr(l);
|
|
|
|
self.hash_expr(r);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::AssignOp(ref o, l, r) => {
|
|
|
|
std::mem::discriminant(&o.node).hash(&mut self.s);
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_expr(l);
|
|
|
|
self.hash_expr(r);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::Block(b, _) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_block(b);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::Binary(op, l, r) => {
|
|
|
|
std::mem::discriminant(&op.node).hash(&mut self.s);
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_expr(l);
|
|
|
|
self.hash_expr(r);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Break(i, ref j) => {
|
2018-01-27 12:57:31 +00:00
|
|
|
if let Some(i) = i.label {
|
2018-06-28 13:46:58 +00:00
|
|
|
self.hash_name(i.ident.name);
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
2021-06-03 06:41:37 +00:00
|
|
|
if let Some(j) = *j {
|
2016-11-25 18:24:55 +00:00
|
|
|
self.hash_expr(&*j);
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::Box(e) | ExprKind::DropTemps(e) | ExprKind::Yield(e, _) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_expr(e);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::Call(fun, args) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_expr(fun);
|
|
|
|
self.hash_exprs(args);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::Cast(e, ty) | ExprKind::Type(e, ty) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_expr(e);
|
2019-02-15 20:21:13 +00:00
|
|
|
self.hash_ty(ty);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Closure(cap, _, eid, _, _) => {
|
2021-06-03 06:41:37 +00:00
|
|
|
std::mem::discriminant(&cap).hash(&mut self.s);
|
2020-07-17 08:47:04 +00:00
|
|
|
// closures inherit TypeckResults
|
2018-12-08 00:56:03 +00:00
|
|
|
self.hash_expr(&self.cx.tcx.hir().body(eid).value);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::Field(e, ref f) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_expr(e);
|
2018-05-31 18:15:48 +00:00
|
|
|
self.hash_name(f.name);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::Index(a, i) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_expr(a);
|
|
|
|
self.hash_expr(i);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::InlineAsm(asm) => {
|
2020-05-06 14:03:53 +00:00
|
|
|
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);
|
2020-11-27 00:00:00 +00:00
|
|
|
for (op, _op_sp) in asm.operands {
|
2020-05-06 14:03:53 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
},
|
2021-04-06 04:50:55 +00:00
|
|
|
InlineAsmOperand::Const { anon_const } => self.hash_body(anon_const.body),
|
|
|
|
InlineAsmOperand::Sym { expr } => self.hash_expr(expr),
|
2020-05-06 14:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-10-13 07:44:47 +00:00
|
|
|
ExprKind::Let(Let { pat, init, ty, .. }) => {
|
|
|
|
self.hash_expr(init);
|
|
|
|
if let Some(ty) = ty {
|
|
|
|
self.hash_ty(ty);
|
|
|
|
}
|
2021-08-08 14:49:13 +00:00
|
|
|
self.hash_pat(pat);
|
|
|
|
},
|
2022-01-12 00:00:00 +00:00
|
|
|
ExprKind::Err => {},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Lit(ref l) => {
|
2019-10-22 09:17:16 +00:00
|
|
|
l.node.hash(&mut self.s);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::Loop(b, ref i, ..) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_block(b);
|
|
|
|
if let Some(i) = *i {
|
2018-06-28 13:46:58 +00:00
|
|
|
self.hash_name(i.ident.name);
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::If(cond, then, ref else_opt) => {
|
2021-01-01 18:38:11 +00:00
|
|
|
self.hash_expr(cond);
|
2021-06-03 06:41:37 +00:00
|
|
|
self.hash_expr(then);
|
|
|
|
if let Some(e) = *else_opt {
|
2021-01-01 18:38:11 +00:00
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::Match(e, arms, ref s) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_expr(e);
|
|
|
|
|
|
|
|
for arm in arms {
|
2021-05-06 09:51:22 +00:00
|
|
|
self.hash_pat(arm.pat);
|
2016-02-09 14:18:27 +00:00
|
|
|
if let Some(ref e) = arm.guard {
|
2018-09-02 07:38:25 +00:00
|
|
|
self.hash_guard(e);
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
2021-06-03 06:41:37 +00:00
|
|
|
self.hash_expr(arm.body);
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
2016-12-02 19:54:05 +00:00
|
|
|
|
|
|
|
s.hash(&mut self.s);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-12-01 17:17:50 +00:00
|
|
|
ExprKind::MethodCall(path, args, ref _fn_span) => {
|
2018-06-28 13:46:58 +00:00
|
|
|
self.hash_name(path.ident.name);
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_exprs(args);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2020-10-06 21:51:31 +00:00
|
|
|
ExprKind::ConstBlock(ref l_id) => {
|
|
|
|
self.hash_body(l_id.body);
|
|
|
|
},
|
2021-12-23 09:02:05 +00:00
|
|
|
ExprKind::Repeat(e, len) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_expr(e);
|
2021-12-23 09:02:05 +00:00
|
|
|
self.hash_array_length(len);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Ret(ref e) => {
|
2021-06-03 06:41:37 +00:00
|
|
|
if let Some(e) = *e {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Path(ref qpath) => {
|
2016-12-02 21:23:24 +00:00
|
|
|
self.hash_qpath(qpath);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::Struct(path, fields, ref expr) => {
|
2016-12-02 21:23:24 +00:00
|
|
|
self.hash_qpath(path);
|
2016-02-09 14:18:27 +00:00
|
|
|
|
|
|
|
for f in fields {
|
2018-05-31 18:15:48 +00:00
|
|
|
self.hash_name(f.ident.name);
|
2021-06-03 06:41:37 +00:00
|
|
|
self.hash_expr(f.expr);
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 06:41:37 +00:00
|
|
|
if let Some(e) = *expr {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2019-12-27 07:12:26 +00:00
|
|
|
ExprKind::Tup(tup) => {
|
2019-02-15 20:21:13 +00:00
|
|
|
self.hash_exprs(tup);
|
|
|
|
},
|
2019-12-27 07:12:26 +00:00
|
|
|
ExprKind::Array(v) => {
|
2019-05-14 13:08:29 +00:00
|
|
|
self.hash_exprs(v);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
ExprKind::Unary(lop, le) => {
|
|
|
|
std::mem::discriminant(&lop).hash(&mut self.s);
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_expr(le);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
pub fn hash_exprs(&mut self, e: &[Expr<'_>]) {
|
2016-02-09 14:18:27 +00:00
|
|
|
for e in e {
|
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 11:57:01 +00:00
|
|
|
pub fn hash_name(&mut self, n: Symbol) {
|
2021-06-03 06:41:37 +00:00
|
|
|
n.hash(&mut self.s);
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 04:02:10 +00:00
|
|
|
pub fn hash_qpath(&mut self, p: &QPath<'_>) {
|
2016-12-02 21:23:24 +00:00
|
|
|
match *p {
|
2021-06-03 06:41:37 +00:00
|
|
|
QPath::Resolved(_, path) => {
|
2016-12-02 21:23:24 +00:00
|
|
|
self.hash_path(path);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
QPath::TypeRelative(_, path) => {
|
2018-06-28 13:46:58 +00:00
|
|
|
self.hash_name(path.ident.name);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2020-08-04 13:24:13 +00:00
|
|
|
QPath::LangItem(lang_item, ..) => {
|
2021-06-03 06:41:37 +00:00
|
|
|
std::mem::discriminant(&lang_item).hash(&mut self.s);
|
2020-08-28 14:10:16 +00:00
|
|
|
},
|
2016-12-02 21:23:24 +00:00
|
|
|
}
|
2020-07-17 08:47:04 +00:00
|
|
|
// self.maybe_typeck_results.unwrap().qpath_res(p, id).hash(&mut self.s);
|
2016-12-01 21:31:56 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 09:51:22 +00:00
|
|
|
pub fn hash_pat(&mut self, pat: &Pat<'_>) {
|
|
|
|
std::mem::discriminant(&pat.kind).hash(&mut self.s);
|
|
|
|
match pat.kind {
|
|
|
|
PatKind::Binding(ann, _, _, pat) => {
|
2021-06-03 06:41:37 +00:00
|
|
|
std::mem::discriminant(&ann).hash(&mut self.s);
|
2021-05-06 09:51:22 +00:00
|
|
|
if let Some(pat) = pat {
|
|
|
|
self.hash_pat(pat);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
PatKind::Box(pat) => self.hash_pat(pat),
|
|
|
|
PatKind::Lit(expr) => self.hash_expr(expr),
|
|
|
|
PatKind::Or(pats) => {
|
|
|
|
for pat in pats {
|
|
|
|
self.hash_pat(pat);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
PatKind::Path(ref qpath) => self.hash_qpath(qpath),
|
|
|
|
PatKind::Range(s, e, i) => {
|
|
|
|
if let Some(s) = s {
|
|
|
|
self.hash_expr(s);
|
|
|
|
}
|
|
|
|
if let Some(e) = e {
|
|
|
|
self.hash_expr(e);
|
|
|
|
}
|
2021-06-03 06:41:37 +00:00
|
|
|
std::mem::discriminant(&i).hash(&mut self.s);
|
2021-05-06 09:51:22 +00:00
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
PatKind::Ref(pat, mu) => {
|
2021-05-06 09:51:22 +00:00
|
|
|
self.hash_pat(pat);
|
2021-06-03 06:41:37 +00:00
|
|
|
std::mem::discriminant(&mu).hash(&mut self.s);
|
2021-05-06 09:51:22 +00:00
|
|
|
},
|
|
|
|
PatKind::Slice(l, m, r) => {
|
|
|
|
for pat in l {
|
|
|
|
self.hash_pat(pat);
|
|
|
|
}
|
|
|
|
if let Some(pat) = m {
|
|
|
|
self.hash_pat(pat);
|
|
|
|
}
|
|
|
|
for pat in r {
|
|
|
|
self.hash_pat(pat);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
PatKind::Struct(ref qpath, fields, e) => {
|
|
|
|
self.hash_qpath(qpath);
|
|
|
|
for f in fields {
|
|
|
|
self.hash_name(f.ident.name);
|
|
|
|
self.hash_pat(f.pat);
|
|
|
|
}
|
2021-06-03 06:41:37 +00:00
|
|
|
e.hash(&mut self.s);
|
2021-05-06 09:51:22 +00:00
|
|
|
},
|
|
|
|
PatKind::Tuple(pats, e) => {
|
|
|
|
for pat in pats {
|
|
|
|
self.hash_pat(pat);
|
|
|
|
}
|
|
|
|
e.hash(&mut self.s);
|
|
|
|
},
|
|
|
|
PatKind::TupleStruct(ref qpath, pats, e) => {
|
|
|
|
self.hash_qpath(qpath);
|
|
|
|
for pat in pats {
|
|
|
|
self.hash_pat(pat);
|
|
|
|
}
|
|
|
|
e.hash(&mut self.s);
|
|
|
|
},
|
|
|
|
PatKind::Wild => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 10:25:22 +00:00
|
|
|
pub fn hash_path(&mut self, path: &Path<'_>) {
|
|
|
|
match path.res {
|
|
|
|
// constant hash since equality is dependant on inter-expression context
|
2022-01-13 12:18:19 +00:00
|
|
|
// e.g. The expressions `if let Some(x) = foo() {}` and `if let Some(y) = foo() {}` are considered equal
|
|
|
|
// even though the binding names are different and they have different `HirId`s.
|
2021-02-25 10:25:22 +00:00
|
|
|
Res::Local(_) => 1_usize.hash(&mut self.s),
|
|
|
|
_ => {
|
|
|
|
for seg in path.segments {
|
|
|
|
self.hash_name(seg.ident.name);
|
2021-06-03 06:41:37 +00:00
|
|
|
self.hash_generic_args(seg.args().args);
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
pub fn hash_stmt(&mut self, b: &Stmt<'_>) {
|
2019-09-27 15:16:06 +00:00
|
|
|
std::mem::discriminant(&b.kind).hash(&mut self.s);
|
2019-05-14 12:13:23 +00:00
|
|
|
|
2019-09-27 15:16:06 +00:00
|
|
|
match &b.kind {
|
2019-05-14 13:08:29 +00:00
|
|
|
StmtKind::Local(local) => {
|
2021-05-06 09:51:22 +00:00
|
|
|
self.hash_pat(local.pat);
|
2021-06-03 06:41:37 +00:00
|
|
|
if let Some(init) = local.init {
|
2019-01-20 10:21:30 +00:00
|
|
|
self.hash_expr(init);
|
2016-10-02 00:17:04 +00:00
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2019-05-08 09:34:35 +00:00
|
|
|
StmtKind::Item(..) => {},
|
2019-05-14 13:08:29 +00:00
|
|
|
StmtKind::Expr(expr) | StmtKind::Semi(expr) => {
|
2016-02-09 14:18:27 +00:00
|
|
|
self.hash_expr(expr);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-02 07:38:25 +00:00
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
pub fn hash_guard(&mut self, g: &Guard<'_>) {
|
2018-09-02 07:38:25 +00:00
|
|
|
match g {
|
2021-06-03 06:41:37 +00:00
|
|
|
Guard::If(expr) | Guard::IfLet(_, expr) => {
|
2018-09-02 07:38:25 +00:00
|
|
|
self.hash_expr(expr);
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-09-02 07:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-15 20:21:13 +00:00
|
|
|
|
2021-05-06 09:51:22 +00:00
|
|
|
pub fn hash_lifetime(&mut self, lifetime: Lifetime) {
|
2019-07-27 22:04:36 +00:00
|
|
|
std::mem::discriminant(&lifetime.name).hash(&mut self.s);
|
2019-02-15 20:21:13 +00:00
|
|
|
if let LifetimeName::Param(ref name) = lifetime.name {
|
2019-07-28 08:31:05 +00:00
|
|
|
std::mem::discriminant(name).hash(&mut self.s);
|
2019-02-15 20:21:13 +00: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 21:59:46 +00:00
|
|
|
ParamName::Error => {},
|
2019-02-15 20:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 04:02:10 +00:00
|
|
|
pub fn hash_ty(&mut self, ty: &Ty<'_>) {
|
2021-05-06 09:51:22 +00:00
|
|
|
std::mem::discriminant(&ty.kind).hash(&mut self.s);
|
2021-04-24 21:41:57 +00:00
|
|
|
self.hash_tykind(&ty.kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hash_tykind(&mut self, ty: &TyKind<'_>) {
|
|
|
|
match ty {
|
2019-07-24 21:27:12 +00:00
|
|
|
TyKind::Slice(ty) => {
|
2019-02-15 20:21:13 +00:00
|
|
|
self.hash_ty(ty);
|
|
|
|
},
|
2021-12-23 09:02:05 +00:00
|
|
|
&TyKind::Array(ty, len) => {
|
2019-02-15 20:21:13 +00:00
|
|
|
self.hash_ty(ty);
|
2021-12-23 09:02:05 +00:00
|
|
|
self.hash_array_length(len);
|
2019-02-15 20:21:13 +00:00
|
|
|
},
|
2021-05-06 09:51:22 +00:00
|
|
|
TyKind::Ptr(ref mut_ty) => {
|
2021-06-03 06:41:37 +00:00
|
|
|
self.hash_ty(mut_ty.ty);
|
2019-02-15 20:21:13 +00:00
|
|
|
mut_ty.mutbl.hash(&mut self.s);
|
|
|
|
},
|
2021-05-06 09:51:22 +00:00
|
|
|
TyKind::Rptr(lifetime, ref mut_ty) => {
|
2021-05-06 15:33:44 +00:00
|
|
|
self.hash_lifetime(*lifetime);
|
2021-06-03 06:41:37 +00:00
|
|
|
self.hash_ty(mut_ty.ty);
|
2019-02-15 20:21:13 +00:00
|
|
|
mut_ty.mutbl.hash(&mut self.s);
|
|
|
|
},
|
2019-07-24 21:27:12 +00:00
|
|
|
TyKind::BareFn(bfn) => {
|
2019-02-15 20:21:13 +00:00
|
|
|
bfn.unsafety.hash(&mut self.s);
|
|
|
|
bfn.abi.hash(&mut self.s);
|
2019-12-30 04:02:10 +00:00
|
|
|
for arg in bfn.decl.inputs {
|
2021-06-03 06:41:37 +00:00
|
|
|
self.hash_ty(arg);
|
2019-02-15 20:21:13 +00:00
|
|
|
}
|
2021-06-03 06:41:37 +00:00
|
|
|
std::mem::discriminant(&bfn.decl.output).hash(&mut self.s);
|
2019-02-15 20:21:13 +00:00
|
|
|
match bfn.decl.output {
|
2021-06-03 06:41:37 +00:00
|
|
|
FnRetTy::DefaultReturn(_) => {},
|
|
|
|
FnRetTy::Return(ty) => {
|
2019-02-15 20:21:13 +00:00
|
|
|
self.hash_ty(ty);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
bfn.decl.c_variadic.hash(&mut self.s);
|
|
|
|
},
|
2019-07-24 21:27:12 +00:00
|
|
|
TyKind::Tup(ty_list) => {
|
2021-05-06 15:33:44 +00:00
|
|
|
for ty in *ty_list {
|
2019-02-15 20:21:13 +00:00
|
|
|
self.hash_ty(ty);
|
|
|
|
}
|
|
|
|
},
|
2021-06-03 06:41:37 +00:00
|
|
|
TyKind::Path(ref qpath) => self.hash_qpath(qpath),
|
2020-05-10 11:15:51 +00:00
|
|
|
TyKind::OpaqueDef(_, arg_list) => {
|
2020-07-14 12:59:59 +00:00
|
|
|
self.hash_generic_args(arg_list);
|
2019-02-15 20:21:13 +00:00
|
|
|
},
|
2021-03-13 12:44:29 +00:00
|
|
|
TyKind::TraitObject(_, lifetime, _) => {
|
2021-05-06 15:33:44 +00:00
|
|
|
self.hash_lifetime(*lifetime);
|
2019-02-15 20:21:13 +00:00
|
|
|
},
|
2019-07-24 21:27:12 +00:00
|
|
|
TyKind::Typeof(anon_const) => {
|
2019-11-05 01:03:03 +00:00
|
|
|
self.hash_body(anon_const.body);
|
2019-02-15 20:21:13 +00:00
|
|
|
},
|
2019-07-24 21:27:12 +00:00
|
|
|
TyKind::Err | TyKind::Infer | TyKind::Never => {},
|
2019-02-15 20:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-05 01:03:03 +00:00
|
|
|
|
2021-12-23 09:02:05 +00:00
|
|
|
pub fn hash_array_length(&mut self, length: ArrayLen) {
|
|
|
|
match length {
|
2022-01-13 12:18:19 +00:00
|
|
|
ArrayLen::Infer(..) => {},
|
2021-12-23 09:02:05 +00:00
|
|
|
ArrayLen::Body(anon_const) => self.hash_body(anon_const.body),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 01:03:03 +00:00
|
|
|
pub fn hash_body(&mut self, body_id: BodyId) {
|
2020-07-17 08:47:04 +00: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-05 01:03:03 +00:00
|
|
|
self.hash_expr(&self.cx.tcx.hir().body(body_id).value);
|
2020-07-17 08:47:04 +00:00
|
|
|
self.maybe_typeck_results = old_maybe_typeck_results;
|
2019-11-05 01:03:03 +00:00
|
|
|
}
|
2020-07-14 12:59:59 +00:00
|
|
|
|
|
|
|
fn hash_generic_args(&mut self, arg_list: &[GenericArg<'_>]) {
|
|
|
|
for arg in arg_list {
|
2021-05-06 09:51:22 +00:00
|
|
|
match *arg {
|
|
|
|
GenericArg::Lifetime(l) => self.hash_lifetime(l),
|
|
|
|
GenericArg::Type(ref ty) => self.hash_ty(ty),
|
2020-07-14 12:59:59 +00:00
|
|
|
GenericArg::Const(ref ca) => self.hash_body(ca.value.body),
|
2021-04-26 18:19:23 +00:00
|
|
|
GenericArg::Infer(ref inf) => self.hash_ty(&inf.to_ty()),
|
2020-07-14 12:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-09 14:18:27 +00:00
|
|
|
}
|