1 extern crate rustc_ast; 2 extern crate rustc_data_structures; 3 extern crate rustc_span; 4 extern crate rustc_target; 5 6 use rustc_ast::ast::{ 7 AngleBracketedArg, AngleBracketedArgs, AnonConst, Arm, AssocItemKind, AssocTyConstraint, 8 AssocTyConstraintKind, Async, AttrId, AttrItem, AttrKind, AttrStyle, Attribute, BareFnTy, 9 BinOpKind, BindingMode, Block, BlockCheckMode, BorrowKind, CaptureBy, Const, Crate, CrateSugar, 10 Defaultness, EnumDef, Expr, ExprKind, Extern, Field, FieldPat, FloatTy, FnDecl, FnHeader, 11 FnRetTy, FnSig, ForeignItemKind, ForeignMod, GenericArg, GenericArgs, GenericBound, 12 GenericParam, GenericParamKind, Generics, GlobalAsm, ImplPolarity, InlineAsm, InlineAsmOperand, 13 InlineAsmOptions, InlineAsmRegOrRegClass, InlineAsmTemplatePiece, IntTy, IsAuto, Item, 14 ItemKind, Label, Lifetime, Lit, LitFloatType, LitIntType, LitKind, LlvmAsmDialect, 15 LlvmInlineAsm, LlvmInlineAsmOutput, Local, MacArgs, MacCall, MacCallStmt, MacDelimiter, 16 MacStmtStyle, MacroDef, Mod, Movability, MutTy, Mutability, NodeId, Param, ParenthesizedArgs, 17 Pat, PatKind, Path, PathSegment, PolyTraitRef, QSelf, RangeEnd, RangeLimits, RangeSyntax, Stmt, 18 StmtKind, StrLit, StrStyle, StructField, TraitBoundModifier, TraitObjectSyntax, TraitRef, Ty, 19 TyKind, UintTy, UnOp, Unsafe, UnsafeSource, UseTree, UseTreeKind, Variant, VariantData, 20 Visibility, VisibilityKind, WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate, 21 WhereRegionPredicate, 22 }; 23 use rustc_ast::ptr::P; 24 use rustc_ast::token::{self, CommentKind, DelimToken, 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::Ident; 30 use rustc_span::{Span, Symbol, SyntaxContext}; 31 use std::mem; 32 33 pub trait SpanlessEq { eq(&self, other: &Self) -> bool34 fn eq(&self, other: &Self) -> bool; 35 } 36 37 impl<T: SpanlessEq> SpanlessEq for P<T> { eq(&self, other: &Self) -> bool38 fn eq(&self, other: &Self) -> bool { 39 SpanlessEq::eq(&**self, &**other) 40 } 41 } 42 43 impl<T: ?Sized + SpanlessEq> SpanlessEq for Lrc<T> { eq(&self, other: &Self) -> bool44 fn eq(&self, other: &Self) -> bool { 45 SpanlessEq::eq(&**self, &**other) 46 } 47 } 48 49 impl<T: SpanlessEq> SpanlessEq for Option<T> { eq(&self, other: &Self) -> bool50 fn eq(&self, other: &Self) -> bool { 51 match (self, other) { 52 (None, None) => true, 53 (Some(this), Some(other)) => SpanlessEq::eq(this, other), 54 _ => false, 55 } 56 } 57 } 58 59 impl<T: SpanlessEq> SpanlessEq for [T] { eq(&self, other: &Self) -> bool60 fn eq(&self, other: &Self) -> bool { 61 self.len() == other.len() && self.iter().zip(other).all(|(a, b)| SpanlessEq::eq(a, b)) 62 } 63 } 64 65 impl<T: SpanlessEq> SpanlessEq for Vec<T> { eq(&self, other: &Self) -> bool66 fn eq(&self, other: &Self) -> bool { 67 <[T] as SpanlessEq>::eq(self, other) 68 } 69 } 70 71 impl<T: SpanlessEq> SpanlessEq for ThinVec<T> { eq(&self, other: &Self) -> bool72 fn eq(&self, other: &Self) -> bool { 73 self.len() == other.len() 74 && self 75 .iter() 76 .zip(other.iter()) 77 .all(|(a, b)| SpanlessEq::eq(a, b)) 78 } 79 } 80 81 impl<T: SpanlessEq> SpanlessEq for Spanned<T> { eq(&self, other: &Self) -> bool82 fn eq(&self, other: &Self) -> bool { 83 SpanlessEq::eq(&self.node, &other.node) 84 } 85 } 86 87 impl<A: SpanlessEq, B: SpanlessEq> SpanlessEq for (A, B) { eq(&self, other: &Self) -> bool88 fn eq(&self, other: &Self) -> bool { 89 SpanlessEq::eq(&self.0, &other.0) && SpanlessEq::eq(&self.1, &other.1) 90 } 91 } 92 93 macro_rules! spanless_eq_true { 94 ($name:ident) => { 95 impl SpanlessEq for $name { 96 fn eq(&self, _other: &Self) -> bool { 97 true 98 } 99 } 100 }; 101 } 102 103 spanless_eq_true!(Span); 104 spanless_eq_true!(DelimSpan); 105 spanless_eq_true!(AttrId); 106 spanless_eq_true!(NodeId); 107 spanless_eq_true!(SyntaxContext); 108 109 macro_rules! spanless_eq_partial_eq { 110 ($name:ident) => { 111 impl SpanlessEq for $name { 112 fn eq(&self, other: &Self) -> bool { 113 PartialEq::eq(self, other) 114 } 115 } 116 }; 117 } 118 119 spanless_eq_partial_eq!(bool); 120 spanless_eq_partial_eq!(u8); 121 spanless_eq_partial_eq!(u16); 122 spanless_eq_partial_eq!(u128); 123 spanless_eq_partial_eq!(usize); 124 spanless_eq_partial_eq!(char); 125 spanless_eq_partial_eq!(String); 126 spanless_eq_partial_eq!(Symbol); 127 spanless_eq_partial_eq!(CommentKind); 128 spanless_eq_partial_eq!(DelimToken); 129 spanless_eq_partial_eq!(InlineAsmOptions); 130 131 macro_rules! spanless_eq_struct { 132 { 133 $name:ident $(<$param:ident>)?; 134 $([$field:ident $other:ident])* 135 $(![$ignore:ident])* 136 } => { 137 impl $(<$param: SpanlessEq>)* SpanlessEq for $name $(<$param>)* { 138 fn eq(&self, other: &Self) -> bool { 139 let $name { $($field,)* $($ignore: _,)* } = self; 140 let $name { $($field: $other,)* $($ignore: _,)* } = other; 141 $(SpanlessEq::eq($field, $other))&&* 142 } 143 } 144 }; 145 146 { 147 $name:ident $(<$param:ident>)?; 148 $([$field:ident $other:ident])* 149 $next:ident 150 $($rest:ident)* 151 $(!$ignore:ident)* 152 } => { 153 spanless_eq_struct! { 154 $name $(<$param>)*; 155 $([$field $other])* 156 [$next other] 157 $($rest)* 158 $(!$ignore)* 159 } 160 }; 161 162 { 163 $name:ident $(<$param:ident>)?; 164 $([$field:ident $other:ident])* 165 $(![$ignore:ident])* 166 !$next:ident 167 $(!$rest:ident)* 168 } => { 169 spanless_eq_struct! { 170 $name $(<$param>)*; 171 $([$field $other])* 172 $(![$ignore])* 173 ![$next] 174 $(!$rest)* 175 } 176 }; 177 } 178 179 macro_rules! spanless_eq_enum { 180 { 181 $name:ident; 182 $([$variant:ident $([$field:tt $this:ident $other:ident])*])* 183 } => { 184 impl SpanlessEq for $name { 185 fn eq(&self, other: &Self) -> bool { 186 match self { 187 $( 188 $name::$variant { .. } => {} 189 )* 190 } 191 #[allow(unreachable_patterns)] 192 match (self, other) { 193 $( 194 ( 195 $name::$variant { $($field: $this),* }, 196 $name::$variant { $($field: $other),* }, 197 ) => { 198 true $(&& SpanlessEq::eq($this, $other))* 199 } 200 )* 201 _ => false, 202 } 203 } 204 } 205 }; 206 207 { 208 $name:ident; 209 $([$variant:ident $($fields:tt)*])* 210 $next:ident [$($named:tt)*] ( $i:tt $($field:tt)* ) 211 $($rest:tt)* 212 } => { 213 spanless_eq_enum! { 214 $name; 215 $([$variant $($fields)*])* 216 $next [$($named)* [$i this other]] ( $($field)* ) 217 $($rest)* 218 } 219 }; 220 221 { 222 $name:ident; 223 $([$variant:ident $($fields:tt)*])* 224 $next:ident [$($named:tt)*] () 225 $($rest:tt)* 226 } => { 227 spanless_eq_enum! { 228 $name; 229 $([$variant $($fields)*])* 230 [$next $($named)*] 231 $($rest)* 232 } 233 }; 234 235 { 236 $name:ident; 237 $([$variant:ident $($fields:tt)*])* 238 $next:ident ( $($field:tt)* ) 239 $($rest:tt)* 240 } => { 241 spanless_eq_enum! { 242 $name; 243 $([$variant $($fields)*])* 244 $next [] ( $($field)* ) 245 $($rest)* 246 } 247 }; 248 249 { 250 $name:ident; 251 $([$variant:ident $($fields:tt)*])* 252 $next:ident 253 $($rest:tt)* 254 } => { 255 spanless_eq_enum! { 256 $name; 257 $([$variant $($fields)*])* 258 [$next] 259 $($rest)* 260 } 261 }; 262 } 263 264 spanless_eq_struct!(AngleBracketedArgs; span args); 265 spanless_eq_struct!(AnonConst; id value); 266 spanless_eq_struct!(Arm; attrs pat guard body span id is_placeholder); 267 spanless_eq_struct!(AssocTyConstraint; id ident kind span); 268 spanless_eq_struct!(AttrItem; path args tokens); 269 spanless_eq_struct!(Attribute; kind id style span); 270 spanless_eq_struct!(BareFnTy; unsafety ext generic_params decl); 271 spanless_eq_struct!(Block; stmts id rules span tokens); 272 spanless_eq_struct!(Crate; module attrs span proc_macros); 273 spanless_eq_struct!(EnumDef; variants); 274 spanless_eq_struct!(Expr; id kind span attrs !tokens); 275 spanless_eq_struct!(Field; attrs id span ident expr is_shorthand is_placeholder); 276 spanless_eq_struct!(FieldPat; ident pat is_shorthand attrs id span is_placeholder); 277 spanless_eq_struct!(FnDecl; inputs output); 278 spanless_eq_struct!(FnHeader; constness asyncness unsafety ext); 279 spanless_eq_struct!(FnSig; header decl span); 280 spanless_eq_struct!(ForeignMod; unsafety abi items); 281 spanless_eq_struct!(GenericParam; id ident attrs bounds is_placeholder kind); 282 spanless_eq_struct!(Generics; params where_clause span); 283 spanless_eq_struct!(GlobalAsm; asm); 284 spanless_eq_struct!(InlineAsm; template operands options line_spans); 285 spanless_eq_struct!(Item<K>; attrs id span vis ident kind !tokens); 286 spanless_eq_struct!(Label; ident); 287 spanless_eq_struct!(Lifetime; id ident); 288 spanless_eq_struct!(Lit; token kind span); 289 spanless_eq_struct!(LlvmInlineAsm; asm asm_str_style outputs inputs clobbers volatile alignstack dialect); 290 spanless_eq_struct!(LlvmInlineAsmOutput; constraint expr is_rw is_indirect); 291 spanless_eq_struct!(Local; pat ty init id span attrs); 292 spanless_eq_struct!(MacCall; path args prior_type_ascription); 293 spanless_eq_struct!(MacCallStmt; mac style attrs); 294 spanless_eq_struct!(MacroDef; body macro_rules); 295 spanless_eq_struct!(Mod; inner unsafety items inline); 296 spanless_eq_struct!(MutTy; ty mutbl); 297 spanless_eq_struct!(Param; attrs ty pat id span is_placeholder); 298 spanless_eq_struct!(ParenthesizedArgs; span inputs output); 299 spanless_eq_struct!(Pat; id kind span tokens); 300 spanless_eq_struct!(Path; span segments tokens); 301 spanless_eq_struct!(PathSegment; ident id args); 302 spanless_eq_struct!(PolyTraitRef; bound_generic_params trait_ref span); 303 spanless_eq_struct!(QSelf; ty path_span position); 304 spanless_eq_struct!(Stmt; id kind span tokens); 305 spanless_eq_struct!(StrLit; style symbol suffix span symbol_unescaped); 306 spanless_eq_struct!(StructField; attrs id span vis ident ty is_placeholder); 307 spanless_eq_struct!(Token; kind span); 308 spanless_eq_struct!(TraitRef; path ref_id); 309 spanless_eq_struct!(Ty; id kind span tokens); 310 spanless_eq_struct!(UseTree; prefix kind span); 311 spanless_eq_struct!(Variant; attrs id span vis ident data disr_expr is_placeholder); 312 spanless_eq_struct!(Visibility; kind span tokens); 313 spanless_eq_struct!(WhereBoundPredicate; span bound_generic_params bounded_ty bounds); 314 spanless_eq_struct!(WhereClause; has_where_token predicates span); 315 spanless_eq_struct!(WhereEqPredicate; id span lhs_ty rhs_ty); 316 spanless_eq_struct!(WhereRegionPredicate; span lifetime bounds); 317 spanless_eq_enum!(AngleBracketedArg; Arg(0) Constraint(0)); 318 spanless_eq_enum!(AssocItemKind; Const(0 1 2) Fn(0 1 2 3) TyAlias(0 1 2 3) MacCall(0)); 319 spanless_eq_enum!(AssocTyConstraintKind; Equality(ty) Bound(bounds)); 320 spanless_eq_enum!(Async; Yes(span closure_id return_impl_trait_id) No); 321 spanless_eq_enum!(AttrKind; Normal(0) DocComment(0 1)); 322 spanless_eq_enum!(AttrStyle; Outer Inner); 323 spanless_eq_enum!(BinOpKind; Add Sub Mul Div Rem And Or BitXor BitAnd BitOr Shl Shr Eq Lt Le Ne Ge Gt); 324 spanless_eq_enum!(BindingMode; ByRef(0) ByValue(0)); 325 spanless_eq_enum!(BlockCheckMode; Default Unsafe(0)); 326 spanless_eq_enum!(BorrowKind; Ref Raw); 327 spanless_eq_enum!(CaptureBy; Value Ref); 328 spanless_eq_enum!(Const; Yes(0) No); 329 spanless_eq_enum!(CrateSugar; PubCrate JustCrate); 330 spanless_eq_enum!(Defaultness; Default(0) Final); 331 spanless_eq_enum!(Extern; None Implicit Explicit(0)); 332 spanless_eq_enum!(FloatTy; F32 F64); 333 spanless_eq_enum!(FnRetTy; Default(0) Ty(0)); 334 spanless_eq_enum!(ForeignItemKind; Static(0 1 2) Fn(0 1 2 3) TyAlias(0 1 2 3) MacCall(0)); 335 spanless_eq_enum!(GenericArg; Lifetime(0) Type(0) Const(0)); 336 spanless_eq_enum!(GenericArgs; AngleBracketed(0) Parenthesized(0)); 337 spanless_eq_enum!(GenericBound; Trait(0 1) Outlives(0)); 338 spanless_eq_enum!(GenericParamKind; Lifetime Type(default) Const(ty kw_span)); 339 spanless_eq_enum!(ImplPolarity; Positive Negative(0)); 340 spanless_eq_enum!(InlineAsmRegOrRegClass; Reg(0) RegClass(0)); 341 spanless_eq_enum!(InlineAsmTemplatePiece; String(0) Placeholder(operand_idx modifier span)); 342 spanless_eq_enum!(IntTy; Isize I8 I16 I32 I64 I128); 343 spanless_eq_enum!(IsAuto; Yes No); 344 spanless_eq_enum!(LitFloatType; Suffixed(0) Unsuffixed); 345 spanless_eq_enum!(LitIntType; Signed(0) Unsigned(0) Unsuffixed); 346 spanless_eq_enum!(LlvmAsmDialect; Att Intel); 347 spanless_eq_enum!(MacArgs; Empty Delimited(0 1 2) Eq(0 1)); 348 spanless_eq_enum!(MacDelimiter; Parenthesis Bracket Brace); 349 spanless_eq_enum!(MacStmtStyle; Semicolon Braces NoBraces); 350 spanless_eq_enum!(Movability; Static Movable); 351 spanless_eq_enum!(Mutability; Mut Not); 352 spanless_eq_enum!(RangeEnd; Included(0) Excluded); 353 spanless_eq_enum!(RangeLimits; HalfOpen Closed); 354 spanless_eq_enum!(StmtKind; Local(0) Item(0) Expr(0) Semi(0) Empty MacCall(0)); 355 spanless_eq_enum!(StrStyle; Cooked Raw(0)); 356 spanless_eq_enum!(TokenTree; Token(0) Delimited(0 1 2)); 357 spanless_eq_enum!(TraitBoundModifier; None Maybe MaybeConst MaybeConstMaybe); 358 spanless_eq_enum!(TraitObjectSyntax; Dyn None); 359 spanless_eq_enum!(UintTy; Usize U8 U16 U32 U64 U128); 360 spanless_eq_enum!(UnOp; Deref Not Neg); 361 spanless_eq_enum!(Unsafe; Yes(0) No); 362 spanless_eq_enum!(UnsafeSource; CompilerGenerated UserProvided); 363 spanless_eq_enum!(UseTreeKind; Simple(0 1 2) Nested(0) Glob); 364 spanless_eq_enum!(VariantData; Struct(0 1) Tuple(0 1) Unit(0)); 365 spanless_eq_enum!(VisibilityKind; Public Crate(0) Restricted(path id) Inherited); 366 spanless_eq_enum!(WherePredicate; BoundPredicate(0) RegionPredicate(0) EqPredicate(0)); 367 spanless_eq_enum!(ExprKind; Box(0) Array(0) ConstBlock(0) Call(0 1) 368 MethodCall(0 1 2) Tup(0) Binary(0 1 2) Unary(0 1) Lit(0) Cast(0 1) Type(0 1) 369 Let(0 1) If(0 1 2) While(0 1 2) ForLoop(0 1 2 3) Loop(0 1) Match(0 1) 370 Closure(0 1 2 3 4 5) Block(0 1) Async(0 1 2) Await(0) TryBlock(0) 371 Assign(0 1 2) AssignOp(0 1 2) Field(0 1) Index(0 1) Range(0 1 2) Path(0 1) 372 AddrOf(0 1 2) Break(0 1) Continue(0) Ret(0) InlineAsm(0) LlvmInlineAsm(0) 373 MacCall(0) Struct(0 1 2) Repeat(0 1) Paren(0) Try(0) Yield(0) Err); 374 spanless_eq_enum!(InlineAsmOperand; In(reg expr) Out(reg late expr) 375 InOut(reg late expr) SplitInOut(reg late in_expr out_expr) Const(expr) 376 Sym(expr)); 377 spanless_eq_enum!(ItemKind; ExternCrate(0) Use(0) Static(0 1 2) Const(0 1 2) 378 Fn(0 1 2 3) Mod(0) ForeignMod(0) GlobalAsm(0) TyAlias(0 1 2 3) Enum(0 1) 379 Struct(0 1) Union(0 1) Trait(0 1 2 3 4) TraitAlias(0 1) 380 Impl(unsafety polarity defaultness constness generics of_trait self_ty items) 381 MacCall(0) MacroDef(0)); 382 spanless_eq_enum!(LitKind; Str(0 1) ByteStr(0) Byte(0) Char(0) Int(0 1) 383 Float(0 1) Bool(0) Err(0)); 384 spanless_eq_enum!(PatKind; Wild Ident(0 1 2) Struct(0 1 2) TupleStruct(0 1) 385 Or(0) Path(0 1) Tuple(0) Box(0) Ref(0 1) Lit(0) Range(0 1 2) Slice(0) Rest 386 Paren(0) MacCall(0)); 387 spanless_eq_enum!(TyKind; Slice(0) Array(0 1) Ptr(0) Rptr(0 1) BareFn(0) Never 388 Tup(0) Path(0 1) TraitObject(0 1) ImplTrait(0 1) Paren(0) Typeof(0) Infer 389 ImplicitSelf MacCall(0) Err CVarArgs); 390 391 impl SpanlessEq for Ident { eq(&self, other: &Self) -> bool392 fn eq(&self, other: &Self) -> bool { 393 self.as_str() == other.as_str() 394 } 395 } 396 397 // Give up on comparing literals inside of macros because there are so many 398 // equivalent representations of the same literal; they are tested elsewhere 399 impl SpanlessEq for token::Lit { eq(&self, other: &Self) -> bool400 fn eq(&self, other: &Self) -> bool { 401 mem::discriminant(self) == mem::discriminant(other) 402 } 403 } 404 405 impl SpanlessEq for RangeSyntax { eq(&self, _other: &Self) -> bool406 fn eq(&self, _other: &Self) -> bool { 407 match self { 408 RangeSyntax::DotDotDot | RangeSyntax::DotDotEq => true, 409 } 410 } 411 } 412 413 impl SpanlessEq for TokenKind { eq(&self, other: &Self) -> bool414 fn eq(&self, other: &Self) -> bool { 415 match (self, other) { 416 (TokenKind::Literal(this), TokenKind::Literal(other)) => SpanlessEq::eq(this, other), 417 (TokenKind::DotDotEq, _) | (TokenKind::DotDotDot, _) => match other { 418 TokenKind::DotDotEq | TokenKind::DotDotDot => true, 419 _ => false, 420 }, 421 _ => self == other, 422 } 423 } 424 } 425 426 impl SpanlessEq for TokenStream { eq(&self, other: &Self) -> bool427 fn eq(&self, other: &Self) -> bool { 428 let mut this = self.clone().into_trees(); 429 let mut other = other.clone().into_trees(); 430 loop { 431 let this = match this.next() { 432 None => return other.next().is_none(), 433 Some(val) => val, 434 }; 435 let other = match other.next() { 436 None => return false, 437 Some(val) => val, 438 }; 439 if !SpanlessEq::eq(&this, &other) { 440 return false; 441 } 442 } 443 } 444 } 445 446 impl SpanlessEq for LazyTokenStream { eq(&self, other: &Self) -> bool447 fn eq(&self, other: &Self) -> bool { 448 let this = self.into_token_stream(); 449 let other = other.into_token_stream(); 450 SpanlessEq::eq(&this, &other) 451 } 452 } 453