1#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
6
7use crate::{both, over};
8use rustc_ast::ptr::P;
9use rustc_ast::{self as ast, *};
10use rustc_span::symbol::Ident;
11use std::mem;
12
13pub mod ident_iter;
14pub use ident_iter::IdentIter;
15
16pub 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
24pub 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
29pub fn eq_id(l: Ident, r: Ident) -> bool {
30 l.name == r.name
31}
32
33pub 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 (Expr(l), Expr(r)) => eq_expr(l, r),
40 (Ident(b1, i1, s1), Ident(b2, i2, s2)) => {
41 b1 == b2 && eq_id(*i1, *i2) && both(s1.as_deref(), s2.as_deref(), eq_pat)
42 },
43 (Range(lf, lt, le), Range(rf, rt, re)) => {
44 eq_expr_opt(lf.as_ref(), rf.as_ref())
45 && eq_expr_opt(lt.as_ref(), rt.as_ref())
46 && eq_range_end(&le.node, &re.node)
47 },
48 (Box(l), Box(r))
49 | (Ref(l, Mutability::Not), Ref(r, Mutability::Not))
50 | (Ref(l, Mutability::Mut), Ref(r, Mutability::Mut)) => eq_pat(l, r),
51 (Tuple(l), Tuple(r)) | (Slice(l), Slice(r)) => over(l, r, |l, r| eq_pat(l, r)),
52 (Path(lq, lp), Path(rq, rp)) => both(lq.as_ref(), rq.as_ref(), eq_qself) && eq_path(lp, rp),
53 (TupleStruct(lqself, lp, lfs), TupleStruct(rqself, rp, rfs)) => {
54 eq_maybe_qself(lqself.as_ref(), rqself.as_ref()) && eq_path(lp, rp) && over(lfs, rfs, |l, r| eq_pat(l, r))
55 },
56 (Struct(lqself, lp, lfs, lr), Struct(rqself, rp, rfs, rr)) => {
57 lr == rr
58 && eq_maybe_qself(lqself.as_ref(), rqself.as_ref())
59 && eq_path(lp, rp)
60 && unordered_over(lfs, rfs, eq_field_pat)
61 },
62 (Or(ls), Or(rs)) => unordered_over(ls, rs, |l, r| eq_pat(l, r)),
63 (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
64 _ => false,
65 }
66}
67
68pub fn eq_range_end(l: &RangeEnd, r: &RangeEnd) -> bool {
69 match (l, r) {
70 (RangeEnd::Excluded, RangeEnd::Excluded) => true,
71 (RangeEnd::Included(l), RangeEnd::Included(r)) => {
72 matches!(l, RangeSyntax::DotDotEq) == matches!(r, RangeSyntax::DotDotEq)
73 },
74 _ => false,
75 }
76}
77
78pub fn eq_field_pat(l: &PatField, r: &PatField) -> bool {
79 l.is_placeholder == r.is_placeholder
80 && eq_id(l.ident, r.ident)
81 && eq_pat(&l.pat, &r.pat)
82 && over(&l.attrs, &r.attrs, eq_attr)
83}
84
85pub fn eq_qself(l: &P<QSelf>, r: &P<QSelf>) -> bool {
86 l.position == r.position && eq_ty(&l.ty, &r.ty)
87}
88
89pub fn eq_maybe_qself(l: Option<&P<QSelf>>, r: Option<&P<QSelf>>) -> bool {
90 match (l, r) {
91 (Some(l), Some(r)) => eq_qself(l, r),
92 (None, None) => true,
93 _ => false,
94 }
95}
96
97pub fn eq_path(l: &Path, r: &Path) -> bool {
98 over(&l.segments, &r.segments, eq_path_seg)
99}
100
101pub fn eq_path_seg(l: &PathSegment, r: &PathSegment) -> bool {
102 eq_id(l.ident, r.ident) && both(l.args.as_ref(), r.args.as_ref(), |l, r| eq_generic_args(l, r))
103}
104
105pub fn eq_generic_args(l: &GenericArgs, r: &GenericArgs) -> bool {
106 match (l, r) {
107 (AngleBracketed(l), AngleBracketed(r)) => over(&l.args, &r.args, eq_angle_arg),
108 (Parenthesized(l), Parenthesized(r)) => {
109 over(&l.inputs, &r.inputs, |l, r| eq_ty(l, r)) && eq_fn_ret_ty(&l.output, &r.output)
110 },
111 _ => false,
112 }
113}
114
115pub fn eq_angle_arg(l: &AngleBracketedArg, r: &AngleBracketedArg) -> bool {
116 match (l, r) {
117 (AngleBracketedArg::Arg(l), AngleBracketedArg::Arg(r)) => eq_generic_arg(l, r),
118 (AngleBracketedArg::Constraint(l), AngleBracketedArg::Constraint(r)) => eq_assoc_item_constraint(l, r),
119 _ => false,
120 }
121}
122
123pub fn eq_generic_arg(l: &GenericArg, r: &GenericArg) -> bool {
124 match (l, r) {
125 (GenericArg::Lifetime(l), GenericArg::Lifetime(r)) => eq_id(l.ident, r.ident),
126 (GenericArg::Type(l), GenericArg::Type(r)) => eq_ty(l, r),
127 (GenericArg::Const(l), GenericArg::Const(r)) => eq_expr(&l.value, &r.value),
128 _ => false,
129 }
130}
131
132pub fn eq_expr_opt(l: Option<&P<Expr>>, r: Option<&P<Expr>>) -> bool {
133 both(l, r, |l, r| eq_expr(l, r))
134}
135
136pub fn eq_struct_rest(l: &StructRest, r: &StructRest) -> bool {
137 match (l, r) {
138 (StructRest::Base(lb), StructRest::Base(rb)) => eq_expr(lb, rb),
139 (StructRest::Rest(_), StructRest::Rest(_)) | (StructRest::None, StructRest::None) => true,
140 _ => false,
141 }
142}
143
144#[allow(clippy::too_many_lines)] pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
146 use ExprKind::*;
147 if !over(&l.attrs, &r.attrs, eq_attr) {
148 return false;
149 }
150 match (&l.kind, &r.kind) {
151 (Paren(l), _) => eq_expr(l, r),
152 (_, Paren(r)) => eq_expr(l, r),
153 (Err(_), Err(_)) => true,
154 (Dummy, _) | (_, Dummy) => unreachable!("comparing `ExprKind::Dummy`"),
155 (Try(l), Try(r)) | (Await(l, _), Await(r, _)) => eq_expr(l, r),
156 (Array(l), Array(r)) => over(l, r, |l, r| eq_expr(l, r)),
157 (Tup(l), Tup(r)) => over(l, r, |l, r| eq_expr(l, r)),
158 (Repeat(le, ls), Repeat(re, rs)) => eq_expr(le, re) && eq_expr(&ls.value, &rs.value),
159 (Call(lc, la), Call(rc, ra)) => eq_expr(lc, rc) && over(la, ra, |l, r| eq_expr(l, r)),
160 (
161 MethodCall(box ast::MethodCall {
162 seg: ls,
163 receiver: lr,
164 args: la,
165 ..
166 }),
167 MethodCall(box ast::MethodCall {
168 seg: rs,
169 receiver: rr,
170 args: ra,
171 ..
172 }),
173 ) => eq_path_seg(ls, rs) && eq_expr(lr, rr) && over(la, ra, |l, r| eq_expr(l, r)),
174 (Binary(lo, ll, lr), Binary(ro, rl, rr)) => lo.node == ro.node && eq_expr(ll, rl) && eq_expr(lr, rr),
175 (Unary(lo, l), Unary(ro, r)) => mem::discriminant(lo) == mem::discriminant(ro) && eq_expr(l, r),
176 (Lit(l), Lit(r)) => l == r,
177 (Cast(l, lt), Cast(r, rt)) | (Type(l, lt), Type(r, rt)) => eq_expr(l, r) && eq_ty(lt, rt),
178 (Let(lp, le, _, _), Let(rp, re, _, _)) => eq_pat(lp, rp) && eq_expr(le, re),
179 (If(lc, lt, le), If(rc, rt, re)) => {
180 eq_expr(lc, rc) && eq_block(lt, rt) && eq_expr_opt(le.as_ref(), re.as_ref())
181 },
182 (While(lc, lt, ll), While(rc, rt, rl)) => {
183 eq_label(ll.as_ref(), rl.as_ref()) && eq_expr(lc, rc) && eq_block(lt, rt)
184 },
185 (
186 ForLoop {
187 pat: lp,
188 iter: li,
189 body: lt,
190 label: ll,
191 kind: lk,
192 },
193 ForLoop {
194 pat: rp,
195 iter: ri,
196 body: rt,
197 label: rl,
198 kind: rk,
199 },
200 ) => eq_label(ll.as_ref(), rl.as_ref()) && eq_pat(lp, rp) && eq_expr(li, ri) && eq_block(lt, rt) && lk == rk,
201 (Loop(lt, ll, _), Loop(rt, rl, _)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_block(lt, rt),
202 (Block(lb, ll), Block(rb, rl)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_block(lb, rb),
203 (TryBlock(l), TryBlock(r)) => eq_block(l, r),
204 (Yield(l), Yield(r)) | (Ret(l), Ret(r)) => eq_expr_opt(l.as_ref(), r.as_ref()),
205 (Break(ll, le), Break(rl, re)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_expr_opt(le.as_ref(), re.as_ref()),
206 (Continue(ll), Continue(rl)) => eq_label(ll.as_ref(), rl.as_ref()),
207 (Assign(l1, l2, _), Assign(r1, r2, _)) | (Index(l1, l2, _), Index(r1, r2, _)) => {
208 eq_expr(l1, r1) && eq_expr(l2, r2)
209 },
210 (AssignOp(lo, lp, lv), AssignOp(ro, rp, rv)) => lo.node == ro.node && eq_expr(lp, rp) && eq_expr(lv, rv),
211 (Field(lp, lf), Field(rp, rf)) => eq_id(*lf, *rf) && eq_expr(lp, rp),
212 (Match(ls, la, lkind), Match(rs, ra, rkind)) => (lkind == rkind) && eq_expr(ls, rs) && over(la, ra, eq_arm),
213 (
214 Closure(box ast::Closure {
215 binder: lb,
216 capture_clause: lc,
217 coroutine_kind: la,
218 movability: lm,
219 fn_decl: lf,
220 body: le,
221 ..
222 }),
223 Closure(box ast::Closure {
224 binder: rb,
225 capture_clause: rc,
226 coroutine_kind: ra,
227 movability: rm,
228 fn_decl: rf,
229 body: re,
230 ..
231 }),
232 ) => {
233 eq_closure_binder(lb, rb)
234 && lc == rc
235 && eq_coroutine_kind(*la, *ra)
236 && lm == rm
237 && eq_fn_decl(lf, rf)
238 && eq_expr(le, re)
239 },
240 (Gen(lc, lb, lk, _), Gen(rc, rb, rk, _)) => lc == rc && eq_block(lb, rb) && lk == rk,
241 (Range(lf, lt, ll), Range(rf, rt, rl)) => {
242 ll == rl && eq_expr_opt(lf.as_ref(), rf.as_ref()) && eq_expr_opt(lt.as_ref(), rt.as_ref())
243 },
244 (AddrOf(lbk, lm, le), AddrOf(rbk, rm, re)) => lbk == rbk && lm == rm && eq_expr(le, re),
245 (Path(lq, lp), Path(rq, rp)) => both(lq.as_ref(), rq.as_ref(), eq_qself) && eq_path(lp, rp),
246 (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
247 (Struct(lse), Struct(rse)) => {
248 eq_maybe_qself(lse.qself.as_ref(), rse.qself.as_ref())
249 && eq_path(&lse.path, &rse.path)
250 && eq_struct_rest(&lse.rest, &rse.rest)
251 && unordered_over(&lse.fields, &rse.fields, eq_field)
252 },
253 _ => false,
254 }
255}
256
257fn eq_coroutine_kind(a: Option<CoroutineKind>, b: Option<CoroutineKind>) -> bool {
258 matches!(
259 (a, b),
260 (Some(CoroutineKind::Async { .. }), Some(CoroutineKind::Async { .. }))
261 | (Some(CoroutineKind::Gen { .. }), Some(CoroutineKind::Gen { .. }))
262 | (
263 Some(CoroutineKind::AsyncGen { .. }),
264 Some(CoroutineKind::AsyncGen { .. })
265 )
266 | (None, None)
267 )
268}
269
270pub fn eq_field(l: &ExprField, r: &ExprField) -> bool {
271 l.is_placeholder == r.is_placeholder
272 && eq_id(l.ident, r.ident)
273 && eq_expr(&l.expr, &r.expr)
274 && over(&l.attrs, &r.attrs, eq_attr)
275}
276
277pub fn eq_arm(l: &Arm, r: &Arm) -> bool {
278 l.is_placeholder == r.is_placeholder
279 && eq_pat(&l.pat, &r.pat)
280 && eq_expr_opt(l.body.as_ref(), r.body.as_ref())
281 && eq_expr_opt(l.guard.as_ref(), r.guard.as_ref())
282 && over(&l.attrs, &r.attrs, eq_attr)
283}
284
285pub fn eq_label(l: Option<&Label>, r: Option<&Label>) -> bool {
286 both(l, r, |l, r| eq_id(l.ident, r.ident))
287}
288
289pub fn eq_block(l: &Block, r: &Block) -> bool {
290 l.rules == r.rules && over(&l.stmts, &r.stmts, eq_stmt)
291}
292
293pub fn eq_stmt(l: &Stmt, r: &Stmt) -> bool {
294 use StmtKind::*;
295 match (&l.kind, &r.kind) {
296 (Let(l), Let(r)) => {
297 eq_pat(&l.pat, &r.pat)
298 && both(l.ty.as_ref(), r.ty.as_ref(), |l, r| eq_ty(l, r))
299 && eq_local_kind(&l.kind, &r.kind)
300 && over(&l.attrs, &r.attrs, eq_attr)
301 },
302 (Item(l), Item(r)) => eq_item(l, r, eq_item_kind),
303 (Expr(l), Expr(r)) | (Semi(l), Semi(r)) => eq_expr(l, r),
304 (Empty, Empty) => true,
305 (MacCall(l), MacCall(r)) => {
306 l.style == r.style && eq_mac_call(&l.mac, &r.mac) && over(&l.attrs, &r.attrs, eq_attr)
307 },
308 _ => false,
309 }
310}
311
312pub fn eq_local_kind(l: &LocalKind, r: &LocalKind) -> bool {
313 use LocalKind::*;
314 match (l, r) {
315 (Decl, Decl) => true,
316 (Init(l), Init(r)) => eq_expr(l, r),
317 (InitElse(li, le), InitElse(ri, re)) => eq_expr(li, ri) && eq_block(le, re),
318 _ => false,
319 }
320}
321
322pub fn eq_item<K>(l: &Item<K>, r: &Item<K>, mut eq_kind: impl FnMut(&K, &K) -> bool) -> bool {
323 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)
324}
325
326#[expect(clippy::similar_names, clippy::too_many_lines)] pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
328 use ItemKind::*;
329 match (l, r) {
330 (ExternCrate(l), ExternCrate(r)) => l == r,
331 (Use(l), Use(r)) => eq_use_tree(l, r),
332 (
333 Static(box StaticItem {
334 ty: lt,
335 mutability: lm,
336 expr: le,
337 safety: ls,
338 }),
339 Static(box StaticItem {
340 ty: rt,
341 mutability: rm,
342 expr: re,
343 safety: rs,
344 }),
345 ) => lm == rm && ls == rs && eq_ty(lt, rt) && eq_expr_opt(le.as_ref(), re.as_ref()),
346 (
347 Const(box ConstItem {
348 defaultness: ld,
349 generics: lg,
350 ty: lt,
351 expr: le,
352 }),
353 Const(box ConstItem {
354 defaultness: rd,
355 generics: rg,
356 ty: rt,
357 expr: re,
358 }),
359 ) => eq_defaultness(*ld, *rd) && eq_generics(lg, rg) && eq_ty(lt, rt) && eq_expr_opt(le.as_ref(), re.as_ref()),
360 (
361 Fn(box ast::Fn {
362 defaultness: ld,
363 sig: lf,
364 generics: lg,
365 contract: lc,
366 body: lb,
367 define_opaque: _,
368 }),
369 Fn(box ast::Fn {
370 defaultness: rd,
371 sig: rf,
372 generics: rg,
373 contract: rc,
374 body: rb,
375 define_opaque: _,
376 }),
377 ) => {
378 eq_defaultness(*ld, *rd)
379 && eq_fn_sig(lf, rf)
380 && eq_generics(lg, rg)
381 && eq_opt_fn_contract(lc, rc)
382 && both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
383 },
384 (Mod(lu, lmk), Mod(ru, rmk)) => {
385 lu == ru
386 && match (lmk, rmk) {
387 (ModKind::Loaded(litems, linline, _, _), ModKind::Loaded(ritems, rinline, _, _)) => {
388 linline == rinline && over(litems, ritems, |l, r| eq_item(l, r, eq_item_kind))
389 },
390 (ModKind::Unloaded, ModKind::Unloaded) => true,
391 _ => false,
392 }
393 },
394 (ForeignMod(l), ForeignMod(r)) => {
395 both(l.abi.as_ref(), r.abi.as_ref(), eq_str_lit)
396 && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
397 },
398 (
399 TyAlias(box ast::TyAlias {
400 defaultness: ld,
401 generics: lg,
402 bounds: lb,
403 ty: lt,
404 ..
405 }),
406 TyAlias(box ast::TyAlias {
407 defaultness: rd,
408 generics: rg,
409 bounds: rb,
410 ty: rt,
411 ..
412 }),
413 ) => {
414 eq_defaultness(*ld, *rd)
415 && eq_generics(lg, rg)
416 && over(lb, rb, eq_generic_bound)
417 && both(lt.as_ref(), rt.as_ref(), |l, r| eq_ty(l, r))
418 },
419 (Enum(le, lg), Enum(re, rg)) => over(&le.variants, &re.variants, eq_variant) && eq_generics(lg, rg),
420 (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => {
421 eq_variant_data(lv, rv) && eq_generics(lg, rg)
422 },
423 (
424 Trait(box ast::Trait {
425 is_auto: la,
426 safety: lu,
427 generics: lg,
428 bounds: lb,
429 items: li,
430 }),
431 Trait(box ast::Trait {
432 is_auto: ra,
433 safety: ru,
434 generics: rg,
435 bounds: rb,
436 items: ri,
437 }),
438 ) => {
439 la == ra
440 && matches!(lu, Safety::Default) == matches!(ru, Safety::Default)
441 && eq_generics(lg, rg)
442 && over(lb, rb, eq_generic_bound)
443 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
444 },
445 (TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, eq_generic_bound),
446 (
447 Impl(box ast::Impl {
448 safety: lu,
449 polarity: lp,
450 defaultness: ld,
451 constness: lc,
452 generics: lg,
453 of_trait: lot,
454 self_ty: lst,
455 items: li,
456 }),
457 Impl(box ast::Impl {
458 safety: ru,
459 polarity: rp,
460 defaultness: rd,
461 constness: rc,
462 generics: rg,
463 of_trait: rot,
464 self_ty: rst,
465 items: ri,
466 }),
467 ) => {
468 matches!(lu, Safety::Default) == matches!(ru, Safety::Default)
469 && matches!(lp, ImplPolarity::Positive) == matches!(rp, ImplPolarity::Positive)
470 && eq_defaultness(*ld, *rd)
471 && matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
472 && eq_generics(lg, rg)
473 && both(lot.as_ref(), rot.as_ref(), |l, r| eq_path(&l.path, &r.path))
474 && eq_ty(lst, rst)
475 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
476 },
477 (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
478 (MacroDef(l), MacroDef(r)) => l.macro_rules == r.macro_rules && eq_delim_args(&l.body, &r.body),
479 _ => false,
480 }
481}
482
483pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
484 use ForeignItemKind::*;
485 match (l, r) {
486 (
487 Static(box StaticItem {
488 ty: lt,
489 mutability: lm,
490 expr: le,
491 safety: ls,
492 }),
493 Static(box StaticItem {
494 ty: rt,
495 mutability: rm,
496 expr: re,
497 safety: rs,
498 }),
499 ) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le.as_ref(), re.as_ref()) && ls == rs,
500 (
501 Fn(box ast::Fn {
502 defaultness: ld,
503 sig: lf,
504 generics: lg,
505 contract: lc,
506 body: lb,
507 define_opaque: _,
508 }),
509 Fn(box ast::Fn {
510 defaultness: rd,
511 sig: rf,
512 generics: rg,
513 contract: rc,
514 body: rb,
515 define_opaque: _,
516 }),
517 ) => {
518 eq_defaultness(*ld, *rd)
519 && eq_fn_sig(lf, rf)
520 && eq_generics(lg, rg)
521 && eq_opt_fn_contract(lc, rc)
522 && both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
523 },
524 (
525 TyAlias(box ast::TyAlias {
526 defaultness: ld,
527 generics: lg,
528 bounds: lb,
529 ty: lt,
530 ..
531 }),
532 TyAlias(box ast::TyAlias {
533 defaultness: rd,
534 generics: rg,
535 bounds: rb,
536 ty: rt,
537 ..
538 }),
539 ) => {
540 eq_defaultness(*ld, *rd)
541 && eq_generics(lg, rg)
542 && over(lb, rb, eq_generic_bound)
543 && both(lt.as_ref(), rt.as_ref(), |l, r| eq_ty(l, r))
544 },
545 (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
546 _ => false,
547 }
548}
549
550pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
551 use AssocItemKind::*;
552 match (l, r) {
553 (
554 Const(box ConstItem {
555 defaultness: ld,
556 generics: lg,
557 ty: lt,
558 expr: le,
559 }),
560 Const(box ConstItem {
561 defaultness: rd,
562 generics: rg,
563 ty: rt,
564 expr: re,
565 }),
566 ) => eq_defaultness(*ld, *rd) && eq_generics(lg, rg) && eq_ty(lt, rt) && eq_expr_opt(le.as_ref(), re.as_ref()),
567 (
568 Fn(box ast::Fn {
569 defaultness: ld,
570 sig: lf,
571 generics: lg,
572 contract: lc,
573 body: lb,
574 define_opaque: _,
575 }),
576 Fn(box ast::Fn {
577 defaultness: rd,
578 sig: rf,
579 generics: rg,
580 contract: rc,
581 body: rb,
582 define_opaque: _,
583 }),
584 ) => {
585 eq_defaultness(*ld, *rd)
586 && eq_fn_sig(lf, rf)
587 && eq_generics(lg, rg)
588 && eq_opt_fn_contract(lc, rc)
589 && both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
590 },
591 (
592 Type(box TyAlias {
593 defaultness: ld,
594 generics: lg,
595 bounds: lb,
596 ty: lt,
597 ..
598 }),
599 Type(box TyAlias {
600 defaultness: rd,
601 generics: rg,
602 bounds: rb,
603 ty: rt,
604 ..
605 }),
606 ) => {
607 eq_defaultness(*ld, *rd)
608 && eq_generics(lg, rg)
609 && over(lb, rb, eq_generic_bound)
610 && both(lt.as_ref(), rt.as_ref(), |l, r| eq_ty(l, r))
611 },
612 (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
613 _ => false,
614 }
615}
616
617pub fn eq_variant(l: &Variant, r: &Variant) -> bool {
618 l.is_placeholder == r.is_placeholder
619 && over(&l.attrs, &r.attrs, eq_attr)
620 && eq_vis(&l.vis, &r.vis)
621 && eq_id(l.ident, r.ident)
622 && eq_variant_data(&l.data, &r.data)
623 && both(l.disr_expr.as_ref(), r.disr_expr.as_ref(), |l, r| {
624 eq_expr(&l.value, &r.value)
625 })
626}
627
628pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
629 use VariantData::*;
630 match (l, r) {
631 (Unit(_), Unit(_)) => true,
632 (Struct { fields: l, .. }, Struct { fields: r, .. }) | (Tuple(l, _), Tuple(r, _)) => {
633 over(l, r, eq_struct_field)
634 },
635 _ => false,
636 }
637}
638
639pub fn eq_struct_field(l: &FieldDef, r: &FieldDef) -> bool {
640 l.is_placeholder == r.is_placeholder
641 && over(&l.attrs, &r.attrs, eq_attr)
642 && eq_vis(&l.vis, &r.vis)
643 && both(l.ident.as_ref(), r.ident.as_ref(), |l, r| eq_id(*l, *r))
644 && eq_ty(&l.ty, &r.ty)
645}
646
647pub fn eq_fn_sig(l: &FnSig, r: &FnSig) -> bool {
648 eq_fn_decl(&l.decl, &r.decl) && eq_fn_header(&l.header, &r.header)
649}
650
651fn eq_opt_coroutine_kind(l: Option<CoroutineKind>, r: Option<CoroutineKind>) -> bool {
652 matches!(
653 (l, r),
654 (Some(CoroutineKind::Async { .. }), Some(CoroutineKind::Async { .. }))
655 | (Some(CoroutineKind::Gen { .. }), Some(CoroutineKind::Gen { .. }))
656 | (
657 Some(CoroutineKind::AsyncGen { .. }),
658 Some(CoroutineKind::AsyncGen { .. })
659 )
660 | (None, None)
661 )
662}
663
664pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool {
665 matches!(l.safety, Safety::Default) == matches!(r.safety, Safety::Default)
666 && eq_opt_coroutine_kind(l.coroutine_kind, r.coroutine_kind)
667 && matches!(l.constness, Const::No) == matches!(r.constness, Const::No)
668 && eq_ext(&l.ext, &r.ext)
669}
670
671#[expect(clippy::ref_option, reason = "This is the type how it is stored in the AST")]
672pub fn eq_opt_fn_contract(l: &Option<P<FnContract>>, r: &Option<P<FnContract>>) -> bool {
673 match (l, r) {
674 (Some(l), Some(r)) => {
675 eq_expr_opt(l.requires.as_ref(), r.requires.as_ref()) && eq_expr_opt(l.ensures.as_ref(), r.ensures.as_ref())
676 },
677 (None, None) => true,
678 (Some(_), None) | (None, Some(_)) => false,
679 }
680}
681
682pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
683 over(&l.params, &r.params, eq_generic_param)
684 && over(&l.where_clause.predicates, &r.where_clause.predicates, |l, r| {
685 eq_where_predicate(l, r)
686 })
687}
688
689pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
690 use WherePredicateKind::*;
691 over(&l.attrs, &r.attrs, eq_attr)
692 && match (&l.kind, &r.kind) {
693 (BoundPredicate(l), BoundPredicate(r)) => {
694 over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
695 eq_generic_param(l, r)
696 }) && eq_ty(&l.bounded_ty, &r.bounded_ty)
697 && over(&l.bounds, &r.bounds, eq_generic_bound)
698 },
699 (RegionPredicate(l), RegionPredicate(r)) => {
700 eq_id(l.lifetime.ident, r.lifetime.ident) && over(&l.bounds, &r.bounds, eq_generic_bound)
701 },
702 (EqPredicate(l), EqPredicate(r)) => eq_ty(&l.lhs_ty, &r.lhs_ty) && eq_ty(&l.rhs_ty, &r.rhs_ty),
703 _ => false,
704 }
705}
706
707pub fn eq_use_tree(l: &UseTree, r: &UseTree) -> bool {
708 eq_path(&l.prefix, &r.prefix) && eq_use_tree_kind(&l.kind, &r.kind)
709}
710
711pub fn eq_anon_const(l: &AnonConst, r: &AnonConst) -> bool {
712 eq_expr(&l.value, &r.value)
713}
714
715pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
716 use UseTreeKind::*;
717 match (l, r) {
718 (Glob, Glob) => true,
719 (Simple(l), Simple(r)) => both(l.as_ref(), r.as_ref(), |l, r| eq_id(*l, *r)),
720 (Nested { items: l, .. }, Nested { items: r, .. }) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
721 _ => false,
722 }
723}
724
725pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
726 matches!(
727 (l, r),
728 (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_))
729 )
730}
731
732pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
733 use VisibilityKind::*;
734 match (&l.kind, &r.kind) {
735 (Public, Public) | (Inherited, Inherited) => true,
736 (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r),
737 _ => false,
738 }
739}
740
741pub fn eq_fn_decl(l: &FnDecl, r: &FnDecl) -> bool {
742 eq_fn_ret_ty(&l.output, &r.output)
743 && over(&l.inputs, &r.inputs, |l, r| {
744 l.is_placeholder == r.is_placeholder
745 && eq_pat(&l.pat, &r.pat)
746 && eq_ty(&l.ty, &r.ty)
747 && over(&l.attrs, &r.attrs, eq_attr)
748 })
749}
750
751pub fn eq_closure_binder(l: &ClosureBinder, r: &ClosureBinder) -> bool {
752 match (l, r) {
753 (ClosureBinder::NotPresent, ClosureBinder::NotPresent) => true,
754 (ClosureBinder::For { generic_params: lp, .. }, ClosureBinder::For { generic_params: rp, .. }) => {
755 lp.len() == rp.len() && std::iter::zip(lp.iter(), rp.iter()).all(|(l, r)| eq_generic_param(l, r))
756 },
757 _ => false,
758 }
759}
760
761pub fn eq_fn_ret_ty(l: &FnRetTy, r: &FnRetTy) -> bool {
762 match (l, r) {
763 (FnRetTy::Default(_), FnRetTy::Default(_)) => true,
764 (FnRetTy::Ty(l), FnRetTy::Ty(r)) => eq_ty(l, r),
765 _ => false,
766 }
767}
768
769pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
770 use TyKind::*;
771 match (&l.kind, &r.kind) {
772 (Paren(l), _) => eq_ty(l, r),
773 (_, Paren(r)) => eq_ty(l, r),
774 (Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err(_), Err(_)) | (CVarArgs, CVarArgs) => {
775 true
776 },
777 (Slice(l), Slice(r)) => eq_ty(l, r),
778 (Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
779 (Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),
780 (Ref(ll, l), Ref(rl, r)) => {
781 both(ll.as_ref(), rl.as_ref(), |l, r| eq_id(l.ident, r.ident)) && l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty)
782 },
783 (PinnedRef(ll, l), PinnedRef(rl, r)) => {
784 both(ll.as_ref(), rl.as_ref(), |l, r| eq_id(l.ident, r.ident)) && l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty)
785 },
786 (BareFn(l), BareFn(r)) => {
787 l.safety == r.safety
788 && eq_ext(&l.ext, &r.ext)
789 && over(&l.generic_params, &r.generic_params, eq_generic_param)
790 && eq_fn_decl(&l.decl, &r.decl)
791 },
792 (Tup(l), Tup(r)) => over(l, r, |l, r| eq_ty(l, r)),
793 (Path(lq, lp), Path(rq, rp)) => both(lq.as_ref(), rq.as_ref(), eq_qself) && eq_path(lp, rp),
794 (TraitObject(lg, ls), TraitObject(rg, rs)) => ls == rs && over(lg, rg, eq_generic_bound),
795 (ImplTrait(_, lg), ImplTrait(_, rg)) => over(lg, rg, eq_generic_bound),
796 (Typeof(l), Typeof(r)) => eq_expr(&l.value, &r.value),
797 (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
798 _ => false,
799 }
800}
801
802pub fn eq_ext(l: &Extern, r: &Extern) -> bool {
803 use Extern::*;
804 match (l, r) {
805 (None, None) | (Implicit(_), Implicit(_)) => true,
806 (Explicit(l, _), Explicit(r, _)) => eq_str_lit(l, r),
807 _ => false,
808 }
809}
810
811pub fn eq_str_lit(l: &StrLit, r: &StrLit) -> bool {
812 l.style == r.style && l.symbol == r.symbol && l.suffix == r.suffix
813}
814
815pub fn eq_poly_ref_trait(l: &PolyTraitRef, r: &PolyTraitRef) -> bool {
816 l.modifiers == r.modifiers
817 && eq_path(&l.trait_ref.path, &r.trait_ref.path)
818 && over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
819 eq_generic_param(l, r)
820 })
821}
822
823pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
824 use GenericParamKind::*;
825 l.is_placeholder == r.is_placeholder
826 && eq_id(l.ident, r.ident)
827 && over(&l.bounds, &r.bounds, eq_generic_bound)
828 && match (&l.kind, &r.kind) {
829 (Lifetime, Lifetime) => true,
830 (Type { default: l }, Type { default: r }) => both(l.as_ref(), r.as_ref(), |l, r| eq_ty(l, r)),
831 (
832 Const {
833 ty: lt,
834 kw_span: _,
835 default: ld,
836 },
837 Const {
838 ty: rt,
839 kw_span: _,
840 default: rd,
841 },
842 ) => eq_ty(lt, rt) && both(ld.as_ref(), rd.as_ref(), eq_anon_const),
843 _ => false,
844 }
845 && over(&l.attrs, &r.attrs, eq_attr)
846}
847
848pub fn eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool {
849 use GenericBound::*;
850 match (l, r) {
851 (Trait(ptr1), Trait(ptr2)) => eq_poly_ref_trait(ptr1, ptr2),
852 (Outlives(l), Outlives(r)) => eq_id(l.ident, r.ident),
853 _ => false,
854 }
855}
856
857pub fn eq_precise_capture(l: &PreciseCapturingArg, r: &PreciseCapturingArg) -> bool {
858 match (l, r) {
859 (PreciseCapturingArg::Lifetime(l), PreciseCapturingArg::Lifetime(r)) => l.ident == r.ident,
860 (PreciseCapturingArg::Arg(l, _), PreciseCapturingArg::Arg(r, _)) => l.segments[0].ident == r.segments[0].ident,
861 _ => false,
862 }
863}
864
865fn eq_term(l: &Term, r: &Term) -> bool {
866 match (l, r) {
867 (Term::Ty(l), Term::Ty(r)) => eq_ty(l, r),
868 (Term::Const(l), Term::Const(r)) => eq_anon_const(l, r),
869 _ => false,
870 }
871}
872
873pub fn eq_assoc_item_constraint(l: &AssocItemConstraint, r: &AssocItemConstraint) -> bool {
874 use AssocItemConstraintKind::*;
875 eq_id(l.ident, r.ident)
876 && match (&l.kind, &r.kind) {
877 (Equality { term: l }, Equality { term: r }) => eq_term(l, r),
878 (Bound { bounds: l }, Bound { bounds: r }) => over(l, r, eq_generic_bound),
879 _ => false,
880 }
881}
882
883pub fn eq_mac_call(l: &MacCall, r: &MacCall) -> bool {
884 eq_path(&l.path, &r.path) && eq_delim_args(&l.args, &r.args)
885}
886
887pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool {
888 use AttrKind::*;
889 l.style == r.style
890 && match (&l.kind, &r.kind) {
891 (DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2,
892 (Normal(l), Normal(r)) => eq_path(&l.item.path, &r.item.path) && eq_attr_args(&l.item.args, &r.item.args),
893 _ => false,
894 }
895}
896
897pub fn eq_attr_args(l: &AttrArgs, r: &AttrArgs) -> bool {
898 use AttrArgs::*;
899 match (l, r) {
900 (Empty, Empty) => true,
901 (Delimited(la), Delimited(ra)) => eq_delim_args(la, ra),
902 (Eq { eq_span: _, expr: le }, Eq { eq_span: _, expr: re }) => eq_expr(le, re),
903 _ => false,
904 }
905}
906
907pub fn eq_delim_args(l: &DelimArgs, r: &DelimArgs) -> bool {
908 l.delim == r.delim && l.tokens.eq_unspanned(&r.tokens)
909}