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