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 }),
368 Fn(box ast::Fn {
369 defaultness: rd,
370 sig: rf,
371 generics: rg,
372 contract: rc,
373 body: rb,
374 }),
375 ) => {
376 eq_defaultness(*ld, *rd)
377 && eq_fn_sig(lf, rf)
378 && eq_generics(lg, rg)
379 && eq_opt_fn_contract(lc, rc)
380 && both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
381 },
382 (Mod(lu, lmk), Mod(ru, rmk)) => {
383 lu == ru
384 && match (lmk, rmk) {
385 (ModKind::Loaded(litems, linline, _, _), ModKind::Loaded(ritems, rinline, _, _)) => {
386 linline == rinline && over(litems, ritems, |l, r| eq_item(l, r, eq_item_kind))
387 },
388 (ModKind::Unloaded, ModKind::Unloaded) => true,
389 _ => false,
390 }
391 },
392 (ForeignMod(l), ForeignMod(r)) => {
393 both(l.abi.as_ref(), r.abi.as_ref(), eq_str_lit)
394 && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
395 },
396 (
397 TyAlias(box ast::TyAlias {
398 defaultness: ld,
399 generics: lg,
400 bounds: lb,
401 ty: lt,
402 ..
403 }),
404 TyAlias(box ast::TyAlias {
405 defaultness: rd,
406 generics: rg,
407 bounds: rb,
408 ty: rt,
409 ..
410 }),
411 ) => {
412 eq_defaultness(*ld, *rd)
413 && eq_generics(lg, rg)
414 && over(lb, rb, eq_generic_bound)
415 && both(lt.as_ref(), rt.as_ref(), |l, r| eq_ty(l, r))
416 },
417 (Enum(le, lg), Enum(re, rg)) => over(&le.variants, &re.variants, eq_variant) && eq_generics(lg, rg),
418 (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => {
419 eq_variant_data(lv, rv) && eq_generics(lg, rg)
420 },
421 (
422 Trait(box ast::Trait {
423 is_auto: la,
424 safety: lu,
425 generics: lg,
426 bounds: lb,
427 items: li,
428 }),
429 Trait(box ast::Trait {
430 is_auto: ra,
431 safety: ru,
432 generics: rg,
433 bounds: rb,
434 items: ri,
435 }),
436 ) => {
437 la == ra
438 && matches!(lu, Safety::Default) == matches!(ru, Safety::Default)
439 && eq_generics(lg, rg)
440 && over(lb, rb, eq_generic_bound)
441 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
442 },
443 (TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, eq_generic_bound),
444 (
445 Impl(box ast::Impl {
446 safety: lu,
447 polarity: lp,
448 defaultness: ld,
449 constness: lc,
450 generics: lg,
451 of_trait: lot,
452 self_ty: lst,
453 items: li,
454 }),
455 Impl(box ast::Impl {
456 safety: ru,
457 polarity: rp,
458 defaultness: rd,
459 constness: rc,
460 generics: rg,
461 of_trait: rot,
462 self_ty: rst,
463 items: ri,
464 }),
465 ) => {
466 matches!(lu, Safety::Default) == matches!(ru, Safety::Default)
467 && matches!(lp, ImplPolarity::Positive) == matches!(rp, ImplPolarity::Positive)
468 && eq_defaultness(*ld, *rd)
469 && matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
470 && eq_generics(lg, rg)
471 && both(lot.as_ref(), rot.as_ref(), |l, r| eq_path(&l.path, &r.path))
472 && eq_ty(lst, rst)
473 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
474 },
475 (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
476 (MacroDef(l), MacroDef(r)) => l.macro_rules == r.macro_rules && eq_delim_args(&l.body, &r.body),
477 _ => false,
478 }
479}
480
481pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
482 use ForeignItemKind::*;
483 match (l, r) {
484 (
485 Static(box StaticItem {
486 ty: lt,
487 mutability: lm,
488 expr: le,
489 safety: ls,
490 }),
491 Static(box StaticItem {
492 ty: rt,
493 mutability: rm,
494 expr: re,
495 safety: rs,
496 }),
497 ) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le.as_ref(), re.as_ref()) && ls == rs,
498 (
499 Fn(box ast::Fn {
500 defaultness: ld,
501 sig: lf,
502 generics: lg,
503 contract: lc,
504 body: lb,
505 }),
506 Fn(box ast::Fn {
507 defaultness: rd,
508 sig: rf,
509 generics: rg,
510 contract: rc,
511 body: rb,
512 }),
513 ) => {
514 eq_defaultness(*ld, *rd)
515 && eq_fn_sig(lf, rf)
516 && eq_generics(lg, rg)
517 && eq_opt_fn_contract(lc, rc)
518 && both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
519 },
520 (
521 TyAlias(box ast::TyAlias {
522 defaultness: ld,
523 generics: lg,
524 bounds: lb,
525 ty: lt,
526 ..
527 }),
528 TyAlias(box ast::TyAlias {
529 defaultness: rd,
530 generics: rg,
531 bounds: rb,
532 ty: rt,
533 ..
534 }),
535 ) => {
536 eq_defaultness(*ld, *rd)
537 && eq_generics(lg, rg)
538 && over(lb, rb, eq_generic_bound)
539 && both(lt.as_ref(), rt.as_ref(), |l, r| eq_ty(l, r))
540 },
541 (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
542 _ => false,
543 }
544}
545
546pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
547 use AssocItemKind::*;
548 match (l, r) {
549 (
550 Const(box ConstItem {
551 defaultness: ld,
552 generics: lg,
553 ty: lt,
554 expr: le,
555 }),
556 Const(box ConstItem {
557 defaultness: rd,
558 generics: rg,
559 ty: rt,
560 expr: re,
561 }),
562 ) => eq_defaultness(*ld, *rd) && eq_generics(lg, rg) && eq_ty(lt, rt) && eq_expr_opt(le.as_ref(), re.as_ref()),
563 (
564 Fn(box ast::Fn {
565 defaultness: ld,
566 sig: lf,
567 generics: lg,
568 contract: lc,
569 body: lb,
570 }),
571 Fn(box ast::Fn {
572 defaultness: rd,
573 sig: rf,
574 generics: rg,
575 contract: rc,
576 body: rb,
577 }),
578 ) => {
579 eq_defaultness(*ld, *rd)
580 && eq_fn_sig(lf, rf)
581 && eq_generics(lg, rg)
582 && eq_opt_fn_contract(lc, rc)
583 && both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
584 },
585 (
586 Type(box TyAlias {
587 defaultness: ld,
588 generics: lg,
589 bounds: lb,
590 ty: lt,
591 ..
592 }),
593 Type(box TyAlias {
594 defaultness: rd,
595 generics: rg,
596 bounds: rb,
597 ty: rt,
598 ..
599 }),
600 ) => {
601 eq_defaultness(*ld, *rd)
602 && eq_generics(lg, rg)
603 && over(lb, rb, eq_generic_bound)
604 && both(lt.as_ref(), rt.as_ref(), |l, r| eq_ty(l, r))
605 },
606 (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
607 _ => false,
608 }
609}
610
611pub fn eq_variant(l: &Variant, r: &Variant) -> bool {
612 l.is_placeholder == r.is_placeholder
613 && over(&l.attrs, &r.attrs, eq_attr)
614 && eq_vis(&l.vis, &r.vis)
615 && eq_id(l.ident, r.ident)
616 && eq_variant_data(&l.data, &r.data)
617 && both(l.disr_expr.as_ref(), r.disr_expr.as_ref(), |l, r| {
618 eq_expr(&l.value, &r.value)
619 })
620}
621
622pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
623 use VariantData::*;
624 match (l, r) {
625 (Unit(_), Unit(_)) => true,
626 (Struct { fields: l, .. }, Struct { fields: r, .. }) | (Tuple(l, _), Tuple(r, _)) => {
627 over(l, r, eq_struct_field)
628 },
629 _ => false,
630 }
631}
632
633pub fn eq_struct_field(l: &FieldDef, r: &FieldDef) -> bool {
634 l.is_placeholder == r.is_placeholder
635 && over(&l.attrs, &r.attrs, eq_attr)
636 && eq_vis(&l.vis, &r.vis)
637 && both(l.ident.as_ref(), r.ident.as_ref(), |l, r| eq_id(*l, *r))
638 && eq_ty(&l.ty, &r.ty)
639}
640
641pub fn eq_fn_sig(l: &FnSig, r: &FnSig) -> bool {
642 eq_fn_decl(&l.decl, &r.decl) && eq_fn_header(&l.header, &r.header)
643}
644
645fn eq_opt_coroutine_kind(l: Option<CoroutineKind>, r: Option<CoroutineKind>) -> bool {
646 matches!(
647 (l, r),
648 (Some(CoroutineKind::Async { .. }), Some(CoroutineKind::Async { .. }))
649 | (Some(CoroutineKind::Gen { .. }), Some(CoroutineKind::Gen { .. }))
650 | (
651 Some(CoroutineKind::AsyncGen { .. }),
652 Some(CoroutineKind::AsyncGen { .. })
653 )
654 | (None, None)
655 )
656}
657
658pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool {
659 matches!(l.safety, Safety::Default) == matches!(r.safety, Safety::Default)
660 && eq_opt_coroutine_kind(l.coroutine_kind, r.coroutine_kind)
661 && matches!(l.constness, Const::No) == matches!(r.constness, Const::No)
662 && eq_ext(&l.ext, &r.ext)
663}
664
665#[expect(clippy::ref_option, reason = "This is the type how it is stored in the AST")]
666pub fn eq_opt_fn_contract(l: &Option<P<FnContract>>, r: &Option<P<FnContract>>) -> bool {
667 match (l, r) {
668 (Some(l), Some(r)) => {
669 eq_expr_opt(l.requires.as_ref(), r.requires.as_ref()) && eq_expr_opt(l.ensures.as_ref(), r.ensures.as_ref())
670 },
671 (None, None) => true,
672 (Some(_), None) | (None, Some(_)) => false,
673 }
674}
675
676pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
677 over(&l.params, &r.params, eq_generic_param)
678 && over(&l.where_clause.predicates, &r.where_clause.predicates, |l, r| {
679 eq_where_predicate(l, r)
680 })
681}
682
683pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
684 use WherePredicateKind::*;
685 match (&l.kind, &r.kind) {
686 (BoundPredicate(l), BoundPredicate(r)) => {
687 over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
688 eq_generic_param(l, r)
689 }) && eq_ty(&l.bounded_ty, &r.bounded_ty)
690 && over(&l.bounds, &r.bounds, eq_generic_bound)
691 },
692 (RegionPredicate(l), RegionPredicate(r)) => {
693 eq_id(l.lifetime.ident, r.lifetime.ident) && over(&l.bounds, &r.bounds, eq_generic_bound)
694 },
695 (EqPredicate(l), EqPredicate(r)) => eq_ty(&l.lhs_ty, &r.lhs_ty) && eq_ty(&l.rhs_ty, &r.rhs_ty),
696 _ => false,
697 }
698}
699
700pub fn eq_use_tree(l: &UseTree, r: &UseTree) -> bool {
701 eq_path(&l.prefix, &r.prefix) && eq_use_tree_kind(&l.kind, &r.kind)
702}
703
704pub fn eq_anon_const(l: &AnonConst, r: &AnonConst) -> bool {
705 eq_expr(&l.value, &r.value)
706}
707
708pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
709 use UseTreeKind::*;
710 match (l, r) {
711 (Glob, Glob) => true,
712 (Simple(l), Simple(r)) => both(l.as_ref(), r.as_ref(), |l, r| eq_id(*l, *r)),
713 (Nested { items: l, .. }, Nested { items: r, .. }) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
714 _ => false,
715 }
716}
717
718pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
719 matches!(
720 (l, r),
721 (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_))
722 )
723}
724
725pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
726 use VisibilityKind::*;
727 match (&l.kind, &r.kind) {
728 (Public, Public) | (Inherited, Inherited) => true,
729 (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r),
730 _ => false,
731 }
732}
733
734pub fn eq_fn_decl(l: &FnDecl, r: &FnDecl) -> bool {
735 eq_fn_ret_ty(&l.output, &r.output)
736 && over(&l.inputs, &r.inputs, |l, r| {
737 l.is_placeholder == r.is_placeholder
738 && eq_pat(&l.pat, &r.pat)
739 && eq_ty(&l.ty, &r.ty)
740 && over(&l.attrs, &r.attrs, eq_attr)
741 })
742}
743
744pub fn eq_closure_binder(l: &ClosureBinder, r: &ClosureBinder) -> bool {
745 match (l, r) {
746 (ClosureBinder::NotPresent, ClosureBinder::NotPresent) => true,
747 (ClosureBinder::For { generic_params: lp, .. }, ClosureBinder::For { generic_params: rp, .. }) => {
748 lp.len() == rp.len() && std::iter::zip(lp.iter(), rp.iter()).all(|(l, r)| eq_generic_param(l, r))
749 },
750 _ => false,
751 }
752}
753
754pub fn eq_fn_ret_ty(l: &FnRetTy, r: &FnRetTy) -> bool {
755 match (l, r) {
756 (FnRetTy::Default(_), FnRetTy::Default(_)) => true,
757 (FnRetTy::Ty(l), FnRetTy::Ty(r)) => eq_ty(l, r),
758 _ => false,
759 }
760}
761
762pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
763 use TyKind::*;
764 match (&l.kind, &r.kind) {
765 (Paren(l), _) => eq_ty(l, r),
766 (_, Paren(r)) => eq_ty(l, r),
767 (Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err(_), Err(_)) | (CVarArgs, CVarArgs) => {
768 true
769 },
770 (Slice(l), Slice(r)) => eq_ty(l, r),
771 (Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
772 (Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),
773 (Ref(ll, l), Ref(rl, r)) => {
774 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)
775 },
776 (PinnedRef(ll, l), PinnedRef(rl, r)) => {
777 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)
778 },
779 (BareFn(l), BareFn(r)) => {
780 l.safety == r.safety
781 && eq_ext(&l.ext, &r.ext)
782 && over(&l.generic_params, &r.generic_params, eq_generic_param)
783 && eq_fn_decl(&l.decl, &r.decl)
784 },
785 (Tup(l), Tup(r)) => over(l, r, |l, r| eq_ty(l, r)),
786 (Path(lq, lp), Path(rq, rp)) => both(lq.as_ref(), rq.as_ref(), eq_qself) && eq_path(lp, rp),
787 (TraitObject(lg, ls), TraitObject(rg, rs)) => ls == rs && over(lg, rg, eq_generic_bound),
788 (ImplTrait(_, lg), ImplTrait(_, rg)) => over(lg, rg, eq_generic_bound),
789 (Typeof(l), Typeof(r)) => eq_expr(&l.value, &r.value),
790 (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
791 _ => false,
792 }
793}
794
795pub fn eq_ext(l: &Extern, r: &Extern) -> bool {
796 use Extern::*;
797 match (l, r) {
798 (None, None) | (Implicit(_), Implicit(_)) => true,
799 (Explicit(l, _), Explicit(r, _)) => eq_str_lit(l, r),
800 _ => false,
801 }
802}
803
804pub fn eq_str_lit(l: &StrLit, r: &StrLit) -> bool {
805 l.style == r.style && l.symbol == r.symbol && l.suffix == r.suffix
806}
807
808pub fn eq_poly_ref_trait(l: &PolyTraitRef, r: &PolyTraitRef) -> bool {
809 l.modifiers == r.modifiers
810 && eq_path(&l.trait_ref.path, &r.trait_ref.path)
811 && over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
812 eq_generic_param(l, r)
813 })
814}
815
816pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
817 use GenericParamKind::*;
818 l.is_placeholder == r.is_placeholder
819 && eq_id(l.ident, r.ident)
820 && over(&l.bounds, &r.bounds, eq_generic_bound)
821 && match (&l.kind, &r.kind) {
822 (Lifetime, Lifetime) => true,
823 (Type { default: l }, Type { default: r }) => both(l.as_ref(), r.as_ref(), |l, r| eq_ty(l, r)),
824 (
825 Const {
826 ty: lt,
827 kw_span: _,
828 default: ld,
829 },
830 Const {
831 ty: rt,
832 kw_span: _,
833 default: rd,
834 },
835 ) => eq_ty(lt, rt) && both(ld.as_ref(), rd.as_ref(), eq_anon_const),
836 _ => false,
837 }
838 && over(&l.attrs, &r.attrs, eq_attr)
839}
840
841pub fn eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool {
842 use GenericBound::*;
843 match (l, r) {
844 (Trait(ptr1), Trait(ptr2)) => eq_poly_ref_trait(ptr1, ptr2),
845 (Outlives(l), Outlives(r)) => eq_id(l.ident, r.ident),
846 _ => false,
847 }
848}
849
850pub fn eq_precise_capture(l: &PreciseCapturingArg, r: &PreciseCapturingArg) -> bool {
851 match (l, r) {
852 (PreciseCapturingArg::Lifetime(l), PreciseCapturingArg::Lifetime(r)) => l.ident == r.ident,
853 (PreciseCapturingArg::Arg(l, _), PreciseCapturingArg::Arg(r, _)) => l.segments[0].ident == r.segments[0].ident,
854 _ => false,
855 }
856}
857
858fn eq_term(l: &Term, r: &Term) -> bool {
859 match (l, r) {
860 (Term::Ty(l), Term::Ty(r)) => eq_ty(l, r),
861 (Term::Const(l), Term::Const(r)) => eq_anon_const(l, r),
862 _ => false,
863 }
864}
865
866pub fn eq_assoc_item_constraint(l: &AssocItemConstraint, r: &AssocItemConstraint) -> bool {
867 use AssocItemConstraintKind::*;
868 eq_id(l.ident, r.ident)
869 && match (&l.kind, &r.kind) {
870 (Equality { term: l }, Equality { term: r }) => eq_term(l, r),
871 (Bound { bounds: l }, Bound { bounds: r }) => over(l, r, eq_generic_bound),
872 _ => false,
873 }
874}
875
876pub fn eq_mac_call(l: &MacCall, r: &MacCall) -> bool {
877 eq_path(&l.path, &r.path) && eq_delim_args(&l.args, &r.args)
878}
879
880pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool {
881 use AttrKind::*;
882 l.style == r.style
883 && match (&l.kind, &r.kind) {
884 (DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2,
885 (Normal(l), Normal(r)) => eq_path(&l.item.path, &r.item.path) && eq_attr_args(&l.item.args, &r.item.args),
886 _ => false,
887 }
888}
889
890pub fn eq_attr_args(l: &AttrArgs, r: &AttrArgs) -> bool {
891 use AttrArgs::*;
892 match (l, r) {
893 (Empty, Empty) => true,
894 (Delimited(la), Delimited(ra)) => eq_delim_args(la, ra),
895 (Eq { eq_span: _, expr: le }, Eq { eq_span: _, expr: re }) => eq_expr(le, re),
896 _ => false,
897 }
898}
899
900pub fn eq_delim_args(l: &DelimArgs, r: &DelimArgs) -> bool {
901 l.delim == r.delim && l.tokens.eq_unspanned(&r.tokens)
902}