]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | //! Utilities for manipulating and extracting information from `rustc_ast::ast`. |
2 | //! | |
3 | //! - The `eq_foobar` functions test for semantic equality but ignores `NodeId`s and `Span`s. | |
4 | ||
5 | #![allow(clippy::similar_names, clippy::wildcard_imports, clippy::enum_glob_use)] | |
6 | ||
7 | use crate::{both, over}; | |
8 | use rustc_ast::ptr::P; | |
9 | use rustc_ast::{self as ast, *}; | |
10 | use rustc_span::symbol::Ident; | |
11 | use std::mem; | |
12 | ||
13 | pub mod ident_iter; | |
14 | pub use ident_iter::IdentIter; | |
15 | ||
16 | pub fn is_useless_with_eq_exprs(kind: BinOpKind) -> bool { | |
17 | use BinOpKind::*; | |
18 | matches!( | |
19 | kind, | |
20 | Sub | Div | Eq | Lt | Le | Gt | Ge | Ne | And | Or | BitXor | BitAnd | BitOr | |
21 | ) | |
22 | } | |
23 | ||
24 | /// Checks if each element in the first slice is contained within the latter as per `eq_fn`. | |
25 | pub fn unordered_over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool { | |
26 | left.len() == right.len() && left.iter().all(|l| right.iter().any(|r| eq_fn(l, r))) | |
27 | } | |
28 | ||
29 | pub fn eq_id(l: Ident, r: Ident) -> bool { | |
30 | l.name == r.name | |
31 | } | |
32 | ||
33 | pub fn eq_pat(l: &Pat, r: &Pat) -> bool { | |
34 | use PatKind::*; | |
35 | match (&l.kind, &r.kind) { | |
36 | (Paren(l), _) => eq_pat(l, r), | |
37 | (_, Paren(r)) => eq_pat(l, r), | |
38 | (Wild, Wild) | (Rest, Rest) => true, | |
39 | (Lit(l), Lit(r)) => eq_expr(l, r), | |
40 | (Ident(b1, i1, s1), Ident(b2, i2, s2)) => b1 == b2 && eq_id(*i1, *i2) && both(s1, s2, |l, r| eq_pat(l, r)), | |
41 | (Range(lf, lt, le), Range(rf, rt, re)) => { | |
42 | eq_expr_opt(lf, rf) && eq_expr_opt(lt, rt) && eq_range_end(&le.node, &re.node) | |
43 | }, | |
44 | (Box(l), Box(r)) | |
45 | | (Ref(l, Mutability::Not), Ref(r, Mutability::Not)) | |
46 | | (Ref(l, Mutability::Mut), Ref(r, Mutability::Mut)) => eq_pat(l, r), | |
47 | (Tuple(l), Tuple(r)) | (Slice(l), Slice(r)) => over(l, r, |l, r| eq_pat(l, r)), | |
c295e0f8 | 48 | (Path(lq, lp), Path(rq, rp)) => both(lq, rq, eq_qself) && eq_path(lp, rp), |
136023e0 XL |
49 | (TupleStruct(lqself, lp, lfs), TupleStruct(rqself, rp, rfs)) => { |
50 | eq_maybe_qself(lqself, rqself) && eq_path(lp, rp) && over(lfs, rfs, |l, r| eq_pat(l, r)) | |
51 | }, | |
17df50a5 | 52 | (Struct(lqself, lp, lfs, lr), Struct(rqself, rp, rfs, rr)) => { |
c295e0f8 | 53 | lr == rr && eq_maybe_qself(lqself, rqself) && eq_path(lp, rp) && unordered_over(lfs, rfs, eq_field_pat) |
f20569fa XL |
54 | }, |
55 | (Or(ls), Or(rs)) => unordered_over(ls, rs, |l, r| eq_pat(l, r)), | |
56 | (MacCall(l), MacCall(r)) => eq_mac_call(l, r), | |
57 | _ => false, | |
58 | } | |
59 | } | |
60 | ||
61 | pub fn eq_range_end(l: &RangeEnd, r: &RangeEnd) -> bool { | |
62 | match (l, r) { | |
63 | (RangeEnd::Excluded, RangeEnd::Excluded) => true, | |
64 | (RangeEnd::Included(l), RangeEnd::Included(r)) => { | |
65 | matches!(l, RangeSyntax::DotDotEq) == matches!(r, RangeSyntax::DotDotEq) | |
66 | }, | |
67 | _ => false, | |
68 | } | |
69 | } | |
70 | ||
71 | pub fn eq_field_pat(l: &PatField, r: &PatField) -> bool { | |
72 | l.is_placeholder == r.is_placeholder | |
73 | && eq_id(l.ident, r.ident) | |
74 | && eq_pat(&l.pat, &r.pat) | |
c295e0f8 | 75 | && over(&l.attrs, &r.attrs, eq_attr) |
f20569fa XL |
76 | } |
77 | ||
487cf647 | 78 | pub fn eq_qself(l: &P<QSelf>, r: &P<QSelf>) -> bool { |
f20569fa XL |
79 | l.position == r.position && eq_ty(&l.ty, &r.ty) |
80 | } | |
81 | ||
487cf647 | 82 | pub fn eq_maybe_qself(l: &Option<P<QSelf>>, r: &Option<P<QSelf>>) -> bool { |
17df50a5 XL |
83 | match (l, r) { |
84 | (Some(l), Some(r)) => eq_qself(l, r), | |
85 | (None, None) => true, | |
136023e0 | 86 | _ => false, |
17df50a5 XL |
87 | } |
88 | } | |
89 | ||
f20569fa | 90 | pub fn eq_path(l: &Path, r: &Path) -> bool { |
c295e0f8 | 91 | over(&l.segments, &r.segments, eq_path_seg) |
f20569fa XL |
92 | } |
93 | ||
94 | pub fn eq_path_seg(l: &PathSegment, r: &PathSegment) -> bool { | |
95 | eq_id(l.ident, r.ident) && both(&l.args, &r.args, |l, r| eq_generic_args(l, r)) | |
96 | } | |
97 | ||
98 | pub fn eq_generic_args(l: &GenericArgs, r: &GenericArgs) -> bool { | |
99 | match (l, r) { | |
c295e0f8 | 100 | (GenericArgs::AngleBracketed(l), GenericArgs::AngleBracketed(r)) => over(&l.args, &r.args, eq_angle_arg), |
f20569fa XL |
101 | (GenericArgs::Parenthesized(l), GenericArgs::Parenthesized(r)) => { |
102 | over(&l.inputs, &r.inputs, |l, r| eq_ty(l, r)) && eq_fn_ret_ty(&l.output, &r.output) | |
103 | }, | |
104 | _ => false, | |
105 | } | |
106 | } | |
107 | ||
108 | pub fn eq_angle_arg(l: &AngleBracketedArg, r: &AngleBracketedArg) -> bool { | |
109 | match (l, r) { | |
110 | (AngleBracketedArg::Arg(l), AngleBracketedArg::Arg(r)) => eq_generic_arg(l, r), | |
111 | (AngleBracketedArg::Constraint(l), AngleBracketedArg::Constraint(r)) => eq_assoc_constraint(l, r), | |
112 | _ => false, | |
113 | } | |
114 | } | |
115 | ||
116 | pub fn eq_generic_arg(l: &GenericArg, r: &GenericArg) -> bool { | |
117 | match (l, r) { | |
118 | (GenericArg::Lifetime(l), GenericArg::Lifetime(r)) => eq_id(l.ident, r.ident), | |
119 | (GenericArg::Type(l), GenericArg::Type(r)) => eq_ty(l, r), | |
120 | (GenericArg::Const(l), GenericArg::Const(r)) => eq_expr(&l.value, &r.value), | |
121 | _ => false, | |
122 | } | |
123 | } | |
124 | ||
125 | pub fn eq_expr_opt(l: &Option<P<Expr>>, r: &Option<P<Expr>>) -> bool { | |
126 | both(l, r, |l, r| eq_expr(l, r)) | |
127 | } | |
128 | ||
129 | pub fn eq_struct_rest(l: &StructRest, r: &StructRest) -> bool { | |
130 | match (l, r) { | |
131 | (StructRest::Base(lb), StructRest::Base(rb)) => eq_expr(lb, rb), | |
132 | (StructRest::Rest(_), StructRest::Rest(_)) | (StructRest::None, StructRest::None) => true, | |
133 | _ => false, | |
134 | } | |
135 | } | |
136 | ||
137 | pub fn eq_expr(l: &Expr, r: &Expr) -> bool { | |
138 | use ExprKind::*; | |
c295e0f8 | 139 | if !over(&l.attrs, &r.attrs, eq_attr) { |
f20569fa XL |
140 | return false; |
141 | } | |
142 | match (&l.kind, &r.kind) { | |
143 | (Paren(l), _) => eq_expr(l, r), | |
144 | (_, Paren(r)) => eq_expr(l, r), | |
145 | (Err, Err) => true, | |
146 | (Box(l), Box(r)) | (Try(l), Try(r)) | (Await(l), Await(r)) => eq_expr(l, r), | |
147 | (Array(l), Array(r)) | (Tup(l), Tup(r)) => over(l, r, |l, r| eq_expr(l, r)), | |
148 | (Repeat(le, ls), Repeat(re, rs)) => eq_expr(le, re) && eq_expr(&ls.value, &rs.value), | |
149 | (Call(lc, la), Call(rc, ra)) => eq_expr(lc, rc) && over(la, ra, |l, r| eq_expr(l, r)), | |
487cf647 FG |
150 | ( |
151 | MethodCall(box ast::MethodCall { | |
152 | seg: ls, | |
153 | receiver: lr, | |
154 | args: la, | |
155 | .. | |
156 | }), | |
157 | MethodCall(box ast::MethodCall { | |
158 | seg: rs, | |
159 | receiver: rr, | |
160 | args: ra, | |
161 | .. | |
162 | }), | |
163 | ) => eq_path_seg(ls, rs) && eq_expr(lr, rr) && over(la, ra, |l, r| eq_expr(l, r)), | |
f20569fa XL |
164 | (Binary(lo, ll, lr), Binary(ro, rl, rr)) => lo.node == ro.node && eq_expr(ll, rl) && eq_expr(lr, rr), |
165 | (Unary(lo, l), Unary(ro, r)) => mem::discriminant(lo) == mem::discriminant(ro) && eq_expr(l, r), | |
487cf647 | 166 | (Lit(l), Lit(r)) => l == r, |
f20569fa | 167 | (Cast(l, lt), Cast(r, rt)) | (Type(l, lt), Type(r, rt)) => eq_expr(l, r) && eq_ty(lt, rt), |
94222f64 | 168 | (Let(lp, le, _), Let(rp, re, _)) => eq_pat(lp, rp) && eq_expr(le, re), |
f20569fa XL |
169 | (If(lc, lt, le), If(rc, rt, re)) => eq_expr(lc, rc) && eq_block(lt, rt) && eq_expr_opt(le, re), |
170 | (While(lc, lt, ll), While(rc, rt, rl)) => eq_label(ll, rl) && eq_expr(lc, rc) && eq_block(lt, rt), | |
171 | (ForLoop(lp, li, lt, ll), ForLoop(rp, ri, rt, rl)) => { | |
172 | eq_label(ll, rl) && eq_pat(lp, rp) && eq_expr(li, ri) && eq_block(lt, rt) | |
173 | }, | |
487cf647 | 174 | (Loop(lt, ll, _), Loop(rt, rl, _)) => eq_label(ll, rl) && eq_block(lt, rt), |
f20569fa XL |
175 | (Block(lb, ll), Block(rb, rl)) => eq_label(ll, rl) && eq_block(lb, rb), |
176 | (TryBlock(l), TryBlock(r)) => eq_block(l, r), | |
177 | (Yield(l), Yield(r)) | (Ret(l), Ret(r)) => eq_expr_opt(l, r), | |
178 | (Break(ll, le), Break(rl, re)) => eq_label(ll, rl) && eq_expr_opt(le, re), | |
179 | (Continue(ll), Continue(rl)) => eq_label(ll, rl), | |
180 | (Assign(l1, l2, _), Assign(r1, r2, _)) | (Index(l1, l2), Index(r1, r2)) => eq_expr(l1, r1) && eq_expr(l2, r2), | |
181 | (AssignOp(lo, lp, lv), AssignOp(ro, rp, rv)) => lo.node == ro.node && eq_expr(lp, rp) && eq_expr(lv, rv), | |
182 | (Field(lp, lf), Field(rp, rf)) => eq_id(*lf, *rf) && eq_expr(lp, rp), | |
c295e0f8 | 183 | (Match(ls, la), Match(rs, ra)) => eq_expr(ls, rs) && over(la, ra, eq_arm), |
487cf647 FG |
184 | ( |
185 | Closure(box ast::Closure { | |
186 | binder: lb, | |
187 | capture_clause: lc, | |
188 | asyncness: la, | |
189 | movability: lm, | |
190 | fn_decl: lf, | |
191 | body: le, | |
192 | .. | |
193 | }), | |
194 | Closure(box ast::Closure { | |
195 | binder: rb, | |
196 | capture_clause: rc, | |
197 | asyncness: ra, | |
198 | movability: rm, | |
199 | fn_decl: rf, | |
200 | body: re, | |
201 | .. | |
202 | }), | |
203 | ) => { | |
064997fb FG |
204 | eq_closure_binder(lb, rb) |
205 | && lc == rc | |
206 | && la.is_async() == ra.is_async() | |
207 | && lm == rm | |
208 | && eq_fn_decl(lf, rf) | |
209 | && eq_expr(le, re) | |
f20569fa XL |
210 | }, |
211 | (Async(lc, _, lb), Async(rc, _, rb)) => lc == rc && eq_block(lb, rb), | |
212 | (Range(lf, lt, ll), Range(rf, rt, rl)) => ll == rl && eq_expr_opt(lf, rf) && eq_expr_opt(lt, rt), | |
213 | (AddrOf(lbk, lm, le), AddrOf(rbk, rm, re)) => lbk == rbk && lm == rm && eq_expr(le, re), | |
c295e0f8 | 214 | (Path(lq, lp), Path(rq, rp)) => both(lq, rq, eq_qself) && eq_path(lp, rp), |
f20569fa XL |
215 | (MacCall(l), MacCall(r)) => eq_mac_call(l, r), |
216 | (Struct(lse), Struct(rse)) => { | |
136023e0 | 217 | eq_maybe_qself(&lse.qself, &rse.qself) |
17df50a5 | 218 | && eq_path(&lse.path, &rse.path) |
cdc7bbd5 | 219 | && eq_struct_rest(&lse.rest, &rse.rest) |
c295e0f8 | 220 | && unordered_over(&lse.fields, &rse.fields, eq_field) |
f20569fa XL |
221 | }, |
222 | _ => false, | |
223 | } | |
224 | } | |
225 | ||
226 | pub fn eq_field(l: &ExprField, r: &ExprField) -> bool { | |
227 | l.is_placeholder == r.is_placeholder | |
228 | && eq_id(l.ident, r.ident) | |
229 | && eq_expr(&l.expr, &r.expr) | |
c295e0f8 | 230 | && over(&l.attrs, &r.attrs, eq_attr) |
f20569fa XL |
231 | } |
232 | ||
233 | pub fn eq_arm(l: &Arm, r: &Arm) -> bool { | |
234 | l.is_placeholder == r.is_placeholder | |
235 | && eq_pat(&l.pat, &r.pat) | |
236 | && eq_expr(&l.body, &r.body) | |
237 | && eq_expr_opt(&l.guard, &r.guard) | |
c295e0f8 | 238 | && over(&l.attrs, &r.attrs, eq_attr) |
f20569fa XL |
239 | } |
240 | ||
241 | pub fn eq_label(l: &Option<Label>, r: &Option<Label>) -> bool { | |
242 | both(l, r, |l, r| eq_id(l.ident, r.ident)) | |
243 | } | |
244 | ||
245 | pub fn eq_block(l: &Block, r: &Block) -> bool { | |
c295e0f8 | 246 | l.rules == r.rules && over(&l.stmts, &r.stmts, eq_stmt) |
f20569fa XL |
247 | } |
248 | ||
249 | pub fn eq_stmt(l: &Stmt, r: &Stmt) -> bool { | |
250 | use StmtKind::*; | |
251 | match (&l.kind, &r.kind) { | |
252 | (Local(l), Local(r)) => { | |
253 | eq_pat(&l.pat, &r.pat) | |
254 | && both(&l.ty, &r.ty, |l, r| eq_ty(l, r)) | |
94222f64 | 255 | && eq_local_kind(&l.kind, &r.kind) |
c295e0f8 | 256 | && over(&l.attrs, &r.attrs, eq_attr) |
f20569fa XL |
257 | }, |
258 | (Item(l), Item(r)) => eq_item(l, r, eq_item_kind), | |
259 | (Expr(l), Expr(r)) | (Semi(l), Semi(r)) => eq_expr(l, r), | |
260 | (Empty, Empty) => true, | |
261 | (MacCall(l), MacCall(r)) => { | |
c295e0f8 | 262 | l.style == r.style && eq_mac_call(&l.mac, &r.mac) && over(&l.attrs, &r.attrs, eq_attr) |
f20569fa XL |
263 | }, |
264 | _ => false, | |
265 | } | |
266 | } | |
267 | ||
94222f64 XL |
268 | pub fn eq_local_kind(l: &LocalKind, r: &LocalKind) -> bool { |
269 | use LocalKind::*; | |
270 | match (l, r) { | |
271 | (Decl, Decl) => true, | |
272 | (Init(l), Init(r)) => eq_expr(l, r), | |
273 | (InitElse(li, le), InitElse(ri, re)) => eq_expr(li, ri) && eq_block(le, re), | |
274 | _ => false, | |
275 | } | |
276 | } | |
277 | ||
f20569fa | 278 | pub fn eq_item<K>(l: &Item<K>, r: &Item<K>, mut eq_kind: impl FnMut(&K, &K) -> bool) -> bool { |
c295e0f8 | 279 | eq_id(l.ident, r.ident) && over(&l.attrs, &r.attrs, eq_attr) && eq_vis(&l.vis, &r.vis) && eq_kind(&l.kind, &r.kind) |
f20569fa XL |
280 | } |
281 | ||
923072b8 | 282 | #[expect(clippy::too_many_lines)] // Just a big match statement |
f20569fa XL |
283 | pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool { |
284 | use ItemKind::*; | |
285 | match (l, r) { | |
286 | (ExternCrate(l), ExternCrate(r)) => l == r, | |
287 | (Use(l), Use(r)) => eq_use_tree(l, r), | |
288 | (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), | |
289 | (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re), | |
a2a8927a XL |
290 | ( |
291 | Fn(box ast::Fn { | |
292 | defaultness: ld, | |
293 | sig: lf, | |
294 | generics: lg, | |
295 | body: lb, | |
296 | }), | |
297 | Fn(box ast::Fn { | |
298 | defaultness: rd, | |
299 | sig: rf, | |
300 | generics: rg, | |
301 | body: rb, | |
302 | }), | |
303 | ) => { | |
f20569fa XL |
304 | eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r)) |
305 | }, | |
306 | (Mod(lu, lmk), Mod(ru, rmk)) => { | |
307 | lu == ru | |
308 | && match (lmk, rmk) { | |
309 | (ModKind::Loaded(litems, linline, _), ModKind::Loaded(ritems, rinline, _)) => { | |
310 | linline == rinline && over(litems, ritems, |l, r| eq_item(l, r, eq_item_kind)) | |
311 | }, | |
312 | (ModKind::Unloaded, ModKind::Unloaded) => true, | |
313 | _ => false, | |
314 | } | |
315 | }, | |
316 | (ForeignMod(l), ForeignMod(r)) => { | |
c295e0f8 | 317 | both(&l.abi, &r.abi, eq_str_lit) && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind)) |
f20569fa | 318 | }, |
a2a8927a XL |
319 | ( |
320 | TyAlias(box ast::TyAlias { | |
321 | defaultness: ld, | |
322 | generics: lg, | |
323 | bounds: lb, | |
324 | ty: lt, | |
5e7ed085 | 325 | .. |
a2a8927a XL |
326 | }), |
327 | TyAlias(box ast::TyAlias { | |
328 | defaultness: rd, | |
329 | generics: rg, | |
330 | bounds: rb, | |
331 | ty: rt, | |
5e7ed085 | 332 | .. |
a2a8927a XL |
333 | }), |
334 | ) => { | |
f20569fa XL |
335 | eq_defaultness(*ld, *rd) |
336 | && eq_generics(lg, rg) | |
c295e0f8 | 337 | && over(lb, rb, eq_generic_bound) |
f20569fa XL |
338 | && both(lt, rt, |l, r| eq_ty(l, r)) |
339 | }, | |
c295e0f8 | 340 | (Enum(le, lg), Enum(re, rg)) => over(&le.variants, &re.variants, eq_variant) && eq_generics(lg, rg), |
f20569fa XL |
341 | (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => { |
342 | eq_variant_data(lv, rv) && eq_generics(lg, rg) | |
343 | }, | |
a2a8927a XL |
344 | ( |
345 | Trait(box ast::Trait { | |
346 | is_auto: la, | |
347 | unsafety: lu, | |
348 | generics: lg, | |
349 | bounds: lb, | |
350 | items: li, | |
351 | }), | |
352 | Trait(box ast::Trait { | |
353 | is_auto: ra, | |
354 | unsafety: ru, | |
355 | generics: rg, | |
356 | bounds: rb, | |
357 | items: ri, | |
358 | }), | |
359 | ) => { | |
f20569fa XL |
360 | la == ra |
361 | && matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No) | |
362 | && eq_generics(lg, rg) | |
c295e0f8 | 363 | && over(lb, rb, eq_generic_bound) |
f20569fa XL |
364 | && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind)) |
365 | }, | |
c295e0f8 | 366 | (TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, eq_generic_bound), |
f20569fa | 367 | ( |
3c0e092e | 368 | Impl(box ast::Impl { |
f20569fa XL |
369 | unsafety: lu, |
370 | polarity: lp, | |
371 | defaultness: ld, | |
372 | constness: lc, | |
373 | generics: lg, | |
374 | of_trait: lot, | |
375 | self_ty: lst, | |
376 | items: li, | |
377 | }), | |
3c0e092e | 378 | Impl(box ast::Impl { |
f20569fa XL |
379 | unsafety: ru, |
380 | polarity: rp, | |
381 | defaultness: rd, | |
382 | constness: rc, | |
383 | generics: rg, | |
384 | of_trait: rot, | |
385 | self_ty: rst, | |
386 | items: ri, | |
387 | }), | |
388 | ) => { | |
389 | matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No) | |
390 | && matches!(lp, ImplPolarity::Positive) == matches!(rp, ImplPolarity::Positive) | |
391 | && eq_defaultness(*ld, *rd) | |
392 | && matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No) | |
393 | && eq_generics(lg, rg) | |
394 | && both(lot, rot, |l, r| eq_path(&l.path, &r.path)) | |
395 | && eq_ty(lst, rst) | |
396 | && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind)) | |
397 | }, | |
398 | (MacCall(l), MacCall(r)) => eq_mac_call(l, r), | |
487cf647 | 399 | (MacroDef(l), MacroDef(r)) => l.macro_rules == r.macro_rules && eq_delim_args(&l.body, &r.body), |
f20569fa XL |
400 | _ => false, |
401 | } | |
402 | } | |
403 | ||
404 | pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool { | |
405 | use ForeignItemKind::*; | |
406 | match (l, r) { | |
407 | (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), | |
a2a8927a XL |
408 | ( |
409 | Fn(box ast::Fn { | |
410 | defaultness: ld, | |
411 | sig: lf, | |
412 | generics: lg, | |
413 | body: lb, | |
414 | }), | |
415 | Fn(box ast::Fn { | |
416 | defaultness: rd, | |
417 | sig: rf, | |
418 | generics: rg, | |
419 | body: rb, | |
420 | }), | |
421 | ) => { | |
f20569fa XL |
422 | eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r)) |
423 | }, | |
a2a8927a XL |
424 | ( |
425 | TyAlias(box ast::TyAlias { | |
426 | defaultness: ld, | |
427 | generics: lg, | |
428 | bounds: lb, | |
429 | ty: lt, | |
5e7ed085 | 430 | .. |
a2a8927a XL |
431 | }), |
432 | TyAlias(box ast::TyAlias { | |
433 | defaultness: rd, | |
434 | generics: rg, | |
435 | bounds: rb, | |
436 | ty: rt, | |
5e7ed085 | 437 | .. |
a2a8927a XL |
438 | }), |
439 | ) => { | |
f20569fa XL |
440 | eq_defaultness(*ld, *rd) |
441 | && eq_generics(lg, rg) | |
c295e0f8 | 442 | && over(lb, rb, eq_generic_bound) |
f20569fa XL |
443 | && both(lt, rt, |l, r| eq_ty(l, r)) |
444 | }, | |
445 | (MacCall(l), MacCall(r)) => eq_mac_call(l, r), | |
446 | _ => false, | |
447 | } | |
448 | } | |
449 | ||
450 | pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool { | |
451 | use AssocItemKind::*; | |
452 | match (l, r) { | |
453 | (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re), | |
a2a8927a XL |
454 | ( |
455 | Fn(box ast::Fn { | |
456 | defaultness: ld, | |
457 | sig: lf, | |
458 | generics: lg, | |
459 | body: lb, | |
460 | }), | |
461 | Fn(box ast::Fn { | |
462 | defaultness: rd, | |
463 | sig: rf, | |
464 | generics: rg, | |
465 | body: rb, | |
466 | }), | |
467 | ) => { | |
f20569fa XL |
468 | eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r)) |
469 | }, | |
a2a8927a | 470 | ( |
2b03887a | 471 | Type(box ast::TyAlias { |
a2a8927a XL |
472 | defaultness: ld, |
473 | generics: lg, | |
474 | bounds: lb, | |
475 | ty: lt, | |
5e7ed085 | 476 | .. |
a2a8927a | 477 | }), |
2b03887a | 478 | Type(box ast::TyAlias { |
a2a8927a XL |
479 | defaultness: rd, |
480 | generics: rg, | |
481 | bounds: rb, | |
482 | ty: rt, | |
5e7ed085 | 483 | .. |
a2a8927a XL |
484 | }), |
485 | ) => { | |
f20569fa XL |
486 | eq_defaultness(*ld, *rd) |
487 | && eq_generics(lg, rg) | |
c295e0f8 | 488 | && over(lb, rb, eq_generic_bound) |
f20569fa XL |
489 | && both(lt, rt, |l, r| eq_ty(l, r)) |
490 | }, | |
491 | (MacCall(l), MacCall(r)) => eq_mac_call(l, r), | |
492 | _ => false, | |
493 | } | |
494 | } | |
495 | ||
496 | pub fn eq_variant(l: &Variant, r: &Variant) -> bool { | |
497 | l.is_placeholder == r.is_placeholder | |
c295e0f8 | 498 | && over(&l.attrs, &r.attrs, eq_attr) |
f20569fa XL |
499 | && eq_vis(&l.vis, &r.vis) |
500 | && eq_id(l.ident, r.ident) | |
501 | && eq_variant_data(&l.data, &r.data) | |
502 | && both(&l.disr_expr, &r.disr_expr, |l, r| eq_expr(&l.value, &r.value)) | |
503 | } | |
504 | ||
505 | pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool { | |
506 | use VariantData::*; | |
507 | match (l, r) { | |
508 | (Unit(_), Unit(_)) => true, | |
c295e0f8 | 509 | (Struct(l, _), Struct(r, _)) | (Tuple(l, _), Tuple(r, _)) => over(l, r, eq_struct_field), |
f20569fa XL |
510 | _ => false, |
511 | } | |
512 | } | |
513 | ||
514 | pub fn eq_struct_field(l: &FieldDef, r: &FieldDef) -> bool { | |
515 | l.is_placeholder == r.is_placeholder | |
c295e0f8 | 516 | && over(&l.attrs, &r.attrs, eq_attr) |
f20569fa XL |
517 | && eq_vis(&l.vis, &r.vis) |
518 | && both(&l.ident, &r.ident, |l, r| eq_id(*l, *r)) | |
519 | && eq_ty(&l.ty, &r.ty) | |
520 | } | |
521 | ||
522 | pub fn eq_fn_sig(l: &FnSig, r: &FnSig) -> bool { | |
523 | eq_fn_decl(&l.decl, &r.decl) && eq_fn_header(&l.header, &r.header) | |
524 | } | |
525 | ||
526 | pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool { | |
527 | matches!(l.unsafety, Unsafe::No) == matches!(r.unsafety, Unsafe::No) | |
528 | && l.asyncness.is_async() == r.asyncness.is_async() | |
529 | && matches!(l.constness, Const::No) == matches!(r.constness, Const::No) | |
530 | && eq_ext(&l.ext, &r.ext) | |
531 | } | |
532 | ||
533 | pub fn eq_generics(l: &Generics, r: &Generics) -> bool { | |
c295e0f8 | 534 | over(&l.params, &r.params, eq_generic_param) |
f20569fa XL |
535 | && over(&l.where_clause.predicates, &r.where_clause.predicates, |l, r| { |
536 | eq_where_predicate(l, r) | |
537 | }) | |
538 | } | |
539 | ||
540 | pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool { | |
541 | use WherePredicate::*; | |
542 | match (l, r) { | |
543 | (BoundPredicate(l), BoundPredicate(r)) => { | |
544 | over(&l.bound_generic_params, &r.bound_generic_params, |l, r| { | |
545 | eq_generic_param(l, r) | |
546 | }) && eq_ty(&l.bounded_ty, &r.bounded_ty) | |
c295e0f8 | 547 | && over(&l.bounds, &r.bounds, eq_generic_bound) |
f20569fa XL |
548 | }, |
549 | (RegionPredicate(l), RegionPredicate(r)) => { | |
c295e0f8 | 550 | eq_id(l.lifetime.ident, r.lifetime.ident) && over(&l.bounds, &r.bounds, eq_generic_bound) |
f20569fa XL |
551 | }, |
552 | (EqPredicate(l), EqPredicate(r)) => eq_ty(&l.lhs_ty, &r.lhs_ty) && eq_ty(&l.rhs_ty, &r.rhs_ty), | |
553 | _ => false, | |
554 | } | |
555 | } | |
556 | ||
557 | pub fn eq_use_tree(l: &UseTree, r: &UseTree) -> bool { | |
558 | eq_path(&l.prefix, &r.prefix) && eq_use_tree_kind(&l.kind, &r.kind) | |
559 | } | |
560 | ||
cdc7bbd5 XL |
561 | pub fn eq_anon_const(l: &AnonConst, r: &AnonConst) -> bool { |
562 | eq_expr(&l.value, &r.value) | |
563 | } | |
564 | ||
f20569fa XL |
565 | pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool { |
566 | use UseTreeKind::*; | |
567 | match (l, r) { | |
568 | (Glob, Glob) => true, | |
487cf647 | 569 | (Simple(l), Simple(r)) => both(l, r, |l, r| eq_id(*l, *r)), |
f20569fa XL |
570 | (Nested(l), Nested(r)) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)), |
571 | _ => false, | |
572 | } | |
573 | } | |
574 | ||
f20569fa XL |
575 | pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool { |
576 | matches!( | |
577 | (l, r), | |
578 | (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_)) | |
579 | ) | |
580 | } | |
581 | ||
582 | pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool { | |
583 | use VisibilityKind::*; | |
584 | match (&l.kind, &r.kind) { | |
923072b8 | 585 | (Public, Public) | (Inherited, Inherited) => true, |
f20569fa XL |
586 | (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r), |
587 | _ => false, | |
588 | } | |
589 | } | |
590 | ||
591 | pub fn eq_fn_decl(l: &FnDecl, r: &FnDecl) -> bool { | |
592 | eq_fn_ret_ty(&l.output, &r.output) | |
593 | && over(&l.inputs, &r.inputs, |l, r| { | |
594 | l.is_placeholder == r.is_placeholder | |
595 | && eq_pat(&l.pat, &r.pat) | |
596 | && eq_ty(&l.ty, &r.ty) | |
c295e0f8 | 597 | && over(&l.attrs, &r.attrs, eq_attr) |
f20569fa XL |
598 | }) |
599 | } | |
600 | ||
064997fb FG |
601 | pub fn eq_closure_binder(l: &ClosureBinder, r: &ClosureBinder) -> bool { |
602 | match (l, r) { | |
603 | (ClosureBinder::NotPresent, ClosureBinder::NotPresent) => true, | |
604 | (ClosureBinder::For { generic_params: lp, .. }, ClosureBinder::For { generic_params: rp, .. }) => { | |
605 | lp.len() == rp.len() && std::iter::zip(lp.iter(), rp.iter()).all(|(l, r)| eq_generic_param(l, r)) | |
606 | }, | |
607 | _ => false, | |
608 | } | |
609 | } | |
610 | ||
f20569fa XL |
611 | pub fn eq_fn_ret_ty(l: &FnRetTy, r: &FnRetTy) -> bool { |
612 | match (l, r) { | |
613 | (FnRetTy::Default(_), FnRetTy::Default(_)) => true, | |
614 | (FnRetTy::Ty(l), FnRetTy::Ty(r)) => eq_ty(l, r), | |
615 | _ => false, | |
616 | } | |
617 | } | |
618 | ||
619 | pub fn eq_ty(l: &Ty, r: &Ty) -> bool { | |
620 | use TyKind::*; | |
621 | match (&l.kind, &r.kind) { | |
622 | (Paren(l), _) => eq_ty(l, r), | |
623 | (_, Paren(r)) => eq_ty(l, r), | |
624 | (Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err, Err) | (CVarArgs, CVarArgs) => true, | |
625 | (Slice(l), Slice(r)) => eq_ty(l, r), | |
626 | (Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value), | |
627 | (Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty), | |
628 | (Rptr(ll, l), Rptr(rl, r)) => { | |
629 | both(ll, rl, |l, r| eq_id(l.ident, r.ident)) && l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty) | |
630 | }, | |
631 | (BareFn(l), BareFn(r)) => { | |
632 | l.unsafety == r.unsafety | |
633 | && eq_ext(&l.ext, &r.ext) | |
c295e0f8 | 634 | && over(&l.generic_params, &r.generic_params, eq_generic_param) |
f20569fa XL |
635 | && eq_fn_decl(&l.decl, &r.decl) |
636 | }, | |
637 | (Tup(l), Tup(r)) => over(l, r, |l, r| eq_ty(l, r)), | |
c295e0f8 XL |
638 | (Path(lq, lp), Path(rq, rp)) => both(lq, rq, eq_qself) && eq_path(lp, rp), |
639 | (TraitObject(lg, ls), TraitObject(rg, rs)) => ls == rs && over(lg, rg, eq_generic_bound), | |
640 | (ImplTrait(_, lg), ImplTrait(_, rg)) => over(lg, rg, eq_generic_bound), | |
f20569fa XL |
641 | (Typeof(l), Typeof(r)) => eq_expr(&l.value, &r.value), |
642 | (MacCall(l), MacCall(r)) => eq_mac_call(l, r), | |
643 | _ => false, | |
644 | } | |
645 | } | |
646 | ||
647 | pub fn eq_ext(l: &Extern, r: &Extern) -> bool { | |
648 | use Extern::*; | |
649 | match (l, r) { | |
064997fb FG |
650 | (None, None) | (Implicit(_), Implicit(_)) => true, |
651 | (Explicit(l, _), Explicit(r, _)) => eq_str_lit(l, r), | |
f20569fa XL |
652 | _ => false, |
653 | } | |
654 | } | |
655 | ||
656 | pub fn eq_str_lit(l: &StrLit, r: &StrLit) -> bool { | |
657 | l.style == r.style && l.symbol == r.symbol && l.suffix == r.suffix | |
658 | } | |
659 | ||
660 | pub fn eq_poly_ref_trait(l: &PolyTraitRef, r: &PolyTraitRef) -> bool { | |
661 | eq_path(&l.trait_ref.path, &r.trait_ref.path) | |
662 | && over(&l.bound_generic_params, &r.bound_generic_params, |l, r| { | |
663 | eq_generic_param(l, r) | |
664 | }) | |
665 | } | |
666 | ||
667 | pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool { | |
668 | use GenericParamKind::*; | |
669 | l.is_placeholder == r.is_placeholder | |
670 | && eq_id(l.ident, r.ident) | |
c295e0f8 | 671 | && over(&l.bounds, &r.bounds, eq_generic_bound) |
f20569fa XL |
672 | && match (&l.kind, &r.kind) { |
673 | (Lifetime, Lifetime) => true, | |
674 | (Type { default: l }, Type { default: r }) => both(l, r, |l, r| eq_ty(l, r)), | |
675 | ( | |
676 | Const { | |
677 | ty: lt, | |
678 | kw_span: _, | |
679 | default: ld, | |
680 | }, | |
681 | Const { | |
682 | ty: rt, | |
683 | kw_span: _, | |
684 | default: rd, | |
685 | }, | |
c295e0f8 | 686 | ) => eq_ty(lt, rt) && both(ld, rd, eq_anon_const), |
f20569fa XL |
687 | _ => false, |
688 | } | |
c295e0f8 | 689 | && over(&l.attrs, &r.attrs, eq_attr) |
f20569fa XL |
690 | } |
691 | ||
692 | pub fn eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool { | |
693 | use GenericBound::*; | |
694 | match (l, r) { | |
695 | (Trait(ptr1, tbm1), Trait(ptr2, tbm2)) => tbm1 == tbm2 && eq_poly_ref_trait(ptr1, ptr2), | |
696 | (Outlives(l), Outlives(r)) => eq_id(l.ident, r.ident), | |
697 | _ => false, | |
698 | } | |
699 | } | |
700 | ||
5099ac24 FG |
701 | fn eq_term(l: &Term, r: &Term) -> bool { |
702 | match (l, r) { | |
703 | (Term::Ty(l), Term::Ty(r)) => eq_ty(l, r), | |
704 | (Term::Const(l), Term::Const(r)) => eq_anon_const(l, r), | |
705 | _ => false, | |
706 | } | |
707 | } | |
708 | ||
709 | pub fn eq_assoc_constraint(l: &AssocConstraint, r: &AssocConstraint) -> bool { | |
710 | use AssocConstraintKind::*; | |
f20569fa XL |
711 | eq_id(l.ident, r.ident) |
712 | && match (&l.kind, &r.kind) { | |
5099ac24 | 713 | (Equality { term: l }, Equality { term: r }) => eq_term(l, r), |
c295e0f8 | 714 | (Bound { bounds: l }, Bound { bounds: r }) => over(l, r, eq_generic_bound), |
f20569fa XL |
715 | _ => false, |
716 | } | |
717 | } | |
718 | ||
719 | pub fn eq_mac_call(l: &MacCall, r: &MacCall) -> bool { | |
487cf647 | 720 | eq_path(&l.path, &r.path) && eq_delim_args(&l.args, &r.args) |
f20569fa XL |
721 | } |
722 | ||
723 | pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool { | |
724 | use AttrKind::*; | |
725 | l.style == r.style | |
726 | && match (&l.kind, &r.kind) { | |
727 | (DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2, | |
487cf647 | 728 | (Normal(l), Normal(r)) => eq_path(&l.item.path, &r.item.path) && eq_attr_args(&l.item.args, &r.item.args), |
f20569fa XL |
729 | _ => false, |
730 | } | |
731 | } | |
732 | ||
487cf647 FG |
733 | pub fn eq_attr_args(l: &AttrArgs, r: &AttrArgs) -> bool { |
734 | use AttrArgs::*; | |
f20569fa XL |
735 | match (l, r) { |
736 | (Empty, Empty) => true, | |
487cf647 FG |
737 | (Delimited(la), Delimited(ra)) => eq_delim_args(la, ra), |
738 | (Eq(_, AttrArgsEq::Ast(le)), Eq(_, AttrArgsEq::Ast(re))) => eq_expr(le, re), | |
739 | (Eq(_, AttrArgsEq::Hir(ll)), Eq(_, AttrArgsEq::Hir(rl))) => ll.kind == rl.kind, | |
f20569fa XL |
740 | _ => false, |
741 | } | |
742 | } | |
487cf647 FG |
743 | |
744 | pub fn eq_delim_args(l: &DelimArgs, r: &DelimArgs) -> bool { | |
745 | l.delim == r.delim && l.tokens.eq_unspanned(&r.tokens) | |
746 | } |