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