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