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