eq.rs 26 KB

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