]> git.lizzy.rs Git - rust.git/blob - clippy_utils/src/ast_utils.rs
rustc_typeck to rustc_hir_analysis
[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, ls, la, _), MethodCall(rc, rs, ra, _)) => {
151             eq_path_seg(lc, rc) && eq_expr(ls, rs) && over(la, ra, |l, r| eq_expr(l, r))
152         },
153         (Binary(lo, ll, lr), Binary(ro, rl, rr)) => lo.node == ro.node && eq_expr(ll, rl) && eq_expr(lr, rr),
154         (Unary(lo, l), Unary(ro, r)) => mem::discriminant(lo) == mem::discriminant(ro) && eq_expr(l, r),
155         (Lit(l), Lit(r)) => l.kind == r.kind,
156         (Cast(l, lt), Cast(r, rt)) | (Type(l, lt), Type(r, rt)) => eq_expr(l, r) && eq_ty(lt, rt),
157         (Let(lp, le, _), Let(rp, re, _)) => eq_pat(lp, rp) && eq_expr(le, re),
158         (If(lc, lt, le), If(rc, rt, re)) => eq_expr(lc, rc) && eq_block(lt, rt) && eq_expr_opt(le, re),
159         (While(lc, lt, ll), While(rc, rt, rl)) => eq_label(ll, rl) && eq_expr(lc, rc) && eq_block(lt, rt),
160         (ForLoop(lp, li, lt, ll), ForLoop(rp, ri, rt, rl)) => {
161             eq_label(ll, rl) && eq_pat(lp, rp) && eq_expr(li, ri) && eq_block(lt, rt)
162         },
163         (Loop(lt, ll), Loop(rt, rl)) => eq_label(ll, rl) && eq_block(lt, rt),
164         (Block(lb, ll), Block(rb, rl)) => eq_label(ll, rl) && eq_block(lb, rb),
165         (TryBlock(l), TryBlock(r)) => eq_block(l, r),
166         (Yield(l), Yield(r)) | (Ret(l), Ret(r)) => eq_expr_opt(l, r),
167         (Break(ll, le), Break(rl, re)) => eq_label(ll, rl) && eq_expr_opt(le, re),
168         (Continue(ll), Continue(rl)) => eq_label(ll, rl),
169         (Assign(l1, l2, _), Assign(r1, r2, _)) | (Index(l1, l2), Index(r1, r2)) => eq_expr(l1, r1) && eq_expr(l2, r2),
170         (AssignOp(lo, lp, lv), AssignOp(ro, rp, rv)) => lo.node == ro.node && eq_expr(lp, rp) && eq_expr(lv, rv),
171         (Field(lp, lf), Field(rp, rf)) => eq_id(*lf, *rf) && eq_expr(lp, rp),
172         (Match(ls, la), Match(rs, ra)) => eq_expr(ls, rs) && over(la, ra, eq_arm),
173         (Closure(lb, lc, la, lm, lf, le, _), Closure(rb, rc, ra, rm, rf, re, _)) => {
174             eq_closure_binder(lb, rb)
175                 && lc == rc
176                 && la.is_async() == ra.is_async()
177                 && lm == rm
178                 && eq_fn_decl(lf, rf)
179                 && eq_expr(le, re)
180         },
181         (Async(lc, _, lb), Async(rc, _, rb)) => lc == rc && eq_block(lb, rb),
182         (Range(lf, lt, ll), Range(rf, rt, rl)) => ll == rl && eq_expr_opt(lf, rf) && eq_expr_opt(lt, rt),
183         (AddrOf(lbk, lm, le), AddrOf(rbk, rm, re)) => lbk == rbk && lm == rm && eq_expr(le, re),
184         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, eq_qself) && eq_path(lp, rp),
185         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
186         (Struct(lse), Struct(rse)) => {
187             eq_maybe_qself(&lse.qself, &rse.qself)
188                 && eq_path(&lse.path, &rse.path)
189                 && eq_struct_rest(&lse.rest, &rse.rest)
190                 && unordered_over(&lse.fields, &rse.fields, eq_field)
191         },
192         _ => false,
193     }
194 }
195
196 pub fn eq_field(l: &ExprField, r: &ExprField) -> bool {
197     l.is_placeholder == r.is_placeholder
198         && eq_id(l.ident, r.ident)
199         && eq_expr(&l.expr, &r.expr)
200         && over(&l.attrs, &r.attrs, eq_attr)
201 }
202
203 pub fn eq_arm(l: &Arm, r: &Arm) -> bool {
204     l.is_placeholder == r.is_placeholder
205         && eq_pat(&l.pat, &r.pat)
206         && eq_expr(&l.body, &r.body)
207         && eq_expr_opt(&l.guard, &r.guard)
208         && over(&l.attrs, &r.attrs, eq_attr)
209 }
210
211 pub fn eq_label(l: &Option<Label>, r: &Option<Label>) -> bool {
212     both(l, r, |l, r| eq_id(l.ident, r.ident))
213 }
214
215 pub fn eq_block(l: &Block, r: &Block) -> bool {
216     l.rules == r.rules && over(&l.stmts, &r.stmts, eq_stmt)
217 }
218
219 pub fn eq_stmt(l: &Stmt, r: &Stmt) -> bool {
220     use StmtKind::*;
221     match (&l.kind, &r.kind) {
222         (Local(l), Local(r)) => {
223             eq_pat(&l.pat, &r.pat)
224                 && both(&l.ty, &r.ty, |l, r| eq_ty(l, r))
225                 && eq_local_kind(&l.kind, &r.kind)
226                 && over(&l.attrs, &r.attrs, eq_attr)
227         },
228         (Item(l), Item(r)) => eq_item(l, r, eq_item_kind),
229         (Expr(l), Expr(r)) | (Semi(l), Semi(r)) => eq_expr(l, r),
230         (Empty, Empty) => true,
231         (MacCall(l), MacCall(r)) => {
232             l.style == r.style && eq_mac_call(&l.mac, &r.mac) && over(&l.attrs, &r.attrs, eq_attr)
233         },
234         _ => false,
235     }
236 }
237
238 pub fn eq_local_kind(l: &LocalKind, r: &LocalKind) -> bool {
239     use LocalKind::*;
240     match (l, r) {
241         (Decl, Decl) => true,
242         (Init(l), Init(r)) => eq_expr(l, r),
243         (InitElse(li, le), InitElse(ri, re)) => eq_expr(li, ri) && eq_block(le, re),
244         _ => false,
245     }
246 }
247
248 pub fn eq_item<K>(l: &Item<K>, r: &Item<K>, mut eq_kind: impl FnMut(&K, &K) -> bool) -> bool {
249     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)
250 }
251
252 #[expect(clippy::too_many_lines)] // Just a big match statement
253 pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
254     use ItemKind::*;
255     match (l, r) {
256         (ExternCrate(l), ExternCrate(r)) => l == r,
257         (Use(l), Use(r)) => eq_use_tree(l, r),
258         (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
259         (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
260         (
261             Fn(box ast::Fn {
262                 defaultness: ld,
263                 sig: lf,
264                 generics: lg,
265                 body: lb,
266             }),
267             Fn(box ast::Fn {
268                 defaultness: rd,
269                 sig: rf,
270                 generics: rg,
271                 body: rb,
272             }),
273         ) => {
274             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
275         },
276         (Mod(lu, lmk), Mod(ru, rmk)) => {
277             lu == ru
278                 && match (lmk, rmk) {
279                     (ModKind::Loaded(litems, linline, _), ModKind::Loaded(ritems, rinline, _)) => {
280                         linline == rinline && over(litems, ritems, |l, r| eq_item(l, r, eq_item_kind))
281                     },
282                     (ModKind::Unloaded, ModKind::Unloaded) => true,
283                     _ => false,
284                 }
285         },
286         (ForeignMod(l), ForeignMod(r)) => {
287             both(&l.abi, &r.abi, eq_str_lit) && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
288         },
289         (
290             TyAlias(box ast::TyAlias {
291                 defaultness: ld,
292                 generics: lg,
293                 bounds: lb,
294                 ty: lt,
295                 ..
296             }),
297             TyAlias(box ast::TyAlias {
298                 defaultness: rd,
299                 generics: rg,
300                 bounds: rb,
301                 ty: rt,
302                 ..
303             }),
304         ) => {
305             eq_defaultness(*ld, *rd)
306                 && eq_generics(lg, rg)
307                 && over(lb, rb, eq_generic_bound)
308                 && both(lt, rt, |l, r| eq_ty(l, r))
309         },
310         (Enum(le, lg), Enum(re, rg)) => over(&le.variants, &re.variants, eq_variant) && eq_generics(lg, rg),
311         (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => {
312             eq_variant_data(lv, rv) && eq_generics(lg, rg)
313         },
314         (
315             Trait(box ast::Trait {
316                 is_auto: la,
317                 unsafety: lu,
318                 generics: lg,
319                 bounds: lb,
320                 items: li,
321             }),
322             Trait(box ast::Trait {
323                 is_auto: ra,
324                 unsafety: ru,
325                 generics: rg,
326                 bounds: rb,
327                 items: ri,
328             }),
329         ) => {
330             la == ra
331                 && matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
332                 && eq_generics(lg, rg)
333                 && over(lb, rb, eq_generic_bound)
334                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
335         },
336         (TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, eq_generic_bound),
337         (
338             Impl(box ast::Impl {
339                 unsafety: lu,
340                 polarity: lp,
341                 defaultness: ld,
342                 constness: lc,
343                 generics: lg,
344                 of_trait: lot,
345                 self_ty: lst,
346                 items: li,
347             }),
348             Impl(box ast::Impl {
349                 unsafety: ru,
350                 polarity: rp,
351                 defaultness: rd,
352                 constness: rc,
353                 generics: rg,
354                 of_trait: rot,
355                 self_ty: rst,
356                 items: ri,
357             }),
358         ) => {
359             matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
360                 && matches!(lp, ImplPolarity::Positive) == matches!(rp, ImplPolarity::Positive)
361                 && eq_defaultness(*ld, *rd)
362                 && matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
363                 && eq_generics(lg, rg)
364                 && both(lot, rot, |l, r| eq_path(&l.path, &r.path))
365                 && eq_ty(lst, rst)
366                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
367         },
368         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
369         (MacroDef(l), MacroDef(r)) => l.macro_rules == r.macro_rules && eq_mac_args(&l.body, &r.body),
370         _ => false,
371     }
372 }
373
374 pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
375     use ForeignItemKind::*;
376     match (l, r) {
377         (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
378         (
379             Fn(box ast::Fn {
380                 defaultness: ld,
381                 sig: lf,
382                 generics: lg,
383                 body: lb,
384             }),
385             Fn(box ast::Fn {
386                 defaultness: rd,
387                 sig: rf,
388                 generics: rg,
389                 body: rb,
390             }),
391         ) => {
392             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
393         },
394         (
395             TyAlias(box ast::TyAlias {
396                 defaultness: ld,
397                 generics: lg,
398                 bounds: lb,
399                 ty: lt,
400                 ..
401             }),
402             TyAlias(box ast::TyAlias {
403                 defaultness: rd,
404                 generics: rg,
405                 bounds: rb,
406                 ty: rt,
407                 ..
408             }),
409         ) => {
410             eq_defaultness(*ld, *rd)
411                 && eq_generics(lg, rg)
412                 && over(lb, rb, eq_generic_bound)
413                 && both(lt, rt, |l, r| eq_ty(l, r))
414         },
415         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
416         _ => false,
417     }
418 }
419
420 pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
421     use AssocItemKind::*;
422     match (l, r) {
423         (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
424         (
425             Fn(box ast::Fn {
426                 defaultness: ld,
427                 sig: lf,
428                 generics: lg,
429                 body: lb,
430             }),
431             Fn(box ast::Fn {
432                 defaultness: rd,
433                 sig: rf,
434                 generics: rg,
435                 body: rb,
436             }),
437         ) => {
438             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
439         },
440         (
441             TyAlias(box ast::TyAlias {
442                 defaultness: ld,
443                 generics: lg,
444                 bounds: lb,
445                 ty: lt,
446                 ..
447             }),
448             TyAlias(box ast::TyAlias {
449                 defaultness: rd,
450                 generics: rg,
451                 bounds: rb,
452                 ty: rt,
453                 ..
454             }),
455         ) => {
456             eq_defaultness(*ld, *rd)
457                 && eq_generics(lg, rg)
458                 && over(lb, rb, eq_generic_bound)
459                 && both(lt, rt, |l, r| eq_ty(l, r))
460         },
461         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
462         _ => false,
463     }
464 }
465
466 pub fn eq_variant(l: &Variant, r: &Variant) -> bool {
467     l.is_placeholder == r.is_placeholder
468         && over(&l.attrs, &r.attrs, eq_attr)
469         && eq_vis(&l.vis, &r.vis)
470         && eq_id(l.ident, r.ident)
471         && eq_variant_data(&l.data, &r.data)
472         && both(&l.disr_expr, &r.disr_expr, |l, r| eq_expr(&l.value, &r.value))
473 }
474
475 pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
476     use VariantData::*;
477     match (l, r) {
478         (Unit(_), Unit(_)) => true,
479         (Struct(l, _), Struct(r, _)) | (Tuple(l, _), Tuple(r, _)) => over(l, r, eq_struct_field),
480         _ => false,
481     }
482 }
483
484 pub fn eq_struct_field(l: &FieldDef, r: &FieldDef) -> bool {
485     l.is_placeholder == r.is_placeholder
486         && over(&l.attrs, &r.attrs, eq_attr)
487         && eq_vis(&l.vis, &r.vis)
488         && both(&l.ident, &r.ident, |l, r| eq_id(*l, *r))
489         && eq_ty(&l.ty, &r.ty)
490 }
491
492 pub fn eq_fn_sig(l: &FnSig, r: &FnSig) -> bool {
493     eq_fn_decl(&l.decl, &r.decl) && eq_fn_header(&l.header, &r.header)
494 }
495
496 pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool {
497     matches!(l.unsafety, Unsafe::No) == matches!(r.unsafety, Unsafe::No)
498         && l.asyncness.is_async() == r.asyncness.is_async()
499         && matches!(l.constness, Const::No) == matches!(r.constness, Const::No)
500         && eq_ext(&l.ext, &r.ext)
501 }
502
503 pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
504     over(&l.params, &r.params, eq_generic_param)
505         && over(&l.where_clause.predicates, &r.where_clause.predicates, |l, r| {
506             eq_where_predicate(l, r)
507         })
508 }
509
510 pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
511     use WherePredicate::*;
512     match (l, r) {
513         (BoundPredicate(l), BoundPredicate(r)) => {
514             over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
515                 eq_generic_param(l, r)
516             }) && eq_ty(&l.bounded_ty, &r.bounded_ty)
517                 && over(&l.bounds, &r.bounds, eq_generic_bound)
518         },
519         (RegionPredicate(l), RegionPredicate(r)) => {
520             eq_id(l.lifetime.ident, r.lifetime.ident) && over(&l.bounds, &r.bounds, eq_generic_bound)
521         },
522         (EqPredicate(l), EqPredicate(r)) => eq_ty(&l.lhs_ty, &r.lhs_ty) && eq_ty(&l.rhs_ty, &r.rhs_ty),
523         _ => false,
524     }
525 }
526
527 pub fn eq_use_tree(l: &UseTree, r: &UseTree) -> bool {
528     eq_path(&l.prefix, &r.prefix) && eq_use_tree_kind(&l.kind, &r.kind)
529 }
530
531 pub fn eq_anon_const(l: &AnonConst, r: &AnonConst) -> bool {
532     eq_expr(&l.value, &r.value)
533 }
534
535 pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
536     use UseTreeKind::*;
537     match (l, r) {
538         (Glob, Glob) => true,
539         (Simple(l, _, _), Simple(r, _, _)) => both(l, r, |l, r| eq_id(*l, *r)),
540         (Nested(l), Nested(r)) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
541         _ => false,
542     }
543 }
544
545 pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
546     matches!(
547         (l, r),
548         (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_))
549     )
550 }
551
552 pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
553     use VisibilityKind::*;
554     match (&l.kind, &r.kind) {
555         (Public, Public) | (Inherited, Inherited) => true,
556         (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r),
557         _ => false,
558     }
559 }
560
561 pub fn eq_fn_decl(l: &FnDecl, r: &FnDecl) -> bool {
562     eq_fn_ret_ty(&l.output, &r.output)
563         && over(&l.inputs, &r.inputs, |l, r| {
564             l.is_placeholder == r.is_placeholder
565                 && eq_pat(&l.pat, &r.pat)
566                 && eq_ty(&l.ty, &r.ty)
567                 && over(&l.attrs, &r.attrs, eq_attr)
568         })
569 }
570
571 pub fn eq_closure_binder(l: &ClosureBinder, r: &ClosureBinder) -> bool {
572     match (l, r) {
573         (ClosureBinder::NotPresent, ClosureBinder::NotPresent) => true,
574         (ClosureBinder::For { generic_params: lp, .. }, ClosureBinder::For { generic_params: rp, .. }) => {
575             lp.len() == rp.len() && std::iter::zip(lp.iter(), rp.iter()).all(|(l, r)| eq_generic_param(l, r))
576         },
577         _ => false,
578     }
579 }
580
581 pub fn eq_fn_ret_ty(l: &FnRetTy, r: &FnRetTy) -> bool {
582     match (l, r) {
583         (FnRetTy::Default(_), FnRetTy::Default(_)) => true,
584         (FnRetTy::Ty(l), FnRetTy::Ty(r)) => eq_ty(l, r),
585         _ => false,
586     }
587 }
588
589 pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
590     use TyKind::*;
591     match (&l.kind, &r.kind) {
592         (Paren(l), _) => eq_ty(l, r),
593         (_, Paren(r)) => eq_ty(l, r),
594         (Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err, Err) | (CVarArgs, CVarArgs) => true,
595         (Slice(l), Slice(r)) => eq_ty(l, r),
596         (Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
597         (Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),
598         (Rptr(ll, l), Rptr(rl, r)) => {
599             both(ll, rl, |l, r| eq_id(l.ident, r.ident)) && l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty)
600         },
601         (BareFn(l), BareFn(r)) => {
602             l.unsafety == r.unsafety
603                 && eq_ext(&l.ext, &r.ext)
604                 && over(&l.generic_params, &r.generic_params, eq_generic_param)
605                 && eq_fn_decl(&l.decl, &r.decl)
606         },
607         (Tup(l), Tup(r)) => over(l, r, |l, r| eq_ty(l, r)),
608         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, eq_qself) && eq_path(lp, rp),
609         (TraitObject(lg, ls), TraitObject(rg, rs)) => ls == rs && over(lg, rg, eq_generic_bound),
610         (ImplTrait(_, lg), ImplTrait(_, rg)) => over(lg, rg, eq_generic_bound),
611         (Typeof(l), Typeof(r)) => eq_expr(&l.value, &r.value),
612         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
613         _ => false,
614     }
615 }
616
617 pub fn eq_ext(l: &Extern, r: &Extern) -> bool {
618     use Extern::*;
619     match (l, r) {
620         (None, None) | (Implicit(_), Implicit(_)) => true,
621         (Explicit(l, _), Explicit(r, _)) => eq_str_lit(l, r),
622         _ => false,
623     }
624 }
625
626 pub fn eq_str_lit(l: &StrLit, r: &StrLit) -> bool {
627     l.style == r.style && l.symbol == r.symbol && l.suffix == r.suffix
628 }
629
630 pub fn eq_poly_ref_trait(l: &PolyTraitRef, r: &PolyTraitRef) -> bool {
631     eq_path(&l.trait_ref.path, &r.trait_ref.path)
632         && over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
633             eq_generic_param(l, r)
634         })
635 }
636
637 pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
638     use GenericParamKind::*;
639     l.is_placeholder == r.is_placeholder
640         && eq_id(l.ident, r.ident)
641         && over(&l.bounds, &r.bounds, eq_generic_bound)
642         && match (&l.kind, &r.kind) {
643             (Lifetime, Lifetime) => true,
644             (Type { default: l }, Type { default: r }) => both(l, r, |l, r| eq_ty(l, r)),
645             (
646                 Const {
647                     ty: lt,
648                     kw_span: _,
649                     default: ld,
650                 },
651                 Const {
652                     ty: rt,
653                     kw_span: _,
654                     default: rd,
655                 },
656             ) => eq_ty(lt, rt) && both(ld, rd, eq_anon_const),
657             _ => false,
658         }
659         && over(&l.attrs, &r.attrs, eq_attr)
660 }
661
662 pub fn eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool {
663     use GenericBound::*;
664     match (l, r) {
665         (Trait(ptr1, tbm1), Trait(ptr2, tbm2)) => tbm1 == tbm2 && eq_poly_ref_trait(ptr1, ptr2),
666         (Outlives(l), Outlives(r)) => eq_id(l.ident, r.ident),
667         _ => false,
668     }
669 }
670
671 fn eq_term(l: &Term, r: &Term) -> bool {
672     match (l, r) {
673         (Term::Ty(l), Term::Ty(r)) => eq_ty(l, r),
674         (Term::Const(l), Term::Const(r)) => eq_anon_const(l, r),
675         _ => false,
676     }
677 }
678
679 pub fn eq_assoc_constraint(l: &AssocConstraint, r: &AssocConstraint) -> bool {
680     use AssocConstraintKind::*;
681     eq_id(l.ident, r.ident)
682         && match (&l.kind, &r.kind) {
683             (Equality { term: l }, Equality { term: r }) => eq_term(l, r),
684             (Bound { bounds: l }, Bound { bounds: r }) => over(l, r, eq_generic_bound),
685             _ => false,
686         }
687 }
688
689 pub fn eq_mac_call(l: &MacCall, r: &MacCall) -> bool {
690     eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args)
691 }
692
693 pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool {
694     use AttrKind::*;
695     l.style == r.style
696         && match (&l.kind, &r.kind) {
697             (DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2,
698             (Normal(l), Normal(r)) => eq_path(&l.item.path, &r.item.path) && eq_mac_args(&l.item.args, &r.item.args),
699             _ => false,
700         }
701 }
702
703 pub fn eq_mac_args(l: &MacArgs, r: &MacArgs) -> bool {
704     use MacArgs::*;
705     match (l, r) {
706         (Empty, Empty) => true,
707         (Delimited(_, ld, lts), Delimited(_, rd, rts)) => ld == rd && lts.eq_unspanned(rts),
708         (Eq(_, MacArgsEq::Ast(le)), Eq(_, MacArgsEq::Ast(re))) => eq_expr(le, re),
709         (Eq(_, MacArgsEq::Hir(ll)), Eq(_, MacArgsEq::Hir(rl))) => ll.kind == rl.kind,
710         _ => false,
711     }
712 }