]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/utils/ast_utils.rs
Use `summary_opts()` in another spot
[rust.git] / src / tools / clippy / clippy_lints / src / utils / ast_utils.rs
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::utils::{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 /// Checks if each element in the first slice is contained within the latter as per `eq_fn`.
14 pub fn unordered_over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
15     left.len() == right.len() && left.iter().all(|l| right.iter().any(|r| eq_fn(l, r)))
16 }
17
18 pub fn eq_id(l: Ident, r: Ident) -> bool {
19     l.name == r.name
20 }
21
22 pub fn eq_pat(l: &Pat, r: &Pat) -> bool {
23     use PatKind::*;
24     match (&l.kind, &r.kind) {
25         (Paren(l), _) => eq_pat(l, r),
26         (_, Paren(r)) => eq_pat(l, r),
27         (Wild, Wild) | (Rest, Rest) => true,
28         (Lit(l), Lit(r)) => eq_expr(l, r),
29         (Ident(b1, i1, s1), Ident(b2, i2, s2)) => b1 == b2 && eq_id(*i1, *i2) && both(s1, s2, |l, r| eq_pat(l, r)),
30         (Range(lf, lt, le), Range(rf, rt, re)) => {
31             eq_expr_opt(lf, rf) && eq_expr_opt(lt, rt) && eq_range_end(&le.node, &re.node)
32         },
33         (Box(l), Box(r))
34         | (Ref(l, Mutability::Not), Ref(r, Mutability::Not))
35         | (Ref(l, Mutability::Mut), Ref(r, Mutability::Mut)) => eq_pat(l, r),
36         (Tuple(l), Tuple(r)) | (Slice(l), Slice(r)) => over(l, r, |l, r| eq_pat(l, r)),
37         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
38         (TupleStruct(lp, lfs), TupleStruct(rp, rfs)) => eq_path(lp, rp) && over(lfs, rfs, |l, r| eq_pat(l, r)),
39         (Struct(lp, lfs, lr), Struct(rp, rfs, rr)) => {
40             lr == rr && eq_path(lp, rp) && unordered_over(lfs, rfs, |lf, rf| eq_field_pat(lf, rf))
41         },
42         (Or(ls), Or(rs)) => unordered_over(ls, rs, |l, r| eq_pat(l, r)),
43         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
44         _ => false,
45     }
46 }
47
48 pub fn eq_range_end(l: &RangeEnd, r: &RangeEnd) -> bool {
49     match (l, r) {
50         (RangeEnd::Excluded, RangeEnd::Excluded) => true,
51         (RangeEnd::Included(l), RangeEnd::Included(r)) => {
52             matches!(l, RangeSyntax::DotDotEq) == matches!(r, RangeSyntax::DotDotEq)
53         },
54         _ => false,
55     }
56 }
57
58 pub fn eq_field_pat(l: &FieldPat, r: &FieldPat) -> bool {
59     l.is_placeholder == r.is_placeholder
60         && eq_id(l.ident, r.ident)
61         && eq_pat(&l.pat, &r.pat)
62         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
63 }
64
65 pub fn eq_qself(l: &QSelf, r: &QSelf) -> bool {
66     l.position == r.position && eq_ty(&l.ty, &r.ty)
67 }
68
69 pub fn eq_path(l: &Path, r: &Path) -> bool {
70     over(&l.segments, &r.segments, |l, r| eq_path_seg(l, r))
71 }
72
73 pub fn eq_path_seg(l: &PathSegment, r: &PathSegment) -> bool {
74     eq_id(l.ident, r.ident) && both(&l.args, &r.args, |l, r| eq_generic_args(l, r))
75 }
76
77 pub fn eq_generic_args(l: &GenericArgs, r: &GenericArgs) -> bool {
78     match (l, r) {
79         (GenericArgs::AngleBracketed(l), GenericArgs::AngleBracketed(r)) => {
80             over(&l.args, &r.args, |l, r| eq_angle_arg(l, r))
81         },
82         (GenericArgs::Parenthesized(l), GenericArgs::Parenthesized(r)) => {
83             over(&l.inputs, &r.inputs, |l, r| eq_ty(l, r)) && eq_fn_ret_ty(&l.output, &r.output)
84         },
85         _ => false,
86     }
87 }
88
89 pub fn eq_angle_arg(l: &AngleBracketedArg, r: &AngleBracketedArg) -> bool {
90     match (l, r) {
91         (AngleBracketedArg::Arg(l), AngleBracketedArg::Arg(r)) => eq_generic_arg(l, r),
92         (AngleBracketedArg::Constraint(l), AngleBracketedArg::Constraint(r)) => eq_assoc_constraint(l, r),
93         _ => false,
94     }
95 }
96
97 pub fn eq_generic_arg(l: &GenericArg, r: &GenericArg) -> bool {
98     match (l, r) {
99         (GenericArg::Lifetime(l), GenericArg::Lifetime(r)) => eq_id(l.ident, r.ident),
100         (GenericArg::Type(l), GenericArg::Type(r)) => eq_ty(l, r),
101         (GenericArg::Const(l), GenericArg::Const(r)) => eq_expr(&l.value, &r.value),
102         _ => false,
103     }
104 }
105
106 pub fn eq_expr_opt(l: &Option<P<Expr>>, r: &Option<P<Expr>>) -> bool {
107     both(l, r, |l, r| eq_expr(l, r))
108 }
109
110 pub fn eq_struct_rest(l: &StructRest, r: &StructRest) -> bool {
111     match (l, r) {
112         (StructRest::Base(lb), StructRest::Base(rb)) => eq_expr(lb, rb),
113         (StructRest::Rest(_), StructRest::Rest(_)) | (StructRest::None, StructRest::None) => true,
114         _ => false,
115     }
116 }
117
118 pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
119     use ExprKind::*;
120     if !over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r)) {
121         return false;
122     }
123     match (&l.kind, &r.kind) {
124         (Paren(l), _) => eq_expr(l, r),
125         (_, Paren(r)) => eq_expr(l, r),
126         (Err, Err) => true,
127         (Box(l), Box(r)) | (Try(l), Try(r)) | (Await(l), Await(r)) => eq_expr(l, r),
128         (Array(l), Array(r)) | (Tup(l), Tup(r)) => over(l, r, |l, r| eq_expr(l, r)),
129         (Repeat(le, ls), Repeat(re, rs)) => eq_expr(le, re) && eq_expr(&ls.value, &rs.value),
130         (Call(lc, la), Call(rc, ra)) => eq_expr(lc, rc) && over(la, ra, |l, r| eq_expr(l, r)),
131         (MethodCall(lc, la, _), MethodCall(rc, ra, _)) => eq_path_seg(lc, rc) && over(la, ra, |l, r| eq_expr(l, r)),
132         (Binary(lo, ll, lr), Binary(ro, rl, rr)) => lo.node == ro.node && eq_expr(ll, rl) && eq_expr(lr, rr),
133         (Unary(lo, l), Unary(ro, r)) => mem::discriminant(lo) == mem::discriminant(ro) && eq_expr(l, r),
134         (Lit(l), Lit(r)) => l.kind == r.kind,
135         (Cast(l, lt), Cast(r, rt)) | (Type(l, lt), Type(r, rt)) => eq_expr(l, r) && eq_ty(lt, rt),
136         (Let(lp, le), Let(rp, re)) => eq_pat(lp, rp) && eq_expr(le, re),
137         (If(lc, lt, le), If(rc, rt, re)) => eq_expr(lc, rc) && eq_block(lt, rt) && eq_expr_opt(le, re),
138         (While(lc, lt, ll), While(rc, rt, rl)) => eq_label(ll, rl) && eq_expr(lc, rc) && eq_block(lt, rt),
139         (ForLoop(lp, li, lt, ll), ForLoop(rp, ri, rt, rl)) => {
140             eq_label(ll, rl) && eq_pat(lp, rp) && eq_expr(li, ri) && eq_block(lt, rt)
141         },
142         (Loop(lt, ll), Loop(rt, rl)) => eq_label(ll, rl) && eq_block(lt, rt),
143         (Block(lb, ll), Block(rb, rl)) => eq_label(ll, rl) && eq_block(lb, rb),
144         (TryBlock(l), TryBlock(r)) => eq_block(l, r),
145         (Yield(l), Yield(r)) | (Ret(l), Ret(r)) => eq_expr_opt(l, r),
146         (Break(ll, le), Break(rl, re)) => eq_label(ll, rl) && eq_expr_opt(le, re),
147         (Continue(ll), Continue(rl)) => eq_label(ll, rl),
148         (Assign(l1, l2, _), Assign(r1, r2, _)) | (Index(l1, l2), Index(r1, r2)) => eq_expr(l1, r1) && eq_expr(l2, r2),
149         (AssignOp(lo, lp, lv), AssignOp(ro, rp, rv)) => lo.node == ro.node && eq_expr(lp, rp) && eq_expr(lv, rv),
150         (Field(lp, lf), Field(rp, rf)) => eq_id(*lf, *rf) && eq_expr(lp, rp),
151         (Match(ls, la), Match(rs, ra)) => eq_expr(ls, rs) && over(la, ra, |l, r| eq_arm(l, r)),
152         (Closure(lc, la, lm, lf, lb, _), Closure(rc, ra, rm, rf, rb, _)) => {
153             lc == rc && la.is_async() == ra.is_async() && lm == rm && eq_fn_decl(lf, rf) && eq_expr(lb, rb)
154         },
155         (Async(lc, _, lb), Async(rc, _, rb)) => lc == rc && eq_block(lb, rb),
156         (Range(lf, lt, ll), Range(rf, rt, rl)) => ll == rl && eq_expr_opt(lf, rf) && eq_expr_opt(lt, rt),
157         (AddrOf(lbk, lm, le), AddrOf(rbk, rm, re)) => lbk == rbk && lm == rm && eq_expr(le, re),
158         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
159         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
160         (Struct(lp, lfs, lb), Struct(rp, rfs, rb)) => {
161             eq_path(lp, rp) && eq_struct_rest(lb, rb) && unordered_over(lfs, rfs, |l, r| eq_field(l, r))
162         },
163         _ => false,
164     }
165 }
166
167 pub fn eq_field(l: &Field, r: &Field) -> bool {
168     l.is_placeholder == r.is_placeholder
169         && eq_id(l.ident, r.ident)
170         && eq_expr(&l.expr, &r.expr)
171         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
172 }
173
174 pub fn eq_arm(l: &Arm, r: &Arm) -> bool {
175     l.is_placeholder == r.is_placeholder
176         && eq_pat(&l.pat, &r.pat)
177         && eq_expr(&l.body, &r.body)
178         && eq_expr_opt(&l.guard, &r.guard)
179         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
180 }
181
182 pub fn eq_label(l: &Option<Label>, r: &Option<Label>) -> bool {
183     both(l, r, |l, r| eq_id(l.ident, r.ident))
184 }
185
186 pub fn eq_block(l: &Block, r: &Block) -> bool {
187     l.rules == r.rules && over(&l.stmts, &r.stmts, |l, r| eq_stmt(l, r))
188 }
189
190 pub fn eq_stmt(l: &Stmt, r: &Stmt) -> bool {
191     use StmtKind::*;
192     match (&l.kind, &r.kind) {
193         (Local(l), Local(r)) => {
194             eq_pat(&l.pat, &r.pat)
195                 && both(&l.ty, &r.ty, |l, r| eq_ty(l, r))
196                 && eq_expr_opt(&l.init, &r.init)
197                 && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
198         },
199         (Item(l), Item(r)) => eq_item(l, r, eq_item_kind),
200         (Expr(l), Expr(r)) | (Semi(l), Semi(r)) => eq_expr(l, r),
201         (Empty, Empty) => true,
202         (MacCall(l), MacCall(r)) => {
203             l.style == r.style && eq_mac_call(&l.mac, &r.mac) && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
204         },
205         _ => false,
206     }
207 }
208
209 pub fn eq_item<K>(l: &Item<K>, r: &Item<K>, mut eq_kind: impl FnMut(&K, &K) -> bool) -> bool {
210     eq_id(l.ident, r.ident)
211         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
212         && eq_vis(&l.vis, &r.vis)
213         && eq_kind(&l.kind, &r.kind)
214 }
215
216 pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
217     use ItemKind::*;
218     match (l, r) {
219         (ExternCrate(l), ExternCrate(r)) => l == r,
220         (Use(l), Use(r)) => eq_use_tree(l, r),
221         (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
222         (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
223         (Fn(ld, lf, lg, lb), Fn(rd, rf, rg, rb)) => {
224             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
225         },
226         (Mod(l), Mod(r)) => l.inline == r.inline && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_item_kind)),
227         (ForeignMod(l), ForeignMod(r)) => {
228             both(&l.abi, &r.abi, |l, r| eq_str_lit(l, r))
229                 && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
230         },
231         (TyAlias(ld, lg, lb, lt), TyAlias(rd, rg, rb, rt)) => {
232             eq_defaultness(*ld, *rd)
233                 && eq_generics(lg, rg)
234                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
235                 && both(lt, rt, |l, r| eq_ty(l, r))
236         },
237         (Enum(le, lg), Enum(re, rg)) => {
238             over(&le.variants, &re.variants, |l, r| eq_variant(l, r)) && eq_generics(lg, rg)
239         },
240         (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => {
241             eq_variant_data(lv, rv) && eq_generics(lg, rg)
242         },
243         (Trait(la, lu, lg, lb, li), Trait(ra, ru, rg, rb, ri)) => {
244             la == ra
245                 && matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
246                 && eq_generics(lg, rg)
247                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
248                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
249         },
250         (TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, |l, r| eq_generic_bound(l, r)),
251         (
252             Impl {
253                 unsafety: lu,
254                 polarity: lp,
255                 defaultness: ld,
256                 constness: lc,
257                 generics: lg,
258                 of_trait: lot,
259                 self_ty: lst,
260                 items: li,
261             },
262             Impl {
263                 unsafety: ru,
264                 polarity: rp,
265                 defaultness: rd,
266                 constness: rc,
267                 generics: rg,
268                 of_trait: rot,
269                 self_ty: rst,
270                 items: ri,
271             },
272         ) => {
273             matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
274                 && matches!(lp, ImplPolarity::Positive) == matches!(rp, ImplPolarity::Positive)
275                 && eq_defaultness(*ld, *rd)
276                 && matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
277                 && eq_generics(lg, rg)
278                 && both(lot, rot, |l, r| eq_path(&l.path, &r.path))
279                 && eq_ty(lst, rst)
280                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
281         },
282         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
283         (MacroDef(l), MacroDef(r)) => l.macro_rules == r.macro_rules && eq_mac_args(&l.body, &r.body),
284         _ => false,
285     }
286 }
287
288 pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
289     use ForeignItemKind::*;
290     match (l, r) {
291         (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
292         (Fn(ld, lf, lg, lb), Fn(rd, rf, rg, rb)) => {
293             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
294         },
295         (TyAlias(ld, lg, lb, lt), TyAlias(rd, rg, rb, rt)) => {
296             eq_defaultness(*ld, *rd)
297                 && eq_generics(lg, rg)
298                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
299                 && both(lt, rt, |l, r| eq_ty(l, r))
300         },
301         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
302         _ => false,
303     }
304 }
305
306 pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
307     use AssocItemKind::*;
308     match (l, r) {
309         (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
310         (Fn(ld, lf, lg, lb), Fn(rd, rf, rg, rb)) => {
311             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
312         },
313         (TyAlias(ld, lg, lb, lt), TyAlias(rd, rg, rb, rt)) => {
314             eq_defaultness(*ld, *rd)
315                 && eq_generics(lg, rg)
316                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
317                 && both(lt, rt, |l, r| eq_ty(l, r))
318         },
319         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
320         _ => false,
321     }
322 }
323
324 pub fn eq_variant(l: &Variant, r: &Variant) -> bool {
325     l.is_placeholder == r.is_placeholder
326         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
327         && eq_vis(&l.vis, &r.vis)
328         && eq_id(l.ident, r.ident)
329         && eq_variant_data(&l.data, &r.data)
330         && both(&l.disr_expr, &r.disr_expr, |l, r| eq_expr(&l.value, &r.value))
331 }
332
333 pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
334     use VariantData::*;
335     match (l, r) {
336         (Unit(_), Unit(_)) => true,
337         (Struct(l, _), Struct(r, _)) | (Tuple(l, _), Tuple(r, _)) => over(l, r, |l, r| eq_struct_field(l, r)),
338         _ => false,
339     }
340 }
341
342 pub fn eq_struct_field(l: &StructField, r: &StructField) -> bool {
343     l.is_placeholder == r.is_placeholder
344         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
345         && eq_vis(&l.vis, &r.vis)
346         && both(&l.ident, &r.ident, |l, r| eq_id(*l, *r))
347         && eq_ty(&l.ty, &r.ty)
348 }
349
350 pub fn eq_fn_sig(l: &FnSig, r: &FnSig) -> bool {
351     eq_fn_decl(&l.decl, &r.decl) && eq_fn_header(&l.header, &r.header)
352 }
353
354 pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool {
355     matches!(l.unsafety, Unsafe::No) == matches!(r.unsafety, Unsafe::No)
356         && l.asyncness.is_async() == r.asyncness.is_async()
357         && matches!(l.constness, Const::No) == matches!(r.constness, Const::No)
358         && eq_ext(&l.ext, &r.ext)
359 }
360
361 pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
362     over(&l.params, &r.params, |l, r| eq_generic_param(l, r))
363         && over(&l.where_clause.predicates, &r.where_clause.predicates, |l, r| {
364             eq_where_predicate(l, r)
365         })
366 }
367
368 pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
369     use WherePredicate::*;
370     match (l, r) {
371         (BoundPredicate(l), BoundPredicate(r)) => {
372             over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
373                 eq_generic_param(l, r)
374             }) && eq_ty(&l.bounded_ty, &r.bounded_ty)
375                 && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
376         },
377         (RegionPredicate(l), RegionPredicate(r)) => {
378             eq_id(l.lifetime.ident, r.lifetime.ident) && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
379         },
380         (EqPredicate(l), EqPredicate(r)) => eq_ty(&l.lhs_ty, &r.lhs_ty) && eq_ty(&l.rhs_ty, &r.rhs_ty),
381         _ => false,
382     }
383 }
384
385 pub fn eq_use_tree(l: &UseTree, r: &UseTree) -> bool {
386     eq_path(&l.prefix, &r.prefix) && eq_use_tree_kind(&l.kind, &r.kind)
387 }
388
389 pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
390     use UseTreeKind::*;
391     match (l, r) {
392         (Glob, Glob) => true,
393         (Simple(l, _, _), Simple(r, _, _)) => both(l, r, |l, r| eq_id(*l, *r)),
394         (Nested(l), Nested(r)) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
395         _ => false,
396     }
397 }
398
399 pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
400     matches!((l, r), (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_)))
401 }
402
403 pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
404     use VisibilityKind::*;
405     match (&l.kind, &r.kind) {
406         (Public, Public) | (Inherited, Inherited) | (Crate(_), Crate(_)) => true,
407         (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r),
408         _ => false,
409     }
410 }
411
412 pub fn eq_fn_decl(l: &FnDecl, r: &FnDecl) -> bool {
413     eq_fn_ret_ty(&l.output, &r.output)
414         && over(&l.inputs, &r.inputs, |l, r| {
415             l.is_placeholder == r.is_placeholder
416                 && eq_pat(&l.pat, &r.pat)
417                 && eq_ty(&l.ty, &r.ty)
418                 && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
419         })
420 }
421
422 pub fn eq_fn_ret_ty(l: &FnRetTy, r: &FnRetTy) -> bool {
423     match (l, r) {
424         (FnRetTy::Default(_), FnRetTy::Default(_)) => true,
425         (FnRetTy::Ty(l), FnRetTy::Ty(r)) => eq_ty(l, r),
426         _ => false,
427     }
428 }
429
430 pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
431     use TyKind::*;
432     match (&l.kind, &r.kind) {
433         (Paren(l), _) => eq_ty(l, r),
434         (_, Paren(r)) => eq_ty(l, r),
435         (Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err, Err) | (CVarArgs, CVarArgs) => true,
436         (Slice(l), Slice(r)) => eq_ty(l, r),
437         (Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
438         (Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),
439         (Rptr(ll, l), Rptr(rl, r)) => {
440             both(ll, rl, |l, r| eq_id(l.ident, r.ident)) && l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty)
441         },
442         (BareFn(l), BareFn(r)) => {
443             l.unsafety == r.unsafety
444                 && eq_ext(&l.ext, &r.ext)
445                 && over(&l.generic_params, &r.generic_params, |l, r| eq_generic_param(l, r))
446                 && eq_fn_decl(&l.decl, &r.decl)
447         },
448         (Tup(l), Tup(r)) => over(l, r, |l, r| eq_ty(l, r)),
449         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
450         (TraitObject(lg, ls), TraitObject(rg, rs)) => ls == rs && over(lg, rg, |l, r| eq_generic_bound(l, r)),
451         (ImplTrait(_, lg), ImplTrait(_, rg)) => over(lg, rg, |l, r| eq_generic_bound(l, r)),
452         (Typeof(l), Typeof(r)) => eq_expr(&l.value, &r.value),
453         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
454         _ => false,
455     }
456 }
457
458 pub fn eq_ext(l: &Extern, r: &Extern) -> bool {
459     use Extern::*;
460     match (l, r) {
461         (None, None) | (Implicit, Implicit) => true,
462         (Explicit(l), Explicit(r)) => eq_str_lit(l, r),
463         _ => false,
464     }
465 }
466
467 pub fn eq_str_lit(l: &StrLit, r: &StrLit) -> bool {
468     l.style == r.style && l.symbol == r.symbol && l.suffix == r.suffix
469 }
470
471 pub fn eq_poly_ref_trait(l: &PolyTraitRef, r: &PolyTraitRef) -> bool {
472     eq_path(&l.trait_ref.path, &r.trait_ref.path)
473         && over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
474             eq_generic_param(l, r)
475         })
476 }
477
478 pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
479     use GenericParamKind::*;
480     l.is_placeholder == r.is_placeholder
481         && eq_id(l.ident, r.ident)
482         && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
483         && match (&l.kind, &r.kind) {
484             (Lifetime, Lifetime) => true,
485             (Type { default: l }, Type { default: r }) => both(l, r, |l, r| eq_ty(l, r)),
486             (Const { ty: l, kw_span: _ }, Const { ty: r, kw_span: _ }) => eq_ty(l, r),
487             _ => false,
488         }
489         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
490 }
491
492 pub fn eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool {
493     use GenericBound::*;
494     match (l, r) {
495         (Trait(ptr1, tbm1), Trait(ptr2, tbm2)) => tbm1 == tbm2 && eq_poly_ref_trait(ptr1, ptr2),
496         (Outlives(l), Outlives(r)) => eq_id(l.ident, r.ident),
497         _ => false,
498     }
499 }
500
501 pub fn eq_assoc_constraint(l: &AssocTyConstraint, r: &AssocTyConstraint) -> bool {
502     use AssocTyConstraintKind::*;
503     eq_id(l.ident, r.ident)
504         && match (&l.kind, &r.kind) {
505             (Equality { ty: l }, Equality { ty: r }) => eq_ty(l, r),
506             (Bound { bounds: l }, Bound { bounds: r }) => over(l, r, |l, r| eq_generic_bound(l, r)),
507             _ => false,
508         }
509 }
510
511 pub fn eq_mac_call(l: &MacCall, r: &MacCall) -> bool {
512     eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args)
513 }
514
515 pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool {
516     use AttrKind::*;
517     l.style == r.style
518         && match (&l.kind, &r.kind) {
519             (DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2,
520             (Normal(l, _), Normal(r, _)) => eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args),
521             _ => false,
522         }
523 }
524
525 pub fn eq_mac_args(l: &MacArgs, r: &MacArgs) -> bool {
526     use MacArgs::*;
527     match (l, r) {
528         (Empty, Empty) => true,
529         (Delimited(_, ld, lts), Delimited(_, rd, rts)) => ld == rd && lts.eq_unspanned(rts),
530         (Eq(_, lts), Eq(_, rts)) => lts.eq_unspanned(rts),
531         _ => false,
532     }
533 }