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