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