• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
is_useless_with_eq_exprs(kind: BinOpKind) -> bool16 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`.
unordered_over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool25 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 
eq_id(l: Ident, r: Ident) -> bool29 pub fn eq_id(l: Ident, r: Ident) -> bool {
30     l.name == r.name
31 }
32 
eq_pat(l: &Pat, r: &Pat) -> bool33 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 
eq_range_end(l: &RangeEnd, r: &RangeEnd) -> bool61 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 
eq_field_pat(l: &PatField, r: &PatField) -> bool71 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 
eq_qself(l: &P<QSelf>, r: &P<QSelf>) -> bool78 pub fn eq_qself(l: &P<QSelf>, r: &P<QSelf>) -> bool {
79     l.position == r.position && eq_ty(&l.ty, &r.ty)
80 }
81 
eq_maybe_qself(l: &Option<P<QSelf>>, r: &Option<P<QSelf>>) -> bool82 pub fn eq_maybe_qself(l: &Option<P<QSelf>>, r: &Option<P<QSelf>>) -> bool {
83     match (l, r) {
84         (Some(l), Some(r)) => eq_qself(l, r),
85         (None, None) => true,
86         _ => false,
87     }
88 }
89 
eq_path(l: &Path, r: &Path) -> bool90 pub fn eq_path(l: &Path, r: &Path) -> bool {
91     over(&l.segments, &r.segments, eq_path_seg)
92 }
93 
eq_path_seg(l: &PathSegment, r: &PathSegment) -> bool94 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 
eq_generic_args(l: &GenericArgs, r: &GenericArgs) -> bool98 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 
eq_angle_arg(l: &AngleBracketedArg, r: &AngleBracketedArg) -> bool108 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 
eq_generic_arg(l: &GenericArg, r: &GenericArg) -> bool116 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 
eq_expr_opt(l: &Option<P<Expr>>, r: &Option<P<Expr>>) -> bool125 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 
eq_struct_rest(l: &StructRest, r: &StructRest) -> bool129 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 
eq_expr(l: &Expr, r: &Expr) -> bool137 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         (Try(l), Try(r)) | (Await(l, _), Await(r, _)) => eq_expr(l, r),
147         (Array(l), Array(r)) => over(l, r, |l, r| eq_expr(l, r)),
148         (Tup(l), Tup(r)) => over(l, r, |l, r| eq_expr(l, r)),
149         (Repeat(le, ls), Repeat(re, rs)) => eq_expr(le, re) && eq_expr(&ls.value, &rs.value),
150         (Call(lc, la), Call(rc, ra)) => eq_expr(lc, rc) && over(la, ra, |l, r| eq_expr(l, r)),
151         (
152             MethodCall(box ast::MethodCall {
153                 seg: ls,
154                 receiver: lr,
155                 args: la,
156                 ..
157             }),
158             MethodCall(box ast::MethodCall {
159                 seg: rs,
160                 receiver: rr,
161                 args: ra,
162                 ..
163             }),
164         ) => eq_path_seg(ls, rs) && eq_expr(lr, rr) && over(la, ra, |l, r| eq_expr(l, r)),
165         (Binary(lo, ll, lr), Binary(ro, rl, rr)) => lo.node == ro.node && eq_expr(ll, rl) && eq_expr(lr, rr),
166         (Unary(lo, l), Unary(ro, r)) => mem::discriminant(lo) == mem::discriminant(ro) && eq_expr(l, r),
167         (Lit(l), Lit(r)) => l == r,
168         (Cast(l, lt), Cast(r, rt)) | (Type(l, lt), Type(r, rt)) => eq_expr(l, r) && eq_ty(lt, rt),
169         (Let(lp, le, _), Let(rp, re, _)) => eq_pat(lp, rp) && eq_expr(le, re),
170         (If(lc, lt, le), If(rc, rt, re)) => eq_expr(lc, rc) && eq_block(lt, rt) && eq_expr_opt(le, re),
171         (While(lc, lt, ll), While(rc, rt, rl)) => eq_label(ll, rl) && eq_expr(lc, rc) && eq_block(lt, rt),
172         (ForLoop(lp, li, lt, ll), ForLoop(rp, ri, rt, rl)) => {
173             eq_label(ll, rl) && eq_pat(lp, rp) && eq_expr(li, ri) && eq_block(lt, rt)
174         },
175         (Loop(lt, ll, _), Loop(rt, rl, _)) => eq_label(ll, rl) && eq_block(lt, rt),
176         (Block(lb, ll), Block(rb, rl)) => eq_label(ll, rl) && eq_block(lb, rb),
177         (TryBlock(l), TryBlock(r)) => eq_block(l, r),
178         (Yield(l), Yield(r)) | (Ret(l), Ret(r)) => eq_expr_opt(l, r),
179         (Break(ll, le), Break(rl, re)) => eq_label(ll, rl) && eq_expr_opt(le, re),
180         (Continue(ll), Continue(rl)) => eq_label(ll, rl),
181         (Assign(l1, l2, _), Assign(r1, r2, _)) | (Index(l1, l2), Index(r1, r2)) => eq_expr(l1, r1) && eq_expr(l2, r2),
182         (AssignOp(lo, lp, lv), AssignOp(ro, rp, rv)) => lo.node == ro.node && eq_expr(lp, rp) && eq_expr(lv, rv),
183         (Field(lp, lf), Field(rp, rf)) => eq_id(*lf, *rf) && eq_expr(lp, rp),
184         (Match(ls, la), Match(rs, ra)) => eq_expr(ls, rs) && over(la, ra, eq_arm),
185         (
186             Closure(box ast::Closure {
187                 binder: lb,
188                 capture_clause: lc,
189                 asyncness: la,
190                 movability: lm,
191                 fn_decl: lf,
192                 body: le,
193                 ..
194             }),
195             Closure(box ast::Closure {
196                 binder: rb,
197                 capture_clause: rc,
198                 asyncness: ra,
199                 movability: rm,
200                 fn_decl: rf,
201                 body: re,
202                 ..
203             }),
204         ) => {
205             eq_closure_binder(lb, rb)
206                 && lc == rc
207                 && la.is_async() == ra.is_async()
208                 && lm == rm
209                 && eq_fn_decl(lf, rf)
210                 && eq_expr(le, re)
211         },
212         (Async(lc, lb), Async(rc, rb)) => lc == rc && eq_block(lb, rb),
213         (Range(lf, lt, ll), Range(rf, rt, rl)) => ll == rl && eq_expr_opt(lf, rf) && eq_expr_opt(lt, rt),
214         (AddrOf(lbk, lm, le), AddrOf(rbk, rm, re)) => lbk == rbk && lm == rm && eq_expr(le, re),
215         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, eq_qself) && eq_path(lp, rp),
216         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
217         (Struct(lse), Struct(rse)) => {
218             eq_maybe_qself(&lse.qself, &rse.qself)
219                 && eq_path(&lse.path, &rse.path)
220                 && eq_struct_rest(&lse.rest, &rse.rest)
221                 && unordered_over(&lse.fields, &rse.fields, eq_field)
222         },
223         _ => false,
224     }
225 }
226 
eq_field(l: &ExprField, r: &ExprField) -> bool227 pub fn eq_field(l: &ExprField, r: &ExprField) -> bool {
228     l.is_placeholder == r.is_placeholder
229         && eq_id(l.ident, r.ident)
230         && eq_expr(&l.expr, &r.expr)
231         && over(&l.attrs, &r.attrs, eq_attr)
232 }
233 
eq_arm(l: &Arm, r: &Arm) -> bool234 pub fn eq_arm(l: &Arm, r: &Arm) -> bool {
235     l.is_placeholder == r.is_placeholder
236         && eq_pat(&l.pat, &r.pat)
237         && eq_expr(&l.body, &r.body)
238         && eq_expr_opt(&l.guard, &r.guard)
239         && over(&l.attrs, &r.attrs, eq_attr)
240 }
241 
eq_label(l: &Option<Label>, r: &Option<Label>) -> bool242 pub fn eq_label(l: &Option<Label>, r: &Option<Label>) -> bool {
243     both(l, r, |l, r| eq_id(l.ident, r.ident))
244 }
245 
eq_block(l: &Block, r: &Block) -> bool246 pub fn eq_block(l: &Block, r: &Block) -> bool {
247     l.rules == r.rules && over(&l.stmts, &r.stmts, eq_stmt)
248 }
249 
eq_stmt(l: &Stmt, r: &Stmt) -> bool250 pub fn eq_stmt(l: &Stmt, r: &Stmt) -> bool {
251     use StmtKind::*;
252     match (&l.kind, &r.kind) {
253         (Local(l), Local(r)) => {
254             eq_pat(&l.pat, &r.pat)
255                 && both(&l.ty, &r.ty, |l, r| eq_ty(l, r))
256                 && eq_local_kind(&l.kind, &r.kind)
257                 && over(&l.attrs, &r.attrs, eq_attr)
258         },
259         (Item(l), Item(r)) => eq_item(l, r, eq_item_kind),
260         (Expr(l), Expr(r)) | (Semi(l), Semi(r)) => eq_expr(l, r),
261         (Empty, Empty) => true,
262         (MacCall(l), MacCall(r)) => {
263             l.style == r.style && eq_mac_call(&l.mac, &r.mac) && over(&l.attrs, &r.attrs, eq_attr)
264         },
265         _ => false,
266     }
267 }
268 
eq_local_kind(l: &LocalKind, r: &LocalKind) -> bool269 pub fn eq_local_kind(l: &LocalKind, r: &LocalKind) -> bool {
270     use LocalKind::*;
271     match (l, r) {
272         (Decl, Decl) => true,
273         (Init(l), Init(r)) => eq_expr(l, r),
274         (InitElse(li, le), InitElse(ri, re)) => eq_expr(li, ri) && eq_block(le, re),
275         _ => false,
276     }
277 }
278 
eq_item<K>(l: &Item<K>, r: &Item<K>, mut eq_kind: impl FnMut(&K, &K) -> bool) -> bool279 pub fn eq_item<K>(l: &Item<K>, r: &Item<K>, mut eq_kind: impl FnMut(&K, &K) -> bool) -> bool {
280     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)
281 }
282 
283 #[expect(clippy::too_many_lines)] // Just a big match statement
eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool284 pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
285     use ItemKind::*;
286     match (l, r) {
287         (ExternCrate(l), ExternCrate(r)) => l == r,
288         (Use(l), Use(r)) => eq_use_tree(l, r),
289         (
290             Static(box ast::StaticItem {
291                 ty: lt,
292                 mutability: lm,
293                 expr: le,
294             }),
295             Static(box ast::StaticItem {
296                 ty: rt,
297                 mutability: rm,
298                 expr: re,
299             }),
300         ) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
301         (
302             Const(box ast::ConstItem {
303                 defaultness: ld,
304                 ty: lt,
305                 expr: le,
306             }),
307             Const(box ast::ConstItem {
308                 defaultness: rd,
309                 ty: rt,
310                 expr: re,
311             }),
312         ) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
313         (
314             Fn(box ast::Fn {
315                 defaultness: ld,
316                 sig: lf,
317                 generics: lg,
318                 body: lb,
319             }),
320             Fn(box ast::Fn {
321                 defaultness: rd,
322                 sig: rf,
323                 generics: rg,
324                 body: rb,
325             }),
326         ) => {
327             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
328         },
329         (Mod(lu, lmk), Mod(ru, rmk)) => {
330             lu == ru
331                 && match (lmk, rmk) {
332                     (ModKind::Loaded(litems, linline, _), ModKind::Loaded(ritems, rinline, _)) => {
333                         linline == rinline && over(litems, ritems, |l, r| eq_item(l, r, eq_item_kind))
334                     },
335                     (ModKind::Unloaded, ModKind::Unloaded) => true,
336                     _ => false,
337                 }
338         },
339         (ForeignMod(l), ForeignMod(r)) => {
340             both(&l.abi, &r.abi, eq_str_lit) && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
341         },
342         (
343             TyAlias(box ast::TyAlias {
344                 defaultness: ld,
345                 generics: lg,
346                 bounds: lb,
347                 ty: lt,
348                 ..
349             }),
350             TyAlias(box ast::TyAlias {
351                 defaultness: rd,
352                 generics: rg,
353                 bounds: rb,
354                 ty: rt,
355                 ..
356             }),
357         ) => {
358             eq_defaultness(*ld, *rd)
359                 && eq_generics(lg, rg)
360                 && over(lb, rb, eq_generic_bound)
361                 && both(lt, rt, |l, r| eq_ty(l, r))
362         },
363         (Enum(le, lg), Enum(re, rg)) => over(&le.variants, &re.variants, eq_variant) && eq_generics(lg, rg),
364         (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => {
365             eq_variant_data(lv, rv) && eq_generics(lg, rg)
366         },
367         (
368             Trait(box ast::Trait {
369                 is_auto: la,
370                 unsafety: lu,
371                 generics: lg,
372                 bounds: lb,
373                 items: li,
374             }),
375             Trait(box ast::Trait {
376                 is_auto: ra,
377                 unsafety: ru,
378                 generics: rg,
379                 bounds: rb,
380                 items: ri,
381             }),
382         ) => {
383             la == ra
384                 && matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
385                 && eq_generics(lg, rg)
386                 && over(lb, rb, eq_generic_bound)
387                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
388         },
389         (TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, eq_generic_bound),
390         (
391             Impl(box ast::Impl {
392                 unsafety: lu,
393                 polarity: lp,
394                 defaultness: ld,
395                 constness: lc,
396                 generics: lg,
397                 of_trait: lot,
398                 self_ty: lst,
399                 items: li,
400             }),
401             Impl(box ast::Impl {
402                 unsafety: ru,
403                 polarity: rp,
404                 defaultness: rd,
405                 constness: rc,
406                 generics: rg,
407                 of_trait: rot,
408                 self_ty: rst,
409                 items: ri,
410             }),
411         ) => {
412             matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
413                 && matches!(lp, ImplPolarity::Positive) == matches!(rp, ImplPolarity::Positive)
414                 && eq_defaultness(*ld, *rd)
415                 && matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
416                 && eq_generics(lg, rg)
417                 && both(lot, rot, |l, r| eq_path(&l.path, &r.path))
418                 && eq_ty(lst, rst)
419                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
420         },
421         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
422         (MacroDef(l), MacroDef(r)) => l.macro_rules == r.macro_rules && eq_delim_args(&l.body, &r.body),
423         _ => false,
424     }
425 }
426 
eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool427 pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
428     use ForeignItemKind::*;
429     match (l, r) {
430         (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
431         (
432             Fn(box ast::Fn {
433                 defaultness: ld,
434                 sig: lf,
435                 generics: lg,
436                 body: lb,
437             }),
438             Fn(box ast::Fn {
439                 defaultness: rd,
440                 sig: rf,
441                 generics: rg,
442                 body: rb,
443             }),
444         ) => {
445             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
446         },
447         (
448             TyAlias(box ast::TyAlias {
449                 defaultness: ld,
450                 generics: lg,
451                 bounds: lb,
452                 ty: lt,
453                 ..
454             }),
455             TyAlias(box ast::TyAlias {
456                 defaultness: rd,
457                 generics: rg,
458                 bounds: rb,
459                 ty: rt,
460                 ..
461             }),
462         ) => {
463             eq_defaultness(*ld, *rd)
464                 && eq_generics(lg, rg)
465                 && over(lb, rb, eq_generic_bound)
466                 && both(lt, rt, |l, r| eq_ty(l, r))
467         },
468         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
469         _ => false,
470     }
471 }
472 
eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool473 pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
474     use AssocItemKind::*;
475     match (l, r) {
476         (
477             Const(box ast::ConstItem {
478                 defaultness: ld,
479                 ty: lt,
480                 expr: le,
481             }),
482             Const(box ast::ConstItem {
483                 defaultness: rd,
484                 ty: rt,
485                 expr: re,
486             }),
487         ) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
488         (
489             Fn(box ast::Fn {
490                 defaultness: ld,
491                 sig: lf,
492                 generics: lg,
493                 body: lb,
494             }),
495             Fn(box ast::Fn {
496                 defaultness: rd,
497                 sig: rf,
498                 generics: rg,
499                 body: rb,
500             }),
501         ) => {
502             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
503         },
504         (
505             Type(box ast::TyAlias {
506                 defaultness: ld,
507                 generics: lg,
508                 bounds: lb,
509                 ty: lt,
510                 ..
511             }),
512             Type(box ast::TyAlias {
513                 defaultness: rd,
514                 generics: rg,
515                 bounds: rb,
516                 ty: rt,
517                 ..
518             }),
519         ) => {
520             eq_defaultness(*ld, *rd)
521                 && eq_generics(lg, rg)
522                 && over(lb, rb, eq_generic_bound)
523                 && both(lt, rt, |l, r| eq_ty(l, r))
524         },
525         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
526         _ => false,
527     }
528 }
529 
eq_variant(l: &Variant, r: &Variant) -> bool530 pub fn eq_variant(l: &Variant, r: &Variant) -> bool {
531     l.is_placeholder == r.is_placeholder
532         && over(&l.attrs, &r.attrs, eq_attr)
533         && eq_vis(&l.vis, &r.vis)
534         && eq_id(l.ident, r.ident)
535         && eq_variant_data(&l.data, &r.data)
536         && both(&l.disr_expr, &r.disr_expr, |l, r| eq_expr(&l.value, &r.value))
537 }
538 
eq_variant_data(l: &VariantData, r: &VariantData) -> bool539 pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
540     use VariantData::*;
541     match (l, r) {
542         (Unit(_), Unit(_)) => true,
543         (Struct(l, _), Struct(r, _)) | (Tuple(l, _), Tuple(r, _)) => over(l, r, eq_struct_field),
544         _ => false,
545     }
546 }
547 
eq_struct_field(l: &FieldDef, r: &FieldDef) -> bool548 pub fn eq_struct_field(l: &FieldDef, r: &FieldDef) -> bool {
549     l.is_placeholder == r.is_placeholder
550         && over(&l.attrs, &r.attrs, eq_attr)
551         && eq_vis(&l.vis, &r.vis)
552         && both(&l.ident, &r.ident, |l, r| eq_id(*l, *r))
553         && eq_ty(&l.ty, &r.ty)
554 }
555 
eq_fn_sig(l: &FnSig, r: &FnSig) -> bool556 pub fn eq_fn_sig(l: &FnSig, r: &FnSig) -> bool {
557     eq_fn_decl(&l.decl, &r.decl) && eq_fn_header(&l.header, &r.header)
558 }
559 
eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool560 pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool {
561     matches!(l.unsafety, Unsafe::No) == matches!(r.unsafety, Unsafe::No)
562         && l.asyncness.is_async() == r.asyncness.is_async()
563         && matches!(l.constness, Const::No) == matches!(r.constness, Const::No)
564         && eq_ext(&l.ext, &r.ext)
565 }
566 
eq_generics(l: &Generics, r: &Generics) -> bool567 pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
568     over(&l.params, &r.params, eq_generic_param)
569         && over(&l.where_clause.predicates, &r.where_clause.predicates, |l, r| {
570             eq_where_predicate(l, r)
571         })
572 }
573 
eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool574 pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
575     use WherePredicate::*;
576     match (l, r) {
577         (BoundPredicate(l), BoundPredicate(r)) => {
578             over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
579                 eq_generic_param(l, r)
580             }) && eq_ty(&l.bounded_ty, &r.bounded_ty)
581                 && over(&l.bounds, &r.bounds, eq_generic_bound)
582         },
583         (RegionPredicate(l), RegionPredicate(r)) => {
584             eq_id(l.lifetime.ident, r.lifetime.ident) && over(&l.bounds, &r.bounds, eq_generic_bound)
585         },
586         (EqPredicate(l), EqPredicate(r)) => eq_ty(&l.lhs_ty, &r.lhs_ty) && eq_ty(&l.rhs_ty, &r.rhs_ty),
587         _ => false,
588     }
589 }
590 
eq_use_tree(l: &UseTree, r: &UseTree) -> bool591 pub fn eq_use_tree(l: &UseTree, r: &UseTree) -> bool {
592     eq_path(&l.prefix, &r.prefix) && eq_use_tree_kind(&l.kind, &r.kind)
593 }
594 
eq_anon_const(l: &AnonConst, r: &AnonConst) -> bool595 pub fn eq_anon_const(l: &AnonConst, r: &AnonConst) -> bool {
596     eq_expr(&l.value, &r.value)
597 }
598 
eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool599 pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
600     use UseTreeKind::*;
601     match (l, r) {
602         (Glob, Glob) => true,
603         (Simple(l), Simple(r)) => both(l, r, |l, r| eq_id(*l, *r)),
604         (Nested(l), Nested(r)) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
605         _ => false,
606     }
607 }
608 
eq_defaultness(l: Defaultness, r: Defaultness) -> bool609 pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
610     matches!(
611         (l, r),
612         (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_))
613     )
614 }
615 
eq_vis(l: &Visibility, r: &Visibility) -> bool616 pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
617     use VisibilityKind::*;
618     match (&l.kind, &r.kind) {
619         (Public, Public) | (Inherited, Inherited) => true,
620         (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r),
621         _ => false,
622     }
623 }
624 
eq_fn_decl(l: &FnDecl, r: &FnDecl) -> bool625 pub fn eq_fn_decl(l: &FnDecl, r: &FnDecl) -> bool {
626     eq_fn_ret_ty(&l.output, &r.output)
627         && over(&l.inputs, &r.inputs, |l, r| {
628             l.is_placeholder == r.is_placeholder
629                 && eq_pat(&l.pat, &r.pat)
630                 && eq_ty(&l.ty, &r.ty)
631                 && over(&l.attrs, &r.attrs, eq_attr)
632         })
633 }
634 
eq_closure_binder(l: &ClosureBinder, r: &ClosureBinder) -> bool635 pub fn eq_closure_binder(l: &ClosureBinder, r: &ClosureBinder) -> bool {
636     match (l, r) {
637         (ClosureBinder::NotPresent, ClosureBinder::NotPresent) => true,
638         (ClosureBinder::For { generic_params: lp, .. }, ClosureBinder::For { generic_params: rp, .. }) => {
639             lp.len() == rp.len() && std::iter::zip(lp.iter(), rp.iter()).all(|(l, r)| eq_generic_param(l, r))
640         },
641         _ => false,
642     }
643 }
644 
eq_fn_ret_ty(l: &FnRetTy, r: &FnRetTy) -> bool645 pub fn eq_fn_ret_ty(l: &FnRetTy, r: &FnRetTy) -> bool {
646     match (l, r) {
647         (FnRetTy::Default(_), FnRetTy::Default(_)) => true,
648         (FnRetTy::Ty(l), FnRetTy::Ty(r)) => eq_ty(l, r),
649         _ => false,
650     }
651 }
652 
eq_ty(l: &Ty, r: &Ty) -> bool653 pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
654     use TyKind::*;
655     match (&l.kind, &r.kind) {
656         (Paren(l), _) => eq_ty(l, r),
657         (_, Paren(r)) => eq_ty(l, r),
658         (Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err, Err) | (CVarArgs, CVarArgs) => true,
659         (Slice(l), Slice(r)) => eq_ty(l, r),
660         (Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
661         (Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),
662         (Ref(ll, l), Ref(rl, r)) => {
663             both(ll, rl, |l, r| eq_id(l.ident, r.ident)) && l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty)
664         },
665         (BareFn(l), BareFn(r)) => {
666             l.unsafety == r.unsafety
667                 && eq_ext(&l.ext, &r.ext)
668                 && over(&l.generic_params, &r.generic_params, eq_generic_param)
669                 && eq_fn_decl(&l.decl, &r.decl)
670         },
671         (Tup(l), Tup(r)) => over(l, r, |l, r| eq_ty(l, r)),
672         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, eq_qself) && eq_path(lp, rp),
673         (TraitObject(lg, ls), TraitObject(rg, rs)) => ls == rs && over(lg, rg, eq_generic_bound),
674         (ImplTrait(_, lg), ImplTrait(_, rg)) => over(lg, rg, eq_generic_bound),
675         (Typeof(l), Typeof(r)) => eq_expr(&l.value, &r.value),
676         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
677         _ => false,
678     }
679 }
680 
eq_ext(l: &Extern, r: &Extern) -> bool681 pub fn eq_ext(l: &Extern, r: &Extern) -> bool {
682     use Extern::*;
683     match (l, r) {
684         (None, None) | (Implicit(_), Implicit(_)) => true,
685         (Explicit(l, _), Explicit(r, _)) => eq_str_lit(l, r),
686         _ => false,
687     }
688 }
689 
eq_str_lit(l: &StrLit, r: &StrLit) -> bool690 pub fn eq_str_lit(l: &StrLit, r: &StrLit) -> bool {
691     l.style == r.style && l.symbol == r.symbol && l.suffix == r.suffix
692 }
693 
eq_poly_ref_trait(l: &PolyTraitRef, r: &PolyTraitRef) -> bool694 pub fn eq_poly_ref_trait(l: &PolyTraitRef, r: &PolyTraitRef) -> bool {
695     eq_path(&l.trait_ref.path, &r.trait_ref.path)
696         && over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
697             eq_generic_param(l, r)
698         })
699 }
700 
eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool701 pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
702     use GenericParamKind::*;
703     l.is_placeholder == r.is_placeholder
704         && eq_id(l.ident, r.ident)
705         && over(&l.bounds, &r.bounds, eq_generic_bound)
706         && match (&l.kind, &r.kind) {
707             (Lifetime, Lifetime) => true,
708             (Type { default: l }, Type { default: r }) => both(l, r, |l, r| eq_ty(l, r)),
709             (
710                 Const {
711                     ty: lt,
712                     kw_span: _,
713                     default: ld,
714                 },
715                 Const {
716                     ty: rt,
717                     kw_span: _,
718                     default: rd,
719                 },
720             ) => eq_ty(lt, rt) && both(ld, rd, eq_anon_const),
721             _ => false,
722         }
723         && over(&l.attrs, &r.attrs, eq_attr)
724 }
725 
eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool726 pub fn eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool {
727     use GenericBound::*;
728     match (l, r) {
729         (Trait(ptr1, tbm1), Trait(ptr2, tbm2)) => tbm1 == tbm2 && eq_poly_ref_trait(ptr1, ptr2),
730         (Outlives(l), Outlives(r)) => eq_id(l.ident, r.ident),
731         _ => false,
732     }
733 }
734 
eq_term(l: &Term, r: &Term) -> bool735 fn eq_term(l: &Term, r: &Term) -> bool {
736     match (l, r) {
737         (Term::Ty(l), Term::Ty(r)) => eq_ty(l, r),
738         (Term::Const(l), Term::Const(r)) => eq_anon_const(l, r),
739         _ => false,
740     }
741 }
742 
eq_assoc_constraint(l: &AssocConstraint, r: &AssocConstraint) -> bool743 pub fn eq_assoc_constraint(l: &AssocConstraint, r: &AssocConstraint) -> bool {
744     use AssocConstraintKind::*;
745     eq_id(l.ident, r.ident)
746         && match (&l.kind, &r.kind) {
747             (Equality { term: l }, Equality { term: r }) => eq_term(l, r),
748             (Bound { bounds: l }, Bound { bounds: r }) => over(l, r, eq_generic_bound),
749             _ => false,
750         }
751 }
752 
eq_mac_call(l: &MacCall, r: &MacCall) -> bool753 pub fn eq_mac_call(l: &MacCall, r: &MacCall) -> bool {
754     eq_path(&l.path, &r.path) && eq_delim_args(&l.args, &r.args)
755 }
756 
eq_attr(l: &Attribute, r: &Attribute) -> bool757 pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool {
758     use AttrKind::*;
759     l.style == r.style
760         && match (&l.kind, &r.kind) {
761             (DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2,
762             (Normal(l), Normal(r)) => eq_path(&l.item.path, &r.item.path) && eq_attr_args(&l.item.args, &r.item.args),
763             _ => false,
764         }
765 }
766 
eq_attr_args(l: &AttrArgs, r: &AttrArgs) -> bool767 pub fn eq_attr_args(l: &AttrArgs, r: &AttrArgs) -> bool {
768     use AttrArgs::*;
769     match (l, r) {
770         (Empty, Empty) => true,
771         (Delimited(la), Delimited(ra)) => eq_delim_args(la, ra),
772         (Eq(_, AttrArgsEq::Ast(le)), Eq(_, AttrArgsEq::Ast(re))) => eq_expr(le, re),
773         (Eq(_, AttrArgsEq::Hir(ll)), Eq(_, AttrArgsEq::Hir(rl))) => ll.kind == rl.kind,
774         _ => false,
775     }
776 }
777 
eq_delim_args(l: &DelimArgs, r: &DelimArgs) -> bool778 pub fn eq_delim_args(l: &DelimArgs, r: &DelimArgs) -> bool {
779     l.delim == r.delim && l.tokens.eq_unspanned(&r.tokens)
780 }
781