1#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
6
7use crate::{both, over};
8use rustc_ast::{self as ast, HasAttrs, *};
9use rustc_span::sym;
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, Y>(left: &[X], right: &[Y], mut eq_fn: impl FnMut(&X, &Y) -> 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_deref(), rf.as_deref())
46 && eq_expr_opt(lt.as_deref(), rt.as_deref())
47 && eq_range_end(le.node, re.node)
48 },
49 (Box(l), Box(r)) => eq_pat(l, r),
50 (Ref(l, l_pin, l_mut), Ref(r, r_pin, r_mut)) => l_pin == r_pin && l_mut == r_mut && eq_pat(l, r),
51 (Tuple(l), Tuple(r)) | (Slice(l), Slice(r)) => over(l, r, eq_pat),
52 (Path(lq, lp), Path(rq, rp)) => both(lq.as_deref(), rq.as_deref(), eq_qself) && eq_path(lp, rp),
53 (TupleStruct(lqself, lp, lfs), TupleStruct(rqself, rp, rfs)) => {
54 eq_maybe_qself(lqself.as_deref(), rqself.as_deref()) && eq_path(lp, rp) && over(lfs, rfs, eq_pat)
55 },
56 (Struct(lqself, lp, lfs, lr), Struct(rqself, rp, rfs, rr)) => {
57 lr == rr
58 && eq_maybe_qself(lqself.as_deref(), rqself.as_deref())
59 && eq_path(lp, rp)
60 && unordered_over(lfs, rfs, eq_field_pat)
61 },
62 (Or(ls), Or(rs)) => unordered_over(ls, rs, eq_pat),
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: &QSelf, r: &QSelf) -> bool {
86 l.position == r.position && eq_ty(&l.ty, &r.ty)
87}
88
89pub fn eq_maybe_qself(l: Option<&QSelf>, r: Option<&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<&Expr>, r: Option<&Expr>) -> bool {
133 both(l, r, eq_expr)
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#[expect(clippy::too_many_lines, reason = "big match statement")]
145pub 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_deref(), re.as_deref())
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(lb, lt), TryBlock(rb, rt)) => eq_block(lb, rb) && both(lt.as_deref(), rt.as_deref(), eq_ty),
204 (Yield(l), Yield(r)) => eq_expr_opt(l.expr().map(Box::as_ref), r.expr().map(Box::as_ref)) && l.same_kind(r),
205 (Ret(l), Ret(r)) => eq_expr_opt(l.as_deref(), r.as_deref()),
206 (Break(ll, le), Break(rl, re)) => {
207 eq_label(ll.as_ref(), rl.as_ref()) && eq_expr_opt(le.as_deref(), re.as_deref())
208 },
209 (Continue(ll), Continue(rl)) => eq_label(ll.as_ref(), rl.as_ref()),
210 (Assign(l1, l2, _), Assign(r1, r2, _)) | (Index(l1, l2, _), Index(r1, r2, _)) => {
211 eq_expr(l1, r1) && eq_expr(l2, r2)
212 },
213 (AssignOp(lo, lp, lv), AssignOp(ro, rp, rv)) => lo.node == ro.node && eq_expr(lp, rp) && eq_expr(lv, rv),
214 (Field(lp, lf), Field(rp, rf)) => eq_id(*lf, *rf) && eq_expr(lp, rp),
215 (Match(ls, la, lkind), Match(rs, ra, rkind)) => (lkind == rkind) && eq_expr(ls, rs) && over(la, ra, eq_arm),
216 (
217 Closure(box ast::Closure {
218 binder: lb,
219 capture_clause: lc,
220 coroutine_kind: la,
221 movability: lm,
222 fn_decl: lf,
223 body: le,
224 ..
225 }),
226 Closure(box ast::Closure {
227 binder: rb,
228 capture_clause: rc,
229 coroutine_kind: ra,
230 movability: rm,
231 fn_decl: rf,
232 body: re,
233 ..
234 }),
235 ) => {
236 eq_closure_binder(lb, rb)
237 && lc == rc
238 && eq_coroutine_kind(*la, *ra)
239 && lm == rm
240 && eq_fn_decl(lf, rf)
241 && eq_expr(le, re)
242 },
243 (Gen(lc, lb, lk, _), Gen(rc, rb, rk, _)) => lc == rc && eq_block(lb, rb) && lk == rk,
244 (Range(lf, lt, ll), Range(rf, rt, rl)) => {
245 ll == rl && eq_expr_opt(lf.as_deref(), rf.as_deref()) && eq_expr_opt(lt.as_deref(), rt.as_deref())
246 },
247 (AddrOf(lbk, lm, le), AddrOf(rbk, rm, re)) => lbk == rbk && lm == rm && eq_expr(le, re),
248 (Path(lq, lp), Path(rq, rp)) => both(lq.as_deref(), rq.as_deref(), eq_qself) && eq_path(lp, rp),
249 (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
250 (Struct(lse), Struct(rse)) => {
251 eq_maybe_qself(lse.qself.as_deref(), rse.qself.as_deref())
252 && eq_path(&lse.path, &rse.path)
253 && eq_struct_rest(&lse.rest, &rse.rest)
254 && unordered_over(&lse.fields, &rse.fields, eq_field)
255 },
256 _ => false,
257 }
258}
259
260fn eq_coroutine_kind(a: Option<CoroutineKind>, b: Option<CoroutineKind>) -> bool {
261 matches!(
262 (a, b),
263 (Some(CoroutineKind::Async { .. }), Some(CoroutineKind::Async { .. }))
264 | (Some(CoroutineKind::Gen { .. }), Some(CoroutineKind::Gen { .. }))
265 | (
266 Some(CoroutineKind::AsyncGen { .. }),
267 Some(CoroutineKind::AsyncGen { .. })
268 )
269 | (None, None)
270 )
271}
272
273pub fn eq_field(l: &ExprField, r: &ExprField) -> bool {
274 l.is_placeholder == r.is_placeholder
275 && eq_id(l.ident, r.ident)
276 && eq_expr(&l.expr, &r.expr)
277 && over(&l.attrs, &r.attrs, eq_attr)
278}
279
280pub fn eq_arm(l: &Arm, r: &Arm) -> bool {
281 l.is_placeholder == r.is_placeholder
282 && eq_pat(&l.pat, &r.pat)
283 && eq_expr_opt(l.body.as_deref(), r.body.as_deref())
284 && eq_expr_opt(l.guard.as_deref().map(|g| &g.cond), r.guard.as_deref().map(|g| &g.cond))
285 && over(&l.attrs, &r.attrs, eq_attr)
286}
287
288pub fn eq_label(l: Option<&Label>, r: Option<&Label>) -> bool {
289 both(l, r, |l, r| eq_id(l.ident, r.ident))
290}
291
292pub fn eq_block(l: &Block, r: &Block) -> bool {
293 l.rules == r.rules && over(&l.stmts, &r.stmts, eq_stmt)
294}
295
296pub fn eq_stmt(l: &Stmt, r: &Stmt) -> bool {
297 use StmtKind::*;
298 match (&l.kind, &r.kind) {
299 (Let(l), Let(r)) => {
300 eq_pat(&l.pat, &r.pat)
301 && both(l.ty.as_ref(), r.ty.as_ref(), |l, r| eq_ty(l, r))
302 && eq_local_kind(&l.kind, &r.kind)
303 && over(&l.attrs, &r.attrs, eq_attr)
304 },
305 (Item(l), Item(r)) => eq_item(l, r, eq_item_kind),
306 (Expr(l), Expr(r)) | (Semi(l), Semi(r)) => eq_expr(l, r),
307 (Empty, Empty) => true,
308 (MacCall(l), MacCall(r)) => {
309 l.style == r.style && eq_mac_call(&l.mac, &r.mac) && over(&l.attrs, &r.attrs, eq_attr)
310 },
311 _ => false,
312 }
313}
314
315pub fn eq_local_kind(l: &LocalKind, r: &LocalKind) -> bool {
316 use LocalKind::*;
317 match (l, r) {
318 (Decl, Decl) => true,
319 (Init(l), Init(r)) => eq_expr(l, r),
320 (InitElse(li, le), InitElse(ri, re)) => eq_expr(li, ri) && eq_block(le, re),
321 _ => false,
322 }
323}
324
325pub fn eq_item<K>(l: &Item<K>, r: &Item<K>, mut eq_kind: impl FnMut(&K, &K) -> bool) -> bool {
326 over(&l.attrs, &r.attrs, eq_attr) && eq_vis(&l.vis, &r.vis) && eq_kind(&l.kind, &r.kind)
327}
328
329#[expect(clippy::too_many_lines, reason = "big match statement")]
330pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
331 use ItemKind::*;
332 match (l, r) {
333 (ExternCrate(ls, li), ExternCrate(rs, ri)) => ls == rs && eq_id(*li, *ri),
334 (Use(l), Use(r)) => eq_use_tree(l, r),
335 (
336 Static(box StaticItem {
337 ident: li,
338 ty: lt,
339 mutability: lm,
340 expr: le,
341 safety: ls,
342 define_opaque: _,
343 eii_impls: _,
344 }),
345 Static(box StaticItem {
346 ident: ri,
347 ty: rt,
348 mutability: rm,
349 expr: re,
350 safety: rs,
351 define_opaque: _,
352 eii_impls: _,
353 }),
354 ) => eq_id(*li, *ri) && lm == rm && ls == rs && eq_ty(lt, rt) && eq_expr_opt(le.as_deref(), re.as_deref()),
355 (
356 Const(box ConstItem {
357 defaultness: ld,
358 ident: li,
359 generics: lg,
360 ty: lt,
361 rhs_kind: lb,
362 define_opaque: _,
363 }),
364 Const(box ConstItem {
365 defaultness: rd,
366 ident: ri,
367 generics: rg,
368 ty: rt,
369 rhs_kind: rb,
370 define_opaque: _,
371 }),
372 ) => {
373 eq_defaultness(*ld, *rd)
374 && eq_id(*li, *ri)
375 && eq_generics(lg, rg)
376 && eq_ty(lt, rt)
377 && both(Some(lb), Some(rb), eq_const_item_rhs)
378 },
379 (
380 Fn(box ast::Fn {
381 defaultness: ld,
382 sig: lf,
383 ident: li,
384 generics: lg,
385 contract: lc,
386 body: lb,
387 define_opaque: _,
388 eii_impls: _,
389 }),
390 Fn(box ast::Fn {
391 defaultness: rd,
392 sig: rf,
393 ident: ri,
394 generics: rg,
395 contract: rc,
396 body: rb,
397 define_opaque: _,
398 eii_impls: _,
399 }),
400 ) => {
401 eq_defaultness(*ld, *rd)
402 && eq_fn_sig(lf, rf)
403 && eq_id(*li, *ri)
404 && eq_generics(lg, rg)
405 && eq_opt_fn_contract(lc, rc)
406 && both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
407 },
408 (Mod(ls, li, lmk), Mod(rs, ri, rmk)) => {
409 ls == rs
410 && eq_id(*li, *ri)
411 && match (lmk, rmk) {
412 (ModKind::Loaded(litems, linline, _), ModKind::Loaded(ritems, rinline, _)) => {
413 linline == rinline && over(litems, ritems, |l, r| eq_item(l, r, eq_item_kind))
414 },
415 (ModKind::Unloaded, ModKind::Unloaded) => true,
416 _ => false,
417 }
418 },
419 (ForeignMod(l), ForeignMod(r)) => {
420 both(l.abi.as_ref(), r.abi.as_ref(), eq_str_lit)
421 && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
422 },
423 (
424 TyAlias(box ast::TyAlias {
425 defaultness: ld,
426 generics: lg,
427 bounds: lb,
428 ty: lt,
429 ..
430 }),
431 TyAlias(box ast::TyAlias {
432 defaultness: rd,
433 generics: rg,
434 bounds: rb,
435 ty: rt,
436 ..
437 }),
438 ) => {
439 eq_defaultness(*ld, *rd)
440 && eq_generics(lg, rg)
441 && over(lb, rb, eq_generic_bound)
442 && both(lt.as_ref(), rt.as_ref(), |l, r| eq_ty(l, r))
443 },
444 (Enum(li, lg, le), Enum(ri, rg, re)) => {
445 eq_id(*li, *ri) && eq_generics(lg, rg) && over(&le.variants, &re.variants, eq_variant)
446 },
447 (Struct(li, lg, lv), Struct(ri, rg, rv)) | (Union(li, lg, lv), Union(ri, rg, rv)) => {
448 eq_id(*li, *ri) && eq_generics(lg, rg) && eq_variant_data(lv, rv)
449 },
450 (
451 Trait(box ast::Trait {
452 impl_restriction: liprt,
453 constness: lc,
454 is_auto: la,
455 safety: lu,
456 ident: li,
457 generics: lg,
458 bounds: lb,
459 items: lis,
460 }),
461 Trait(box ast::Trait {
462 impl_restriction: riprt,
463 constness: rc,
464 is_auto: ra,
465 safety: ru,
466 ident: ri,
467 generics: rg,
468 bounds: rb,
469 items: ris,
470 }),
471 ) => {
472 eq_impl_restriction(liprt, riprt)
473 && matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
474 && la == ra
475 && matches!(lu, Safety::Default) == matches!(ru, Safety::Default)
476 && eq_id(*li, *ri)
477 && eq_generics(lg, rg)
478 && over(lb, rb, eq_generic_bound)
479 && over(lis, ris, |l, r| eq_item(l, r, eq_assoc_item_kind))
480 },
481 (
482 TraitAlias(box ast::TraitAlias {
483 ident: li,
484 generics: lg,
485 bounds: lb,
486 constness: lc,
487 }),
488 TraitAlias(box ast::TraitAlias {
489 ident: ri,
490 generics: rg,
491 bounds: rb,
492 constness: rc,
493 }),
494 ) => {
495 matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
496 && eq_id(*li, *ri)
497 && eq_generics(lg, rg)
498 && over(lb, rb, eq_generic_bound)
499 },
500 (
501 Impl(ast::Impl {
502 generics: lg,
503 of_trait: lot,
504 self_ty: lst,
505 items: li,
506 constness: lc,
507 }),
508 Impl(ast::Impl {
509 generics: rg,
510 of_trait: rot,
511 self_ty: rst,
512 items: ri,
513 constness: rc,
514 }),
515 ) => {
516 eq_generics(lg, rg)
517 && both(lot.as_deref(), rot.as_deref(), |l, r| {
518 matches!(l.safety, Safety::Default) == matches!(r.safety, Safety::Default)
519 && matches!(l.polarity, ImplPolarity::Positive) == matches!(r.polarity, ImplPolarity::Positive)
520 && eq_defaultness(l.defaultness, r.defaultness)
521 && matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
522 && eq_path(&l.trait_ref.path, &r.trait_ref.path)
523 })
524 && eq_ty(lst, rst)
525 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
526 },
527 (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
528 (MacroDef(li, ld), MacroDef(ri, rd)) => {
529 eq_id(*li, *ri) && ld.macro_rules == rd.macro_rules && eq_delim_args(&ld.body, &rd.body)
530 },
531 _ => false,
532 }
533}
534
535pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
536 use ForeignItemKind::*;
537 match (l, r) {
538 (
539 Static(box StaticItem {
540 ident: li,
541 ty: lt,
542 mutability: lm,
543 expr: le,
544 safety: ls,
545 define_opaque: _,
546 eii_impls: _,
547 }),
548 Static(box StaticItem {
549 ident: ri,
550 ty: rt,
551 mutability: rm,
552 expr: re,
553 safety: rs,
554 define_opaque: _,
555 eii_impls: _,
556 }),
557 ) => eq_id(*li, *ri) && eq_ty(lt, rt) && lm == rm && eq_expr_opt(le.as_deref(), re.as_deref()) && ls == rs,
558 (
559 Fn(box ast::Fn {
560 defaultness: ld,
561 sig: lf,
562 ident: li,
563 generics: lg,
564 contract: lc,
565 body: lb,
566 define_opaque: _,
567 eii_impls: _,
568 }),
569 Fn(box ast::Fn {
570 defaultness: rd,
571 sig: rf,
572 ident: ri,
573 generics: rg,
574 contract: rc,
575 body: rb,
576 define_opaque: _,
577 eii_impls: _,
578 }),
579 ) => {
580 eq_defaultness(*ld, *rd)
581 && eq_fn_sig(lf, rf)
582 && eq_id(*li, *ri)
583 && eq_generics(lg, rg)
584 && eq_opt_fn_contract(lc, rc)
585 && both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
586 },
587 (
588 TyAlias(box ast::TyAlias {
589 defaultness: ld,
590 ident: li,
591 generics: lg,
592 after_where_clause: lw,
593 bounds: lb,
594 ty: lt,
595 }),
596 TyAlias(box ast::TyAlias {
597 defaultness: rd,
598 ident: ri,
599 generics: rg,
600 after_where_clause: rw,
601 bounds: rb,
602 ty: rt,
603 }),
604 ) => {
605 eq_defaultness(*ld, *rd)
606 && eq_id(*li, *ri)
607 && eq_generics(lg, rg)
608 && over(&lw.predicates, &rw.predicates, eq_where_predicate)
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_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
618 use AssocItemKind::*;
619 match (l, r) {
620 (
621 Const(box ConstItem {
622 defaultness: ld,
623 ident: li,
624 generics: lg,
625 ty: lt,
626 rhs_kind: lb,
627 define_opaque: _,
628 }),
629 Const(box ConstItem {
630 defaultness: rd,
631 ident: ri,
632 generics: rg,
633 ty: rt,
634 rhs_kind: rb,
635 define_opaque: _,
636 }),
637 ) => {
638 eq_defaultness(*ld, *rd)
639 && eq_id(*li, *ri)
640 && eq_generics(lg, rg)
641 && eq_ty(lt, rt)
642 && both(Some(lb), Some(rb), eq_const_item_rhs)
643 },
644 (
645 Fn(box ast::Fn {
646 defaultness: ld,
647 sig: lf,
648 ident: li,
649 generics: lg,
650 contract: lc,
651 body: lb,
652 define_opaque: _,
653 eii_impls: _,
654 }),
655 Fn(box ast::Fn {
656 defaultness: rd,
657 sig: rf,
658 ident: ri,
659 generics: rg,
660 contract: rc,
661 body: rb,
662 define_opaque: _,
663 eii_impls: _,
664 }),
665 ) => {
666 eq_defaultness(*ld, *rd)
667 && eq_fn_sig(lf, rf)
668 && eq_id(*li, *ri)
669 && eq_generics(lg, rg)
670 && eq_opt_fn_contract(lc, rc)
671 && both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
672 },
673 (
674 Type(box TyAlias {
675 defaultness: ld,
676 ident: li,
677 generics: lg,
678 after_where_clause: lw,
679 bounds: lb,
680 ty: lt,
681 }),
682 Type(box TyAlias {
683 defaultness: rd,
684 ident: ri,
685 generics: rg,
686 after_where_clause: rw,
687 bounds: rb,
688 ty: rt,
689 }),
690 ) => {
691 eq_defaultness(*ld, *rd)
692 && eq_id(*li, *ri)
693 && eq_generics(lg, rg)
694 && over(&lw.predicates, &rw.predicates, eq_where_predicate)
695 && over(lb, rb, eq_generic_bound)
696 && both(lt.as_ref(), rt.as_ref(), |l, r| eq_ty(l, r))
697 },
698 (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
699 _ => false,
700 }
701}
702
703pub fn eq_variant(l: &Variant, r: &Variant) -> bool {
704 l.is_placeholder == r.is_placeholder
705 && over(&l.attrs, &r.attrs, eq_attr)
706 && eq_vis(&l.vis, &r.vis)
707 && eq_id(l.ident, r.ident)
708 && eq_variant_data(&l.data, &r.data)
709 && both(l.disr_expr.as_ref(), r.disr_expr.as_ref(), |l, r| {
710 eq_expr(&l.value, &r.value)
711 })
712}
713
714pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
715 use VariantData::*;
716 match (l, r) {
717 (Unit(_), Unit(_)) => true,
718 (Struct { fields: l, .. }, Struct { fields: r, .. }) | (Tuple(l, _), Tuple(r, _)) => {
719 over(l, r, eq_struct_field)
720 },
721 _ => false,
722 }
723}
724
725pub fn eq_struct_field(l: &FieldDef, r: &FieldDef) -> bool {
726 l.is_placeholder == r.is_placeholder
727 && over(&l.attrs, &r.attrs, eq_attr)
728 && eq_vis(&l.vis, &r.vis)
729 && both(l.ident.as_ref(), r.ident.as_ref(), |l, r| eq_id(*l, *r))
730 && eq_ty(&l.ty, &r.ty)
731}
732
733pub fn eq_fn_sig(l: &FnSig, r: &FnSig) -> bool {
734 eq_fn_decl(&l.decl, &r.decl) && eq_fn_header(&l.header, &r.header)
735}
736
737fn eq_opt_coroutine_kind(l: Option<CoroutineKind>, r: Option<CoroutineKind>) -> bool {
738 matches!(
739 (l, r),
740 (Some(CoroutineKind::Async { .. }), Some(CoroutineKind::Async { .. }))
741 | (Some(CoroutineKind::Gen { .. }), Some(CoroutineKind::Gen { .. }))
742 | (
743 Some(CoroutineKind::AsyncGen { .. }),
744 Some(CoroutineKind::AsyncGen { .. })
745 )
746 | (None, None)
747 )
748}
749
750pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool {
751 matches!(l.safety, Safety::Default) == matches!(r.safety, Safety::Default)
752 && eq_opt_coroutine_kind(l.coroutine_kind, r.coroutine_kind)
753 && matches!(l.constness, Const::No) == matches!(r.constness, Const::No)
754 && eq_ext(&l.ext, &r.ext)
755}
756
757#[expect(clippy::ref_option, reason = "This is the type how it is stored in the AST")]
758pub fn eq_opt_fn_contract(l: &Option<Box<FnContract>>, r: &Option<Box<FnContract>>) -> bool {
759 match (l, r) {
760 (Some(l), Some(r)) => {
761 eq_expr_opt(l.requires.as_deref(), r.requires.as_deref())
762 && eq_expr_opt(l.ensures.as_deref(), r.ensures.as_deref())
763 },
764 (None, None) => true,
765 (Some(_), None) | (None, Some(_)) => false,
766 }
767}
768
769pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
770 over(&l.params, &r.params, eq_generic_param)
771 && over(&l.where_clause.predicates, &r.where_clause.predicates, |l, r| {
772 eq_where_predicate(l, r)
773 })
774}
775
776pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
777 use WherePredicateKind::*;
778 over(&l.attrs, &r.attrs, eq_attr)
779 && match (&l.kind, &r.kind) {
780 (BoundPredicate(l), BoundPredicate(r)) => {
781 over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
782 eq_generic_param(l, r)
783 }) && eq_ty(&l.bounded_ty, &r.bounded_ty)
784 && over(&l.bounds, &r.bounds, eq_generic_bound)
785 },
786 (RegionPredicate(l), RegionPredicate(r)) => {
787 eq_id(l.lifetime.ident, r.lifetime.ident) && over(&l.bounds, &r.bounds, eq_generic_bound)
788 },
789 (EqPredicate(l), EqPredicate(r)) => eq_ty(&l.lhs_ty, &r.lhs_ty) && eq_ty(&l.rhs_ty, &r.rhs_ty),
790 _ => false,
791 }
792}
793
794pub fn eq_use_tree(l: &UseTree, r: &UseTree) -> bool {
795 eq_path(&l.prefix, &r.prefix) && eq_use_tree_kind(&l.kind, &r.kind)
796}
797
798pub fn eq_anon_const(l: &AnonConst, r: &AnonConst) -> bool {
799 eq_expr(&l.value, &r.value)
800}
801
802pub fn eq_const_item_rhs(l: &ConstItemRhsKind, r: &ConstItemRhsKind) -> bool {
803 use ConstItemRhsKind::*;
804 match (l, r) {
805 (TypeConst { rhs: Some(l) }, TypeConst { rhs: Some(r) }) => eq_anon_const(l, r),
806 (TypeConst { rhs: None }, TypeConst { rhs: None }) | (Body { rhs: None }, Body { rhs: None }) => true,
807 (Body { rhs: Some(l) }, Body { rhs: Some(r) }) => eq_expr(l, r),
808 (TypeConst { rhs: Some(..) }, TypeConst { rhs: None })
809 | (TypeConst { rhs: None }, TypeConst { rhs: Some(..) })
810 | (Body { rhs: None }, Body { rhs: Some(..) })
811 | (Body { rhs: Some(..) }, Body { rhs: None })
812 | (TypeConst { .. }, Body { .. })
813 | (Body { .. }, TypeConst { .. }) => false,
814 }
815}
816
817pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
818 use UseTreeKind::*;
819 match (l, r) {
820 (Glob(_), Glob(_)) => true,
821 (Simple(l), Simple(r)) => both(l.as_ref(), r.as_ref(), |l, r| eq_id(*l, *r)),
822 (Nested { items: l, .. }, Nested { items: r, .. }) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
823 _ => false,
824 }
825}
826
827pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
828 matches!(
829 (l, r),
830 (Defaultness::Implicit, Defaultness::Implicit)
831 | (Defaultness::Default(_), Defaultness::Default(_))
832 | (Defaultness::Final(_), Defaultness::Final(_))
833 )
834}
835
836pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
837 use VisibilityKind::*;
838 match (&l.kind, &r.kind) {
839 (Public, Public) | (Inherited, Inherited) => true,
840 (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r),
841 _ => false,
842 }
843}
844
845pub fn eq_impl_restriction(l: &ImplRestriction, r: &ImplRestriction) -> bool {
846 eq_restriction_kind(&l.kind, &r.kind)
847}
848
849fn eq_restriction_kind(l: &RestrictionKind, r: &RestrictionKind) -> bool {
850 match (l, r) {
851 (RestrictionKind::Unrestricted, RestrictionKind::Unrestricted) => true,
852 (
853 RestrictionKind::Restricted {
854 path: l_path,
855 shorthand: l_short,
856 id: _,
857 },
858 RestrictionKind::Restricted {
859 path: r_path,
860 shorthand: r_short,
861 id: _,
862 },
863 ) => l_short == r_short && eq_path(l_path, r_path),
864 _ => false,
865 }
866}
867
868pub fn eq_fn_decl(l: &FnDecl, r: &FnDecl) -> bool {
869 eq_fn_ret_ty(&l.output, &r.output)
870 && over(&l.inputs, &r.inputs, |l, r| {
871 l.is_placeholder == r.is_placeholder
872 && eq_pat(&l.pat, &r.pat)
873 && eq_ty(&l.ty, &r.ty)
874 && over(&l.attrs, &r.attrs, eq_attr)
875 })
876}
877
878pub fn eq_closure_binder(l: &ClosureBinder, r: &ClosureBinder) -> bool {
879 match (l, r) {
880 (ClosureBinder::NotPresent, ClosureBinder::NotPresent) => true,
881 (ClosureBinder::For { generic_params: lp, .. }, ClosureBinder::For { generic_params: rp, .. }) => {
882 lp.len() == rp.len() && std::iter::zip(lp.iter(), rp.iter()).all(|(l, r)| eq_generic_param(l, r))
883 },
884 _ => false,
885 }
886}
887
888pub fn eq_fn_ret_ty(l: &FnRetTy, r: &FnRetTy) -> bool {
889 match (l, r) {
890 (FnRetTy::Default(_), FnRetTy::Default(_)) => true,
891 (FnRetTy::Ty(l), FnRetTy::Ty(r)) => eq_ty(l, r),
892 _ => false,
893 }
894}
895
896pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
897 use TyKind::*;
898 match (&l.kind, &r.kind) {
899 (Paren(l), _) => eq_ty(l, r),
900 (_, Paren(r)) => eq_ty(l, r),
901 (Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err(_), Err(_)) | (CVarArgs, CVarArgs) => {
902 true
903 },
904 (Slice(l), Slice(r)) => eq_ty(l, r),
905 (Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
906 (Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),
907 (Ref(ll, l), Ref(rl, r)) => {
908 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)
909 },
910 (PinnedRef(ll, l), PinnedRef(rl, r)) => {
911 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)
912 },
913 (FnPtr(l), FnPtr(r)) => {
914 l.safety == r.safety
915 && eq_ext(&l.ext, &r.ext)
916 && over(&l.generic_params, &r.generic_params, eq_generic_param)
917 && eq_fn_decl(&l.decl, &r.decl)
918 },
919 (Tup(l), Tup(r)) => over(l, r, |l, r| eq_ty(l, r)),
920 (Path(lq, lp), Path(rq, rp)) => both(lq.as_deref(), rq.as_deref(), eq_qself) && eq_path(lp, rp),
921 (TraitObject(lg, ls), TraitObject(rg, rs)) => ls == rs && over(lg, rg, eq_generic_bound),
922 (ImplTrait(_, lg), ImplTrait(_, rg)) => over(lg, rg, eq_generic_bound),
923 (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
924 _ => false,
925 }
926}
927
928pub fn eq_ext(l: &Extern, r: &Extern) -> bool {
929 use Extern::*;
930 match (l, r) {
931 (None, None) | (Implicit(_), Implicit(_)) => true,
932 (Explicit(l, _), Explicit(r, _)) => eq_str_lit(l, r),
933 _ => false,
934 }
935}
936
937pub fn eq_str_lit(l: &StrLit, r: &StrLit) -> bool {
938 l.style == r.style && l.symbol == r.symbol && l.suffix == r.suffix
939}
940
941pub fn eq_poly_ref_trait(l: &PolyTraitRef, r: &PolyTraitRef) -> bool {
942 l.modifiers == r.modifiers
943 && eq_path(&l.trait_ref.path, &r.trait_ref.path)
944 && over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
945 eq_generic_param(l, r)
946 })
947}
948
949pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
950 use GenericParamKind::*;
951 l.is_placeholder == r.is_placeholder
952 && eq_id(l.ident, r.ident)
953 && over(&l.bounds, &r.bounds, eq_generic_bound)
954 && match (&l.kind, &r.kind) {
955 (Lifetime, Lifetime) => true,
956 (Type { default: l }, Type { default: r }) => both(l.as_ref(), r.as_ref(), |l, r| eq_ty(l, r)),
957 (
958 Const {
959 ty: lt,
960 default: ld,
961 span: _,
962 },
963 Const {
964 ty: rt,
965 default: rd,
966 span: _,
967 },
968 ) => eq_ty(lt, rt) && both(ld.as_ref(), rd.as_ref(), eq_anon_const),
969 _ => false,
970 }
971 && over(&l.attrs, &r.attrs, eq_attr)
972}
973
974pub fn eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool {
975 use GenericBound::*;
976 match (l, r) {
977 (Trait(ptr1), Trait(ptr2)) => eq_poly_ref_trait(ptr1, ptr2),
978 (Outlives(l), Outlives(r)) => eq_id(l.ident, r.ident),
979 _ => false,
980 }
981}
982
983pub fn eq_precise_capture(l: &PreciseCapturingArg, r: &PreciseCapturingArg) -> bool {
984 match (l, r) {
985 (PreciseCapturingArg::Lifetime(l), PreciseCapturingArg::Lifetime(r)) => l.ident == r.ident,
986 (PreciseCapturingArg::Arg(l, _), PreciseCapturingArg::Arg(r, _)) => l.segments[0].ident == r.segments[0].ident,
987 _ => false,
988 }
989}
990
991fn eq_term(l: &Term, r: &Term) -> bool {
992 match (l, r) {
993 (Term::Ty(l), Term::Ty(r)) => eq_ty(l, r),
994 (Term::Const(l), Term::Const(r)) => eq_anon_const(l, r),
995 _ => false,
996 }
997}
998
999pub fn eq_assoc_item_constraint(l: &AssocItemConstraint, r: &AssocItemConstraint) -> bool {
1000 use AssocItemConstraintKind::*;
1001 eq_id(l.ident, r.ident)
1002 && match (&l.kind, &r.kind) {
1003 (Equality { term: l }, Equality { term: r }) => eq_term(l, r),
1004 (Bound { bounds: l }, Bound { bounds: r }) => over(l, r, eq_generic_bound),
1005 _ => false,
1006 }
1007}
1008
1009pub fn eq_mac_call(l: &MacCall, r: &MacCall) -> bool {
1010 eq_path(&l.path, &r.path) && eq_delim_args(&l.args, &r.args)
1011}
1012
1013pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool {
1014 use AttrKind::*;
1015 l.style == r.style
1016 && match (&l.kind, &r.kind) {
1017 (DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2,
1018 (Normal(l), Normal(r)) => {
1019 eq_path(&l.item.path, &r.item.path) && eq_attr_item_kind(&l.item.args, &r.item.args)
1020 },
1021 _ => false,
1022 }
1023}
1024
1025pub fn eq_attr_item_kind(l: &AttrItemKind, r: &AttrItemKind) -> bool {
1026 match (l, r) {
1027 (AttrItemKind::Unparsed(l), AttrItemKind::Unparsed(r)) => eq_attr_args(l, r),
1028 (AttrItemKind::Parsed(_l), AttrItemKind::Parsed(_r)) => todo!(),
1029 _ => false,
1030 }
1031}
1032
1033pub fn eq_attr_args(l: &AttrArgs, r: &AttrArgs) -> bool {
1034 use AttrArgs::*;
1035 match (l, r) {
1036 (Empty, Empty) => true,
1037 (Delimited(la), Delimited(ra)) => eq_delim_args(la, ra),
1038 (Eq { eq_span: _, expr: le }, Eq { eq_span: _, expr: re }) => eq_expr(le, re),
1039 _ => false,
1040 }
1041}
1042
1043pub fn eq_delim_args(l: &DelimArgs, r: &DelimArgs) -> bool {
1044 l.delim == r.delim
1045 && l.tokens.len() == r.tokens.len()
1046 && l.tokens.iter().zip(r.tokens.iter()).all(|(a, b)| a.eq_unspanned(b))
1047}
1048
1049pub fn is_cfg_test(item: &impl HasAttrs) -> bool {
1051 item.attrs().iter().any(|attr| {
1052 if attr.has_name(sym::cfg)
1053 && let Some(item_list) = attr.meta_item_list()
1054 && item_list.iter().any(|item| item.has_name(sym::test))
1055 {
1056 true
1057 } else {
1058 false
1059 }
1060 })
1061}