• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 extern crate rustc_ast;
2 extern crate rustc_data_structures;
3 extern crate rustc_span;
4 
5 use rustc_ast::ast::{
6     AngleBracketedArg, AngleBracketedArgs, AnonConst, Arm, AssocItemKind, AssocTyConstraint,
7     AssocTyConstraintKind, Async, AttrId, AttrItem, AttrKind, AttrStyle, Attribute, BareFnTy,
8     BinOpKind, BindingMode, Block, BlockCheckMode, BorrowKind, CaptureBy, Const, Crate, CrateSugar,
9     Defaultness, EnumDef, Expr, ExprField, ExprKind, Extern, FieldDef, FloatTy, FnDecl, FnHeader,
10     FnKind, FnRetTy, FnSig, ForeignItemKind, ForeignMod, GenericArg, GenericArgs, GenericBound,
11     GenericParam, GenericParamKind, Generics, GlobalAsm, ImplKind, ImplPolarity, Inline, InlineAsm,
12     InlineAsmOperand, InlineAsmOptions, InlineAsmRegOrRegClass, InlineAsmTemplatePiece, IntTy,
13     IsAuto, Item, ItemKind, Label, Lifetime, Lit, LitFloatType, LitIntType, LitKind,
14     LlvmAsmDialect, LlvmInlineAsm, LlvmInlineAsmOutput, Local, MacArgs, MacCall, MacCallStmt,
15     MacDelimiter, MacStmtStyle, MacroDef, ModKind, Movability, MutTy, Mutability, NodeId, Param,
16     ParenthesizedArgs, Pat, PatField, PatKind, Path, PathSegment, PolyTraitRef, QSelf, RangeEnd,
17     RangeLimits, RangeSyntax, Stmt, StmtKind, StrLit, StrStyle, StructExpr, StructRest,
18     TraitBoundModifier, TraitKind, TraitObjectSyntax, TraitRef, Ty, TyAliasKind, TyKind, UintTy,
19     UnOp, Unsafe, UnsafeSource, UseTree, UseTreeKind, Variant, VariantData, Visibility,
20     VisibilityKind, WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate,
21     WhereRegionPredicate,
22 };
23 use rustc_ast::ptr::P;
24 use rustc_ast::token::{self, CommentKind, DelimToken, Nonterminal, Token, TokenKind};
25 use rustc_ast::tokenstream::{DelimSpan, LazyTokenStream, TokenStream, TokenTree};
26 use rustc_data_structures::sync::Lrc;
27 use rustc_data_structures::thin_vec::ThinVec;
28 use rustc_span::source_map::Spanned;
29 use rustc_span::symbol::{sym, Ident};
30 use rustc_span::{Span, Symbol, SyntaxContext, DUMMY_SP};
31 
32 pub trait SpanlessEq {
eq(&self, other: &Self) -> bool33     fn eq(&self, other: &Self) -> bool;
34 }
35 
36 impl<T: SpanlessEq> SpanlessEq for Box<T> {
eq(&self, other: &Self) -> bool37     fn eq(&self, other: &Self) -> bool {
38         SpanlessEq::eq(&**self, &**other)
39     }
40 }
41 
42 impl<T: SpanlessEq> SpanlessEq for P<T> {
eq(&self, other: &Self) -> bool43     fn eq(&self, other: &Self) -> bool {
44         SpanlessEq::eq(&**self, &**other)
45     }
46 }
47 
48 impl<T: ?Sized + SpanlessEq> SpanlessEq for Lrc<T> {
eq(&self, other: &Self) -> bool49     fn eq(&self, other: &Self) -> bool {
50         SpanlessEq::eq(&**self, &**other)
51     }
52 }
53 
54 impl<T: SpanlessEq> SpanlessEq for Option<T> {
eq(&self, other: &Self) -> bool55     fn eq(&self, other: &Self) -> bool {
56         match (self, other) {
57             (None, None) => true,
58             (Some(this), Some(other)) => SpanlessEq::eq(this, other),
59             _ => false,
60         }
61     }
62 }
63 
64 impl<T: SpanlessEq> SpanlessEq for [T] {
eq(&self, other: &Self) -> bool65     fn eq(&self, other: &Self) -> bool {
66         self.len() == other.len() && self.iter().zip(other).all(|(a, b)| SpanlessEq::eq(a, b))
67     }
68 }
69 
70 impl<T: SpanlessEq> SpanlessEq for Vec<T> {
eq(&self, other: &Self) -> bool71     fn eq(&self, other: &Self) -> bool {
72         <[T] as SpanlessEq>::eq(self, other)
73     }
74 }
75 
76 impl<T: SpanlessEq> SpanlessEq for ThinVec<T> {
eq(&self, other: &Self) -> bool77     fn eq(&self, other: &Self) -> bool {
78         self.len() == other.len()
79             && self
80                 .iter()
81                 .zip(other.iter())
82                 .all(|(a, b)| SpanlessEq::eq(a, b))
83     }
84 }
85 
86 impl<T: SpanlessEq> SpanlessEq for Spanned<T> {
eq(&self, other: &Self) -> bool87     fn eq(&self, other: &Self) -> bool {
88         SpanlessEq::eq(&self.node, &other.node)
89     }
90 }
91 
92 impl<A: SpanlessEq, B: SpanlessEq> SpanlessEq for (A, B) {
eq(&self, other: &Self) -> bool93     fn eq(&self, other: &Self) -> bool {
94         SpanlessEq::eq(&self.0, &other.0) && SpanlessEq::eq(&self.1, &other.1)
95     }
96 }
97 
98 macro_rules! spanless_eq_true {
99     ($name:ty) => {
100         impl SpanlessEq for $name {
101             fn eq(&self, _other: &Self) -> bool {
102                 true
103             }
104         }
105     };
106 }
107 
108 spanless_eq_true!(Span);
109 spanless_eq_true!(DelimSpan);
110 spanless_eq_true!(AttrId);
111 spanless_eq_true!(NodeId);
112 spanless_eq_true!(SyntaxContext);
113 
114 macro_rules! spanless_eq_partial_eq {
115     ($name:ty) => {
116         impl SpanlessEq for $name {
117             fn eq(&self, other: &Self) -> bool {
118                 PartialEq::eq(self, other)
119             }
120         }
121     };
122 }
123 
124 spanless_eq_partial_eq!(bool);
125 spanless_eq_partial_eq!(u8);
126 spanless_eq_partial_eq!(u16);
127 spanless_eq_partial_eq!(u128);
128 spanless_eq_partial_eq!(usize);
129 spanless_eq_partial_eq!(char);
130 spanless_eq_partial_eq!(String);
131 spanless_eq_partial_eq!(Symbol);
132 spanless_eq_partial_eq!(CommentKind);
133 spanless_eq_partial_eq!(DelimToken);
134 spanless_eq_partial_eq!(InlineAsmOptions);
135 spanless_eq_partial_eq!(token::LitKind);
136 
137 macro_rules! spanless_eq_struct {
138     {
139         $($name:ident)::+ $(<$param:ident>)?
140         $([$field:tt $this:ident $other:ident])*
141         $(![$ignore:tt])*;
142     } => {
143         impl $(<$param: SpanlessEq>)* SpanlessEq for $($name)::+ $(<$param>)* {
144             fn eq(&self, other: &Self) -> bool {
145                 let $($name)::+ { $($field: $this,)* $($ignore: _,)* } = self;
146                 let $($name)::+ { $($field: $other,)* $($ignore: _,)* } = other;
147                 true $(&& SpanlessEq::eq($this, $other))*
148             }
149         }
150     };
151 
152     {
153         $($name:ident)::+ $(<$param:ident>)?
154         $([$field:tt $this:ident $other:ident])*
155         $(![$ignore:tt])*;
156         !$next:tt
157         $($rest:tt)*
158     } => {
159         spanless_eq_struct! {
160             $($name)::+ $(<$param>)*
161             $([$field $this $other])*
162             $(![$ignore])*
163             ![$next];
164             $($rest)*
165         }
166     };
167 
168     {
169         $($name:ident)::+ $(<$param:ident>)?
170         $([$field:tt $this:ident $other:ident])*
171         $(![$ignore:tt])*;
172         $next:tt
173         $($rest:tt)*
174     } => {
175         spanless_eq_struct! {
176             $($name)::+ $(<$param>)*
177             $([$field $this $other])*
178             [$next this other]
179             $(![$ignore])*;
180             $($rest)*
181         }
182     };
183 }
184 
185 macro_rules! spanless_eq_enum {
186     {
187         $($name:ident)::+;
188         $([$($variant:ident)::+; $([$field:tt $this:ident $other:ident])* $(![$ignore:tt])*])*
189     } => {
190         impl SpanlessEq for $($name)::+ {
191             fn eq(&self, other: &Self) -> bool {
192                 match self {
193                     $(
194                         $($variant)::+ { .. } => {}
195                     )*
196                 }
197                 #[allow(unreachable_patterns)]
198                 match (self, other) {
199                     $(
200                         (
201                             $($variant)::+ { $($field: $this,)* $($ignore: _,)* },
202                             $($variant)::+ { $($field: $other,)* $($ignore: _,)* },
203                         ) => {
204                             true $(&& SpanlessEq::eq($this, $other))*
205                         }
206                     )*
207                     _ => false,
208                 }
209             }
210         }
211     };
212 
213     {
214         $($name:ident)::+;
215         $([$($variant:ident)::+; $($fields:tt)*])*
216         $next:ident [$([$($named:tt)*])* $(![$ignore:tt])*] (!$i:tt $($field:tt)*)
217         $($rest:tt)*
218     } => {
219         spanless_eq_enum! {
220             $($name)::+;
221             $([$($variant)::+; $($fields)*])*
222             $next [$([$($named)*])* $(![$ignore])* ![$i]] ($($field)*)
223             $($rest)*
224         }
225     };
226 
227     {
228         $($name:ident)::+;
229         $([$($variant:ident)::+; $($fields:tt)*])*
230         $next:ident [$([$($named:tt)*])* $(![$ignore:tt])*] ($i:tt $($field:tt)*)
231         $($rest:tt)*
232     } => {
233         spanless_eq_enum! {
234             $($name)::+;
235             $([$($variant)::+; $($fields)*])*
236             $next [$([$($named)*])* [$i this other] $(![$ignore])*] ($($field)*)
237             $($rest)*
238         }
239     };
240 
241     {
242         $($name:ident)::+;
243         $([$($variant:ident)::+; $($fields:tt)*])*
244         $next:ident [$($named:tt)*] ()
245         $($rest:tt)*
246     } => {
247         spanless_eq_enum! {
248             $($name)::+;
249             $([$($variant)::+; $($fields)*])*
250             [$($name)::+::$next; $($named)*]
251             $($rest)*
252         }
253     };
254 
255     {
256         $($name:ident)::+;
257         $([$($variant:ident)::+; $($fields:tt)*])*
258         $next:ident ($($field:tt)*)
259         $($rest:tt)*
260     } => {
261         spanless_eq_enum! {
262             $($name)::+;
263             $([$($variant)::+; $($fields)*])*
264             $next [] ($($field)*)
265             $($rest)*
266         }
267     };
268 
269     {
270         $($name:ident)::+;
271         $([$($variant:ident)::+; $($fields:tt)*])*
272         $next:ident
273         $($rest:tt)*
274     } => {
275         spanless_eq_enum! {
276             $($name)::+;
277             $([$($variant)::+; $($fields)*])*
278             [$($name)::+::$next;]
279             $($rest)*
280         }
281     };
282 }
283 
284 spanless_eq_struct!(AngleBracketedArgs; span args);
285 spanless_eq_struct!(AnonConst; id value);
286 spanless_eq_struct!(Arm; attrs pat guard body span id is_placeholder);
287 spanless_eq_struct!(AssocTyConstraint; id ident gen_args kind span);
288 spanless_eq_struct!(AttrItem; path args tokens);
289 spanless_eq_struct!(Attribute; kind id style span);
290 spanless_eq_struct!(BareFnTy; unsafety ext generic_params decl);
291 spanless_eq_struct!(Block; stmts id rules span tokens);
292 spanless_eq_struct!(Crate; attrs items span proc_macros);
293 spanless_eq_struct!(EnumDef; variants);
294 spanless_eq_struct!(Expr; id kind span attrs !tokens);
295 spanless_eq_struct!(ExprField; attrs id span ident expr is_shorthand is_placeholder);
296 spanless_eq_struct!(FieldDef; attrs id span vis ident ty is_placeholder);
297 spanless_eq_struct!(FnDecl; inputs output);
298 spanless_eq_struct!(FnHeader; constness asyncness unsafety ext);
299 spanless_eq_struct!(FnKind; 0 1 2 3);
300 spanless_eq_struct!(FnSig; header decl span);
301 spanless_eq_struct!(ForeignMod; unsafety abi items);
302 spanless_eq_struct!(GenericParam; id ident attrs bounds is_placeholder kind);
303 spanless_eq_struct!(Generics; params where_clause span);
304 spanless_eq_struct!(GlobalAsm; asm);
305 spanless_eq_struct!(ImplKind; unsafety polarity defaultness constness generics of_trait self_ty items);
306 spanless_eq_struct!(InlineAsm; template operands options line_spans);
307 spanless_eq_struct!(Item<K>; attrs id span vis ident kind !tokens);
308 spanless_eq_struct!(Label; ident);
309 spanless_eq_struct!(Lifetime; id ident);
310 spanless_eq_struct!(Lit; token kind span);
311 spanless_eq_struct!(LlvmInlineAsm; asm asm_str_style outputs inputs clobbers volatile alignstack dialect);
312 spanless_eq_struct!(LlvmInlineAsmOutput; constraint expr is_rw is_indirect);
313 spanless_eq_struct!(Local; pat ty init id span attrs !tokens);
314 spanless_eq_struct!(MacCall; path args prior_type_ascription);
315 spanless_eq_struct!(MacCallStmt; mac style attrs tokens);
316 spanless_eq_struct!(MacroDef; body macro_rules);
317 spanless_eq_struct!(MutTy; ty mutbl);
318 spanless_eq_struct!(ParenthesizedArgs; span inputs inputs_span output);
319 spanless_eq_struct!(Pat; id kind span tokens);
320 spanless_eq_struct!(PatField; ident pat is_shorthand attrs id span is_placeholder);
321 spanless_eq_struct!(Path; span segments tokens);
322 spanless_eq_struct!(PathSegment; ident id args);
323 spanless_eq_struct!(PolyTraitRef; bound_generic_params trait_ref span);
324 spanless_eq_struct!(QSelf; ty path_span position);
325 spanless_eq_struct!(Stmt; id kind span);
326 spanless_eq_struct!(StrLit; style symbol suffix span symbol_unescaped);
327 spanless_eq_struct!(StructExpr; path fields rest);
328 spanless_eq_struct!(Token; kind span);
329 spanless_eq_struct!(TraitKind; 0 1 2 3 4);
330 spanless_eq_struct!(TraitRef; path ref_id);
331 spanless_eq_struct!(Ty; id kind span tokens);
332 spanless_eq_struct!(TyAliasKind; 0 1 2 3);
333 spanless_eq_struct!(UseTree; prefix kind span);
334 spanless_eq_struct!(Variant; attrs id span !vis ident data disr_expr is_placeholder);
335 spanless_eq_struct!(Visibility; kind span tokens);
336 spanless_eq_struct!(WhereBoundPredicate; span bound_generic_params bounded_ty bounds);
337 spanless_eq_struct!(WhereClause; has_where_token predicates span);
338 spanless_eq_struct!(WhereEqPredicate; id span lhs_ty rhs_ty);
339 spanless_eq_struct!(WhereRegionPredicate; span lifetime bounds);
340 spanless_eq_struct!(token::Lit; kind symbol suffix);
341 spanless_eq_enum!(AngleBracketedArg; Arg(0) Constraint(0));
342 spanless_eq_enum!(AssocItemKind; Const(0 1 2) Fn(0) TyAlias(0) MacCall(0));
343 spanless_eq_enum!(AssocTyConstraintKind; Equality(ty) Bound(bounds));
344 spanless_eq_enum!(Async; Yes(span closure_id return_impl_trait_id) No);
345 spanless_eq_enum!(AttrStyle; Outer Inner);
346 spanless_eq_enum!(BinOpKind; Add Sub Mul Div Rem And Or BitXor BitAnd BitOr Shl Shr Eq Lt Le Ne Ge Gt);
347 spanless_eq_enum!(BindingMode; ByRef(0) ByValue(0));
348 spanless_eq_enum!(BlockCheckMode; Default Unsafe(0));
349 spanless_eq_enum!(BorrowKind; Ref Raw);
350 spanless_eq_enum!(CaptureBy; Value Ref);
351 spanless_eq_enum!(Const; Yes(0) No);
352 spanless_eq_enum!(CrateSugar; PubCrate JustCrate);
353 spanless_eq_enum!(Defaultness; Default(0) Final);
354 spanless_eq_enum!(Extern; None Implicit Explicit(0));
355 spanless_eq_enum!(FloatTy; F32 F64);
356 spanless_eq_enum!(FnRetTy; Default(0) Ty(0));
357 spanless_eq_enum!(ForeignItemKind; Static(0 1 2) Fn(0) TyAlias(0) MacCall(0));
358 spanless_eq_enum!(GenericArg; Lifetime(0) Type(0) Const(0));
359 spanless_eq_enum!(GenericArgs; AngleBracketed(0) Parenthesized(0));
360 spanless_eq_enum!(GenericBound; Trait(0 1) Outlives(0));
361 spanless_eq_enum!(GenericParamKind; Lifetime Type(default) Const(ty kw_span default));
362 spanless_eq_enum!(ImplPolarity; Positive Negative(0));
363 spanless_eq_enum!(Inline; Yes No);
364 spanless_eq_enum!(InlineAsmRegOrRegClass; Reg(0) RegClass(0));
365 spanless_eq_enum!(InlineAsmTemplatePiece; String(0) Placeholder(operand_idx modifier span));
366 spanless_eq_enum!(IntTy; Isize I8 I16 I32 I64 I128);
367 spanless_eq_enum!(IsAuto; Yes No);
368 spanless_eq_enum!(LitFloatType; Suffixed(0) Unsuffixed);
369 spanless_eq_enum!(LitIntType; Signed(0) Unsigned(0) Unsuffixed);
370 spanless_eq_enum!(LlvmAsmDialect; Att Intel);
371 spanless_eq_enum!(MacArgs; Empty Delimited(0 1 2) Eq(0 1));
372 spanless_eq_enum!(MacDelimiter; Parenthesis Bracket Brace);
373 spanless_eq_enum!(MacStmtStyle; Semicolon Braces NoBraces);
374 spanless_eq_enum!(ModKind; Loaded(0 1 2) Unloaded);
375 spanless_eq_enum!(Movability; Static Movable);
376 spanless_eq_enum!(Mutability; Mut Not);
377 spanless_eq_enum!(RangeEnd; Included(0) Excluded);
378 spanless_eq_enum!(RangeLimits; HalfOpen Closed);
379 spanless_eq_enum!(StmtKind; Local(0) Item(0) Expr(0) Semi(0) Empty MacCall(0));
380 spanless_eq_enum!(StrStyle; Cooked Raw(0));
381 spanless_eq_enum!(StructRest; Base(0) Rest(0) None);
382 spanless_eq_enum!(TokenTree; Token(0) Delimited(0 1 2));
383 spanless_eq_enum!(TraitBoundModifier; None Maybe MaybeConst MaybeConstMaybe);
384 spanless_eq_enum!(TraitObjectSyntax; Dyn None);
385 spanless_eq_enum!(UintTy; Usize U8 U16 U32 U64 U128);
386 spanless_eq_enum!(UnOp; Deref Not Neg);
387 spanless_eq_enum!(Unsafe; Yes(0) No);
388 spanless_eq_enum!(UnsafeSource; CompilerGenerated UserProvided);
389 spanless_eq_enum!(UseTreeKind; Simple(0 1 2) Nested(0) Glob);
390 spanless_eq_enum!(VariantData; Struct(0 1) Tuple(0 1) Unit(0));
391 spanless_eq_enum!(VisibilityKind; Public Crate(0) Restricted(path id) Inherited);
392 spanless_eq_enum!(WherePredicate; BoundPredicate(0) RegionPredicate(0) EqPredicate(0));
393 spanless_eq_enum!(ExprKind; Box(0) Array(0) ConstBlock(0) Call(0 1)
394     MethodCall(0 1 2) Tup(0) Binary(0 1 2) Unary(0 1) Lit(0) Cast(0 1) Type(0 1)
395     Let(0 1) If(0 1 2) While(0 1 2) ForLoop(0 1 2 3) Loop(0 1) Match(0 1)
396     Closure(0 1 2 3 4 5) Block(0 1) Async(0 1 2) Await(0) TryBlock(0)
397     Assign(0 1 2) AssignOp(0 1 2) Field(0 1) Index(0 1) Underscore Range(0 1 2)
398     Path(0 1) AddrOf(0 1 2) Break(0 1) Continue(0) Ret(0) InlineAsm(0)
399     LlvmInlineAsm(0) MacCall(0) Struct(0) Repeat(0 1) Paren(0) Try(0) Yield(0)
400     Err);
401 spanless_eq_enum!(InlineAsmOperand; In(reg expr) Out(reg late expr)
402     InOut(reg late expr) SplitInOut(reg late in_expr out_expr) Const(anon_const)
403     Sym(expr));
404 spanless_eq_enum!(ItemKind; ExternCrate(0) Use(0) Static(0 1 2) Const(0 1 2)
405     Fn(0) Mod(0 1) ForeignMod(0) GlobalAsm(0) TyAlias(0) Enum(0 1) Struct(0 1)
406     Union(0 1) Trait(0) TraitAlias(0 1) Impl(0) MacCall(0) MacroDef(0));
407 spanless_eq_enum!(LitKind; Str(0 1) ByteStr(0) Byte(0) Char(0) Int(0 1)
408     Float(0 1) Bool(0) Err(0));
409 spanless_eq_enum!(PatKind; Wild Ident(0 1 2) Struct(0 1 2) TupleStruct(0 1)
410     Or(0) Path(0 1) Tuple(0) Box(0) Ref(0 1) Lit(0) Range(0 1 2) Slice(0) Rest
411     Paren(0) MacCall(0));
412 spanless_eq_enum!(TyKind; Slice(0) Array(0 1) Ptr(0) Rptr(0 1) BareFn(0) Never
413     Tup(0) Path(0 1) TraitObject(0 1) ImplTrait(0 1) Paren(0) Typeof(0) Infer
414     ImplicitSelf MacCall(0) Err CVarArgs);
415 
416 impl SpanlessEq for Ident {
eq(&self, other: &Self) -> bool417     fn eq(&self, other: &Self) -> bool {
418         self.as_str() == other.as_str()
419     }
420 }
421 
422 impl SpanlessEq for RangeSyntax {
eq(&self, _other: &Self) -> bool423     fn eq(&self, _other: &Self) -> bool {
424         match self {
425             RangeSyntax::DotDotDot | RangeSyntax::DotDotEq => true,
426         }
427     }
428 }
429 
430 impl SpanlessEq for Param {
eq(&self, other: &Self) -> bool431     fn eq(&self, other: &Self) -> bool {
432         let Param {
433             attrs,
434             ty,
435             pat,
436             id,
437             span: _,
438             is_placeholder,
439         } = self;
440         let Param {
441             attrs: attrs2,
442             ty: ty2,
443             pat: pat2,
444             id: id2,
445             span: _,
446             is_placeholder: is_placeholder2,
447         } = other;
448         SpanlessEq::eq(id, id2)
449             && SpanlessEq::eq(is_placeholder, is_placeholder2)
450             && (matches!(ty.kind, TyKind::Err)
451                 || matches!(ty2.kind, TyKind::Err)
452                 || SpanlessEq::eq(attrs, attrs2)
453                     && SpanlessEq::eq(ty, ty2)
454                     && SpanlessEq::eq(pat, pat2))
455     }
456 }
457 
458 impl SpanlessEq for TokenKind {
eq(&self, other: &Self) -> bool459     fn eq(&self, other: &Self) -> bool {
460         match (self, other) {
461             (TokenKind::Literal(this), TokenKind::Literal(other)) => SpanlessEq::eq(this, other),
462             (TokenKind::DotDotEq, _) | (TokenKind::DotDotDot, _) => match other {
463                 TokenKind::DotDotEq | TokenKind::DotDotDot => true,
464                 _ => false,
465             },
466             (TokenKind::Interpolated(this), TokenKind::Interpolated(other)) => {
467                 match (this.as_ref(), other.as_ref()) {
468                     (Nonterminal::NtExpr(this), Nonterminal::NtExpr(other)) => {
469                         SpanlessEq::eq(this, other)
470                     }
471                     _ => this == other,
472                 }
473             }
474             _ => self == other,
475         }
476     }
477 }
478 
479 impl SpanlessEq for TokenStream {
eq(&self, other: &Self) -> bool480     fn eq(&self, other: &Self) -> bool {
481         let mut this_trees = self.trees();
482         let mut other_trees = other.trees();
483         loop {
484             let this = match this_trees.next() {
485                 None => return other_trees.next().is_none(),
486                 Some(tree) => tree,
487             };
488             let other = match other_trees.next() {
489                 None => return false,
490                 Some(tree) => tree,
491             };
492             if SpanlessEq::eq(&this, &other) {
493                 continue;
494             }
495             if let (TokenTree::Token(this), TokenTree::Token(other)) = (this, other) {
496                 if match (&this.kind, &other.kind) {
497                     (TokenKind::Literal(this), TokenKind::Literal(other)) => {
498                         SpanlessEq::eq(this, other)
499                     }
500                     (TokenKind::DocComment(_kind, style, symbol), TokenKind::Pound) => {
501                         doc_comment(*style, *symbol, &mut other_trees)
502                     }
503                     (TokenKind::Pound, TokenKind::DocComment(_kind, style, symbol)) => {
504                         doc_comment(*style, *symbol, &mut this_trees)
505                     }
506                     _ => false,
507                 } {
508                     continue;
509                 }
510             }
511             return false;
512         }
513     }
514 }
515 
doc_comment<'a>( style: AttrStyle, unescaped: Symbol, trees: &mut impl Iterator<Item = TokenTree>, ) -> bool516 fn doc_comment<'a>(
517     style: AttrStyle,
518     unescaped: Symbol,
519     trees: &mut impl Iterator<Item = TokenTree>,
520 ) -> bool {
521     if match style {
522         AttrStyle::Outer => false,
523         AttrStyle::Inner => true,
524     } {
525         match trees.next() {
526             Some(TokenTree::Token(Token {
527                 kind: TokenKind::Not,
528                 span: _,
529             })) => {}
530             _ => return false,
531         }
532     }
533     let stream = match trees.next() {
534         Some(TokenTree::Delimited(_span, DelimToken::Bracket, stream)) => stream,
535         _ => return false,
536     };
537     let mut trees = stream.trees();
538     match trees.next() {
539         Some(TokenTree::Token(Token {
540             kind: TokenKind::Ident(symbol, false),
541             span: _,
542         })) if symbol == sym::doc => {}
543         _ => return false,
544     }
545     match trees.next() {
546         Some(TokenTree::Token(Token {
547             kind: TokenKind::Eq,
548             span: _,
549         })) => {}
550         _ => return false,
551     }
552     match trees.next() {
553         Some(TokenTree::Token(token)) => {
554             is_escaped_literal(&token, unescaped) && trees.next().is_none()
555         }
556         _ => false,
557     }
558 }
559 
is_escaped_literal(token: &Token, unescaped: Symbol) -> bool560 fn is_escaped_literal(token: &Token, unescaped: Symbol) -> bool {
561     match match token {
562         Token {
563             kind: TokenKind::Literal(lit),
564             span: _,
565         } => Lit::from_lit_token(*lit, DUMMY_SP),
566         Token {
567             kind: TokenKind::Interpolated(nonterminal),
568             span: _,
569         } => match nonterminal.as_ref() {
570             Nonterminal::NtExpr(expr) => match &expr.kind {
571                 ExprKind::Lit(lit) => Ok(lit.clone()),
572                 _ => return false,
573             },
574             _ => return false,
575         },
576         _ => return false,
577     } {
578         Ok(Lit {
579             token:
580                 token::Lit {
581                     kind: token::LitKind::Str,
582                     symbol: _,
583                     suffix: None,
584                 },
585             kind: LitKind::Str(symbol, StrStyle::Cooked),
586             span: _,
587         }) => symbol.as_str().replace('\r', "") == unescaped.as_str().replace('\r', ""),
588         _ => false,
589     }
590 }
591 
592 impl SpanlessEq for LazyTokenStream {
eq(&self, other: &Self) -> bool593     fn eq(&self, other: &Self) -> bool {
594         let this = self.create_token_stream();
595         let other = other.create_token_stream();
596         SpanlessEq::eq(&this, &other)
597     }
598 }
599 
600 impl SpanlessEq for AttrKind {
eq(&self, other: &Self) -> bool601     fn eq(&self, other: &Self) -> bool {
602         match (self, other) {
603             (AttrKind::Normal(item, tokens), AttrKind::Normal(item2, tokens2)) => {
604                 SpanlessEq::eq(item, item2) && SpanlessEq::eq(tokens, tokens2)
605             }
606             (AttrKind::DocComment(kind, symbol), AttrKind::DocComment(kind2, symbol2)) => {
607                 SpanlessEq::eq(kind, kind2) && SpanlessEq::eq(symbol, symbol2)
608             }
609             (AttrKind::DocComment(kind, unescaped), AttrKind::Normal(item2, _tokens)) => {
610                 match kind {
611                     CommentKind::Line | CommentKind::Block => {}
612                 }
613                 let path = Path::from_ident(Ident::with_dummy_span(sym::doc));
614                 SpanlessEq::eq(&path, &item2.path)
615                     && match &item2.args {
616                         MacArgs::Empty | MacArgs::Delimited(..) => false,
617                         MacArgs::Eq(_span, token) => is_escaped_literal(token, *unescaped),
618                     }
619             }
620             (AttrKind::Normal(..), AttrKind::DocComment(..)) => SpanlessEq::eq(other, self),
621         }
622     }
623 }
624