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