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