1 #![allow(unused_macro_rules)]
2 
3 extern crate rustc_ast;
4 extern crate rustc_data_structures;
5 extern crate rustc_driver;
6 extern crate rustc_span;
7 extern crate thin_vec;
8 
9 use rustc_ast::ast::AngleBracketedArg;
10 use rustc_ast::ast::AngleBracketedArgs;
11 use rustc_ast::ast::AnonConst;
12 use rustc_ast::ast::Arm;
13 use rustc_ast::ast::AssocConstraint;
14 use rustc_ast::ast::AssocConstraintKind;
15 use rustc_ast::ast::AssocItemKind;
16 use rustc_ast::ast::AttrArgs;
17 use rustc_ast::ast::AttrArgsEq;
18 use rustc_ast::ast::AttrId;
19 use rustc_ast::ast::AttrItem;
20 use rustc_ast::ast::AttrKind;
21 use rustc_ast::ast::AttrStyle;
22 use rustc_ast::ast::Attribute;
23 use rustc_ast::ast::BareFnTy;
24 use rustc_ast::ast::BinOpKind;
25 use rustc_ast::ast::BindingAnnotation;
26 use rustc_ast::ast::Block;
27 use rustc_ast::ast::BlockCheckMode;
28 use rustc_ast::ast::BorrowKind;
29 use rustc_ast::ast::BoundAsyncness;
30 use rustc_ast::ast::BoundConstness;
31 use rustc_ast::ast::BoundPolarity;
32 use rustc_ast::ast::ByRef;
33 use rustc_ast::ast::CaptureBy;
34 use rustc_ast::ast::Closure;
35 use rustc_ast::ast::ClosureBinder;
36 use rustc_ast::ast::Const;
37 use rustc_ast::ast::ConstItem;
38 use rustc_ast::ast::CoroutineKind;
39 use rustc_ast::ast::Crate;
40 use rustc_ast::ast::Defaultness;
41 use rustc_ast::ast::Delegation;
42 use rustc_ast::ast::DelimArgs;
43 use rustc_ast::ast::EnumDef;
44 use rustc_ast::ast::Expr;
45 use rustc_ast::ast::ExprField;
46 use rustc_ast::ast::ExprKind;
47 use rustc_ast::ast::Extern;
48 use rustc_ast::ast::FieldDef;
49 use rustc_ast::ast::FloatTy;
50 use rustc_ast::ast::Fn;
51 use rustc_ast::ast::FnDecl;
52 use rustc_ast::ast::FnHeader;
53 use rustc_ast::ast::FnRetTy;
54 use rustc_ast::ast::FnSig;
55 use rustc_ast::ast::ForLoopKind;
56 use rustc_ast::ast::ForeignItemKind;
57 use rustc_ast::ast::ForeignMod;
58 use rustc_ast::ast::FormatAlignment;
59 use rustc_ast::ast::FormatArgPosition;
60 use rustc_ast::ast::FormatArgPositionKind;
61 use rustc_ast::ast::FormatArgs;
62 use rustc_ast::ast::FormatArgsPiece;
63 use rustc_ast::ast::FormatArgument;
64 use rustc_ast::ast::FormatArgumentKind;
65 use rustc_ast::ast::FormatArguments;
66 use rustc_ast::ast::FormatCount;
67 use rustc_ast::ast::FormatDebugHex;
68 use rustc_ast::ast::FormatOptions;
69 use rustc_ast::ast::FormatPlaceholder;
70 use rustc_ast::ast::FormatSign;
71 use rustc_ast::ast::FormatTrait;
72 use rustc_ast::ast::GenBlockKind;
73 use rustc_ast::ast::GenericArg;
74 use rustc_ast::ast::GenericArgs;
75 use rustc_ast::ast::GenericBound;
76 use rustc_ast::ast::GenericParam;
77 use rustc_ast::ast::GenericParamKind;
78 use rustc_ast::ast::Generics;
79 use rustc_ast::ast::Impl;
80 use rustc_ast::ast::ImplPolarity;
81 use rustc_ast::ast::Inline;
82 use rustc_ast::ast::InlineAsm;
83 use rustc_ast::ast::InlineAsmOperand;
84 use rustc_ast::ast::InlineAsmOptions;
85 use rustc_ast::ast::InlineAsmRegOrRegClass;
86 use rustc_ast::ast::InlineAsmSym;
87 use rustc_ast::ast::InlineAsmTemplatePiece;
88 use rustc_ast::ast::IntTy;
89 use rustc_ast::ast::IsAuto;
90 use rustc_ast::ast::Item;
91 use rustc_ast::ast::ItemKind;
92 use rustc_ast::ast::Label;
93 use rustc_ast::ast::Lifetime;
94 use rustc_ast::ast::LitFloatType;
95 use rustc_ast::ast::LitIntType;
96 use rustc_ast::ast::LitKind;
97 use rustc_ast::ast::Local;
98 use rustc_ast::ast::LocalKind;
99 use rustc_ast::ast::MacCall;
100 use rustc_ast::ast::MacCallStmt;
101 use rustc_ast::ast::MacStmtStyle;
102 use rustc_ast::ast::MacroDef;
103 use rustc_ast::ast::MatchKind;
104 use rustc_ast::ast::MetaItemLit;
105 use rustc_ast::ast::MethodCall;
106 use rustc_ast::ast::ModKind;
107 use rustc_ast::ast::ModSpans;
108 use rustc_ast::ast::Movability;
109 use rustc_ast::ast::MutTy;
110 use rustc_ast::ast::Mutability;
111 use rustc_ast::ast::NodeId;
112 use rustc_ast::ast::NormalAttr;
113 use rustc_ast::ast::Param;
114 use rustc_ast::ast::ParenthesizedArgs;
115 use rustc_ast::ast::Pat;
116 use rustc_ast::ast::PatField;
117 use rustc_ast::ast::PatFieldsRest;
118 use rustc_ast::ast::PatKind;
119 use rustc_ast::ast::Path;
120 use rustc_ast::ast::PathSegment;
121 use rustc_ast::ast::PolyTraitRef;
122 use rustc_ast::ast::QSelf;
123 use rustc_ast::ast::RangeEnd;
124 use rustc_ast::ast::RangeLimits;
125 use rustc_ast::ast::RangeSyntax;
126 use rustc_ast::ast::StaticItem;
127 use rustc_ast::ast::Stmt;
128 use rustc_ast::ast::StmtKind;
129 use rustc_ast::ast::StrLit;
130 use rustc_ast::ast::StrStyle;
131 use rustc_ast::ast::StructExpr;
132 use rustc_ast::ast::StructRest;
133 use rustc_ast::ast::Term;
134 use rustc_ast::ast::Trait;
135 use rustc_ast::ast::TraitBoundModifiers;
136 use rustc_ast::ast::TraitObjectSyntax;
137 use rustc_ast::ast::TraitRef;
138 use rustc_ast::ast::Ty;
139 use rustc_ast::ast::TyAlias;
140 use rustc_ast::ast::TyAliasWhereClause;
141 use rustc_ast::ast::TyAliasWhereClauses;
142 use rustc_ast::ast::TyKind;
143 use rustc_ast::ast::UintTy;
144 use rustc_ast::ast::UnOp;
145 use rustc_ast::ast::Unsafe;
146 use rustc_ast::ast::UnsafeSource;
147 use rustc_ast::ast::UseTree;
148 use rustc_ast::ast::UseTreeKind;
149 use rustc_ast::ast::Variant;
150 use rustc_ast::ast::VariantData;
151 use rustc_ast::ast::Visibility;
152 use rustc_ast::ast::VisibilityKind;
153 use rustc_ast::ast::WhereBoundPredicate;
154 use rustc_ast::ast::WhereClause;
155 use rustc_ast::ast::WhereEqPredicate;
156 use rustc_ast::ast::WherePredicate;
157 use rustc_ast::ast::WhereRegionPredicate;
158 use rustc_ast::ptr::P;
159 use rustc_ast::token::{
160     self, CommentKind, Delimiter, IdentIsRaw, Lit, Nonterminal, Token, TokenKind,
161 };
162 use rustc_ast::tokenstream::{
163     AttrTokenStream, AttrTokenTree, AttributesData, DelimSpacing, DelimSpan, LazyAttrTokenStream,
164     Spacing, TokenStream, TokenTree,
165 };
166 use rustc_data_structures::packed::Pu128;
167 use rustc_data_structures::sync::Lrc;
168 use rustc_span::source_map::Spanned;
169 use rustc_span::symbol::{sym, Ident};
170 use rustc_span::{ErrorGuaranteed, Span, Symbol, SyntaxContext, DUMMY_SP};
171 use std::collections::HashMap;
172 use std::hash::{BuildHasher, Hash};
173 use thin_vec::ThinVec;
174 
175 pub trait SpanlessEq {
eq(&self, other: &Self) -> bool176     fn eq(&self, other: &Self) -> bool;
177 }
178 
179 impl<T: ?Sized + SpanlessEq> SpanlessEq for Box<T> {
eq(&self, other: &Self) -> bool180     fn eq(&self, other: &Self) -> bool {
181         SpanlessEq::eq(&**self, &**other)
182     }
183 }
184 
185 impl<T: ?Sized + SpanlessEq> SpanlessEq for P<T> {
eq(&self, other: &Self) -> bool186     fn eq(&self, other: &Self) -> bool {
187         SpanlessEq::eq(&**self, &**other)
188     }
189 }
190 
191 impl<T: ?Sized + SpanlessEq> SpanlessEq for Lrc<T> {
eq(&self, other: &Self) -> bool192     fn eq(&self, other: &Self) -> bool {
193         SpanlessEq::eq(&**self, &**other)
194     }
195 }
196 
197 impl<T: SpanlessEq> SpanlessEq for Option<T> {
eq(&self, other: &Self) -> bool198     fn eq(&self, other: &Self) -> bool {
199         match (self, other) {
200             (None, None) => true,
201             (Some(this), Some(other)) => SpanlessEq::eq(this, other),
202             _ => false,
203         }
204     }
205 }
206 
207 impl<T: SpanlessEq, E: SpanlessEq> SpanlessEq for Result<T, E> {
eq(&self, other: &Self) -> bool208     fn eq(&self, other: &Self) -> bool {
209         match (self, other) {
210             (Ok(this), Ok(other)) => SpanlessEq::eq(this, other),
211             (Err(this), Err(other)) => SpanlessEq::eq(this, other),
212             _ => false,
213         }
214     }
215 }
216 
217 impl<T: SpanlessEq> SpanlessEq for [T] {
eq(&self, other: &Self) -> bool218     fn eq(&self, other: &Self) -> bool {
219         self.len() == other.len() && self.iter().zip(other).all(|(a, b)| SpanlessEq::eq(a, b))
220     }
221 }
222 
223 impl<T: SpanlessEq> SpanlessEq for Vec<T> {
eq(&self, other: &Self) -> bool224     fn eq(&self, other: &Self) -> bool {
225         <[T] as SpanlessEq>::eq(self, other)
226     }
227 }
228 
229 impl<T: SpanlessEq> SpanlessEq for ThinVec<T> {
eq(&self, other: &Self) -> bool230     fn eq(&self, other: &Self) -> bool {
231         self.len() == other.len()
232             && self
233                 .iter()
234                 .zip(other.iter())
235                 .all(|(a, b)| SpanlessEq::eq(a, b))
236     }
237 }
238 
239 impl<K: Eq + Hash, V: SpanlessEq, S: BuildHasher> SpanlessEq for HashMap<K, V, S> {
eq(&self, other: &Self) -> bool240     fn eq(&self, other: &Self) -> bool {
241         self.len() == other.len()
242             && self.iter().all(|(key, this_v)| {
243                 other
244                     .get(key)
245                     .map_or(false, |other_v| SpanlessEq::eq(this_v, other_v))
246             })
247     }
248 }
249 
250 impl<T: SpanlessEq> SpanlessEq for Spanned<T> {
eq(&self, other: &Self) -> bool251     fn eq(&self, other: &Self) -> bool {
252         SpanlessEq::eq(&self.node, &other.node)
253     }
254 }
255 
256 impl<A: SpanlessEq, B: SpanlessEq> SpanlessEq for (A, B) {
eq(&self, other: &Self) -> bool257     fn eq(&self, other: &Self) -> bool {
258         SpanlessEq::eq(&self.0, &other.0) && SpanlessEq::eq(&self.1, &other.1)
259     }
260 }
261 
262 impl<A: SpanlessEq, B: SpanlessEq, C: SpanlessEq> SpanlessEq for (A, B, C) {
eq(&self, other: &Self) -> bool263     fn eq(&self, other: &Self) -> bool {
264         SpanlessEq::eq(&self.0, &other.0)
265             && SpanlessEq::eq(&self.1, &other.1)
266             && SpanlessEq::eq(&self.2, &other.2)
267     }
268 }
269 
270 macro_rules! spanless_eq_true {
271     ($name:ty) => {
272         impl SpanlessEq for $name {
273             fn eq(&self, _other: &Self) -> bool {
274                 true
275             }
276         }
277     };
278 }
279 
280 spanless_eq_true!(Span);
281 spanless_eq_true!(DelimSpan);
282 spanless_eq_true!(AttrId);
283 spanless_eq_true!(NodeId);
284 spanless_eq_true!(SyntaxContext);
285 spanless_eq_true!(Spacing);
286 
287 macro_rules! spanless_eq_partial_eq {
288     ($name:ty) => {
289         impl SpanlessEq for $name {
290             fn eq(&self, other: &Self) -> bool {
291                 PartialEq::eq(self, other)
292             }
293         }
294     };
295 }
296 
297 spanless_eq_partial_eq!(bool);
298 spanless_eq_partial_eq!(u8);
299 spanless_eq_partial_eq!(u16);
300 spanless_eq_partial_eq!(u32);
301 spanless_eq_partial_eq!(u128);
302 spanless_eq_partial_eq!(usize);
303 spanless_eq_partial_eq!(char);
304 spanless_eq_partial_eq!(String);
305 spanless_eq_partial_eq!(Pu128);
306 spanless_eq_partial_eq!(Symbol);
307 spanless_eq_partial_eq!(CommentKind);
308 spanless_eq_partial_eq!(Delimiter);
309 spanless_eq_partial_eq!(InlineAsmOptions);
310 spanless_eq_partial_eq!(token::LitKind);
311 spanless_eq_partial_eq!(ErrorGuaranteed);
312 
313 macro_rules! spanless_eq_struct {
314     {
315         $($name:ident)::+ $(<$param:ident>)?
316         $([$field:tt $this:ident $other:ident])*
317         $(![$ignore:tt])*;
318     } => {
319         impl $(<$param: SpanlessEq>)* SpanlessEq for $($name)::+ $(<$param>)* {
320             fn eq(&self, other: &Self) -> bool {
321                 let $($name)::+ { $($field: $this,)* $($ignore: _,)* } = self;
322                 let $($name)::+ { $($field: $other,)* $($ignore: _,)* } = other;
323                 true $(&& SpanlessEq::eq($this, $other))*
324             }
325         }
326     };
327 
328     {
329         $($name:ident)::+ $(<$param:ident>)?
330         $([$field:tt $this:ident $other:ident])*
331         $(![$ignore:tt])*;
332         !$next:tt
333         $($rest:tt)*
334     } => {
335         spanless_eq_struct! {
336             $($name)::+ $(<$param>)*
337             $([$field $this $other])*
338             $(![$ignore])*
339             ![$next];
340             $($rest)*
341         }
342     };
343 
344     {
345         $($name:ident)::+ $(<$param:ident>)?
346         $([$field:tt $this:ident $other:ident])*
347         $(![$ignore:tt])*;
348         $next:tt
349         $($rest:tt)*
350     } => {
351         spanless_eq_struct! {
352             $($name)::+ $(<$param>)*
353             $([$field $this $other])*
354             [$next this other]
355             $(![$ignore])*;
356             $($rest)*
357         }
358     };
359 }
360 
361 macro_rules! spanless_eq_enum {
362     {
363         $($name:ident)::+;
364         $([$($variant:ident)::+; $([$field:tt $this:ident $other:ident])* $(![$ignore:tt])*])*
365     } => {
366         impl SpanlessEq for $($name)::+ {
367             fn eq(&self, other: &Self) -> bool {
368                 match self {
369                     $(
370                         $($variant)::+ { .. } => {}
371                     )*
372                 }
373                 #[allow(unreachable_patterns)]
374                 match (self, other) {
375                     $(
376                         (
377                             $($variant)::+ { $($field: $this,)* $($ignore: _,)* },
378                             $($variant)::+ { $($field: $other,)* $($ignore: _,)* },
379                         ) => {
380                             true $(&& SpanlessEq::eq($this, $other))*
381                         }
382                     )*
383                     _ => false,
384                 }
385             }
386         }
387     };
388 
389     {
390         $($name:ident)::+;
391         $([$($variant:ident)::+; $($fields:tt)*])*
392         $next:ident [$([$($named:tt)*])* $(![$ignore:tt])*] (!$i:tt $($field:tt)*)
393         $($rest:tt)*
394     } => {
395         spanless_eq_enum! {
396             $($name)::+;
397             $([$($variant)::+; $($fields)*])*
398             $next [$([$($named)*])* $(![$ignore])* ![$i]] ($($field)*)
399             $($rest)*
400         }
401     };
402 
403     {
404         $($name:ident)::+;
405         $([$($variant:ident)::+; $($fields:tt)*])*
406         $next:ident [$([$($named:tt)*])* $(![$ignore:tt])*] ($i:tt $($field:tt)*)
407         $($rest:tt)*
408     } => {
409         spanless_eq_enum! {
410             $($name)::+;
411             $([$($variant)::+; $($fields)*])*
412             $next [$([$($named)*])* [$i this other] $(![$ignore])*] ($($field)*)
413             $($rest)*
414         }
415     };
416 
417     {
418         $($name:ident)::+;
419         $([$($variant:ident)::+; $($fields:tt)*])*
420         $next:ident [$($named:tt)*] ()
421         $($rest:tt)*
422     } => {
423         spanless_eq_enum! {
424             $($name)::+;
425             $([$($variant)::+; $($fields)*])*
426             [$($name)::+::$next; $($named)*]
427             $($rest)*
428         }
429     };
430 
431     {
432         $($name:ident)::+;
433         $([$($variant:ident)::+; $($fields:tt)*])*
434         $next:ident ($($field:tt)*)
435         $($rest:tt)*
436     } => {
437         spanless_eq_enum! {
438             $($name)::+;
439             $([$($variant)::+; $($fields)*])*
440             $next [] ($($field)*)
441             $($rest)*
442         }
443     };
444 
445     {
446         $($name:ident)::+;
447         $([$($variant:ident)::+; $($fields:tt)*])*
448         $next:ident
449         $($rest:tt)*
450     } => {
451         spanless_eq_enum! {
452             $($name)::+;
453             $([$($variant)::+; $($fields)*])*
454             [$($name)::+::$next;]
455             $($rest)*
456         }
457     };
458 }
459 
460 spanless_eq_struct!(AngleBracketedArgs; span args);
461 spanless_eq_struct!(AnonConst; id value);
462 spanless_eq_struct!(Arm; attrs pat guard body span id is_placeholder);
463 spanless_eq_struct!(AssocConstraint; id ident gen_args kind span);
464 spanless_eq_struct!(AttrItem; path args tokens);
465 spanless_eq_struct!(AttrTokenStream; 0);
466 spanless_eq_struct!(Attribute; kind id style span);
467 spanless_eq_struct!(AttributesData; attrs tokens);
468 spanless_eq_struct!(BareFnTy; unsafety ext generic_params decl decl_span);
469 spanless_eq_struct!(BindingAnnotation; 0 1);
470 spanless_eq_struct!(Block; stmts id rules span tokens could_be_bare_literal);
471 spanless_eq_struct!(Closure; binder capture_clause constness coroutine_kind movability fn_decl body !fn_decl_span !fn_arg_span);
472 spanless_eq_struct!(ConstItem; defaultness generics ty expr);
473 spanless_eq_struct!(Crate; attrs items spans id is_placeholder);
474 spanless_eq_struct!(Delegation; id qself path body);
475 spanless_eq_struct!(DelimArgs; dspan delim tokens);
476 spanless_eq_struct!(DelimSpacing; open close);
477 spanless_eq_struct!(EnumDef; variants);
478 spanless_eq_struct!(Expr; id kind span attrs !tokens);
479 spanless_eq_struct!(ExprField; attrs id span ident expr is_shorthand is_placeholder);
480 spanless_eq_struct!(FieldDef; attrs id span vis ident ty is_placeholder);
481 spanless_eq_struct!(Fn; defaultness generics sig body);
482 spanless_eq_struct!(FnDecl; inputs output);
483 spanless_eq_struct!(FnHeader; constness coroutine_kind unsafety ext);
484 spanless_eq_struct!(FnSig; header decl span);
485 spanless_eq_struct!(ForeignMod; unsafety abi items);
486 spanless_eq_struct!(FormatArgPosition; index kind span);
487 spanless_eq_struct!(FormatArgs; span template arguments);
488 spanless_eq_struct!(FormatArgument; kind expr);
489 spanless_eq_struct!(FormatOptions; width precision alignment fill sign alternate zero_pad debug_hex);
490 spanless_eq_struct!(FormatPlaceholder; argument span format_trait format_options);
491 spanless_eq_struct!(GenericParam; id ident attrs bounds is_placeholder kind !colon_span);
492 spanless_eq_struct!(Generics; params where_clause span);
493 spanless_eq_struct!(Impl; defaultness unsafety generics constness polarity of_trait self_ty items);
494 spanless_eq_struct!(InlineAsm; template template_strs operands clobber_abis options line_spans);
495 spanless_eq_struct!(InlineAsmSym; id qself path);
496 spanless_eq_struct!(Item<K>; attrs id span vis ident kind !tokens);
497 spanless_eq_struct!(Label; ident);
498 spanless_eq_struct!(Lifetime; id ident);
499 spanless_eq_struct!(Lit; kind symbol suffix);
500 spanless_eq_struct!(Local; id pat ty kind span colon_sp attrs !tokens);
501 spanless_eq_struct!(MacCall; path args);
502 spanless_eq_struct!(MacCallStmt; mac style attrs tokens);
503 spanless_eq_struct!(MacroDef; body macro_rules);
504 spanless_eq_struct!(MetaItemLit; symbol suffix kind span);
505 spanless_eq_struct!(MethodCall; seg receiver args !span);
506 spanless_eq_struct!(ModSpans; !inner_span !inject_use_span);
507 spanless_eq_struct!(MutTy; ty mutbl);
508 spanless_eq_struct!(NormalAttr; item tokens);
509 spanless_eq_struct!(ParenthesizedArgs; span inputs inputs_span output);
510 spanless_eq_struct!(Pat; id kind span tokens);
511 spanless_eq_struct!(PatField; ident pat is_shorthand attrs id span is_placeholder);
512 spanless_eq_struct!(Path; span segments tokens);
513 spanless_eq_struct!(PathSegment; ident id args);
514 spanless_eq_struct!(PolyTraitRef; bound_generic_params trait_ref span);
515 spanless_eq_struct!(QSelf; ty path_span position);
516 spanless_eq_struct!(StaticItem; ty mutability expr);
517 spanless_eq_struct!(Stmt; id kind span);
518 spanless_eq_struct!(StrLit; symbol suffix symbol_unescaped style span);
519 spanless_eq_struct!(StructExpr; qself path fields rest);
520 spanless_eq_struct!(Token; kind span);
521 spanless_eq_struct!(Trait; unsafety is_auto generics bounds items);
522 spanless_eq_struct!(TraitBoundModifiers; constness asyncness polarity);
523 spanless_eq_struct!(TraitRef; path ref_id);
524 spanless_eq_struct!(Ty; id kind span tokens);
525 spanless_eq_struct!(TyAlias; defaultness generics where_clauses bounds ty);
526 spanless_eq_struct!(TyAliasWhereClause; !has_where_token span);
527 spanless_eq_struct!(TyAliasWhereClauses; before after !split);
528 spanless_eq_struct!(UseTree; prefix kind span);
529 spanless_eq_struct!(Variant; attrs id span !vis ident data disr_expr is_placeholder);
530 spanless_eq_struct!(Visibility; kind span tokens);
531 spanless_eq_struct!(WhereBoundPredicate; span bound_generic_params bounded_ty bounds);
532 spanless_eq_struct!(WhereClause; has_where_token predicates span);
533 spanless_eq_struct!(WhereEqPredicate; span lhs_ty rhs_ty);
534 spanless_eq_struct!(WhereRegionPredicate; span lifetime bounds);
535 spanless_eq_enum!(AngleBracketedArg; Arg(0) Constraint(0));
536 spanless_eq_enum!(AssocConstraintKind; Equality(term) Bound(bounds));
537 spanless_eq_enum!(AssocItemKind; Const(0) Fn(0) Type(0) MacCall(0) Delegation(0));
538 spanless_eq_enum!(AttrArgs; Empty Delimited(0) Eq(0 1));
539 spanless_eq_enum!(AttrArgsEq; Ast(0) Hir(0));
540 spanless_eq_enum!(AttrStyle; Outer Inner);
541 spanless_eq_enum!(AttrTokenTree; Token(0 1) Delimited(0 1 2 3) Attributes(0));
542 spanless_eq_enum!(BinOpKind; Add Sub Mul Div Rem And Or BitXor BitAnd BitOr Shl Shr Eq Lt Le Ne Ge Gt);
543 spanless_eq_enum!(BlockCheckMode; Default Unsafe(0));
544 spanless_eq_enum!(BorrowKind; Ref Raw);
545 spanless_eq_enum!(BoundAsyncness; Normal Async(0));
546 spanless_eq_enum!(BoundConstness; Never Always(0) Maybe(0));
547 spanless_eq_enum!(BoundPolarity; Positive Negative(0) Maybe(0));
548 spanless_eq_enum!(ByRef; Yes(0) No);
549 spanless_eq_enum!(CaptureBy; Value(move_kw) Ref);
550 spanless_eq_enum!(ClosureBinder; NotPresent For(span generic_params));
551 spanless_eq_enum!(Const; Yes(0) No);
552 spanless_eq_enum!(Defaultness; Default(0) Final);
553 spanless_eq_enum!(Extern; None Implicit(0) Explicit(0 1));
554 spanless_eq_enum!(FloatTy; F16 F32 F64 F128);
555 spanless_eq_enum!(FnRetTy; Default(0) Ty(0));
556 spanless_eq_enum!(ForLoopKind; For ForAwait);
557 spanless_eq_enum!(ForeignItemKind; Static(0 1 2) Fn(0) TyAlias(0) MacCall(0));
558 spanless_eq_enum!(FormatAlignment; Left Right Center);
559 spanless_eq_enum!(FormatArgPositionKind; Implicit Number Named);
560 spanless_eq_enum!(FormatArgsPiece; Literal(0) Placeholder(0));
561 spanless_eq_enum!(FormatArgumentKind; Normal Named(0) Captured(0));
562 spanless_eq_enum!(FormatCount; Literal(0) Argument(0));
563 spanless_eq_enum!(FormatDebugHex; Lower Upper);
564 spanless_eq_enum!(FormatSign; Plus Minus);
565 spanless_eq_enum!(FormatTrait; Display Debug LowerExp UpperExp Octal Pointer Binary LowerHex UpperHex);
566 spanless_eq_enum!(GenBlockKind; Async Gen AsyncGen);
567 spanless_eq_enum!(GenericArg; Lifetime(0) Type(0) Const(0));
568 spanless_eq_enum!(GenericArgs; AngleBracketed(0) Parenthesized(0));
569 spanless_eq_enum!(GenericBound; Trait(0 1) Outlives(0));
570 spanless_eq_enum!(GenericParamKind; Lifetime Type(default) Const(ty kw_span default));
571 spanless_eq_enum!(ImplPolarity; Positive Negative(0));
572 spanless_eq_enum!(Inline; Yes No);
573 spanless_eq_enum!(InlineAsmRegOrRegClass; Reg(0) RegClass(0));
574 spanless_eq_enum!(InlineAsmTemplatePiece; String(0) Placeholder(operand_idx modifier span));
575 spanless_eq_enum!(IntTy; Isize I8 I16 I32 I64 I128);
576 spanless_eq_enum!(IsAuto; Yes No);
577 spanless_eq_enum!(LitFloatType; Suffixed(0) Unsuffixed);
578 spanless_eq_enum!(LitIntType; Signed(0) Unsigned(0) Unsuffixed);
579 spanless_eq_enum!(LocalKind; Decl Init(0) InitElse(0 1));
580 spanless_eq_enum!(MacStmtStyle; Semicolon Braces NoBraces);
581 spanless_eq_enum!(MatchKind; Prefix Postfix);
582 spanless_eq_enum!(ModKind; Loaded(0 1 2) Unloaded);
583 spanless_eq_enum!(Movability; Static Movable);
584 spanless_eq_enum!(Mutability; Mut Not);
585 spanless_eq_enum!(PatFieldsRest; Rest None);
586 spanless_eq_enum!(RangeEnd; Included(0) Excluded);
587 spanless_eq_enum!(RangeLimits; HalfOpen Closed);
588 spanless_eq_enum!(StmtKind; Let(0) Item(0) Expr(0) Semi(0) Empty MacCall(0));
589 spanless_eq_enum!(StrStyle; Cooked Raw(0));
590 spanless_eq_enum!(StructRest; Base(0) Rest(0) None);
591 spanless_eq_enum!(Term; Ty(0) Const(0));
592 spanless_eq_enum!(TokenTree; Token(0 1) Delimited(0 1 2 3));
593 spanless_eq_enum!(TraitObjectSyntax; Dyn DynStar None);
594 spanless_eq_enum!(UintTy; Usize U8 U16 U32 U64 U128);
595 spanless_eq_enum!(UnOp; Deref Not Neg);
596 spanless_eq_enum!(Unsafe; Yes(0) No);
597 spanless_eq_enum!(UnsafeSource; CompilerGenerated UserProvided);
598 spanless_eq_enum!(UseTreeKind; Simple(0) Nested(0) Glob);
599 spanless_eq_enum!(VariantData; Struct(fields recovered) Tuple(0 1) Unit(0));
600 spanless_eq_enum!(VisibilityKind; Public Restricted(path id shorthand) Inherited);
601 spanless_eq_enum!(WherePredicate; BoundPredicate(0) RegionPredicate(0) EqPredicate(0));
602 spanless_eq_enum!(CoroutineKind; Async(span closure_id return_impl_trait_id)
603     Gen(span closure_id return_impl_trait_id)
604     AsyncGen(span closure_id return_impl_trait_id));
605 spanless_eq_enum!(ExprKind; Array(0) ConstBlock(0) Call(0 1) MethodCall(0)
606     Tup(0) Binary(0 1 2) Unary(0 1) Lit(0) Cast(0 1) Type(0 1) Let(0 1 2 3)
607     If(0 1 2) While(0 1 2) ForLoop(pat iter body label kind) Loop(0 1 2)
608     Match(0 1 2) Closure(0) Block(0 1) Gen(0 1 2) Await(0 1) TryBlock(0)
609     Assign(0 1 2) AssignOp(0 1 2) Field(0 1) Index(0 1 2) Underscore
610     Range(0 1 2) Path(0 1) AddrOf(0 1 2) Break(0 1) Continue(0) Ret(0)
611     InlineAsm(0) OffsetOf(0 1) MacCall(0) Struct(0) Repeat(0 1) Paren(0) Try(0)
612     Yield(0) Yeet(0) Become(0) IncludedBytes(0) FormatArgs(0) Err(0) Dummy);
613 spanless_eq_enum!(InlineAsmOperand; In(reg expr) Out(reg late expr)
614     InOut(reg late expr) SplitInOut(reg late in_expr out_expr) Const(anon_const)
615     Sym(sym) Label(block));
616 spanless_eq_enum!(ItemKind; ExternCrate(0) Use(0) Static(0) Const(0) Fn(0)
617     Mod(0 1) ForeignMod(0) GlobalAsm(0) TyAlias(0) Enum(0 1) Struct(0 1)
618     Union(0 1) Trait(0) TraitAlias(0 1) Impl(0) MacCall(0) MacroDef(0)
619     Delegation(0));
620 spanless_eq_enum!(LitKind; Str(0 1) ByteStr(0 1) CStr(0 1) Byte(0) Char(0)
621     Int(0 1) Float(0 1) Bool(0) Err(0));
622 spanless_eq_enum!(PatKind; Wild Ident(0 1 2) Struct(0 1 2 3) TupleStruct(0 1 2)
623     Or(0) Path(0 1) Tuple(0) Box(0) Deref(0) Ref(0 1) Lit(0) Range(0 1 2)
624     Slice(0) Rest Never Paren(0) MacCall(0) Err(0));
625 spanless_eq_enum!(TyKind; Slice(0) Array(0 1) Ptr(0) Ref(0 1) BareFn(0) Never
626     Tup(0) AnonStruct(0 1) AnonUnion(0 1) Path(0 1) TraitObject(0 1)
627     ImplTrait(0 1) Paren(0) Typeof(0) Infer ImplicitSelf MacCall(0) CVarArgs
628     Dummy Err(0));
629 
630 impl SpanlessEq for Ident {
eq(&self, other: &Self) -> bool631     fn eq(&self, other: &Self) -> bool {
632         self.as_str() == other.as_str()
633     }
634 }
635 
636 impl SpanlessEq for RangeSyntax {
eq(&self, _other: &Self) -> bool637     fn eq(&self, _other: &Self) -> bool {
638         match self {
639             RangeSyntax::DotDotDot | RangeSyntax::DotDotEq => true,
640         }
641     }
642 }
643 
644 impl SpanlessEq for Param {
eq(&self, other: &Self) -> bool645     fn eq(&self, other: &Self) -> bool {
646         let Param {
647             attrs,
648             ty,
649             pat,
650             id,
651             span: _,
652             is_placeholder,
653         } = self;
654         let Param {
655             attrs: attrs2,
656             ty: ty2,
657             pat: pat2,
658             id: id2,
659             span: _,
660             is_placeholder: is_placeholder2,
661         } = other;
662         SpanlessEq::eq(id, id2)
663             && SpanlessEq::eq(is_placeholder, is_placeholder2)
664             && (matches!(ty.kind, TyKind::Err(_))
665                 || matches!(ty2.kind, TyKind::Err(_))
666                 || SpanlessEq::eq(attrs, attrs2)
667                     && SpanlessEq::eq(ty, ty2)
668                     && SpanlessEq::eq(pat, pat2))
669     }
670 }
671 
672 impl SpanlessEq for TokenKind {
eq(&self, other: &Self) -> bool673     fn eq(&self, other: &Self) -> bool {
674         match (self, other) {
675             (TokenKind::Literal(this), TokenKind::Literal(other)) => SpanlessEq::eq(this, other),
676             (TokenKind::DotDotEq | TokenKind::DotDotDot, _) => match other {
677                 TokenKind::DotDotEq | TokenKind::DotDotDot => true,
678                 _ => false,
679             },
680             (TokenKind::Interpolated(this), TokenKind::Interpolated(other)) => {
681                 let (this, this_span) = this.as_ref();
682                 let (other, other_span) = other.as_ref();
683                 SpanlessEq::eq(this_span, other_span)
684                     && match (this, other) {
685                         (Nonterminal::NtExpr(this), Nonterminal::NtExpr(other)) => {
686                             SpanlessEq::eq(this, other)
687                         }
688                         _ => this == other,
689                     }
690             }
691             _ => self == other,
692         }
693     }
694 }
695 
696 impl SpanlessEq for TokenStream {
eq(&self, other: &Self) -> bool697     fn eq(&self, other: &Self) -> bool {
698         let mut this_trees = self.trees();
699         let mut other_trees = other.trees();
700         loop {
701             let this = match this_trees.next() {
702                 None => return other_trees.next().is_none(),
703                 Some(tree) => tree,
704             };
705             let other = match other_trees.next() {
706                 None => return false,
707                 Some(tree) => tree,
708             };
709             if SpanlessEq::eq(this, other) {
710                 continue;
711             }
712             if let (TokenTree::Token(this, _), TokenTree::Token(other, _)) = (this, other) {
713                 if match (&this.kind, &other.kind) {
714                     (TokenKind::Literal(this), TokenKind::Literal(other)) => {
715                         SpanlessEq::eq(this, other)
716                     }
717                     (TokenKind::DocComment(_kind, style, symbol), TokenKind::Pound) => {
718                         doc_comment(*style, *symbol, &mut other_trees)
719                     }
720                     (TokenKind::Pound, TokenKind::DocComment(_kind, style, symbol)) => {
721                         doc_comment(*style, *symbol, &mut this_trees)
722                     }
723                     _ => false,
724                 } {
725                     continue;
726                 }
727             }
728             return false;
729         }
730     }
731 }
732 
doc_comment<'a>( style: AttrStyle, unescaped: Symbol, trees: &mut impl Iterator<Item = &'a TokenTree>, ) -> bool733 fn doc_comment<'a>(
734     style: AttrStyle,
735     unescaped: Symbol,
736     trees: &mut impl Iterator<Item = &'a TokenTree>,
737 ) -> bool {
738     if match style {
739         AttrStyle::Outer => false,
740         AttrStyle::Inner => true,
741     } {
742         match trees.next() {
743             Some(TokenTree::Token(
744                 Token {
745                     kind: TokenKind::Not,
746                     span: _,
747                 },
748                 _spacing,
749             )) => {}
750             _ => return false,
751         }
752     }
753     let stream = match trees.next() {
754         Some(TokenTree::Delimited(_span, _spacing, Delimiter::Bracket, stream)) => stream,
755         _ => return false,
756     };
757     let mut trees = stream.trees();
758     match trees.next() {
759         Some(TokenTree::Token(
760             Token {
761                 kind: TokenKind::Ident(symbol, IdentIsRaw::No),
762                 span: _,
763             },
764             _spacing,
765         )) if *symbol == sym::doc => {}
766         _ => return false,
767     }
768     match trees.next() {
769         Some(TokenTree::Token(
770             Token {
771                 kind: TokenKind::Eq,
772                 span: _,
773             },
774             _spacing,
775         )) => {}
776         _ => return false,
777     }
778     match trees.next() {
779         Some(TokenTree::Token(token, _spacing)) => {
780             is_escaped_literal_token(token, unescaped) && trees.next().is_none()
781         }
782         _ => false,
783     }
784 }
785 
is_escaped_literal_token(token: &Token, unescaped: Symbol) -> bool786 fn is_escaped_literal_token(token: &Token, unescaped: Symbol) -> bool {
787     match token {
788         Token {
789             kind: TokenKind::Literal(lit),
790             span: _,
791         } => match MetaItemLit::from_token_lit(*lit, DUMMY_SP) {
792             Ok(lit) => is_escaped_literal_meta_item_lit(&lit, unescaped),
793             Err(_) => false,
794         },
795         Token {
796             kind: TokenKind::Interpolated(nonterminal),
797             span: _,
798         } => match &nonterminal.0 {
799             Nonterminal::NtExpr(expr) => match &expr.kind {
800                 ExprKind::Lit(lit) => is_escaped_lit(lit, unescaped),
801                 _ => false,
802             },
803             _ => false,
804         },
805         _ => false,
806     }
807 }
808 
is_escaped_literal_attr_args(value: &AttrArgsEq, unescaped: Symbol) -> bool809 fn is_escaped_literal_attr_args(value: &AttrArgsEq, unescaped: Symbol) -> bool {
810     match value {
811         AttrArgsEq::Ast(expr) => match &expr.kind {
812             ExprKind::Lit(lit) => is_escaped_lit(lit, unescaped),
813             _ => false,
814         },
815         AttrArgsEq::Hir(lit) => is_escaped_literal_meta_item_lit(lit, unescaped),
816     }
817 }
818 
is_escaped_literal_meta_item_lit(lit: &MetaItemLit, unescaped: Symbol) -> bool819 fn is_escaped_literal_meta_item_lit(lit: &MetaItemLit, unescaped: Symbol) -> bool {
820     match lit {
821         MetaItemLit {
822             symbol: _,
823             suffix: None,
824             kind,
825             span: _,
826         } => is_escaped_lit_kind(kind, unescaped),
827         _ => false,
828     }
829 }
830 
is_escaped_lit(lit: &Lit, unescaped: Symbol) -> bool831 fn is_escaped_lit(lit: &Lit, unescaped: Symbol) -> bool {
832     match lit {
833         Lit {
834             kind: token::LitKind::Str,
835             symbol: _,
836             suffix: None,
837         } => match LitKind::from_token_lit(*lit) {
838             Ok(lit_kind) => is_escaped_lit_kind(&lit_kind, unescaped),
839             _ => false,
840         },
841         _ => false,
842     }
843 }
844 
is_escaped_lit_kind(kind: &LitKind, unescaped: Symbol) -> bool845 fn is_escaped_lit_kind(kind: &LitKind, unescaped: Symbol) -> bool {
846     match kind {
847         LitKind::Str(symbol, StrStyle::Cooked) => {
848             symbol.as_str().replace('\r', "") == unescaped.as_str().replace('\r', "")
849         }
850         _ => false,
851     }
852 }
853 
854 impl SpanlessEq for LazyAttrTokenStream {
eq(&self, other: &Self) -> bool855     fn eq(&self, other: &Self) -> bool {
856         let this = self.to_attr_token_stream();
857         let other = other.to_attr_token_stream();
858         SpanlessEq::eq(&this, &other)
859     }
860 }
861 
862 impl SpanlessEq for AttrKind {
eq(&self, other: &Self) -> bool863     fn eq(&self, other: &Self) -> bool {
864         match (self, other) {
865             (AttrKind::Normal(normal), AttrKind::Normal(normal2)) => {
866                 SpanlessEq::eq(normal, normal2)
867             }
868             (AttrKind::DocComment(kind, symbol), AttrKind::DocComment(kind2, symbol2)) => {
869                 SpanlessEq::eq(kind, kind2) && SpanlessEq::eq(symbol, symbol2)
870             }
871             (AttrKind::DocComment(kind, unescaped), AttrKind::Normal(normal2)) => {
872                 match kind {
873                     CommentKind::Line | CommentKind::Block => {}
874                 }
875                 let path = Path::from_ident(Ident::with_dummy_span(sym::doc));
876                 SpanlessEq::eq(&path, &normal2.item.path)
877                     && match &normal2.item.args {
878                         AttrArgs::Empty | AttrArgs::Delimited(_) => false,
879                         AttrArgs::Eq(_span, value) => {
880                             is_escaped_literal_attr_args(value, *unescaped)
881                         }
882                     }
883             }
884             (AttrKind::Normal(_), AttrKind::DocComment(..)) => SpanlessEq::eq(other, self),
885         }
886     }
887 }
888 
889 impl SpanlessEq for FormatArguments {
eq(&self, other: &Self) -> bool890     fn eq(&self, other: &Self) -> bool {
891         SpanlessEq::eq(self.all_args(), other.all_args())
892     }
893 }
894