]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/utils/ast_utils.rs
Auto merge of #76378 - petrochenkov:lldtest, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / clippy_lints / src / utils / ast_utils.rs
1 //! Utilities for manipulating and extracting information from `rustc_ast::ast`.
2 //!
3 //! - The `eq_foobar` functions test for semantic equality but ignores `NodeId`s and `Span`s.
4
5 #![allow(clippy::similar_names, clippy::wildcard_imports, clippy::enum_glob_use)]
6
7 use crate::utils::{both, over};
8 use rustc_ast::ptr::P;
9 use rustc_ast::{self as ast, *};
10 use rustc_span::symbol::Ident;
11 use std::mem;
12
13 /// Checks if each element in the first slice is contained within the latter as per `eq_fn`.
14 pub fn unordered_over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
15     left.len() == right.len() && left.iter().all(|l| right.iter().any(|r| eq_fn(l, r)))
16 }
17
18 pub fn eq_id(l: Ident, r: Ident) -> bool {
19     l.name == r.name
20 }
21
22 pub fn eq_pat(l: &Pat, r: &Pat) -> bool {
23     use PatKind::*;
24     match (&l.kind, &r.kind) {
25         (Paren(l), _) => eq_pat(l, r),
26         (_, Paren(r)) => eq_pat(l, r),
27         (Wild, Wild) | (Rest, Rest) => true,
28         (Lit(l), Lit(r)) => eq_expr(l, r),
29         (Ident(b1, i1, s1), Ident(b2, i2, s2)) => b1 == b2 && eq_id(*i1, *i2) && both(s1, s2, |l, r| eq_pat(l, r)),
30         (Range(lf, lt, le), Range(rf, rt, re)) => {
31             eq_expr_opt(lf, rf) && eq_expr_opt(lt, rt) && eq_range_end(&le.node, &re.node)
32         },
33         (Box(l), Box(r))
34         | (Ref(l, Mutability::Not), Ref(r, Mutability::Not))
35         | (Ref(l, Mutability::Mut), Ref(r, Mutability::Mut)) => eq_pat(l, r),
36         (Tuple(l), Tuple(r)) | (Slice(l), Slice(r)) => over(l, r, |l, r| eq_pat(l, r)),
37         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
38         (TupleStruct(lp, lfs), TupleStruct(rp, rfs)) => eq_path(lp, rp) && over(lfs, rfs, |l, r| eq_pat(l, r)),
39         (Struct(lp, lfs, lr), Struct(rp, rfs, rr)) => {
40             lr == rr && eq_path(lp, rp) && unordered_over(lfs, rfs, |lf, rf| eq_field_pat(lf, rf))
41         },
42         (Or(ls), Or(rs)) => unordered_over(ls, rs, |l, r| eq_pat(l, r)),
43         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
44         _ => false,
45     }
46 }
47
48 pub fn eq_range_end(l: &RangeEnd, r: &RangeEnd) -> bool {
49     match (l, r) {
50         (RangeEnd::Excluded, RangeEnd::Excluded) => true,
51         (RangeEnd::Included(l), RangeEnd::Included(r)) => {
52             matches!(l, RangeSyntax::DotDotEq) == matches!(r, RangeSyntax::DotDotEq)
53         },
54         _ => false,
55     }
56 }
57
58 pub fn eq_field_pat(l: &FieldPat, r: &FieldPat) -> bool {
59     l.is_placeholder == r.is_placeholder
60         && eq_id(l.ident, r.ident)
61         && eq_pat(&l.pat, &r.pat)
62         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
63 }
64
65 pub fn eq_qself(l: &QSelf, r: &QSelf) -> bool {
66     l.position == r.position && eq_ty(&l.ty, &r.ty)
67 }
68
69 pub fn eq_path(l: &Path, r: &Path) -> bool {
70     over(&l.segments, &r.segments, |l, r| eq_path_seg(l, r))
71 }
72
73 pub fn eq_path_seg(l: &PathSegment, r: &PathSegment) -> bool {
74     eq_id(l.ident, r.ident) && both(&l.args, &r.args, |l, r| eq_generic_args(l, r))
75 }
76
77 pub fn eq_generic_args(l: &GenericArgs, r: &GenericArgs) -> bool {
78     match (l, r) {
79         (GenericArgs::AngleBracketed(l), GenericArgs::AngleBracketed(r)) => {
80             over(&l.args, &r.args, |l, r| eq_angle_arg(l, r))
81         },
82         (GenericArgs::Parenthesized(l), GenericArgs::Parenthesized(r)) => {
83             over(&l.inputs, &r.inputs, |l, r| eq_ty(l, r)) && eq_fn_ret_ty(&l.output, &r.output)
84         },
85         _ => false,
86     }
87 }
88
89 pub fn eq_angle_arg(l: &AngleBracketedArg, r: &AngleBracketedArg) -> bool {
90     match (l, r) {
91         (AngleBracketedArg::Arg(l), AngleBracketedArg::Arg(r)) => eq_generic_arg(l, r),
92         (AngleBracketedArg::Constraint(l), AngleBracketedArg::Constraint(r)) => eq_assoc_constraint(l, r),
93         _ => false,
94     }
95 }
96
97 pub fn eq_generic_arg(l: &GenericArg, r: &GenericArg) -> bool {
98     match (l, r) {
99         (GenericArg::Lifetime(l), GenericArg::Lifetime(r)) => eq_id(l.ident, r.ident),
100         (GenericArg::Type(l), GenericArg::Type(r)) => eq_ty(l, r),
101         (GenericArg::Const(l), GenericArg::Const(r)) => eq_expr(&l.value, &r.value),
102         _ => false,
103     }
104 }
105
106 pub fn eq_expr_opt(l: &Option<P<Expr>>, r: &Option<P<Expr>>) -> bool {
107     both(l, r, |l, r| eq_expr(l, r))
108 }
109
110 pub fn eq_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)) => l.style == r.style && eq_mac_call(&l.mac, &r.mac) && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r)),
195         _ => false,
196     }
197 }
198
199 pub fn eq_item<K>(l: &Item<K>, r: &Item<K>, mut eq_kind: impl FnMut(&K, &K) -> bool) -> bool {
200     eq_id(l.ident, r.ident)
201         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
202         && eq_vis(&l.vis, &r.vis)
203         && eq_kind(&l.kind, &r.kind)
204 }
205
206 pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
207     use ItemKind::*;
208     match (l, r) {
209         (ExternCrate(l), ExternCrate(r)) => l == r,
210         (Use(l), Use(r)) => eq_use_tree(l, r),
211         (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
212         (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
213         (Fn(ld, lf, lg, lb), Fn(rd, rf, rg, rb)) => {
214             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
215         },
216         (Mod(l), Mod(r)) => l.inline == r.inline && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_item_kind)),
217         (ForeignMod(l), ForeignMod(r)) => {
218             both(&l.abi, &r.abi, |l, r| eq_str_lit(l, r))
219                 && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
220         },
221         (TyAlias(ld, lg, lb, lt), TyAlias(rd, rg, rb, rt)) => {
222             eq_defaultness(*ld, *rd)
223                 && eq_generics(lg, rg)
224                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
225                 && both(lt, rt, |l, r| eq_ty(l, r))
226         },
227         (Enum(le, lg), Enum(re, rg)) => {
228             over(&le.variants, &re.variants, |l, r| eq_variant(l, r)) && eq_generics(lg, rg)
229         },
230         (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => {
231             eq_variant_data(lv, rv) && eq_generics(lg, rg)
232         },
233         (Trait(la, lu, lg, lb, li), Trait(ra, ru, rg, rb, ri)) => {
234             la == ra
235                 && matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
236                 && eq_generics(lg, rg)
237                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
238                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
239         },
240         (TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, |l, r| eq_generic_bound(l, r)),
241         (
242             Impl {
243                 unsafety: lu,
244                 polarity: lp,
245                 defaultness: ld,
246                 constness: lc,
247                 generics: lg,
248                 of_trait: lot,
249                 self_ty: lst,
250                 items: li,
251             },
252             Impl {
253                 unsafety: ru,
254                 polarity: rp,
255                 defaultness: rd,
256                 constness: rc,
257                 generics: rg,
258                 of_trait: rot,
259                 self_ty: rst,
260                 items: ri,
261             },
262         ) => {
263             matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
264                 && matches!(lp, ImplPolarity::Positive) == matches!(rp, ImplPolarity::Positive)
265                 && eq_defaultness(*ld, *rd)
266                 && matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
267                 && eq_generics(lg, rg)
268                 && both(lot, rot, |l, r| eq_path(&l.path, &r.path))
269                 && eq_ty(lst, rst)
270                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
271         },
272         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
273         (MacroDef(l), MacroDef(r)) => l.macro_rules == r.macro_rules && eq_mac_args(&l.body, &r.body),
274         _ => false,
275     }
276 }
277
278 pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
279     use ForeignItemKind::*;
280     match (l, r) {
281         (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
282         (Fn(ld, lf, lg, lb), Fn(rd, rf, rg, rb)) => {
283             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
284         },
285         (TyAlias(ld, lg, lb, lt), TyAlias(rd, rg, rb, rt)) => {
286             eq_defaultness(*ld, *rd)
287                 && eq_generics(lg, rg)
288                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
289                 && both(lt, rt, |l, r| eq_ty(l, r))
290         },
291         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
292         _ => false,
293     }
294 }
295
296 pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
297     use AssocItemKind::*;
298     match (l, r) {
299         (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
300         (Fn(ld, lf, lg, lb), Fn(rd, rf, rg, rb)) => {
301             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
302         },
303         (TyAlias(ld, lg, lb, lt), TyAlias(rd, rg, rb, rt)) => {
304             eq_defaultness(*ld, *rd)
305                 && eq_generics(lg, rg)
306                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
307                 && both(lt, rt, |l, r| eq_ty(l, r))
308         },
309         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
310         _ => false,
311     }
312 }
313
314 pub fn eq_variant(l: &Variant, r: &Variant) -> bool {
315     l.is_placeholder == r.is_placeholder
316         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
317         && eq_vis(&l.vis, &r.vis)
318         && eq_id(l.ident, r.ident)
319         && eq_variant_data(&l.data, &r.data)
320         && both(&l.disr_expr, &r.disr_expr, |l, r| eq_expr(&l.value, &r.value))
321 }
322
323 pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
324     use VariantData::*;
325     match (l, r) {
326         (Unit(_), Unit(_)) => true,
327         (Struct(l, _), Struct(r, _)) | (Tuple(l, _), Tuple(r, _)) => over(l, r, |l, r| eq_struct_field(l, r)),
328         _ => false,
329     }
330 }
331
332 pub fn eq_struct_field(l: &StructField, r: &StructField) -> bool {
333     l.is_placeholder == r.is_placeholder
334         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
335         && eq_vis(&l.vis, &r.vis)
336         && both(&l.ident, &r.ident, |l, r| eq_id(*l, *r))
337         && eq_ty(&l.ty, &r.ty)
338 }
339
340 pub fn eq_fn_sig(l: &FnSig, r: &FnSig) -> bool {
341     eq_fn_decl(&l.decl, &r.decl) && eq_fn_header(&l.header, &r.header)
342 }
343
344 pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool {
345     matches!(l.unsafety, Unsafe::No) == matches!(r.unsafety, Unsafe::No)
346         && l.asyncness.is_async() == r.asyncness.is_async()
347         && matches!(l.constness, Const::No) == matches!(r.constness, Const::No)
348         && eq_ext(&l.ext, &r.ext)
349 }
350
351 pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
352     over(&l.params, &r.params, |l, r| eq_generic_param(l, r))
353         && over(&l.where_clause.predicates, &r.where_clause.predicates, |l, r| {
354             eq_where_predicate(l, r)
355         })
356 }
357
358 pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
359     use WherePredicate::*;
360     match (l, r) {
361         (BoundPredicate(l), BoundPredicate(r)) => {
362             over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
363                 eq_generic_param(l, r)
364             }) && eq_ty(&l.bounded_ty, &r.bounded_ty)
365                 && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
366         },
367         (RegionPredicate(l), RegionPredicate(r)) => {
368             eq_id(l.lifetime.ident, r.lifetime.ident) && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
369         },
370         (EqPredicate(l), EqPredicate(r)) => eq_ty(&l.lhs_ty, &r.lhs_ty) && eq_ty(&l.rhs_ty, &r.rhs_ty),
371         _ => false,
372     }
373 }
374
375 pub fn eq_use_tree(l: &UseTree, r: &UseTree) -> bool {
376     eq_path(&l.prefix, &r.prefix) && eq_use_tree_kind(&l.kind, &r.kind)
377 }
378
379 pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
380     use UseTreeKind::*;
381     match (l, r) {
382         (Glob, Glob) => true,
383         (Simple(l, _, _), Simple(r, _, _)) => both(l, r, |l, r| eq_id(*l, *r)),
384         (Nested(l), Nested(r)) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
385         _ => false,
386     }
387 }
388
389 pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
390     matches!((l, r), (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_)))
391 }
392
393 pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
394     use VisibilityKind::*;
395     match (&l.node, &r.node) {
396         (Public, Public) | (Inherited, Inherited) | (Crate(_), Crate(_)) => true,
397         (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r),
398         _ => false,
399     }
400 }
401
402 pub fn eq_fn_decl(l: &FnDecl, r: &FnDecl) -> bool {
403     eq_fn_ret_ty(&l.output, &r.output)
404         && over(&l.inputs, &r.inputs, |l, r| {
405             l.is_placeholder == r.is_placeholder
406                 && eq_pat(&l.pat, &r.pat)
407                 && eq_ty(&l.ty, &r.ty)
408                 && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
409         })
410 }
411
412 pub fn eq_fn_ret_ty(l: &FnRetTy, r: &FnRetTy) -> bool {
413     match (l, r) {
414         (FnRetTy::Default(_), FnRetTy::Default(_)) => true,
415         (FnRetTy::Ty(l), FnRetTy::Ty(r)) => eq_ty(l, r),
416         _ => false,
417     }
418 }
419
420 pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
421     use TyKind::*;
422     match (&l.kind, &r.kind) {
423         (Paren(l), _) => eq_ty(l, r),
424         (_, Paren(r)) => eq_ty(l, r),
425         (Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err, Err) | (CVarArgs, CVarArgs) => true,
426         (Slice(l), Slice(r)) => eq_ty(l, r),
427         (Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
428         (Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),
429         (Rptr(ll, l), Rptr(rl, r)) => {
430             both(ll, rl, |l, r| eq_id(l.ident, r.ident)) && l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty)
431         },
432         (BareFn(l), BareFn(r)) => {
433             l.unsafety == r.unsafety
434                 && eq_ext(&l.ext, &r.ext)
435                 && over(&l.generic_params, &r.generic_params, |l, r| eq_generic_param(l, r))
436                 && eq_fn_decl(&l.decl, &r.decl)
437         },
438         (Tup(l), Tup(r)) => over(l, r, |l, r| eq_ty(l, r)),
439         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
440         (TraitObject(lg, ls), TraitObject(rg, rs)) => ls == rs && over(lg, rg, |l, r| eq_generic_bound(l, r)),
441         (ImplTrait(_, lg), ImplTrait(_, rg)) => over(lg, rg, |l, r| eq_generic_bound(l, r)),
442         (Typeof(l), Typeof(r)) => eq_expr(&l.value, &r.value),
443         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
444         _ => false,
445     }
446 }
447
448 pub fn eq_ext(l: &Extern, r: &Extern) -> bool {
449     use Extern::*;
450     match (l, r) {
451         (None, None) | (Implicit, Implicit) => true,
452         (Explicit(l), Explicit(r)) => eq_str_lit(l, r),
453         _ => false,
454     }
455 }
456
457 pub fn eq_str_lit(l: &StrLit, r: &StrLit) -> bool {
458     l.style == r.style && l.symbol == r.symbol && l.suffix == r.suffix
459 }
460
461 pub fn eq_poly_ref_trait(l: &PolyTraitRef, r: &PolyTraitRef) -> bool {
462     eq_path(&l.trait_ref.path, &r.trait_ref.path)
463         && over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
464             eq_generic_param(l, r)
465         })
466 }
467
468 pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
469     use GenericParamKind::*;
470     l.is_placeholder == r.is_placeholder
471         && eq_id(l.ident, r.ident)
472         && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
473         && match (&l.kind, &r.kind) {
474             (Lifetime, Lifetime) => true,
475             (Type { default: l }, Type { default: r }) => both(l, r, |l, r| eq_ty(l, r)),
476             (Const { ty: l, kw_span: _ }, Const { ty: r, kw_span: _ }) => eq_ty(l, r),
477             _ => false,
478         }
479         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
480 }
481
482 pub fn eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool {
483     use GenericBound::*;
484     match (l, r) {
485         (Trait(ptr1, tbm1), Trait(ptr2, tbm2)) => tbm1 == tbm2 && eq_poly_ref_trait(ptr1, ptr2),
486         (Outlives(l), Outlives(r)) => eq_id(l.ident, r.ident),
487         _ => false,
488     }
489 }
490
491 pub fn eq_assoc_constraint(l: &AssocTyConstraint, r: &AssocTyConstraint) -> bool {
492     use AssocTyConstraintKind::*;
493     eq_id(l.ident, r.ident)
494         && match (&l.kind, &r.kind) {
495             (Equality { ty: l }, Equality { ty: r }) => eq_ty(l, r),
496             (Bound { bounds: l }, Bound { bounds: r }) => over(l, r, |l, r| eq_generic_bound(l, r)),
497             _ => false,
498         }
499 }
500
501 pub fn eq_mac_call(l: &MacCall, r: &MacCall) -> bool {
502     eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args)
503 }
504
505 pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool {
506     use AttrKind::*;
507     l.style == r.style
508         && match (&l.kind, &r.kind) {
509             (DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2,
510             (Normal(l), Normal(r)) => eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args),
511             _ => false,
512         }
513 }
514
515 pub fn eq_mac_args(l: &MacArgs, r: &MacArgs) -> bool {
516     use MacArgs::*;
517     match (l, r) {
518         (Empty, Empty) => true,
519         (Delimited(_, ld, lts), Delimited(_, rd, rts)) => ld == rd && lts.eq_unspanned(rts),
520         (Eq(_, lts), Eq(_, rts)) => lts.eq_unspanned(rts),
521         _ => false,
522     }
523 }