rust-clippy/clippy_lints/src/utils/hir_utils.rs

627 lines
25 KiB
Rust
Raw Normal View History

2018-05-30 08:15:50 +00:00
use crate::consts::{constant_simple, constant_context};
2016-02-06 19:13:25 +00:00
use rustc::lint::*;
use rustc::hir::*;
2018-05-13 11:16:31 +00:00
use rustc::ty::{TypeckTables};
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;
2018-05-30 08:15:50 +00:00
use crate::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>,
2018-05-13 11:16:31 +00:00
tables: &'a TypeckTables<'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 {
cx,
2018-05-13 11:16:31 +00:00
tables: cx.tables,
2016-02-24 16:38:57 +00:00
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,
2018-05-13 11:16:31 +00:00
tables: self.cx.tables,
2016-02-24 16:38:57 +00:00
ignore_fn: true,
}
2016-02-06 19:13:25 +00:00
}
/// Check whether two statements are the same.
2018-05-17 18:17:21 +00:00
pub fn eq_stmt(&mut self, left: &Stmt, right: &Stmt) -> bool {
2016-02-06 19:13:25 +00:00
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.
2018-05-17 18:17:21 +00:00
pub fn eq_block(&mut 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
}
2018-05-17 18:17:21 +00:00
pub fn eq_expr(&mut self, left: &Expr, right: &Expr) -> bool {
if self.ignore_fn && differing_macro_contexts(left.span, right.span) {
return false;
}
2018-05-13 11:16:31 +00:00
if let (Some(l), Some(r)) = (constant_simple(self.cx, self.tables, left), constant_simple(self.cx, self.tables, right)) {
2016-02-06 19:13:25 +00:00
if l == r {
return true;
}
}
match (&left.node, &right.node) {
2018-07-12 07:30:57 +00:00
(&ExprKind::AddrOf(l_mut, ref le), &ExprKind::AddrOf(r_mut, ref re)) => l_mut == r_mut && self.eq_expr(le, re),
(&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
2018-06-28 13:46:58 +00:00
both(&li.label, &ri.label, |l, r| l.ident.as_str() == r.ident.as_str())
2017-02-27 08:49:02 +00:00
},
2018-07-12 07:30:57 +00:00
(&ExprKind::Assign(ref ll, ref lr), &ExprKind::Assign(ref rl, ref rr)) => self.eq_expr(ll, rl) && self.eq_expr(lr, rr),
(&ExprKind::AssignOp(ref lo, ref ll, ref lr), &ExprKind::AssignOp(ref ro, ref rl, ref rr)) => {
2016-02-06 19:13:25 +00:00
lo.node == ro.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::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 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)) => {
2018-06-28 13:46:58 +00:00
both(&li.label, &ri.label, |l, r| l.ident.as_str() == r.ident.as_str())
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
},
2018-07-12 07:30:57 +00:00
(&ExprKind::Box(ref l), &ExprKind::Box(ref r)) => self.eq_expr(l, r),
(&ExprKind::Call(ref l_fun, ref l_args), &ExprKind::Call(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
},
2018-07-12 07:30:57 +00: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),
(&ExprKind::Field(ref l_f_exp, ref l_f_ident), &ExprKind::Field(ref 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
},
2018-07-12 07:30:57 +00:00
(&ExprKind::Index(ref la, ref li), &ExprKind::Index(ref ra, ref ri)) => self.eq_expr(la, ra) && self.eq_expr(li, ri),
(&ExprKind::If(ref lc, ref lt, ref le), &ExprKind::If(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
},
2018-07-12 07:30:57 +00: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 13:46:58 +00:00
lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.as_str() == r.ident.as_str())
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
(&ExprKind::Match(ref le, ref la, ref ls), &ExprKind::Match(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
},
2018-07-12 07:30:57 +00:00
(&ExprKind::MethodCall(ref l_path, _, ref l_args), &ExprKind::MethodCall(ref r_path, _, ref r_args)) => {
!self.ignore_fn && self.eq_path_segment(l_path, r_path) && self.eq_exprs(l_args, r_args)
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
(&ExprKind::Repeat(ref le, ref ll_id), &ExprKind::Repeat(ref re, ref rl_id)) => {
let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(ll_id.body));
let ll = celcx.expr(&self.cx.tcx.hir.body(ll_id.body).value);
let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(rl_id.body));
let rl = celcx.expr(&self.cx.tcx.hir.body(rl_id.body).value);
self.eq_expr(le, re) && 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),
(&ExprKind::Struct(ref l_path, ref lf, ref lo), &ExprKind::Struct(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
},
2018-07-12 07:30:57 +00:00
(&ExprKind::Tup(ref l_tup), &ExprKind::Tup(ref r_tup)) => self.eq_exprs(l_tup, r_tup),
(&ExprKind::Unary(l_op, ref le), &ExprKind::Unary(r_op, ref re)) => l_op == r_op && self.eq_expr(le, re),
(&ExprKind::Array(ref l), &ExprKind::Array(ref r)) => self.eq_exprs(l, r),
(&ExprKind::While(ref lc, ref lb, ref ll), &ExprKind::While(ref rc, ref rb, ref rl)) => {
2018-06-28 13:46:58 +00:00
self.eq_expr(lc, rc) && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.as_str() == r.ident.as_str())
2016-12-20 17:21:30 +00:00
},
2016-02-06 19:13:25 +00:00
_ => false,
}
}
2018-05-17 18:17:21 +00:00
fn eq_exprs(&mut 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))
}
2018-05-17 18:17:21 +00:00
fn eq_field(&mut self, left: &Field, right: &Field) -> bool {
2018-05-29 08:56:58 +00:00
left.ident.name == right.ident.name && self.eq_expr(&left.expr, &right.expr)
2016-03-07 15:30:02 +00:00
}
fn eq_generic_arg(&mut self, left: &GenericArg, right: &GenericArg) -> bool {
match (left, right) {
(GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => self.eq_lifetime(l_lt, r_lt),
(GenericArg::Type(l_ty), GenericArg::Type(r_ty)) => self.eq_ty(l_ty, r_ty),
_ => false,
}
}
2018-05-17 18:17:21 +00:00
fn eq_lifetime(&mut 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.
2018-05-17 18:17:21 +00:00
pub fn eq_pat(&mut self, left: &Pat, right: &Pat) -> bool {
2016-02-06 19:13:25 +00:00
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),
(&PatKind::TupleStruct(ref lp, ref la, ls), &PatKind::TupleStruct(ref rp, ref ra, rs)) => {
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)) => {
2018-06-28 13:46:58 +00:00
lb == rb && li.name.as_str() == ri.name.as_str() && both(lp, rp, |l, r| self.eq_pat(l, r))
2016-12-20 17:21:30 +00:00
},
(&PatKind::Path(ref l), &PatKind::Path(ref r)) => self.eq_qpath(l, r),
(&PatKind::Lit(ref l), &PatKind::Lit(ref r)) => self.eq_expr(l, r),
(&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
},
(&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),
(&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
},
(&PatKind::Wild, &PatKind::Wild) => true,
2016-02-06 19:13:25 +00:00
_ => false,
}
}
2018-05-17 18:17:21 +00:00
fn eq_qpath(&mut 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,
}
}
2018-05-17 18:17:21 +00:00
fn eq_path(&mut 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))
}
fn eq_path_parameters(&mut self, left: &GenericArgs, right: &GenericArgs) -> bool {
2017-08-25 07:30:21 +00:00
if !(left.parenthesized || right.parenthesized) {
over(&left.args, &right.args, |l, r| self.eq_generic_arg(l, r)) // FIXME(flip1995): may not work
2017-11-04 19:55:56 +00:00
&& 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
}
}
pub fn eq_path_segments(&mut self, left: &[PathSegment], right: &[PathSegment]) -> bool {
left.len() == right.len() && left.iter().zip(right).all(|(l, r)| self.eq_path_segment(l, r))
}
pub fn eq_path_segment(&mut self, left: &PathSegment, right: &PathSegment) -> bool {
// The == of idents doesn't work with different contexts,
// we have to be explicit about hygiene
2018-06-28 13:46:58 +00:00
if left.ident.as_str() != right.ident.as_str() {
return false;
}
match (&left.args, &right.args) {
(&None, &None) => true,
(&Some(ref l), &Some(ref r)) => self.eq_path_parameters(l, r),
2017-11-04 19:55:56 +00:00
_ => false,
}
}
pub fn eq_ty(&mut self, left: &Ty, right: &Ty) -> bool {
self.eq_ty_kind(&left.node, &right.node)
}
pub fn eq_ty_kind(&mut self, left: &Ty_, right: &Ty_) -> bool {
match (left, right) {
(&TySlice(ref l_vec), &TySlice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
(&TyArray(ref lt, ref ll_id), &TyArray(ref rt, ref rl_id)) => {
2018-05-17 18:17:21 +00:00
let full_table = self.tables;
let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(ll_id.body));
self.tables = self.cx.tcx.body_tables(ll_id.body);
let ll = celcx.expr(&self.cx.tcx.hir.body(ll_id.body).value);
2018-05-17 18:17:21 +00:00
let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(rl_id.body));
self.tables = self.cx.tcx.body_tables(rl_id.body);
let rl = celcx.expr(&self.cx.tcx.hir.body(rl_id.body).value);
2018-05-17 18:17:21 +00:00
let eq_ty = self.eq_ty(lt, rt);
self.tables = full_table;
eq_ty && ll == rl
2017-01-04 23:53:16 +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
},
(&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,
}
}
2018-05-17 18:17:21 +00:00
fn eq_type_binding(&mut self, left: &TypeBinding, right: &TypeBinding) -> bool {
2018-06-28 13:46:58 +00:00
left.ident.name == right.ident.name && self.eq_ty(&left.ty, &right.ty)
}
2016-02-06 19:13:25 +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)),
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
/// 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>,
2018-05-13 11:16:31 +00:00
tables: &'a TypeckTables<'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> {
2018-05-13 11:16:31 +00:00
pub fn new(cx: &'a LateContext<'a, 'tcx>, tables: &'a TypeckTables<'tcx>) -> Self {
2017-08-21 11:32:12 +00:00
Self {
cx,
2018-05-13 11:16:31 +00:00
tables,
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);
}
match b.rules {
BlockCheckMode::DefaultBlock => 0,
BlockCheckMode::UnsafeBlock(_) => 1,
BlockCheckMode::PushUnsafeBlock(_) => 2,
BlockCheckMode::PopUnsafeBlock(_) => 3,
}.hash(&mut self.s);
2016-02-09 14:18:27 +00:00
}
#[allow(many_single_char_names)]
2016-02-09 14:18:27 +00:00
pub fn hash_expr(&mut self, e: &Expr) {
2018-05-13 11:16:31 +00:00
if let Some(e) = constant_simple(self.cx, self.tables, e) {
2016-02-09 14:18:27 +00:00
return e.hash(&mut self.s);
}
match e.node {
2018-07-12 07:30:57 +00:00
ExprKind::AddrOf(m, ref e) => {
let c: fn(_, _) -> _ = ExprKind::AddrOf;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
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) => {
let c: fn(_) -> _ = ExprKind::Continue;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
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
},
2018-07-12 07:30:57 +00:00
ExprKind::Yield(ref e) => {
let c: fn(_) -> _ = ExprKind::Yield;
2017-08-30 08:54:24 +00:00
c.hash(&mut self.s);
self.hash_expr(e);
},
2018-07-12 07:30:57 +00:00
ExprKind::Assign(ref l, ref r) => {
let c: fn(_, _) -> _ = ExprKind::Assign;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
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::AssignOp(ref o, ref l, ref r) => {
let c: fn(_, _, _) -> _ = ExprKind::AssignOp;
2016-02-09 14:18:27 +00:00
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
},
2018-07-12 07:30:57 +00:00
ExprKind::Block(ref b, _) => {
let c: fn(_, _) -> _ = ExprKind::Block;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
self.hash_block(b);
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
ExprKind::Binary(op, ref l, ref r) => {
let c: fn(_, _, _) -> _ = ExprKind::Binary;
2016-02-09 14:18:27 +00:00
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
},
2018-07-12 07:30:57 +00:00
ExprKind::Break(i, ref j) => {
let c: fn(_, _) -> _ = ExprKind::Break;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
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
}
if let Some(ref j) = *j {
self.hash_expr(&*j);
}
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
ExprKind::Box(ref e) => {
let c: fn(_) -> _ = ExprKind::Box;
2016-02-09 14:18:27 +00:00
c.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::Call(ref fun, ref args) => {
let c: fn(_, _) -> _ = ExprKind::Call;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
self.hash_expr(fun);
self.hash_exprs(args);
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
ExprKind::Cast(ref e, ref _ty) => {
let c: fn(_, _) -> _ = ExprKind::Cast;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
self.hash_expr(e);
// TODO: _ty
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
ExprKind::Closure(cap, _, eid, _, _) => {
let c: fn(_, _, _, _, _) -> _ = ExprKind::Closure;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
match cap {
CaptureClause::CaptureByValue => 0,
CaptureClause::CaptureByRef => 1,
}.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
},
2018-07-12 07:30:57 +00:00
ExprKind::Field(ref e, ref f) => {
let c: fn(_, _) -> _ = ExprKind::Field;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
self.hash_expr(e);
self.hash_name(f.name);
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
ExprKind::Index(ref a, ref i) => {
let c: fn(_, _) -> _ = ExprKind::Index;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
self.hash_expr(a);
self.hash_expr(i);
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
ExprKind::InlineAsm(..) => {
let c: fn(_, _, _) -> _ = ExprKind::InlineAsm;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
ExprKind::If(ref cond, ref t, ref e) => {
let c: fn(_, _, _) -> _ = ExprKind::If;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
self.hash_expr(cond);
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
},
2018-07-12 07:30:57 +00:00
ExprKind::Lit(ref l) => {
let c: fn(_) -> _ = ExprKind::Lit;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
l.hash(&mut self.s);
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
ExprKind::Loop(ref b, ref i, _) => {
let c: fn(_, _, _) -> _ = ExprKind::Loop;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
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
},
2018-07-12 07:30:57 +00:00
ExprKind::Match(ref e, ref arms, ref s) => {
let c: fn(_, _, _) -> _ = ExprKind::Match;
2016-02-09 14:18:27 +00:00
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);
}
s.hash(&mut self.s);
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
ExprKind::MethodCall(ref path, ref _tys, ref args) => {
let c: fn(_, _, _) -> _ = ExprKind::MethodCall;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
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
},
2018-07-12 07:30:57 +00:00
ExprKind::Repeat(ref e, ref l_id) => {
let c: fn(_, _) -> _ = ExprKind::Repeat;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
self.hash_expr(e);
2018-05-17 18:17:21 +00:00
let full_table = self.tables;
self.tables = self.cx.tcx.body_tables(l_id.body);
self.hash_expr(&self.cx.tcx.hir.body(l_id.body).value);
2018-05-17 18:17:21 +00:00
self.tables = full_table;
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
ExprKind::Ret(ref e) => {
let c: fn(_) -> _ = ExprKind::Ret;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
if let Some(ref e) = *e {
self.hash_expr(e);
}
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
ExprKind::Path(ref qpath) => {
let c: fn(_) -> _ = ExprKind::Path;
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
},
2018-07-12 07:30:57 +00:00
ExprKind::Struct(ref path, ref fields, ref expr) => {
let c: fn(_, _, _) -> _ = ExprKind::Struct;
2016-02-09 14:18:27 +00:00
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.ident.name);
2016-02-09 14:18:27 +00:00
self.hash_expr(&f.expr);
}
if let Some(ref e) = *expr {
self.hash_expr(e);
}
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
ExprKind::Tup(ref tup) => {
let c: fn(_) -> _ = ExprKind::Tup;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
self.hash_exprs(tup);
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
ExprKind::Type(ref e, ref _ty) => {
let c: fn(_, _) -> _ = ExprKind::Type;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
self.hash_expr(e);
// TODO: _ty
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
ExprKind::Unary(lop, ref le) => {
let c: fn(_, _) -> _ = ExprKind::Unary;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
lop.hash(&mut self.s);
self.hash_expr(le);
2016-12-20 17:21:30 +00:00
},
2018-07-12 07:30:57 +00:00
ExprKind::Array(ref v) => {
let c: fn(_) -> _ = ExprKind::Array;
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
},
2018-07-12 07:30:57 +00:00
ExprKind::While(ref cond, ref b, l) => {
let c: fn(_, _, _) -> _ = ExprKind::While;
2016-02-09 14:18:27 +00:00
c.hash(&mut self.s);
self.hash_expr(cond);
self.hash_block(b);
if let Some(l) = l {
2018-06-28 13:46:58 +00:00
self.hash_name(l.ident.name);
2016-02-09 14:18:27 +00:00
}
2016-12-20 17:21:30 +00:00
},
2016-02-09 14:18:27 +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) {
2016-02-09 14:18:27 +00:00
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) => {
2018-06-28 13:46:58 +00:00
self.hash_name(path.ident.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-02-09 14:18:27 +00:00
pub fn hash_path(&mut self, p: &Path) {
p.is_global().hash(&mut self.s);
2016-02-09 14:18:27 +00:00
for p in &p.segments {
2018-06-28 13:46:58 +00:00
self.hash_name(p.ident.name);
2016-02-09 14:18:27 +00:00
}
}
pub fn hash_stmt(&mut self, b: &Stmt) {
match b.node {
StmtDecl(ref decl, _) => {
2016-02-09 14:18:27 +00:00
let c: fn(_, _) -> _ = StmtDecl;
c.hash(&mut self.s);
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
}
}
}