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