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