]> git.lizzy.rs Git - rust.git/blob - clippy_utils/src/ast_utils.rs
Auto merge of #9178 - alex-semenyuk:match_like_matches_macro_fix, r=Jarcho
[rust.git] / clippy_utils / src / ast_utils.rs
1 //! Utilities for manipulating and extracting information from `rustc_ast::ast`.
2 //!
3 //! - The `eq_foobar` functions test for semantic equality but ignores `NodeId`s and `Span`s.
4
5 #![allow(clippy::similar_names, clippy::wildcard_imports, clippy::enum_glob_use)]
6
7 use crate::{both, over};
8 use rustc_ast::ptr::P;
9 use rustc_ast::{self as ast, *};
10 use rustc_span::symbol::Ident;
11 use std::mem;
12
13 pub mod ident_iter;
14 pub use ident_iter::IdentIter;
15
16 pub fn is_useless_with_eq_exprs(kind: BinOpKind) -> bool {
17     use BinOpKind::*;
18     matches!(
19         kind,
20         Sub | Div | Eq | Lt | Le | Gt | Ge | Ne | And | Or | BitXor | BitAnd | BitOr
21     )
22 }
23
24 /// Checks if each element in the first slice is contained within the latter as per `eq_fn`.
25 pub fn unordered_over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
26     left.len() == right.len() && left.iter().all(|l| right.iter().any(|r| eq_fn(l, r)))
27 }
28
29 pub fn eq_id(l: Ident, r: Ident) -> bool {
30     l.name == r.name
31 }
32
33 pub fn eq_pat(l: &Pat, r: &Pat) -> bool {
34     use PatKind::*;
35     match (&l.kind, &r.kind) {
36         (Paren(l), _) => eq_pat(l, r),
37         (_, Paren(r)) => eq_pat(l, r),
38         (Wild, Wild) | (Rest, Rest) => true,
39         (Lit(l), Lit(r)) => eq_expr(l, r),
40         (Ident(b1, i1, s1), Ident(b2, i2, s2)) => b1 == b2 && eq_id(*i1, *i2) && both(s1, s2, |l, r| eq_pat(l, r)),
41         (Range(lf, lt, le), Range(rf, rt, re)) => {
42             eq_expr_opt(lf, rf) && eq_expr_opt(lt, rt) && eq_range_end(&le.node, &re.node)
43         },
44         (Box(l), Box(r))
45         | (Ref(l, Mutability::Not), Ref(r, Mutability::Not))
46         | (Ref(l, Mutability::Mut), Ref(r, Mutability::Mut)) => eq_pat(l, r),
47         (Tuple(l), Tuple(r)) | (Slice(l), Slice(r)) => over(l, r, |l, r| eq_pat(l, r)),
48         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, eq_qself) && eq_path(lp, rp),
49         (TupleStruct(lqself, lp, lfs), TupleStruct(rqself, rp, rfs)) => {
50             eq_maybe_qself(lqself, rqself) && eq_path(lp, rp) && over(lfs, rfs, |l, r| eq_pat(l, r))
51         },
52         (Struct(lqself, lp, lfs, lr), Struct(rqself, rp, rfs, rr)) => {
53             lr == rr && eq_maybe_qself(lqself, rqself) && eq_path(lp, rp) && unordered_over(lfs, rfs, eq_field_pat)
54         },
55         (Or(ls), Or(rs)) => unordered_over(ls, rs, |l, r| eq_pat(l, r)),
56         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
57         _ => false,
58     }
59 }
60
61 pub fn eq_range_end(l: &RangeEnd, r: &RangeEnd) -> bool {
62     match (l, r) {
63         (RangeEnd::Excluded, RangeEnd::Excluded) => true,
64         (RangeEnd::Included(l), RangeEnd::Included(r)) => {
65             matches!(l, RangeSyntax::DotDotEq) == matches!(r, RangeSyntax::DotDotEq)
66         },
67         _ => false,
68     }
69 }
70
71 pub fn eq_field_pat(l: &PatField, r: &PatField) -> bool {
72     l.is_placeholder == r.is_placeholder
73         && eq_id(l.ident, r.ident)
74         && eq_pat(&l.pat, &r.pat)
75         && over(&l.attrs, &r.attrs, eq_attr)
76 }
77
78 pub fn eq_qself(l: &QSelf, r: &QSelf) -> bool {
79     l.position == r.position && eq_ty(&l.ty, &r.ty)
80 }
81
82 pub fn eq_maybe_qself(l: &Option<QSelf>, r: &Option<QSelf>) -> bool {
83     match (l, r) {
84         (Some(l), Some(r)) => eq_qself(l, r),
85         (None, None) => true,
86         _ => false,
87     }
88 }
89
90 pub fn eq_path(l: &Path, r: &Path) -> bool {
91     over(&l.segments, &r.segments, eq_path_seg)
92 }
93
94 pub fn eq_path_seg(l: &PathSegment, r: &PathSegment) -> bool {
95     eq_id(l.ident, r.ident) && both(&l.args, &r.args, |l, r| eq_generic_args(l, r))
96 }
97
98 pub fn eq_generic_args(l: &GenericArgs, r: &GenericArgs) -> bool {
99     match (l, r) {
100         (GenericArgs::AngleBracketed(l), GenericArgs::AngleBracketed(r)) => over(&l.args, &r.args, eq_angle_arg),
101         (GenericArgs::Parenthesized(l), GenericArgs::Parenthesized(r)) => {
102             over(&l.inputs, &r.inputs, |l, r| eq_ty(l, r)) && eq_fn_ret_ty(&l.output, &r.output)
103         },
104         _ => false,
105     }
106 }
107
108 pub fn eq_angle_arg(l: &AngleBracketedArg, r: &AngleBracketedArg) -> bool {
109     match (l, r) {
110         (AngleBracketedArg::Arg(l), AngleBracketedArg::Arg(r)) => eq_generic_arg(l, r),
111         (AngleBracketedArg::Constraint(l), AngleBracketedArg::Constraint(r)) => eq_assoc_constraint(l, r),
112         _ => false,
113     }
114 }
115
116 pub fn eq_generic_arg(l: &GenericArg, r: &GenericArg) -> bool {
117     match (l, r) {
118         (GenericArg::Lifetime(l), GenericArg::Lifetime(r)) => eq_id(l.ident, r.ident),
119         (GenericArg::Type(l), GenericArg::Type(r)) => eq_ty(l, r),
120         (GenericArg::Const(l), GenericArg::Const(r)) => eq_expr(&l.value, &r.value),
121         _ => false,
122     }
123 }
124
125 pub fn eq_expr_opt(l: &Option<P<Expr>>, r: &Option<P<Expr>>) -> bool {
126     both(l, r, |l, r| eq_expr(l, r))
127 }
128
129 pub fn eq_struct_rest(l: &StructRest, r: &StructRest) -> bool {
130     match (l, r) {
131         (StructRest::Base(lb), StructRest::Base(rb)) => eq_expr(lb, rb),
132         (StructRest::Rest(_), StructRest::Rest(_)) | (StructRest::None, StructRest::None) => true,
133         _ => false,
134     }
135 }
136
137 pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
138     use ExprKind::*;
139     if !over(&l.attrs, &r.attrs, eq_attr) {
140         return false;
141     }
142     match (&l.kind, &r.kind) {
143         (Paren(l), _) => eq_expr(l, r),
144         (_, Paren(r)) => eq_expr(l, r),
145         (Err, Err) => true,
146         (Box(l), Box(r)) | (Try(l), Try(r)) | (Await(l), Await(r)) => eq_expr(l, r),
147         (Array(l), Array(r)) | (Tup(l), Tup(r)) => over(l, r, |l, r| eq_expr(l, r)),
148         (Repeat(le, ls), Repeat(re, rs)) => eq_expr(le, re) && eq_expr(&ls.value, &rs.value),
149         (Call(lc, la), Call(rc, ra)) => eq_expr(lc, rc) && over(la, ra, |l, r| eq_expr(l, r)),
150         (MethodCall(lc, la, _), MethodCall(rc, ra, _)) => eq_path_seg(lc, rc) && over(la, ra, |l, r| eq_expr(l, r)),
151         (Binary(lo, ll, lr), Binary(ro, rl, rr)) => lo.node == ro.node && eq_expr(ll, rl) && eq_expr(lr, rr),
152         (Unary(lo, l), Unary(ro, r)) => mem::discriminant(lo) == mem::discriminant(ro) && eq_expr(l, r),
153         (Lit(l), Lit(r)) => l.kind == r.kind,
154         (Cast(l, lt), Cast(r, rt)) | (Type(l, lt), Type(r, rt)) => eq_expr(l, r) && eq_ty(lt, rt),
155         (Let(lp, le, _), Let(rp, re, _)) => eq_pat(lp, rp) && eq_expr(le, re),
156         (If(lc, lt, le), If(rc, rt, re)) => eq_expr(lc, rc) && eq_block(lt, rt) && eq_expr_opt(le, re),
157         (While(lc, lt, ll), While(rc, rt, rl)) => eq_label(ll, rl) && eq_expr(lc, rc) && eq_block(lt, rt),
158         (ForLoop(lp, li, lt, ll), ForLoop(rp, ri, rt, rl)) => {
159             eq_label(ll, rl) && eq_pat(lp, rp) && eq_expr(li, ri) && eq_block(lt, rt)
160         },
161         (Loop(lt, ll), Loop(rt, rl)) => eq_label(ll, rl) && eq_block(lt, rt),
162         (Block(lb, ll), Block(rb, rl)) => eq_label(ll, rl) && eq_block(lb, rb),
163         (TryBlock(l), TryBlock(r)) => eq_block(l, r),
164         (Yield(l), Yield(r)) | (Ret(l), Ret(r)) => eq_expr_opt(l, r),
165         (Break(ll, le), Break(rl, re)) => eq_label(ll, rl) && eq_expr_opt(le, re),
166         (Continue(ll), Continue(rl)) => eq_label(ll, rl),
167         (Assign(l1, l2, _), Assign(r1, r2, _)) | (Index(l1, l2), Index(r1, r2)) => eq_expr(l1, r1) && eq_expr(l2, r2),
168         (AssignOp(lo, lp, lv), AssignOp(ro, rp, rv)) => lo.node == ro.node && eq_expr(lp, rp) && eq_expr(lv, rv),
169         (Field(lp, lf), Field(rp, rf)) => eq_id(*lf, *rf) && eq_expr(lp, rp),
170         (Match(ls, la), Match(rs, ra)) => eq_expr(ls, rs) && over(la, ra, eq_arm),
171         (Closure(lb, lc, la, lm, lf, le, _), Closure(rb, rc, ra, rm, rf, re, _)) => {
172             eq_closure_binder(lb, rb)
173                 && lc == rc
174                 && la.is_async() == ra.is_async()
175                 && lm == rm
176                 && eq_fn_decl(lf, rf)
177                 && eq_expr(le, re)
178         },
179         (Async(lc, _, lb), Async(rc, _, rb)) => lc == rc && eq_block(lb, rb),
180         (Range(lf, lt, ll), Range(rf, rt, rl)) => ll == rl && eq_expr_opt(lf, rf) && eq_expr_opt(lt, rt),
181         (AddrOf(lbk, lm, le), AddrOf(rbk, rm, re)) => lbk == rbk && lm == rm && eq_expr(le, re),
182         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, eq_qself) && eq_path(lp, rp),
183         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
184         (Struct(lse), Struct(rse)) => {
185             eq_maybe_qself(&lse.qself, &rse.qself)
186                 && eq_path(&lse.path, &rse.path)
187                 && eq_struct_rest(&lse.rest, &rse.rest)
188                 && unordered_over(&lse.fields, &rse.fields, eq_field)
189         },
190         _ => false,
191     }
192 }
193
194 pub fn eq_field(l: &ExprField, r: &ExprField) -> bool {
195     l.is_placeholder == r.is_placeholder
196         && eq_id(l.ident, r.ident)
197         && eq_expr(&l.expr, &r.expr)
198         && over(&l.attrs, &r.attrs, eq_attr)
199 }
200
201 pub fn eq_arm(l: &Arm, r: &Arm) -> bool {
202     l.is_placeholder == r.is_placeholder
203         && eq_pat(&l.pat, &r.pat)
204         && eq_expr(&l.body, &r.body)
205         && eq_expr_opt(&l.guard, &r.guard)
206         && over(&l.attrs, &r.attrs, eq_attr)
207 }
208
209 pub fn eq_label(l: &Option<Label>, r: &Option<Label>) -> bool {
210     both(l, r, |l, r| eq_id(l.ident, r.ident))
211 }
212
213 pub fn eq_block(l: &Block, r: &Block) -> bool {
214     l.rules == r.rules && over(&l.stmts, &r.stmts, eq_stmt)
215 }
216
217 pub fn eq_stmt(l: &Stmt, r: &Stmt) -> bool {
218     use StmtKind::*;
219     match (&l.kind, &r.kind) {
220         (Local(l), Local(r)) => {
221             eq_pat(&l.pat, &r.pat)
222                 && both(&l.ty, &r.ty, |l, r| eq_ty(l, r))
223                 && eq_local_kind(&l.kind, &r.kind)
224                 && over(&l.attrs, &r.attrs, eq_attr)
225         },
226         (Item(l), Item(r)) => eq_item(l, r, eq_item_kind),
227         (Expr(l), Expr(r)) | (Semi(l), Semi(r)) => eq_expr(l, r),
228         (Empty, Empty) => true,
229         (MacCall(l), MacCall(r)) => {
230             l.style == r.style && eq_mac_call(&l.mac, &r.mac) && over(&l.attrs, &r.attrs, eq_attr)
231         },
232         _ => false,
233     }
234 }
235
236 pub fn eq_local_kind(l: &LocalKind, r: &LocalKind) -> bool {
237     use LocalKind::*;
238     match (l, r) {
239         (Decl, Decl) => true,
240         (Init(l), Init(r)) => eq_expr(l, r),
241         (InitElse(li, le), InitElse(ri, re)) => eq_expr(li, ri) && eq_block(le, re),
242         _ => false,
243     }
244 }
245
246 pub fn eq_item<K>(l: &Item<K>, r: &Item<K>, mut eq_kind: impl FnMut(&K, &K) -> bool) -> bool {
247     eq_id(l.ident, r.ident) && over(&l.attrs, &r.attrs, eq_attr) && eq_vis(&l.vis, &r.vis) && eq_kind(&l.kind, &r.kind)
248 }
249
250 #[expect(clippy::too_many_lines)] // Just a big match statement
251 pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
252     use ItemKind::*;
253     match (l, r) {
254         (ExternCrate(l), ExternCrate(r)) => l == r,
255         (Use(l), Use(r)) => eq_use_tree(l, r),
256         (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
257         (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
258         (
259             Fn(box ast::Fn {
260                 defaultness: ld,
261                 sig: lf,
262                 generics: lg,
263                 body: lb,
264             }),
265             Fn(box ast::Fn {
266                 defaultness: rd,
267                 sig: rf,
268                 generics: rg,
269                 body: rb,
270             }),
271         ) => {
272             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
273         },
274         (Mod(lu, lmk), Mod(ru, rmk)) => {
275             lu == ru
276                 && match (lmk, rmk) {
277                     (ModKind::Loaded(litems, linline, _), ModKind::Loaded(ritems, rinline, _)) => {
278                         linline == rinline && over(litems, ritems, |l, r| eq_item(l, r, eq_item_kind))
279                     },
280                     (ModKind::Unloaded, ModKind::Unloaded) => true,
281                     _ => false,
282                 }
283         },
284         (ForeignMod(l), ForeignMod(r)) => {
285             both(&l.abi, &r.abi, eq_str_lit) && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
286         },
287         (
288             TyAlias(box ast::TyAlias {
289                 defaultness: ld,
290                 generics: lg,
291                 bounds: lb,
292                 ty: lt,
293                 ..
294             }),
295             TyAlias(box ast::TyAlias {
296                 defaultness: rd,
297                 generics: rg,
298                 bounds: rb,
299                 ty: rt,
300                 ..
301             }),
302         ) => {
303             eq_defaultness(*ld, *rd)
304                 && eq_generics(lg, rg)
305                 && over(lb, rb, eq_generic_bound)
306                 && both(lt, rt, |l, r| eq_ty(l, r))
307         },
308         (Enum(le, lg), Enum(re, rg)) => over(&le.variants, &re.variants, eq_variant) && eq_generics(lg, rg),
309         (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => {
310             eq_variant_data(lv, rv) && eq_generics(lg, rg)
311         },
312         (
313             Trait(box ast::Trait {
314                 is_auto: la,
315                 unsafety: lu,
316                 generics: lg,
317                 bounds: lb,
318                 items: li,
319             }),
320             Trait(box ast::Trait {
321                 is_auto: ra,
322                 unsafety: ru,
323                 generics: rg,
324                 bounds: rb,
325                 items: ri,
326             }),
327         ) => {
328             la == ra
329                 && matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
330                 && eq_generics(lg, rg)
331                 && over(lb, rb, eq_generic_bound)
332                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
333         },
334         (TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, eq_generic_bound),
335         (
336             Impl(box ast::Impl {
337                 unsafety: lu,
338                 polarity: lp,
339                 defaultness: ld,
340                 constness: lc,
341                 generics: lg,
342                 of_trait: lot,
343                 self_ty: lst,
344                 items: li,
345             }),
346             Impl(box ast::Impl {
347                 unsafety: ru,
348                 polarity: rp,
349                 defaultness: rd,
350                 constness: rc,
351                 generics: rg,
352                 of_trait: rot,
353                 self_ty: rst,
354                 items: ri,
355             }),
356         ) => {
357             matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
358                 && matches!(lp, ImplPolarity::Positive) == matches!(rp, ImplPolarity::Positive)
359                 && eq_defaultness(*ld, *rd)
360                 && matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
361                 && eq_generics(lg, rg)
362                 && both(lot, rot, |l, r| eq_path(&l.path, &r.path))
363                 && eq_ty(lst, rst)
364                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
365         },
366         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
367         (MacroDef(l), MacroDef(r)) => l.macro_rules == r.macro_rules && eq_mac_args(&l.body, &r.body),
368         _ => false,
369     }
370 }
371
372 pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
373     use ForeignItemKind::*;
374     match (l, r) {
375         (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
376         (
377             Fn(box ast::Fn {
378                 defaultness: ld,
379                 sig: lf,
380                 generics: lg,
381                 body: lb,
382             }),
383             Fn(box ast::Fn {
384                 defaultness: rd,
385                 sig: rf,
386                 generics: rg,
387                 body: rb,
388             }),
389         ) => {
390             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
391         },
392         (
393             TyAlias(box ast::TyAlias {
394                 defaultness: ld,
395                 generics: lg,
396                 bounds: lb,
397                 ty: lt,
398                 ..
399             }),
400             TyAlias(box ast::TyAlias {
401                 defaultness: rd,
402                 generics: rg,
403                 bounds: rb,
404                 ty: rt,
405                 ..
406             }),
407         ) => {
408             eq_defaultness(*ld, *rd)
409                 && eq_generics(lg, rg)
410                 && over(lb, rb, eq_generic_bound)
411                 && both(lt, rt, |l, r| eq_ty(l, r))
412         },
413         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
414         _ => false,
415     }
416 }
417
418 pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
419     use AssocItemKind::*;
420     match (l, r) {
421         (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
422         (
423             Fn(box ast::Fn {
424                 defaultness: ld,
425                 sig: lf,
426                 generics: lg,
427                 body: lb,
428             }),
429             Fn(box ast::Fn {
430                 defaultness: rd,
431                 sig: rf,
432                 generics: rg,
433                 body: rb,
434             }),
435         ) => {
436             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
437         },
438         (
439             TyAlias(box ast::TyAlias {
440                 defaultness: ld,
441                 generics: lg,
442                 bounds: lb,
443                 ty: lt,
444                 ..
445             }),
446             TyAlias(box ast::TyAlias {
447                 defaultness: rd,
448                 generics: rg,
449                 bounds: rb,
450                 ty: rt,
451                 ..
452             }),
453         ) => {
454             eq_defaultness(*ld, *rd)
455                 && eq_generics(lg, rg)
456                 && over(lb, rb, eq_generic_bound)
457                 && both(lt, rt, |l, r| eq_ty(l, r))
458         },
459         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
460         _ => false,
461     }
462 }
463
464 pub fn eq_variant(l: &Variant, r: &Variant) -> bool {
465     l.is_placeholder == r.is_placeholder
466         && over(&l.attrs, &r.attrs, eq_attr)
467         && eq_vis(&l.vis, &r.vis)
468         && eq_id(l.ident, r.ident)
469         && eq_variant_data(&l.data, &r.data)
470         && both(&l.disr_expr, &r.disr_expr, |l, r| eq_expr(&l.value, &r.value))
471 }
472
473 pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
474     use VariantData::*;
475     match (l, r) {
476         (Unit(_), Unit(_)) => true,
477         (Struct(l, _), Struct(r, _)) | (Tuple(l, _), Tuple(r, _)) => over(l, r, eq_struct_field),
478         _ => false,
479     }
480 }
481
482 pub fn eq_struct_field(l: &FieldDef, r: &FieldDef) -> bool {
483     l.is_placeholder == r.is_placeholder
484         && over(&l.attrs, &r.attrs, eq_attr)
485         && eq_vis(&l.vis, &r.vis)
486         && both(&l.ident, &r.ident, |l, r| eq_id(*l, *r))
487         && eq_ty(&l.ty, &r.ty)
488 }
489
490 pub fn eq_fn_sig(l: &FnSig, r: &FnSig) -> bool {
491     eq_fn_decl(&l.decl, &r.decl) && eq_fn_header(&l.header, &r.header)
492 }
493
494 pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool {
495     matches!(l.unsafety, Unsafe::No) == matches!(r.unsafety, Unsafe::No)
496         && l.asyncness.is_async() == r.asyncness.is_async()
497         && matches!(l.constness, Const::No) == matches!(r.constness, Const::No)
498         && eq_ext(&l.ext, &r.ext)
499 }
500
501 pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
502     over(&l.params, &r.params, eq_generic_param)
503         && over(&l.where_clause.predicates, &r.where_clause.predicates, |l, r| {
504             eq_where_predicate(l, r)
505         })
506 }
507
508 pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
509     use WherePredicate::*;
510     match (l, r) {
511         (BoundPredicate(l), BoundPredicate(r)) => {
512             over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
513                 eq_generic_param(l, r)
514             }) && eq_ty(&l.bounded_ty, &r.bounded_ty)
515                 && over(&l.bounds, &r.bounds, eq_generic_bound)
516         },
517         (RegionPredicate(l), RegionPredicate(r)) => {
518             eq_id(l.lifetime.ident, r.lifetime.ident) && over(&l.bounds, &r.bounds, eq_generic_bound)
519         },
520         (EqPredicate(l), EqPredicate(r)) => eq_ty(&l.lhs_ty, &r.lhs_ty) && eq_ty(&l.rhs_ty, &r.rhs_ty),
521         _ => false,
522     }
523 }
524
525 pub fn eq_use_tree(l: &UseTree, r: &UseTree) -> bool {
526     eq_path(&l.prefix, &r.prefix) && eq_use_tree_kind(&l.kind, &r.kind)
527 }
528
529 pub fn eq_anon_const(l: &AnonConst, r: &AnonConst) -> bool {
530     eq_expr(&l.value, &r.value)
531 }
532
533 pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
534     use UseTreeKind::*;
535     match (l, r) {
536         (Glob, Glob) => true,
537         (Simple(l, _, _), Simple(r, _, _)) => both(l, r, |l, r| eq_id(*l, *r)),
538         (Nested(l), Nested(r)) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
539         _ => false,
540     }
541 }
542
543 pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
544     matches!(
545         (l, r),
546         (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_))
547     )
548 }
549
550 pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
551     use VisibilityKind::*;
552     match (&l.kind, &r.kind) {
553         (Public, Public) | (Inherited, Inherited) => true,
554         (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r),
555         _ => false,
556     }
557 }
558
559 pub fn eq_fn_decl(l: &FnDecl, r: &FnDecl) -> bool {
560     eq_fn_ret_ty(&l.output, &r.output)
561         && over(&l.inputs, &r.inputs, |l, r| {
562             l.is_placeholder == r.is_placeholder
563                 && eq_pat(&l.pat, &r.pat)
564                 && eq_ty(&l.ty, &r.ty)
565                 && over(&l.attrs, &r.attrs, eq_attr)
566         })
567 }
568
569 pub fn eq_closure_binder(l: &ClosureBinder, r: &ClosureBinder) -> bool {
570     match (l, r) {
571         (ClosureBinder::NotPresent, ClosureBinder::NotPresent) => true,
572         (ClosureBinder::For { generic_params: lp, .. }, ClosureBinder::For { generic_params: rp, .. }) => {
573             lp.len() == rp.len() && std::iter::zip(lp.iter(), rp.iter()).all(|(l, r)| eq_generic_param(l, r))
574         },
575         _ => false,
576     }
577 }
578
579 pub fn eq_fn_ret_ty(l: &FnRetTy, r: &FnRetTy) -> bool {
580     match (l, r) {
581         (FnRetTy::Default(_), FnRetTy::Default(_)) => true,
582         (FnRetTy::Ty(l), FnRetTy::Ty(r)) => eq_ty(l, r),
583         _ => false,
584     }
585 }
586
587 pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
588     use TyKind::*;
589     match (&l.kind, &r.kind) {
590         (Paren(l), _) => eq_ty(l, r),
591         (_, Paren(r)) => eq_ty(l, r),
592         (Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err, Err) | (CVarArgs, CVarArgs) => true,
593         (Slice(l), Slice(r)) => eq_ty(l, r),
594         (Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
595         (Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),
596         (Rptr(ll, l), Rptr(rl, r)) => {
597             both(ll, rl, |l, r| eq_id(l.ident, r.ident)) && l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty)
598         },
599         (BareFn(l), BareFn(r)) => {
600             l.unsafety == r.unsafety
601                 && eq_ext(&l.ext, &r.ext)
602                 && over(&l.generic_params, &r.generic_params, eq_generic_param)
603                 && eq_fn_decl(&l.decl, &r.decl)
604         },
605         (Tup(l), Tup(r)) => over(l, r, |l, r| eq_ty(l, r)),
606         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, eq_qself) && eq_path(lp, rp),
607         (TraitObject(lg, ls), TraitObject(rg, rs)) => ls == rs && over(lg, rg, eq_generic_bound),
608         (ImplTrait(_, lg), ImplTrait(_, rg)) => over(lg, rg, eq_generic_bound),
609         (Typeof(l), Typeof(r)) => eq_expr(&l.value, &r.value),
610         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
611         _ => false,
612     }
613 }
614
615 pub fn eq_ext(l: &Extern, r: &Extern) -> bool {
616     use Extern::*;
617     match (l, r) {
618         (None, None) | (Implicit(_), Implicit(_)) => true,
619         (Explicit(l, _), Explicit(r, _)) => eq_str_lit(l, r),
620         _ => false,
621     }
622 }
623
624 pub fn eq_str_lit(l: &StrLit, r: &StrLit) -> bool {
625     l.style == r.style && l.symbol == r.symbol && l.suffix == r.suffix
626 }
627
628 pub fn eq_poly_ref_trait(l: &PolyTraitRef, r: &PolyTraitRef) -> bool {
629     eq_path(&l.trait_ref.path, &r.trait_ref.path)
630         && over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
631             eq_generic_param(l, r)
632         })
633 }
634
635 pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
636     use GenericParamKind::*;
637     l.is_placeholder == r.is_placeholder
638         && eq_id(l.ident, r.ident)
639         && over(&l.bounds, &r.bounds, eq_generic_bound)
640         && match (&l.kind, &r.kind) {
641             (Lifetime, Lifetime) => true,
642             (Type { default: l }, Type { default: r }) => both(l, r, |l, r| eq_ty(l, r)),
643             (
644                 Const {
645                     ty: lt,
646                     kw_span: _,
647                     default: ld,
648                 },
649                 Const {
650                     ty: rt,
651                     kw_span: _,
652                     default: rd,
653                 },
654             ) => eq_ty(lt, rt) && both(ld, rd, eq_anon_const),
655             _ => false,
656         }
657         && over(&l.attrs, &r.attrs, eq_attr)
658 }
659
660 pub fn eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool {
661     use GenericBound::*;
662     match (l, r) {
663         (Trait(ptr1, tbm1), Trait(ptr2, tbm2)) => tbm1 == tbm2 && eq_poly_ref_trait(ptr1, ptr2),
664         (Outlives(l), Outlives(r)) => eq_id(l.ident, r.ident),
665         _ => false,
666     }
667 }
668
669 fn eq_term(l: &Term, r: &Term) -> bool {
670     match (l, r) {
671         (Term::Ty(l), Term::Ty(r)) => eq_ty(l, r),
672         (Term::Const(l), Term::Const(r)) => eq_anon_const(l, r),
673         _ => false,
674     }
675 }
676
677 pub fn eq_assoc_constraint(l: &AssocConstraint, r: &AssocConstraint) -> bool {
678     use AssocConstraintKind::*;
679     eq_id(l.ident, r.ident)
680         && match (&l.kind, &r.kind) {
681             (Equality { term: l }, Equality { term: r }) => eq_term(l, r),
682             (Bound { bounds: l }, Bound { bounds: r }) => over(l, r, eq_generic_bound),
683             _ => false,
684         }
685 }
686
687 pub fn eq_mac_call(l: &MacCall, r: &MacCall) -> bool {
688     eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args)
689 }
690
691 pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool {
692     use AttrKind::*;
693     l.style == r.style
694         && match (&l.kind, &r.kind) {
695             (DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2,
696             (Normal(l, _), Normal(r, _)) => eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args),
697             _ => false,
698         }
699 }
700
701 pub fn eq_mac_args(l: &MacArgs, r: &MacArgs) -> bool {
702     use MacArgs::*;
703     match (l, r) {
704         (Empty, Empty) => true,
705         (Delimited(_, ld, lts), Delimited(_, rd, rts)) => ld == rd && lts.eq_unspanned(rts),
706         (Eq(_, MacArgsEq::Ast(le)), Eq(_, MacArgsEq::Ast(re))) => eq_expr(le, re),
707         (Eq(_, MacArgsEq::Hir(ll)), Eq(_, MacArgsEq::Hir(rl))) => ll.kind == rl.kind,
708         _ => false,
709     }
710 }