| 1 | use super::*; |
| 2 | use crate::derive::{Data, DataEnum, DataStruct, DataUnion, DeriveInput}; |
| 3 | use crate::punctuated::Punctuated; |
| 4 | use proc_macro2::TokenStream; |
| 5 | |
| 6 | #[cfg (feature = "parsing" )] |
| 7 | use std::mem; |
| 8 | |
| 9 | ast_enum_of_structs! { |
| 10 | /// Things that can appear directly inside of a module or scope. |
| 11 | /// |
| 12 | /// # Syntax tree enum |
| 13 | /// |
| 14 | /// This type is a [syntax tree enum]. |
| 15 | /// |
| 16 | /// [syntax tree enum]: Expr#syntax-tree-enums |
| 17 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 18 | #[non_exhaustive ] |
| 19 | pub enum Item { |
| 20 | /// A constant item: `const MAX: u16 = 65535`. |
| 21 | Const(ItemConst), |
| 22 | |
| 23 | /// An enum definition: `enum Foo<A, B> { A(A), B(B) }`. |
| 24 | Enum(ItemEnum), |
| 25 | |
| 26 | /// An `extern crate` item: `extern crate serde`. |
| 27 | ExternCrate(ItemExternCrate), |
| 28 | |
| 29 | /// A free-standing function: `fn process(n: usize) -> Result<()> { ... |
| 30 | /// }`. |
| 31 | Fn(ItemFn), |
| 32 | |
| 33 | /// A block of foreign items: `extern "C" { ... }`. |
| 34 | ForeignMod(ItemForeignMod), |
| 35 | |
| 36 | /// An impl block providing trait or associated items: `impl<A> Trait |
| 37 | /// for Data<A> { ... }`. |
| 38 | Impl(ItemImpl), |
| 39 | |
| 40 | /// A macro invocation, which includes `macro_rules!` definitions. |
| 41 | Macro(ItemMacro), |
| 42 | |
| 43 | /// A module or module declaration: `mod m` or `mod m { ... }`. |
| 44 | Mod(ItemMod), |
| 45 | |
| 46 | /// A static item: `static BIKE: Shed = Shed(42)`. |
| 47 | Static(ItemStatic), |
| 48 | |
| 49 | /// A struct definition: `struct Foo<A> { x: A }`. |
| 50 | Struct(ItemStruct), |
| 51 | |
| 52 | /// A trait definition: `pub trait Iterator { ... }`. |
| 53 | Trait(ItemTrait), |
| 54 | |
| 55 | /// A trait alias: `pub trait SharableIterator = Iterator + Sync`. |
| 56 | TraitAlias(ItemTraitAlias), |
| 57 | |
| 58 | /// A type alias: `type Result<T> = std::result::Result<T, MyError>`. |
| 59 | Type(ItemType), |
| 60 | |
| 61 | /// A union definition: `union Foo<A, B> { x: A, y: B }`. |
| 62 | Union(ItemUnion), |
| 63 | |
| 64 | /// A use declaration: `use std::collections::HashMap`. |
| 65 | Use(ItemUse), |
| 66 | |
| 67 | /// Tokens forming an item not interpreted by Syn. |
| 68 | Verbatim(TokenStream), |
| 69 | |
| 70 | // For testing exhaustiveness in downstream code, use the following idiom: |
| 71 | // |
| 72 | // match item { |
| 73 | // Item::Const(item) => {...} |
| 74 | // Item::Enum(item) => {...} |
| 75 | // ... |
| 76 | // Item::Verbatim(item) => {...} |
| 77 | // |
| 78 | // #[cfg_attr(test, deny(non_exhaustive_omitted_patterns))] |
| 79 | // _ => { /* some sane fallback */ } |
| 80 | // } |
| 81 | // |
| 82 | // This way we fail your tests but don't break your library when adding |
| 83 | // a variant. You will be notified by a test failure when a variant is |
| 84 | // added, so that you can add code to handle it, but your library will |
| 85 | // continue to compile and work for downstream users in the interim. |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | ast_struct! { |
| 90 | /// A constant item: `const MAX: u16 = 65535`. |
| 91 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 92 | pub struct ItemConst { |
| 93 | pub attrs: Vec<Attribute>, |
| 94 | pub vis: Visibility, |
| 95 | pub const_token: Token![const], |
| 96 | pub ident: Ident, |
| 97 | pub generics: Generics, |
| 98 | pub colon_token: Token![:], |
| 99 | pub ty: Box<Type>, |
| 100 | pub eq_token: Token![=], |
| 101 | pub expr: Box<Expr>, |
| 102 | pub semi_token: Token![;], |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | ast_struct! { |
| 107 | /// An enum definition: `enum Foo<A, B> { A(A), B(B) }`. |
| 108 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 109 | pub struct ItemEnum { |
| 110 | pub attrs: Vec<Attribute>, |
| 111 | pub vis: Visibility, |
| 112 | pub enum_token: Token![enum], |
| 113 | pub ident: Ident, |
| 114 | pub generics: Generics, |
| 115 | pub brace_token: token::Brace, |
| 116 | pub variants: Punctuated<Variant, Token![,]>, |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | ast_struct! { |
| 121 | /// An `extern crate` item: `extern crate serde`. |
| 122 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 123 | pub struct ItemExternCrate { |
| 124 | pub attrs: Vec<Attribute>, |
| 125 | pub vis: Visibility, |
| 126 | pub extern_token: Token![extern], |
| 127 | pub crate_token: Token![crate], |
| 128 | pub ident: Ident, |
| 129 | pub rename: Option<(Token![as], Ident)>, |
| 130 | pub semi_token: Token![;], |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | ast_struct! { |
| 135 | /// A free-standing function: `fn process(n: usize) -> Result<()> { ... }`. |
| 136 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 137 | pub struct ItemFn { |
| 138 | pub attrs: Vec<Attribute>, |
| 139 | pub vis: Visibility, |
| 140 | pub sig: Signature, |
| 141 | pub block: Box<Block>, |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | ast_struct! { |
| 146 | /// A block of foreign items: `extern "C" { ... }`. |
| 147 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 148 | pub struct ItemForeignMod { |
| 149 | pub attrs: Vec<Attribute>, |
| 150 | pub unsafety: Option<Token![unsafe]>, |
| 151 | pub abi: Abi, |
| 152 | pub brace_token: token::Brace, |
| 153 | pub items: Vec<ForeignItem>, |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | ast_struct! { |
| 158 | /// An impl block providing trait or associated items: `impl<A> Trait |
| 159 | /// for Data<A> { ... }`. |
| 160 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 161 | pub struct ItemImpl { |
| 162 | pub attrs: Vec<Attribute>, |
| 163 | pub defaultness: Option<Token![default]>, |
| 164 | pub unsafety: Option<Token![unsafe]>, |
| 165 | pub impl_token: Token![impl], |
| 166 | pub generics: Generics, |
| 167 | /// Trait this impl implements. |
| 168 | pub trait_: Option<(Option<Token![!]>, Path, Token![for])>, |
| 169 | /// The Self type of the impl. |
| 170 | pub self_ty: Box<Type>, |
| 171 | pub brace_token: token::Brace, |
| 172 | pub items: Vec<ImplItem>, |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | ast_struct! { |
| 177 | /// A macro invocation, which includes `macro_rules!` definitions. |
| 178 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 179 | pub struct ItemMacro { |
| 180 | pub attrs: Vec<Attribute>, |
| 181 | /// The `example` in `macro_rules! example { ... }`. |
| 182 | pub ident: Option<Ident>, |
| 183 | pub mac: Macro, |
| 184 | pub semi_token: Option<Token![;]>, |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | ast_struct! { |
| 189 | /// A module or module declaration: `mod m` or `mod m { ... }`. |
| 190 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 191 | pub struct ItemMod { |
| 192 | pub attrs: Vec<Attribute>, |
| 193 | pub vis: Visibility, |
| 194 | pub unsafety: Option<Token![unsafe]>, |
| 195 | pub mod_token: Token![mod], |
| 196 | pub ident: Ident, |
| 197 | pub content: Option<(token::Brace, Vec<Item>)>, |
| 198 | pub semi: Option<Token![;]>, |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | ast_struct! { |
| 203 | /// A static item: `static BIKE: Shed = Shed(42)`. |
| 204 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 205 | pub struct ItemStatic { |
| 206 | pub attrs: Vec<Attribute>, |
| 207 | pub vis: Visibility, |
| 208 | pub static_token: Token![static], |
| 209 | pub mutability: StaticMutability, |
| 210 | pub ident: Ident, |
| 211 | pub colon_token: Token![:], |
| 212 | pub ty: Box<Type>, |
| 213 | pub eq_token: Token![=], |
| 214 | pub expr: Box<Expr>, |
| 215 | pub semi_token: Token![;], |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | ast_struct! { |
| 220 | /// A struct definition: `struct Foo<A> { x: A }`. |
| 221 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 222 | pub struct ItemStruct { |
| 223 | pub attrs: Vec<Attribute>, |
| 224 | pub vis: Visibility, |
| 225 | pub struct_token: Token![struct], |
| 226 | pub ident: Ident, |
| 227 | pub generics: Generics, |
| 228 | pub fields: Fields, |
| 229 | pub semi_token: Option<Token![;]>, |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | ast_struct! { |
| 234 | /// A trait definition: `pub trait Iterator { ... }`. |
| 235 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 236 | pub struct ItemTrait { |
| 237 | pub attrs: Vec<Attribute>, |
| 238 | pub vis: Visibility, |
| 239 | pub unsafety: Option<Token![unsafe]>, |
| 240 | pub auto_token: Option<Token![auto]>, |
| 241 | pub restriction: Option<ImplRestriction>, |
| 242 | pub trait_token: Token![trait], |
| 243 | pub ident: Ident, |
| 244 | pub generics: Generics, |
| 245 | pub colon_token: Option<Token![:]>, |
| 246 | pub supertraits: Punctuated<TypeParamBound, Token![+]>, |
| 247 | pub brace_token: token::Brace, |
| 248 | pub items: Vec<TraitItem>, |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | ast_struct! { |
| 253 | /// A trait alias: `pub trait SharableIterator = Iterator + Sync`. |
| 254 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 255 | pub struct ItemTraitAlias { |
| 256 | pub attrs: Vec<Attribute>, |
| 257 | pub vis: Visibility, |
| 258 | pub trait_token: Token![trait], |
| 259 | pub ident: Ident, |
| 260 | pub generics: Generics, |
| 261 | pub eq_token: Token![=], |
| 262 | pub bounds: Punctuated<TypeParamBound, Token![+]>, |
| 263 | pub semi_token: Token![;], |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | ast_struct! { |
| 268 | /// A type alias: `type Result<T> = std::result::Result<T, MyError>`. |
| 269 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 270 | pub struct ItemType { |
| 271 | pub attrs: Vec<Attribute>, |
| 272 | pub vis: Visibility, |
| 273 | pub type_token: Token![type], |
| 274 | pub ident: Ident, |
| 275 | pub generics: Generics, |
| 276 | pub eq_token: Token![=], |
| 277 | pub ty: Box<Type>, |
| 278 | pub semi_token: Token![;], |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | ast_struct! { |
| 283 | /// A union definition: `union Foo<A, B> { x: A, y: B }`. |
| 284 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 285 | pub struct ItemUnion { |
| 286 | pub attrs: Vec<Attribute>, |
| 287 | pub vis: Visibility, |
| 288 | pub union_token: Token![union], |
| 289 | pub ident: Ident, |
| 290 | pub generics: Generics, |
| 291 | pub fields: FieldsNamed, |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | ast_struct! { |
| 296 | /// A use declaration: `use std::collections::HashMap`. |
| 297 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 298 | pub struct ItemUse { |
| 299 | pub attrs: Vec<Attribute>, |
| 300 | pub vis: Visibility, |
| 301 | pub use_token: Token![use], |
| 302 | pub leading_colon: Option<Token![::]>, |
| 303 | pub tree: UseTree, |
| 304 | pub semi_token: Token![;], |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | impl Item { |
| 309 | #[cfg (feature = "parsing" )] |
| 310 | pub(crate) fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> { |
| 311 | match self { |
| 312 | Item::Const(ItemConst { attrs, .. }) |
| 313 | | Item::Enum(ItemEnum { attrs, .. }) |
| 314 | | Item::ExternCrate(ItemExternCrate { attrs, .. }) |
| 315 | | Item::Fn(ItemFn { attrs, .. }) |
| 316 | | Item::ForeignMod(ItemForeignMod { attrs, .. }) |
| 317 | | Item::Impl(ItemImpl { attrs, .. }) |
| 318 | | Item::Macro(ItemMacro { attrs, .. }) |
| 319 | | Item::Mod(ItemMod { attrs, .. }) |
| 320 | | Item::Static(ItemStatic { attrs, .. }) |
| 321 | | Item::Struct(ItemStruct { attrs, .. }) |
| 322 | | Item::Trait(ItemTrait { attrs, .. }) |
| 323 | | Item::TraitAlias(ItemTraitAlias { attrs, .. }) |
| 324 | | Item::Type(ItemType { attrs, .. }) |
| 325 | | Item::Union(ItemUnion { attrs, .. }) |
| 326 | | Item::Use(ItemUse { attrs, .. }) => mem::replace(attrs, new), |
| 327 | Item::Verbatim(_) => Vec::new(), |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | impl From<DeriveInput> for Item { |
| 333 | fn from(input: DeriveInput) -> Item { |
| 334 | match input.data { |
| 335 | Data::Struct(data) => Item::Struct(ItemStruct { |
| 336 | attrs: input.attrs, |
| 337 | vis: input.vis, |
| 338 | struct_token: data.struct_token, |
| 339 | ident: input.ident, |
| 340 | generics: input.generics, |
| 341 | fields: data.fields, |
| 342 | semi_token: data.semi_token, |
| 343 | }), |
| 344 | Data::Enum(data) => Item::Enum(ItemEnum { |
| 345 | attrs: input.attrs, |
| 346 | vis: input.vis, |
| 347 | enum_token: data.enum_token, |
| 348 | ident: input.ident, |
| 349 | generics: input.generics, |
| 350 | brace_token: data.brace_token, |
| 351 | variants: data.variants, |
| 352 | }), |
| 353 | Data::Union(data) => Item::Union(ItemUnion { |
| 354 | attrs: input.attrs, |
| 355 | vis: input.vis, |
| 356 | union_token: data.union_token, |
| 357 | ident: input.ident, |
| 358 | generics: input.generics, |
| 359 | fields: data.fields, |
| 360 | }), |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | impl From<ItemStruct> for DeriveInput { |
| 366 | fn from(input: ItemStruct) -> DeriveInput { |
| 367 | DeriveInput { |
| 368 | attrs: input.attrs, |
| 369 | vis: input.vis, |
| 370 | ident: input.ident, |
| 371 | generics: input.generics, |
| 372 | data: Data::Struct(DataStruct { |
| 373 | struct_token: input.struct_token, |
| 374 | fields: input.fields, |
| 375 | semi_token: input.semi_token, |
| 376 | }), |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | impl From<ItemEnum> for DeriveInput { |
| 382 | fn from(input: ItemEnum) -> DeriveInput { |
| 383 | DeriveInput { |
| 384 | attrs: input.attrs, |
| 385 | vis: input.vis, |
| 386 | ident: input.ident, |
| 387 | generics: input.generics, |
| 388 | data: Data::Enum(DataEnum { |
| 389 | enum_token: input.enum_token, |
| 390 | brace_token: input.brace_token, |
| 391 | variants: input.variants, |
| 392 | }), |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | impl From<ItemUnion> for DeriveInput { |
| 398 | fn from(input: ItemUnion) -> DeriveInput { |
| 399 | DeriveInput { |
| 400 | attrs: input.attrs, |
| 401 | vis: input.vis, |
| 402 | ident: input.ident, |
| 403 | generics: input.generics, |
| 404 | data: Data::Union(DataUnion { |
| 405 | union_token: input.union_token, |
| 406 | fields: input.fields, |
| 407 | }), |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | ast_enum_of_structs! { |
| 413 | /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`. |
| 414 | /// |
| 415 | /// # Syntax tree enum |
| 416 | /// |
| 417 | /// This type is a [syntax tree enum]. |
| 418 | /// |
| 419 | /// [syntax tree enum]: Expr#syntax-tree-enums |
| 420 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 421 | pub enum UseTree { |
| 422 | /// A path prefix of imports in a `use` item: `std::...`. |
| 423 | Path(UsePath), |
| 424 | |
| 425 | /// An identifier imported by a `use` item: `HashMap`. |
| 426 | Name(UseName), |
| 427 | |
| 428 | /// An renamed identifier imported by a `use` item: `HashMap as Map`. |
| 429 | Rename(UseRename), |
| 430 | |
| 431 | /// A glob import in a `use` item: `*`. |
| 432 | Glob(UseGlob), |
| 433 | |
| 434 | /// A braced group of imports in a `use` item: `{A, B, C}`. |
| 435 | Group(UseGroup), |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | ast_struct! { |
| 440 | /// A path prefix of imports in a `use` item: `std::...`. |
| 441 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 442 | pub struct UsePath { |
| 443 | pub ident: Ident, |
| 444 | pub colon2_token: Token![::], |
| 445 | pub tree: Box<UseTree>, |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | ast_struct! { |
| 450 | /// An identifier imported by a `use` item: `HashMap`. |
| 451 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 452 | pub struct UseName { |
| 453 | pub ident: Ident, |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | ast_struct! { |
| 458 | /// An renamed identifier imported by a `use` item: `HashMap as Map`. |
| 459 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 460 | pub struct UseRename { |
| 461 | pub ident: Ident, |
| 462 | pub as_token: Token![as], |
| 463 | pub rename: Ident, |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | ast_struct! { |
| 468 | /// A glob import in a `use` item: `*`. |
| 469 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 470 | pub struct UseGlob { |
| 471 | pub star_token: Token![*], |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | ast_struct! { |
| 476 | /// A braced group of imports in a `use` item: `{A, B, C}`. |
| 477 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 478 | pub struct UseGroup { |
| 479 | pub brace_token: token::Brace, |
| 480 | pub items: Punctuated<UseTree, Token![,]>, |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | ast_enum_of_structs! { |
| 485 | /// An item within an `extern` block. |
| 486 | /// |
| 487 | /// # Syntax tree enum |
| 488 | /// |
| 489 | /// This type is a [syntax tree enum]. |
| 490 | /// |
| 491 | /// [syntax tree enum]: Expr#syntax-tree-enums |
| 492 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 493 | #[non_exhaustive ] |
| 494 | pub enum ForeignItem { |
| 495 | /// A foreign function in an `extern` block. |
| 496 | Fn(ForeignItemFn), |
| 497 | |
| 498 | /// A foreign static item in an `extern` block: `static ext: u8`. |
| 499 | Static(ForeignItemStatic), |
| 500 | |
| 501 | /// A foreign type in an `extern` block: `type void`. |
| 502 | Type(ForeignItemType), |
| 503 | |
| 504 | /// A macro invocation within an extern block. |
| 505 | Macro(ForeignItemMacro), |
| 506 | |
| 507 | /// Tokens in an `extern` block not interpreted by Syn. |
| 508 | Verbatim(TokenStream), |
| 509 | |
| 510 | // For testing exhaustiveness in downstream code, use the following idiom: |
| 511 | // |
| 512 | // match item { |
| 513 | // ForeignItem::Fn(item) => {...} |
| 514 | // ForeignItem::Static(item) => {...} |
| 515 | // ... |
| 516 | // ForeignItem::Verbatim(item) => {...} |
| 517 | // |
| 518 | // #[cfg_attr(test, deny(non_exhaustive_omitted_patterns))] |
| 519 | // _ => { /* some sane fallback */ } |
| 520 | // } |
| 521 | // |
| 522 | // This way we fail your tests but don't break your library when adding |
| 523 | // a variant. You will be notified by a test failure when a variant is |
| 524 | // added, so that you can add code to handle it, but your library will |
| 525 | // continue to compile and work for downstream users in the interim. |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | ast_struct! { |
| 530 | /// A foreign function in an `extern` block. |
| 531 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 532 | pub struct ForeignItemFn { |
| 533 | pub attrs: Vec<Attribute>, |
| 534 | pub vis: Visibility, |
| 535 | pub sig: Signature, |
| 536 | pub semi_token: Token![;], |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | ast_struct! { |
| 541 | /// A foreign static item in an `extern` block: `static ext: u8`. |
| 542 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 543 | pub struct ForeignItemStatic { |
| 544 | pub attrs: Vec<Attribute>, |
| 545 | pub vis: Visibility, |
| 546 | pub static_token: Token![static], |
| 547 | pub mutability: StaticMutability, |
| 548 | pub ident: Ident, |
| 549 | pub colon_token: Token![:], |
| 550 | pub ty: Box<Type>, |
| 551 | pub semi_token: Token![;], |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | ast_struct! { |
| 556 | /// A foreign type in an `extern` block: `type void`. |
| 557 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 558 | pub struct ForeignItemType { |
| 559 | pub attrs: Vec<Attribute>, |
| 560 | pub vis: Visibility, |
| 561 | pub type_token: Token![type], |
| 562 | pub ident: Ident, |
| 563 | pub generics: Generics, |
| 564 | pub semi_token: Token![;], |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | ast_struct! { |
| 569 | /// A macro invocation within an extern block. |
| 570 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 571 | pub struct ForeignItemMacro { |
| 572 | pub attrs: Vec<Attribute>, |
| 573 | pub mac: Macro, |
| 574 | pub semi_token: Option<Token![;]>, |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | ast_enum_of_structs! { |
| 579 | /// An item declaration within the definition of a trait. |
| 580 | /// |
| 581 | /// # Syntax tree enum |
| 582 | /// |
| 583 | /// This type is a [syntax tree enum]. |
| 584 | /// |
| 585 | /// [syntax tree enum]: Expr#syntax-tree-enums |
| 586 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 587 | #[non_exhaustive ] |
| 588 | pub enum TraitItem { |
| 589 | /// An associated constant within the definition of a trait. |
| 590 | Const(TraitItemConst), |
| 591 | |
| 592 | /// An associated function within the definition of a trait. |
| 593 | Fn(TraitItemFn), |
| 594 | |
| 595 | /// An associated type within the definition of a trait. |
| 596 | Type(TraitItemType), |
| 597 | |
| 598 | /// A macro invocation within the definition of a trait. |
| 599 | Macro(TraitItemMacro), |
| 600 | |
| 601 | /// Tokens within the definition of a trait not interpreted by Syn. |
| 602 | Verbatim(TokenStream), |
| 603 | |
| 604 | // For testing exhaustiveness in downstream code, use the following idiom: |
| 605 | // |
| 606 | // match item { |
| 607 | // TraitItem::Const(item) => {...} |
| 608 | // TraitItem::Fn(item) => {...} |
| 609 | // ... |
| 610 | // TraitItem::Verbatim(item) => {...} |
| 611 | // |
| 612 | // #[cfg_attr(test, deny(non_exhaustive_omitted_patterns))] |
| 613 | // _ => { /* some sane fallback */ } |
| 614 | // } |
| 615 | // |
| 616 | // This way we fail your tests but don't break your library when adding |
| 617 | // a variant. You will be notified by a test failure when a variant is |
| 618 | // added, so that you can add code to handle it, but your library will |
| 619 | // continue to compile and work for downstream users in the interim. |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | ast_struct! { |
| 624 | /// An associated constant within the definition of a trait. |
| 625 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 626 | pub struct TraitItemConst { |
| 627 | pub attrs: Vec<Attribute>, |
| 628 | pub const_token: Token![const], |
| 629 | pub ident: Ident, |
| 630 | pub generics: Generics, |
| 631 | pub colon_token: Token![:], |
| 632 | pub ty: Type, |
| 633 | pub default: Option<(Token![=], Expr)>, |
| 634 | pub semi_token: Token![;], |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | ast_struct! { |
| 639 | /// An associated function within the definition of a trait. |
| 640 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 641 | pub struct TraitItemFn { |
| 642 | pub attrs: Vec<Attribute>, |
| 643 | pub sig: Signature, |
| 644 | pub default: Option<Block>, |
| 645 | pub semi_token: Option<Token![;]>, |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | ast_struct! { |
| 650 | /// An associated type within the definition of a trait. |
| 651 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 652 | pub struct TraitItemType { |
| 653 | pub attrs: Vec<Attribute>, |
| 654 | pub type_token: Token![type], |
| 655 | pub ident: Ident, |
| 656 | pub generics: Generics, |
| 657 | pub colon_token: Option<Token![:]>, |
| 658 | pub bounds: Punctuated<TypeParamBound, Token![+]>, |
| 659 | pub default: Option<(Token![=], Type)>, |
| 660 | pub semi_token: Token![;], |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | ast_struct! { |
| 665 | /// A macro invocation within the definition of a trait. |
| 666 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 667 | pub struct TraitItemMacro { |
| 668 | pub attrs: Vec<Attribute>, |
| 669 | pub mac: Macro, |
| 670 | pub semi_token: Option<Token![;]>, |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | ast_enum_of_structs! { |
| 675 | /// An item within an impl block. |
| 676 | /// |
| 677 | /// # Syntax tree enum |
| 678 | /// |
| 679 | /// This type is a [syntax tree enum]. |
| 680 | /// |
| 681 | /// [syntax tree enum]: Expr#syntax-tree-enums |
| 682 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 683 | #[non_exhaustive ] |
| 684 | pub enum ImplItem { |
| 685 | /// An associated constant within an impl block. |
| 686 | Const(ImplItemConst), |
| 687 | |
| 688 | /// An associated function within an impl block. |
| 689 | Fn(ImplItemFn), |
| 690 | |
| 691 | /// An associated type within an impl block. |
| 692 | Type(ImplItemType), |
| 693 | |
| 694 | /// A macro invocation within an impl block. |
| 695 | Macro(ImplItemMacro), |
| 696 | |
| 697 | /// Tokens within an impl block not interpreted by Syn. |
| 698 | Verbatim(TokenStream), |
| 699 | |
| 700 | // For testing exhaustiveness in downstream code, use the following idiom: |
| 701 | // |
| 702 | // match item { |
| 703 | // ImplItem::Const(item) => {...} |
| 704 | // ImplItem::Fn(item) => {...} |
| 705 | // ... |
| 706 | // ImplItem::Verbatim(item) => {...} |
| 707 | // |
| 708 | // #[cfg_attr(test, deny(non_exhaustive_omitted_patterns))] |
| 709 | // _ => { /* some sane fallback */ } |
| 710 | // } |
| 711 | // |
| 712 | // This way we fail your tests but don't break your library when adding |
| 713 | // a variant. You will be notified by a test failure when a variant is |
| 714 | // added, so that you can add code to handle it, but your library will |
| 715 | // continue to compile and work for downstream users in the interim. |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | ast_struct! { |
| 720 | /// An associated constant within an impl block. |
| 721 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 722 | pub struct ImplItemConst { |
| 723 | pub attrs: Vec<Attribute>, |
| 724 | pub vis: Visibility, |
| 725 | pub defaultness: Option<Token![default]>, |
| 726 | pub const_token: Token![const], |
| 727 | pub ident: Ident, |
| 728 | pub generics: Generics, |
| 729 | pub colon_token: Token![:], |
| 730 | pub ty: Type, |
| 731 | pub eq_token: Token![=], |
| 732 | pub expr: Expr, |
| 733 | pub semi_token: Token![;], |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | ast_struct! { |
| 738 | /// An associated function within an impl block. |
| 739 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 740 | pub struct ImplItemFn { |
| 741 | pub attrs: Vec<Attribute>, |
| 742 | pub vis: Visibility, |
| 743 | pub defaultness: Option<Token![default]>, |
| 744 | pub sig: Signature, |
| 745 | pub block: Block, |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | ast_struct! { |
| 750 | /// An associated type within an impl block. |
| 751 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 752 | pub struct ImplItemType { |
| 753 | pub attrs: Vec<Attribute>, |
| 754 | pub vis: Visibility, |
| 755 | pub defaultness: Option<Token![default]>, |
| 756 | pub type_token: Token![type], |
| 757 | pub ident: Ident, |
| 758 | pub generics: Generics, |
| 759 | pub eq_token: Token![=], |
| 760 | pub ty: Type, |
| 761 | pub semi_token: Token![;], |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | ast_struct! { |
| 766 | /// A macro invocation within an impl block. |
| 767 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 768 | pub struct ImplItemMacro { |
| 769 | pub attrs: Vec<Attribute>, |
| 770 | pub mac: Macro, |
| 771 | pub semi_token: Option<Token![;]>, |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | ast_struct! { |
| 776 | /// A function signature in a trait or implementation: `unsafe fn |
| 777 | /// initialize(&self)`. |
| 778 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 779 | pub struct Signature { |
| 780 | pub constness: Option<Token![const]>, |
| 781 | pub asyncness: Option<Token![async]>, |
| 782 | pub unsafety: Option<Token![unsafe]>, |
| 783 | pub abi: Option<Abi>, |
| 784 | pub fn_token: Token![fn], |
| 785 | pub ident: Ident, |
| 786 | pub generics: Generics, |
| 787 | pub paren_token: token::Paren, |
| 788 | pub inputs: Punctuated<FnArg, Token![,]>, |
| 789 | pub variadic: Option<Variadic>, |
| 790 | pub output: ReturnType, |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | impl Signature { |
| 795 | /// A method's `self` receiver, such as `&self` or `self: Box<Self>`. |
| 796 | pub fn receiver(&self) -> Option<&Receiver> { |
| 797 | let arg = self.inputs.first()?; |
| 798 | match arg { |
| 799 | FnArg::Receiver(receiver) => Some(receiver), |
| 800 | FnArg::Typed(_) => None, |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | ast_enum_of_structs! { |
| 806 | /// An argument in a function signature: the `n: usize` in `fn f(n: usize)`. |
| 807 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 808 | pub enum FnArg { |
| 809 | /// The `self` argument of an associated method. |
| 810 | Receiver(Receiver), |
| 811 | |
| 812 | /// A function argument accepted by pattern and type. |
| 813 | Typed(PatType), |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | ast_struct! { |
| 818 | /// The `self` argument of an associated method. |
| 819 | /// |
| 820 | /// If `colon_token` is present, the receiver is written with an explicit |
| 821 | /// type such as `self: Box<Self>`. If `colon_token` is absent, the receiver |
| 822 | /// is written in shorthand such as `self` or `&self` or `&mut self`. In the |
| 823 | /// shorthand case, the type in `ty` is reconstructed as one of `Self`, |
| 824 | /// `&Self`, or `&mut Self`. |
| 825 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 826 | pub struct Receiver { |
| 827 | pub attrs: Vec<Attribute>, |
| 828 | pub reference: Option<(Token![&], Option<Lifetime>)>, |
| 829 | pub mutability: Option<Token![mut]>, |
| 830 | pub self_token: Token![self], |
| 831 | pub colon_token: Option<Token![:]>, |
| 832 | pub ty: Box<Type>, |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | impl Receiver { |
| 837 | pub fn lifetime(&self) -> Option<&Lifetime> { |
| 838 | self.reference.as_ref()?.1.as_ref() |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | ast_struct! { |
| 843 | /// The variadic argument of a foreign function. |
| 844 | /// |
| 845 | /// ```rust |
| 846 | /// # struct c_char; |
| 847 | /// # struct c_int; |
| 848 | /// # |
| 849 | /// extern "C" { |
| 850 | /// fn printf(format: *const c_char, ...) -> c_int; |
| 851 | /// // ^^^ |
| 852 | /// } |
| 853 | /// ``` |
| 854 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 855 | pub struct Variadic { |
| 856 | pub attrs: Vec<Attribute>, |
| 857 | pub pat: Option<(Box<Pat>, Token![:])>, |
| 858 | pub dots: Token![...], |
| 859 | pub comma: Option<Token![,]>, |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | ast_enum! { |
| 864 | /// The mutability of an `Item::Static` or `ForeignItem::Static`. |
| 865 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 866 | #[non_exhaustive ] |
| 867 | pub enum StaticMutability { |
| 868 | Mut(Token![mut]), |
| 869 | None, |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | ast_enum! { |
| 874 | /// Unused, but reserved for RFC 3323 restrictions. |
| 875 | #[cfg_attr (doc_cfg, doc(cfg(feature = "full" )))] |
| 876 | #[non_exhaustive ] |
| 877 | pub enum ImplRestriction {} |
| 878 | |
| 879 | |
| 880 | // TODO: https://rust-lang.github.io/rfcs/3323-restrictions.html |
| 881 | // |
| 882 | // pub struct ImplRestriction { |
| 883 | // pub impl_token: Token![impl], |
| 884 | // pub paren_token: token::Paren, |
| 885 | // pub in_token: Option<Token![in]>, |
| 886 | // pub path: Box<Path>, |
| 887 | // } |
| 888 | } |
| 889 | |
| 890 | #[cfg (feature = "parsing" )] |
| 891 | pub(crate) mod parsing { |
| 892 | use super::*; |
| 893 | use crate::ext::IdentExt; |
| 894 | use crate::parse::discouraged::Speculative; |
| 895 | use crate::parse::{Parse, ParseBuffer, ParseStream, Result}; |
| 896 | |
| 897 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 898 | impl Parse for Item { |
| 899 | fn parse(input: ParseStream) -> Result<Self> { |
| 900 | let begin = input.fork(); |
| 901 | let attrs = input.call(Attribute::parse_outer)?; |
| 902 | parse_rest_of_item(begin, attrs, input) |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | pub(crate) fn parse_rest_of_item( |
| 907 | begin: ParseBuffer, |
| 908 | mut attrs: Vec<Attribute>, |
| 909 | input: ParseStream, |
| 910 | ) -> Result<Item> { |
| 911 | let ahead = input.fork(); |
| 912 | let vis: Visibility = ahead.parse()?; |
| 913 | |
| 914 | let lookahead = ahead.lookahead1(); |
| 915 | let mut item = if lookahead.peek(Token![fn]) || peek_signature(&ahead) { |
| 916 | let vis: Visibility = input.parse()?; |
| 917 | let sig: Signature = input.parse()?; |
| 918 | if input.peek(Token![;]) { |
| 919 | input.parse::<Token![;]>()?; |
| 920 | Ok(Item::Verbatim(verbatim::between(&begin, input))) |
| 921 | } else { |
| 922 | parse_rest_of_fn(input, Vec::new(), vis, sig).map(Item::Fn) |
| 923 | } |
| 924 | } else if lookahead.peek(Token![extern]) { |
| 925 | ahead.parse::<Token![extern]>()?; |
| 926 | let lookahead = ahead.lookahead1(); |
| 927 | if lookahead.peek(Token![crate]) { |
| 928 | input.parse().map(Item::ExternCrate) |
| 929 | } else if lookahead.peek(token::Brace) { |
| 930 | input.parse().map(Item::ForeignMod) |
| 931 | } else if lookahead.peek(LitStr) { |
| 932 | ahead.parse::<LitStr>()?; |
| 933 | let lookahead = ahead.lookahead1(); |
| 934 | if lookahead.peek(token::Brace) { |
| 935 | input.parse().map(Item::ForeignMod) |
| 936 | } else { |
| 937 | Err(lookahead.error()) |
| 938 | } |
| 939 | } else { |
| 940 | Err(lookahead.error()) |
| 941 | } |
| 942 | } else if lookahead.peek(Token![use]) { |
| 943 | let allow_crate_root_in_path = true; |
| 944 | match parse_item_use(input, allow_crate_root_in_path)? { |
| 945 | Some(item_use) => Ok(Item::Use(item_use)), |
| 946 | None => Ok(Item::Verbatim(verbatim::between(&begin, input))), |
| 947 | } |
| 948 | } else if lookahead.peek(Token![static]) { |
| 949 | let vis = input.parse()?; |
| 950 | let static_token = input.parse()?; |
| 951 | let mutability = input.parse()?; |
| 952 | let ident = input.parse()?; |
| 953 | if input.peek(Token![=]) { |
| 954 | input.parse::<Token![=]>()?; |
| 955 | input.parse::<Expr>()?; |
| 956 | input.parse::<Token![;]>()?; |
| 957 | Ok(Item::Verbatim(verbatim::between(&begin, input))) |
| 958 | } else { |
| 959 | let colon_token = input.parse()?; |
| 960 | let ty = input.parse()?; |
| 961 | if input.peek(Token![;]) { |
| 962 | input.parse::<Token![;]>()?; |
| 963 | Ok(Item::Verbatim(verbatim::between(&begin, input))) |
| 964 | } else { |
| 965 | Ok(Item::Static(ItemStatic { |
| 966 | attrs: Vec::new(), |
| 967 | vis, |
| 968 | static_token, |
| 969 | mutability, |
| 970 | ident, |
| 971 | colon_token, |
| 972 | ty, |
| 973 | eq_token: input.parse()?, |
| 974 | expr: input.parse()?, |
| 975 | semi_token: input.parse()?, |
| 976 | })) |
| 977 | } |
| 978 | } |
| 979 | } else if lookahead.peek(Token![const]) { |
| 980 | let vis = input.parse()?; |
| 981 | let const_token: Token![const] = input.parse()?; |
| 982 | let lookahead = input.lookahead1(); |
| 983 | let ident = if lookahead.peek(Ident) || lookahead.peek(Token![_]) { |
| 984 | input.call(Ident::parse_any)? |
| 985 | } else { |
| 986 | return Err(lookahead.error()); |
| 987 | }; |
| 988 | let mut generics: Generics = input.parse()?; |
| 989 | let colon_token = input.parse()?; |
| 990 | let ty = input.parse()?; |
| 991 | let value = if let Some(eq_token) = input.parse::<Option<Token![=]>>()? { |
| 992 | let expr: Expr = input.parse()?; |
| 993 | Some((eq_token, expr)) |
| 994 | } else { |
| 995 | None |
| 996 | }; |
| 997 | generics.where_clause = input.parse()?; |
| 998 | let semi_token: Token![;] = input.parse()?; |
| 999 | match value { |
| 1000 | Some((eq_token, expr)) |
| 1001 | if generics.lt_token.is_none() && generics.where_clause.is_none() => |
| 1002 | { |
| 1003 | Ok(Item::Const(ItemConst { |
| 1004 | attrs: Vec::new(), |
| 1005 | vis, |
| 1006 | const_token, |
| 1007 | ident, |
| 1008 | generics, |
| 1009 | colon_token, |
| 1010 | ty, |
| 1011 | eq_token, |
| 1012 | expr: Box::new(expr), |
| 1013 | semi_token, |
| 1014 | })) |
| 1015 | } |
| 1016 | _ => Ok(Item::Verbatim(verbatim::between(&begin, input))), |
| 1017 | } |
| 1018 | } else if lookahead.peek(Token![unsafe]) { |
| 1019 | ahead.parse::<Token![unsafe]>()?; |
| 1020 | let lookahead = ahead.lookahead1(); |
| 1021 | if lookahead.peek(Token![trait]) |
| 1022 | || lookahead.peek(Token![auto]) && ahead.peek2(Token![trait]) |
| 1023 | { |
| 1024 | input.parse().map(Item::Trait) |
| 1025 | } else if lookahead.peek(Token![impl]) { |
| 1026 | let allow_verbatim_impl = true; |
| 1027 | if let Some(item) = parse_impl(input, allow_verbatim_impl)? { |
| 1028 | Ok(Item::Impl(item)) |
| 1029 | } else { |
| 1030 | Ok(Item::Verbatim(verbatim::between(&begin, input))) |
| 1031 | } |
| 1032 | } else if lookahead.peek(Token![extern]) { |
| 1033 | input.parse().map(Item::ForeignMod) |
| 1034 | } else if lookahead.peek(Token![mod]) { |
| 1035 | input.parse().map(Item::Mod) |
| 1036 | } else { |
| 1037 | Err(lookahead.error()) |
| 1038 | } |
| 1039 | } else if lookahead.peek(Token![mod]) { |
| 1040 | input.parse().map(Item::Mod) |
| 1041 | } else if lookahead.peek(Token![type]) { |
| 1042 | parse_item_type(begin, input) |
| 1043 | } else if lookahead.peek(Token![struct]) { |
| 1044 | input.parse().map(Item::Struct) |
| 1045 | } else if lookahead.peek(Token![enum]) { |
| 1046 | input.parse().map(Item::Enum) |
| 1047 | } else if lookahead.peek(Token![union]) && ahead.peek2(Ident) { |
| 1048 | input.parse().map(Item::Union) |
| 1049 | } else if lookahead.peek(Token![trait]) { |
| 1050 | input.call(parse_trait_or_trait_alias) |
| 1051 | } else if lookahead.peek(Token![auto]) && ahead.peek2(Token![trait]) { |
| 1052 | input.parse().map(Item::Trait) |
| 1053 | } else if lookahead.peek(Token![impl]) |
| 1054 | || lookahead.peek(Token![default]) && !ahead.peek2(Token![!]) |
| 1055 | { |
| 1056 | let allow_verbatim_impl = true; |
| 1057 | if let Some(item) = parse_impl(input, allow_verbatim_impl)? { |
| 1058 | Ok(Item::Impl(item)) |
| 1059 | } else { |
| 1060 | Ok(Item::Verbatim(verbatim::between(&begin, input))) |
| 1061 | } |
| 1062 | } else if lookahead.peek(Token![macro]) { |
| 1063 | input.advance_to(&ahead); |
| 1064 | parse_macro2(begin, vis, input) |
| 1065 | } else if vis.is_inherited() |
| 1066 | && (lookahead.peek(Ident) |
| 1067 | || lookahead.peek(Token![self]) |
| 1068 | || lookahead.peek(Token![super]) |
| 1069 | || lookahead.peek(Token![crate]) |
| 1070 | || lookahead.peek(Token![::])) |
| 1071 | { |
| 1072 | input.parse().map(Item::Macro) |
| 1073 | } else { |
| 1074 | Err(lookahead.error()) |
| 1075 | }?; |
| 1076 | |
| 1077 | attrs.extend(item.replace_attrs(Vec::new())); |
| 1078 | item.replace_attrs(attrs); |
| 1079 | Ok(item) |
| 1080 | } |
| 1081 | |
| 1082 | struct FlexibleItemType { |
| 1083 | vis: Visibility, |
| 1084 | defaultness: Option<Token![default]>, |
| 1085 | type_token: Token![type], |
| 1086 | ident: Ident, |
| 1087 | generics: Generics, |
| 1088 | colon_token: Option<Token![:]>, |
| 1089 | bounds: Punctuated<TypeParamBound, Token![+]>, |
| 1090 | ty: Option<(Token![=], Type)>, |
| 1091 | semi_token: Token![;], |
| 1092 | } |
| 1093 | |
| 1094 | enum TypeDefaultness { |
| 1095 | Optional, |
| 1096 | Disallowed, |
| 1097 | } |
| 1098 | |
| 1099 | enum WhereClauseLocation { |
| 1100 | // type Ty<T> where T: 'static = T; |
| 1101 | BeforeEq, |
| 1102 | // type Ty<T> = T where T: 'static; |
| 1103 | AfterEq, |
| 1104 | // TODO: goes away once the migration period on rust-lang/rust#89122 is over |
| 1105 | Both, |
| 1106 | } |
| 1107 | |
| 1108 | impl FlexibleItemType { |
| 1109 | fn parse( |
| 1110 | input: ParseStream, |
| 1111 | allow_defaultness: TypeDefaultness, |
| 1112 | where_clause_location: WhereClauseLocation, |
| 1113 | ) -> Result<Self> { |
| 1114 | let vis: Visibility = input.parse()?; |
| 1115 | let defaultness: Option<Token![default]> = match allow_defaultness { |
| 1116 | TypeDefaultness::Optional => input.parse()?, |
| 1117 | TypeDefaultness::Disallowed => None, |
| 1118 | }; |
| 1119 | let type_token: Token![type] = input.parse()?; |
| 1120 | let ident: Ident = input.parse()?; |
| 1121 | let mut generics: Generics = input.parse()?; |
| 1122 | let (colon_token, bounds) = Self::parse_optional_bounds(input)?; |
| 1123 | |
| 1124 | match where_clause_location { |
| 1125 | WhereClauseLocation::BeforeEq | WhereClauseLocation::Both => { |
| 1126 | generics.where_clause = input.parse()?; |
| 1127 | } |
| 1128 | WhereClauseLocation::AfterEq => {} |
| 1129 | } |
| 1130 | |
| 1131 | let ty = Self::parse_optional_definition(input)?; |
| 1132 | |
| 1133 | match where_clause_location { |
| 1134 | WhereClauseLocation::AfterEq | WhereClauseLocation::Both |
| 1135 | if generics.where_clause.is_none() => |
| 1136 | { |
| 1137 | generics.where_clause = input.parse()?; |
| 1138 | } |
| 1139 | _ => {} |
| 1140 | } |
| 1141 | |
| 1142 | let semi_token: Token![;] = input.parse()?; |
| 1143 | |
| 1144 | Ok(FlexibleItemType { |
| 1145 | vis, |
| 1146 | defaultness, |
| 1147 | type_token, |
| 1148 | ident, |
| 1149 | generics, |
| 1150 | colon_token, |
| 1151 | bounds, |
| 1152 | ty, |
| 1153 | semi_token, |
| 1154 | }) |
| 1155 | } |
| 1156 | |
| 1157 | fn parse_optional_bounds( |
| 1158 | input: ParseStream, |
| 1159 | ) -> Result<(Option<Token![:]>, Punctuated<TypeParamBound, Token![+]>)> { |
| 1160 | let colon_token: Option<Token![:]> = input.parse()?; |
| 1161 | |
| 1162 | let mut bounds = Punctuated::new(); |
| 1163 | if colon_token.is_some() { |
| 1164 | loop { |
| 1165 | if input.peek(Token![where]) || input.peek(Token![=]) || input.peek(Token![;]) { |
| 1166 | break; |
| 1167 | } |
| 1168 | bounds.push_value(input.parse::<TypeParamBound>()?); |
| 1169 | if input.peek(Token![where]) || input.peek(Token![=]) || input.peek(Token![;]) { |
| 1170 | break; |
| 1171 | } |
| 1172 | bounds.push_punct(input.parse::<Token![+]>()?); |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | Ok((colon_token, bounds)) |
| 1177 | } |
| 1178 | |
| 1179 | fn parse_optional_definition(input: ParseStream) -> Result<Option<(Token![=], Type)>> { |
| 1180 | let eq_token: Option<Token![=]> = input.parse()?; |
| 1181 | if let Some(eq_token) = eq_token { |
| 1182 | let definition: Type = input.parse()?; |
| 1183 | Ok(Some((eq_token, definition))) |
| 1184 | } else { |
| 1185 | Ok(None) |
| 1186 | } |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1191 | impl Parse for ItemMacro { |
| 1192 | fn parse(input: ParseStream) -> Result<Self> { |
| 1193 | let attrs = input.call(Attribute::parse_outer)?; |
| 1194 | let path = input.call(Path::parse_mod_style)?; |
| 1195 | let bang_token: Token![!] = input.parse()?; |
| 1196 | let ident: Option<Ident> = if input.peek(Token![try]) { |
| 1197 | input.call(Ident::parse_any).map(Some) |
| 1198 | } else { |
| 1199 | input.parse() |
| 1200 | }?; |
| 1201 | let (delimiter, tokens) = input.call(mac::parse_delimiter)?; |
| 1202 | let semi_token: Option<Token![;]> = if !delimiter.is_brace() { |
| 1203 | Some(input.parse()?) |
| 1204 | } else { |
| 1205 | None |
| 1206 | }; |
| 1207 | Ok(ItemMacro { |
| 1208 | attrs, |
| 1209 | ident, |
| 1210 | mac: Macro { |
| 1211 | path, |
| 1212 | bang_token, |
| 1213 | delimiter, |
| 1214 | tokens, |
| 1215 | }, |
| 1216 | semi_token, |
| 1217 | }) |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | fn parse_macro2(begin: ParseBuffer, _vis: Visibility, input: ParseStream) -> Result<Item> { |
| 1222 | input.parse::<Token![macro]>()?; |
| 1223 | input.parse::<Ident>()?; |
| 1224 | |
| 1225 | let mut lookahead = input.lookahead1(); |
| 1226 | if lookahead.peek(token::Paren) { |
| 1227 | let paren_content; |
| 1228 | parenthesized!(paren_content in input); |
| 1229 | paren_content.parse::<TokenStream>()?; |
| 1230 | lookahead = input.lookahead1(); |
| 1231 | } |
| 1232 | |
| 1233 | if lookahead.peek(token::Brace) { |
| 1234 | let brace_content; |
| 1235 | braced!(brace_content in input); |
| 1236 | brace_content.parse::<TokenStream>()?; |
| 1237 | } else { |
| 1238 | return Err(lookahead.error()); |
| 1239 | } |
| 1240 | |
| 1241 | Ok(Item::Verbatim(verbatim::between(&begin, input))) |
| 1242 | } |
| 1243 | |
| 1244 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1245 | impl Parse for ItemExternCrate { |
| 1246 | fn parse(input: ParseStream) -> Result<Self> { |
| 1247 | Ok(ItemExternCrate { |
| 1248 | attrs: input.call(Attribute::parse_outer)?, |
| 1249 | vis: input.parse()?, |
| 1250 | extern_token: input.parse()?, |
| 1251 | crate_token: input.parse()?, |
| 1252 | ident: { |
| 1253 | if input.peek(Token![self]) { |
| 1254 | input.call(Ident::parse_any)? |
| 1255 | } else { |
| 1256 | input.parse()? |
| 1257 | } |
| 1258 | }, |
| 1259 | rename: { |
| 1260 | if input.peek(Token![as]) { |
| 1261 | let as_token: Token![as] = input.parse()?; |
| 1262 | let rename: Ident = if input.peek(Token![_]) { |
| 1263 | Ident::from(input.parse::<Token![_]>()?) |
| 1264 | } else { |
| 1265 | input.parse()? |
| 1266 | }; |
| 1267 | Some((as_token, rename)) |
| 1268 | } else { |
| 1269 | None |
| 1270 | } |
| 1271 | }, |
| 1272 | semi_token: input.parse()?, |
| 1273 | }) |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1278 | impl Parse for ItemUse { |
| 1279 | fn parse(input: ParseStream) -> Result<Self> { |
| 1280 | let allow_crate_root_in_path = false; |
| 1281 | parse_item_use(input, allow_crate_root_in_path).map(Option::unwrap) |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | fn parse_item_use( |
| 1286 | input: ParseStream, |
| 1287 | allow_crate_root_in_path: bool, |
| 1288 | ) -> Result<Option<ItemUse>> { |
| 1289 | let attrs = input.call(Attribute::parse_outer)?; |
| 1290 | let vis: Visibility = input.parse()?; |
| 1291 | let use_token: Token![use] = input.parse()?; |
| 1292 | let leading_colon: Option<Token![::]> = input.parse()?; |
| 1293 | let tree = parse_use_tree(input, allow_crate_root_in_path && leading_colon.is_none())?; |
| 1294 | let semi_token: Token![;] = input.parse()?; |
| 1295 | |
| 1296 | let tree = match tree { |
| 1297 | Some(tree) => tree, |
| 1298 | None => return Ok(None), |
| 1299 | }; |
| 1300 | |
| 1301 | Ok(Some(ItemUse { |
| 1302 | attrs, |
| 1303 | vis, |
| 1304 | use_token, |
| 1305 | leading_colon, |
| 1306 | tree, |
| 1307 | semi_token, |
| 1308 | })) |
| 1309 | } |
| 1310 | |
| 1311 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1312 | impl Parse for UseTree { |
| 1313 | fn parse(input: ParseStream) -> Result<UseTree> { |
| 1314 | let allow_crate_root_in_path = false; |
| 1315 | parse_use_tree(input, allow_crate_root_in_path).map(Option::unwrap) |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | fn parse_use_tree( |
| 1320 | input: ParseStream, |
| 1321 | allow_crate_root_in_path: bool, |
| 1322 | ) -> Result<Option<UseTree>> { |
| 1323 | let lookahead = input.lookahead1(); |
| 1324 | if lookahead.peek(Ident) |
| 1325 | || lookahead.peek(Token![self]) |
| 1326 | || lookahead.peek(Token![super]) |
| 1327 | || lookahead.peek(Token![crate]) |
| 1328 | || lookahead.peek(Token![try]) |
| 1329 | { |
| 1330 | let ident = input.call(Ident::parse_any)?; |
| 1331 | if input.peek(Token![::]) { |
| 1332 | Ok(Some(UseTree::Path(UsePath { |
| 1333 | ident, |
| 1334 | colon2_token: input.parse()?, |
| 1335 | tree: Box::new(input.parse()?), |
| 1336 | }))) |
| 1337 | } else if input.peek(Token![as]) { |
| 1338 | Ok(Some(UseTree::Rename(UseRename { |
| 1339 | ident, |
| 1340 | as_token: input.parse()?, |
| 1341 | rename: { |
| 1342 | if input.peek(Ident) { |
| 1343 | input.parse()? |
| 1344 | } else if input.peek(Token![_]) { |
| 1345 | Ident::from(input.parse::<Token![_]>()?) |
| 1346 | } else { |
| 1347 | return Err(input.error("expected identifier or underscore" )); |
| 1348 | } |
| 1349 | }, |
| 1350 | }))) |
| 1351 | } else { |
| 1352 | Ok(Some(UseTree::Name(UseName { ident }))) |
| 1353 | } |
| 1354 | } else if lookahead.peek(Token![*]) { |
| 1355 | Ok(Some(UseTree::Glob(UseGlob { |
| 1356 | star_token: input.parse()?, |
| 1357 | }))) |
| 1358 | } else if lookahead.peek(token::Brace) { |
| 1359 | let content; |
| 1360 | let brace_token = braced!(content in input); |
| 1361 | let mut items = Punctuated::new(); |
| 1362 | let mut has_any_crate_root_in_path = false; |
| 1363 | loop { |
| 1364 | if content.is_empty() { |
| 1365 | break; |
| 1366 | } |
| 1367 | let this_tree_starts_with_crate_root = |
| 1368 | allow_crate_root_in_path && content.parse::<Option<Token![::]>>()?.is_some(); |
| 1369 | has_any_crate_root_in_path |= this_tree_starts_with_crate_root; |
| 1370 | match parse_use_tree( |
| 1371 | &content, |
| 1372 | allow_crate_root_in_path && !this_tree_starts_with_crate_root, |
| 1373 | )? { |
| 1374 | Some(tree) => items.push_value(tree), |
| 1375 | None => has_any_crate_root_in_path = true, |
| 1376 | } |
| 1377 | if content.is_empty() { |
| 1378 | break; |
| 1379 | } |
| 1380 | let comma: Token![,] = content.parse()?; |
| 1381 | items.push_punct(comma); |
| 1382 | } |
| 1383 | if has_any_crate_root_in_path { |
| 1384 | Ok(None) |
| 1385 | } else { |
| 1386 | Ok(Some(UseTree::Group(UseGroup { brace_token, items }))) |
| 1387 | } |
| 1388 | } else { |
| 1389 | Err(lookahead.error()) |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1394 | impl Parse for ItemStatic { |
| 1395 | fn parse(input: ParseStream) -> Result<Self> { |
| 1396 | Ok(ItemStatic { |
| 1397 | attrs: input.call(Attribute::parse_outer)?, |
| 1398 | vis: input.parse()?, |
| 1399 | static_token: input.parse()?, |
| 1400 | mutability: input.parse()?, |
| 1401 | ident: input.parse()?, |
| 1402 | colon_token: input.parse()?, |
| 1403 | ty: input.parse()?, |
| 1404 | eq_token: input.parse()?, |
| 1405 | expr: input.parse()?, |
| 1406 | semi_token: input.parse()?, |
| 1407 | }) |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1412 | impl Parse for ItemConst { |
| 1413 | fn parse(input: ParseStream) -> Result<Self> { |
| 1414 | let attrs = input.call(Attribute::parse_outer)?; |
| 1415 | let vis: Visibility = input.parse()?; |
| 1416 | let const_token: Token![const] = input.parse()?; |
| 1417 | |
| 1418 | let lookahead = input.lookahead1(); |
| 1419 | let ident = if lookahead.peek(Ident) || lookahead.peek(Token![_]) { |
| 1420 | input.call(Ident::parse_any)? |
| 1421 | } else { |
| 1422 | return Err(lookahead.error()); |
| 1423 | }; |
| 1424 | |
| 1425 | let colon_token: Token![:] = input.parse()?; |
| 1426 | let ty: Type = input.parse()?; |
| 1427 | let eq_token: Token![=] = input.parse()?; |
| 1428 | let expr: Expr = input.parse()?; |
| 1429 | let semi_token: Token![;] = input.parse()?; |
| 1430 | |
| 1431 | Ok(ItemConst { |
| 1432 | attrs, |
| 1433 | vis, |
| 1434 | const_token, |
| 1435 | ident, |
| 1436 | generics: Generics::default(), |
| 1437 | colon_token, |
| 1438 | ty: Box::new(ty), |
| 1439 | eq_token, |
| 1440 | expr: Box::new(expr), |
| 1441 | semi_token, |
| 1442 | }) |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | fn peek_signature(input: ParseStream) -> bool { |
| 1447 | let fork = input.fork(); |
| 1448 | fork.parse::<Option<Token![const]>>().is_ok() |
| 1449 | && fork.parse::<Option<Token![async]>>().is_ok() |
| 1450 | && fork.parse::<Option<Token![unsafe]>>().is_ok() |
| 1451 | && fork.parse::<Option<Abi>>().is_ok() |
| 1452 | && fork.peek(Token![fn]) |
| 1453 | } |
| 1454 | |
| 1455 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1456 | impl Parse for Signature { |
| 1457 | fn parse(input: ParseStream) -> Result<Self> { |
| 1458 | let constness: Option<Token![const]> = input.parse()?; |
| 1459 | let asyncness: Option<Token![async]> = input.parse()?; |
| 1460 | let unsafety: Option<Token![unsafe]> = input.parse()?; |
| 1461 | let abi: Option<Abi> = input.parse()?; |
| 1462 | let fn_token: Token![fn] = input.parse()?; |
| 1463 | let ident: Ident = input.parse()?; |
| 1464 | let mut generics: Generics = input.parse()?; |
| 1465 | |
| 1466 | let content; |
| 1467 | let paren_token = parenthesized!(content in input); |
| 1468 | let (inputs, variadic) = parse_fn_args(&content)?; |
| 1469 | |
| 1470 | let output: ReturnType = input.parse()?; |
| 1471 | generics.where_clause = input.parse()?; |
| 1472 | |
| 1473 | Ok(Signature { |
| 1474 | constness, |
| 1475 | asyncness, |
| 1476 | unsafety, |
| 1477 | abi, |
| 1478 | fn_token, |
| 1479 | ident, |
| 1480 | generics, |
| 1481 | paren_token, |
| 1482 | inputs, |
| 1483 | variadic, |
| 1484 | output, |
| 1485 | }) |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1490 | impl Parse for ItemFn { |
| 1491 | fn parse(input: ParseStream) -> Result<Self> { |
| 1492 | let outer_attrs = input.call(Attribute::parse_outer)?; |
| 1493 | let vis: Visibility = input.parse()?; |
| 1494 | let sig: Signature = input.parse()?; |
| 1495 | parse_rest_of_fn(input, outer_attrs, vis, sig) |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | fn parse_rest_of_fn( |
| 1500 | input: ParseStream, |
| 1501 | mut attrs: Vec<Attribute>, |
| 1502 | vis: Visibility, |
| 1503 | sig: Signature, |
| 1504 | ) -> Result<ItemFn> { |
| 1505 | let content; |
| 1506 | let brace_token = braced!(content in input); |
| 1507 | attr::parsing::parse_inner(&content, &mut attrs)?; |
| 1508 | let stmts = content.call(Block::parse_within)?; |
| 1509 | |
| 1510 | Ok(ItemFn { |
| 1511 | attrs, |
| 1512 | vis, |
| 1513 | sig, |
| 1514 | block: Box::new(Block { brace_token, stmts }), |
| 1515 | }) |
| 1516 | } |
| 1517 | |
| 1518 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1519 | impl Parse for FnArg { |
| 1520 | fn parse(input: ParseStream) -> Result<Self> { |
| 1521 | let allow_variadic = false; |
| 1522 | let attrs = input.call(Attribute::parse_outer)?; |
| 1523 | match parse_fn_arg_or_variadic(input, attrs, allow_variadic)? { |
| 1524 | FnArgOrVariadic::FnArg(arg) => Ok(arg), |
| 1525 | FnArgOrVariadic::Variadic(_) => unreachable!(), |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | enum FnArgOrVariadic { |
| 1531 | FnArg(FnArg), |
| 1532 | Variadic(Variadic), |
| 1533 | } |
| 1534 | |
| 1535 | fn parse_fn_arg_or_variadic( |
| 1536 | input: ParseStream, |
| 1537 | attrs: Vec<Attribute>, |
| 1538 | allow_variadic: bool, |
| 1539 | ) -> Result<FnArgOrVariadic> { |
| 1540 | let ahead = input.fork(); |
| 1541 | if let Ok(mut receiver) = ahead.parse::<Receiver>() { |
| 1542 | input.advance_to(&ahead); |
| 1543 | receiver.attrs = attrs; |
| 1544 | return Ok(FnArgOrVariadic::FnArg(FnArg::Receiver(receiver))); |
| 1545 | } |
| 1546 | |
| 1547 | // Hack to parse pre-2018 syntax in |
| 1548 | // test/ui/rfc-2565-param-attrs/param-attrs-pretty.rs |
| 1549 | // because the rest of the test case is valuable. |
| 1550 | if input.peek(Ident) && input.peek2(Token![<]) { |
| 1551 | let span = input.fork().parse::<Ident>()?.span(); |
| 1552 | return Ok(FnArgOrVariadic::FnArg(FnArg::Typed(PatType { |
| 1553 | attrs, |
| 1554 | pat: Box::new(Pat::Wild(PatWild { |
| 1555 | attrs: Vec::new(), |
| 1556 | underscore_token: Token, |
| 1557 | })), |
| 1558 | colon_token: Token, |
| 1559 | ty: input.parse()?, |
| 1560 | }))); |
| 1561 | } |
| 1562 | |
| 1563 | let pat = Box::new(Pat::parse_single(input)?); |
| 1564 | let colon_token: Token![:] = input.parse()?; |
| 1565 | |
| 1566 | if allow_variadic { |
| 1567 | if let Some(dots) = input.parse::<Option<Token![...]>>()? { |
| 1568 | return Ok(FnArgOrVariadic::Variadic(Variadic { |
| 1569 | attrs, |
| 1570 | pat: Some((pat, colon_token)), |
| 1571 | dots, |
| 1572 | comma: None, |
| 1573 | })); |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | Ok(FnArgOrVariadic::FnArg(FnArg::Typed(PatType { |
| 1578 | attrs, |
| 1579 | pat, |
| 1580 | colon_token, |
| 1581 | ty: input.parse()?, |
| 1582 | }))) |
| 1583 | } |
| 1584 | |
| 1585 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1586 | impl Parse for Receiver { |
| 1587 | fn parse(input: ParseStream) -> Result<Self> { |
| 1588 | let reference = if input.peek(Token![&]) { |
| 1589 | let ampersand: Token![&] = input.parse()?; |
| 1590 | let lifetime: Option<Lifetime> = input.parse()?; |
| 1591 | Some((ampersand, lifetime)) |
| 1592 | } else { |
| 1593 | None |
| 1594 | }; |
| 1595 | let mutability: Option<Token![mut]> = input.parse()?; |
| 1596 | let self_token: Token![self] = input.parse()?; |
| 1597 | let colon_token: Option<Token![:]> = if reference.is_some() { |
| 1598 | None |
| 1599 | } else { |
| 1600 | input.parse()? |
| 1601 | }; |
| 1602 | let ty: Type = if colon_token.is_some() { |
| 1603 | input.parse()? |
| 1604 | } else { |
| 1605 | let mut ty = Type::Path(TypePath { |
| 1606 | qself: None, |
| 1607 | path: Path::from(Ident::new("Self" , self_token.span)), |
| 1608 | }); |
| 1609 | if let Some((ampersand, lifetime)) = reference.as_ref() { |
| 1610 | ty = Type::Reference(TypeReference { |
| 1611 | and_token: Token, |
| 1612 | lifetime: lifetime.clone(), |
| 1613 | mutability: mutability.as_ref().map(|m| Token), |
| 1614 | elem: Box::new(ty), |
| 1615 | }); |
| 1616 | } |
| 1617 | ty |
| 1618 | }; |
| 1619 | Ok(Receiver { |
| 1620 | attrs: Vec::new(), |
| 1621 | reference, |
| 1622 | mutability, |
| 1623 | self_token, |
| 1624 | colon_token, |
| 1625 | ty: Box::new(ty), |
| 1626 | }) |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | fn parse_fn_args( |
| 1631 | input: ParseStream, |
| 1632 | ) -> Result<(Punctuated<FnArg, Token![,]>, Option<Variadic>)> { |
| 1633 | let mut args = Punctuated::new(); |
| 1634 | let mut variadic = None; |
| 1635 | let mut has_receiver = false; |
| 1636 | |
| 1637 | while !input.is_empty() { |
| 1638 | let attrs = input.call(Attribute::parse_outer)?; |
| 1639 | |
| 1640 | if let Some(dots) = input.parse::<Option<Token![...]>>()? { |
| 1641 | variadic = Some(Variadic { |
| 1642 | attrs, |
| 1643 | pat: None, |
| 1644 | dots, |
| 1645 | comma: if input.is_empty() { |
| 1646 | None |
| 1647 | } else { |
| 1648 | Some(input.parse()?) |
| 1649 | }, |
| 1650 | }); |
| 1651 | break; |
| 1652 | } |
| 1653 | |
| 1654 | let allow_variadic = true; |
| 1655 | let arg = match parse_fn_arg_or_variadic(input, attrs, allow_variadic)? { |
| 1656 | FnArgOrVariadic::FnArg(arg) => arg, |
| 1657 | FnArgOrVariadic::Variadic(arg) => { |
| 1658 | variadic = Some(Variadic { |
| 1659 | comma: if input.is_empty() { |
| 1660 | None |
| 1661 | } else { |
| 1662 | Some(input.parse()?) |
| 1663 | }, |
| 1664 | ..arg |
| 1665 | }); |
| 1666 | break; |
| 1667 | } |
| 1668 | }; |
| 1669 | |
| 1670 | match &arg { |
| 1671 | FnArg::Receiver(receiver) if has_receiver => { |
| 1672 | return Err(Error::new( |
| 1673 | receiver.self_token.span, |
| 1674 | "unexpected second method receiver" , |
| 1675 | )); |
| 1676 | } |
| 1677 | FnArg::Receiver(receiver) if !args.is_empty() => { |
| 1678 | return Err(Error::new( |
| 1679 | receiver.self_token.span, |
| 1680 | "unexpected method receiver" , |
| 1681 | )); |
| 1682 | } |
| 1683 | FnArg::Receiver(_) => has_receiver = true, |
| 1684 | FnArg::Typed(_) => {} |
| 1685 | } |
| 1686 | args.push_value(arg); |
| 1687 | |
| 1688 | if input.is_empty() { |
| 1689 | break; |
| 1690 | } |
| 1691 | |
| 1692 | let comma: Token![,] = input.parse()?; |
| 1693 | args.push_punct(comma); |
| 1694 | } |
| 1695 | |
| 1696 | Ok((args, variadic)) |
| 1697 | } |
| 1698 | |
| 1699 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1700 | impl Parse for ItemMod { |
| 1701 | fn parse(input: ParseStream) -> Result<Self> { |
| 1702 | let mut attrs = input.call(Attribute::parse_outer)?; |
| 1703 | let vis: Visibility = input.parse()?; |
| 1704 | let unsafety: Option<Token![unsafe]> = input.parse()?; |
| 1705 | let mod_token: Token![mod] = input.parse()?; |
| 1706 | let ident: Ident = if input.peek(Token![try]) { |
| 1707 | input.call(Ident::parse_any) |
| 1708 | } else { |
| 1709 | input.parse() |
| 1710 | }?; |
| 1711 | |
| 1712 | let lookahead = input.lookahead1(); |
| 1713 | if lookahead.peek(Token![;]) { |
| 1714 | Ok(ItemMod { |
| 1715 | attrs, |
| 1716 | vis, |
| 1717 | unsafety, |
| 1718 | mod_token, |
| 1719 | ident, |
| 1720 | content: None, |
| 1721 | semi: Some(input.parse()?), |
| 1722 | }) |
| 1723 | } else if lookahead.peek(token::Brace) { |
| 1724 | let content; |
| 1725 | let brace_token = braced!(content in input); |
| 1726 | attr::parsing::parse_inner(&content, &mut attrs)?; |
| 1727 | |
| 1728 | let mut items = Vec::new(); |
| 1729 | while !content.is_empty() { |
| 1730 | items.push(content.parse()?); |
| 1731 | } |
| 1732 | |
| 1733 | Ok(ItemMod { |
| 1734 | attrs, |
| 1735 | vis, |
| 1736 | unsafety, |
| 1737 | mod_token, |
| 1738 | ident, |
| 1739 | content: Some((brace_token, items)), |
| 1740 | semi: None, |
| 1741 | }) |
| 1742 | } else { |
| 1743 | Err(lookahead.error()) |
| 1744 | } |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1749 | impl Parse for ItemForeignMod { |
| 1750 | fn parse(input: ParseStream) -> Result<Self> { |
| 1751 | let mut attrs = input.call(Attribute::parse_outer)?; |
| 1752 | let unsafety: Option<Token![unsafe]> = input.parse()?; |
| 1753 | let abi: Abi = input.parse()?; |
| 1754 | |
| 1755 | let content; |
| 1756 | let brace_token = braced!(content in input); |
| 1757 | attr::parsing::parse_inner(&content, &mut attrs)?; |
| 1758 | let mut items = Vec::new(); |
| 1759 | while !content.is_empty() { |
| 1760 | items.push(content.parse()?); |
| 1761 | } |
| 1762 | |
| 1763 | Ok(ItemForeignMod { |
| 1764 | attrs, |
| 1765 | unsafety, |
| 1766 | abi, |
| 1767 | brace_token, |
| 1768 | items, |
| 1769 | }) |
| 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1774 | impl Parse for ForeignItem { |
| 1775 | fn parse(input: ParseStream) -> Result<Self> { |
| 1776 | let begin = input.fork(); |
| 1777 | let mut attrs = input.call(Attribute::parse_outer)?; |
| 1778 | let ahead = input.fork(); |
| 1779 | let vis: Visibility = ahead.parse()?; |
| 1780 | |
| 1781 | let lookahead = ahead.lookahead1(); |
| 1782 | let mut item = if lookahead.peek(Token![fn]) || peek_signature(&ahead) { |
| 1783 | let vis: Visibility = input.parse()?; |
| 1784 | let sig: Signature = input.parse()?; |
| 1785 | if input.peek(token::Brace) { |
| 1786 | let content; |
| 1787 | braced!(content in input); |
| 1788 | content.call(Attribute::parse_inner)?; |
| 1789 | content.call(Block::parse_within)?; |
| 1790 | |
| 1791 | Ok(ForeignItem::Verbatim(verbatim::between(&begin, input))) |
| 1792 | } else { |
| 1793 | Ok(ForeignItem::Fn(ForeignItemFn { |
| 1794 | attrs: Vec::new(), |
| 1795 | vis, |
| 1796 | sig, |
| 1797 | semi_token: input.parse()?, |
| 1798 | })) |
| 1799 | } |
| 1800 | } else if lookahead.peek(Token![static]) { |
| 1801 | let vis = input.parse()?; |
| 1802 | let static_token = input.parse()?; |
| 1803 | let mutability = input.parse()?; |
| 1804 | let ident = input.parse()?; |
| 1805 | let colon_token = input.parse()?; |
| 1806 | let ty = input.parse()?; |
| 1807 | if input.peek(Token![=]) { |
| 1808 | input.parse::<Token![=]>()?; |
| 1809 | input.parse::<Expr>()?; |
| 1810 | input.parse::<Token![;]>()?; |
| 1811 | Ok(ForeignItem::Verbatim(verbatim::between(&begin, input))) |
| 1812 | } else { |
| 1813 | Ok(ForeignItem::Static(ForeignItemStatic { |
| 1814 | attrs: Vec::new(), |
| 1815 | vis, |
| 1816 | static_token, |
| 1817 | mutability, |
| 1818 | ident, |
| 1819 | colon_token, |
| 1820 | ty, |
| 1821 | semi_token: input.parse()?, |
| 1822 | })) |
| 1823 | } |
| 1824 | } else if lookahead.peek(Token![type]) { |
| 1825 | parse_foreign_item_type(begin, input) |
| 1826 | } else if vis.is_inherited() |
| 1827 | && (lookahead.peek(Ident) |
| 1828 | || lookahead.peek(Token![self]) |
| 1829 | || lookahead.peek(Token![super]) |
| 1830 | || lookahead.peek(Token![crate]) |
| 1831 | || lookahead.peek(Token![::])) |
| 1832 | { |
| 1833 | input.parse().map(ForeignItem::Macro) |
| 1834 | } else { |
| 1835 | Err(lookahead.error()) |
| 1836 | }?; |
| 1837 | |
| 1838 | let item_attrs = match &mut item { |
| 1839 | ForeignItem::Fn(item) => &mut item.attrs, |
| 1840 | ForeignItem::Static(item) => &mut item.attrs, |
| 1841 | ForeignItem::Type(item) => &mut item.attrs, |
| 1842 | ForeignItem::Macro(item) => &mut item.attrs, |
| 1843 | ForeignItem::Verbatim(_) => return Ok(item), |
| 1844 | }; |
| 1845 | attrs.append(item_attrs); |
| 1846 | *item_attrs = attrs; |
| 1847 | |
| 1848 | Ok(item) |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1853 | impl Parse for ForeignItemFn { |
| 1854 | fn parse(input: ParseStream) -> Result<Self> { |
| 1855 | let attrs = input.call(Attribute::parse_outer)?; |
| 1856 | let vis: Visibility = input.parse()?; |
| 1857 | let sig: Signature = input.parse()?; |
| 1858 | let semi_token: Token![;] = input.parse()?; |
| 1859 | Ok(ForeignItemFn { |
| 1860 | attrs, |
| 1861 | vis, |
| 1862 | sig, |
| 1863 | semi_token, |
| 1864 | }) |
| 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1869 | impl Parse for ForeignItemStatic { |
| 1870 | fn parse(input: ParseStream) -> Result<Self> { |
| 1871 | Ok(ForeignItemStatic { |
| 1872 | attrs: input.call(Attribute::parse_outer)?, |
| 1873 | vis: input.parse()?, |
| 1874 | static_token: input.parse()?, |
| 1875 | mutability: input.parse()?, |
| 1876 | ident: input.parse()?, |
| 1877 | colon_token: input.parse()?, |
| 1878 | ty: input.parse()?, |
| 1879 | semi_token: input.parse()?, |
| 1880 | }) |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1885 | impl Parse for ForeignItemType { |
| 1886 | fn parse(input: ParseStream) -> Result<Self> { |
| 1887 | Ok(ForeignItemType { |
| 1888 | attrs: input.call(Attribute::parse_outer)?, |
| 1889 | vis: input.parse()?, |
| 1890 | type_token: input.parse()?, |
| 1891 | ident: input.parse()?, |
| 1892 | generics: { |
| 1893 | let mut generics: Generics = input.parse()?; |
| 1894 | generics.where_clause = input.parse()?; |
| 1895 | generics |
| 1896 | }, |
| 1897 | semi_token: input.parse()?, |
| 1898 | }) |
| 1899 | } |
| 1900 | } |
| 1901 | |
| 1902 | fn parse_foreign_item_type(begin: ParseBuffer, input: ParseStream) -> Result<ForeignItem> { |
| 1903 | let FlexibleItemType { |
| 1904 | vis, |
| 1905 | defaultness: _, |
| 1906 | type_token, |
| 1907 | ident, |
| 1908 | generics, |
| 1909 | colon_token, |
| 1910 | bounds: _, |
| 1911 | ty, |
| 1912 | semi_token, |
| 1913 | } = FlexibleItemType::parse( |
| 1914 | input, |
| 1915 | TypeDefaultness::Disallowed, |
| 1916 | WhereClauseLocation::Both, |
| 1917 | )?; |
| 1918 | |
| 1919 | if colon_token.is_some() || ty.is_some() { |
| 1920 | Ok(ForeignItem::Verbatim(verbatim::between(&begin, input))) |
| 1921 | } else { |
| 1922 | Ok(ForeignItem::Type(ForeignItemType { |
| 1923 | attrs: Vec::new(), |
| 1924 | vis, |
| 1925 | type_token, |
| 1926 | ident, |
| 1927 | generics, |
| 1928 | semi_token, |
| 1929 | })) |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1934 | impl Parse for ForeignItemMacro { |
| 1935 | fn parse(input: ParseStream) -> Result<Self> { |
| 1936 | let attrs = input.call(Attribute::parse_outer)?; |
| 1937 | let mac: Macro = input.parse()?; |
| 1938 | let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() { |
| 1939 | None |
| 1940 | } else { |
| 1941 | Some(input.parse()?) |
| 1942 | }; |
| 1943 | Ok(ForeignItemMacro { |
| 1944 | attrs, |
| 1945 | mac, |
| 1946 | semi_token, |
| 1947 | }) |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 1952 | impl Parse for ItemType { |
| 1953 | fn parse(input: ParseStream) -> Result<Self> { |
| 1954 | Ok(ItemType { |
| 1955 | attrs: input.call(Attribute::parse_outer)?, |
| 1956 | vis: input.parse()?, |
| 1957 | type_token: input.parse()?, |
| 1958 | ident: input.parse()?, |
| 1959 | generics: { |
| 1960 | let mut generics: Generics = input.parse()?; |
| 1961 | generics.where_clause = input.parse()?; |
| 1962 | generics |
| 1963 | }, |
| 1964 | eq_token: input.parse()?, |
| 1965 | ty: input.parse()?, |
| 1966 | semi_token: input.parse()?, |
| 1967 | }) |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | fn parse_item_type(begin: ParseBuffer, input: ParseStream) -> Result<Item> { |
| 1972 | let FlexibleItemType { |
| 1973 | vis, |
| 1974 | defaultness: _, |
| 1975 | type_token, |
| 1976 | ident, |
| 1977 | generics, |
| 1978 | colon_token, |
| 1979 | bounds: _, |
| 1980 | ty, |
| 1981 | semi_token, |
| 1982 | } = FlexibleItemType::parse( |
| 1983 | input, |
| 1984 | TypeDefaultness::Disallowed, |
| 1985 | WhereClauseLocation::BeforeEq, |
| 1986 | )?; |
| 1987 | |
| 1988 | let (eq_token, ty) = match ty { |
| 1989 | Some(ty) if colon_token.is_none() => ty, |
| 1990 | _ => return Ok(Item::Verbatim(verbatim::between(&begin, input))), |
| 1991 | }; |
| 1992 | |
| 1993 | Ok(Item::Type(ItemType { |
| 1994 | attrs: Vec::new(), |
| 1995 | vis, |
| 1996 | type_token, |
| 1997 | ident, |
| 1998 | generics, |
| 1999 | eq_token, |
| 2000 | ty: Box::new(ty), |
| 2001 | semi_token, |
| 2002 | })) |
| 2003 | } |
| 2004 | |
| 2005 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2006 | impl Parse for ItemStruct { |
| 2007 | fn parse(input: ParseStream) -> Result<Self> { |
| 2008 | let attrs = input.call(Attribute::parse_outer)?; |
| 2009 | let vis = input.parse::<Visibility>()?; |
| 2010 | let struct_token = input.parse::<Token![struct]>()?; |
| 2011 | let ident = input.parse::<Ident>()?; |
| 2012 | let generics = input.parse::<Generics>()?; |
| 2013 | let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?; |
| 2014 | Ok(ItemStruct { |
| 2015 | attrs, |
| 2016 | vis, |
| 2017 | struct_token, |
| 2018 | ident, |
| 2019 | generics: Generics { |
| 2020 | where_clause, |
| 2021 | ..generics |
| 2022 | }, |
| 2023 | fields, |
| 2024 | semi_token, |
| 2025 | }) |
| 2026 | } |
| 2027 | } |
| 2028 | |
| 2029 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2030 | impl Parse for ItemEnum { |
| 2031 | fn parse(input: ParseStream) -> Result<Self> { |
| 2032 | let attrs = input.call(Attribute::parse_outer)?; |
| 2033 | let vis = input.parse::<Visibility>()?; |
| 2034 | let enum_token = input.parse::<Token![enum]>()?; |
| 2035 | let ident = input.parse::<Ident>()?; |
| 2036 | let generics = input.parse::<Generics>()?; |
| 2037 | let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?; |
| 2038 | Ok(ItemEnum { |
| 2039 | attrs, |
| 2040 | vis, |
| 2041 | enum_token, |
| 2042 | ident, |
| 2043 | generics: Generics { |
| 2044 | where_clause, |
| 2045 | ..generics |
| 2046 | }, |
| 2047 | brace_token, |
| 2048 | variants, |
| 2049 | }) |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2054 | impl Parse for ItemUnion { |
| 2055 | fn parse(input: ParseStream) -> Result<Self> { |
| 2056 | let attrs = input.call(Attribute::parse_outer)?; |
| 2057 | let vis = input.parse::<Visibility>()?; |
| 2058 | let union_token = input.parse::<Token![union]>()?; |
| 2059 | let ident = input.parse::<Ident>()?; |
| 2060 | let generics = input.parse::<Generics>()?; |
| 2061 | let (where_clause, fields) = derive::parsing::data_union(input)?; |
| 2062 | Ok(ItemUnion { |
| 2063 | attrs, |
| 2064 | vis, |
| 2065 | union_token, |
| 2066 | ident, |
| 2067 | generics: Generics { |
| 2068 | where_clause, |
| 2069 | ..generics |
| 2070 | }, |
| 2071 | fields, |
| 2072 | }) |
| 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | fn parse_trait_or_trait_alias(input: ParseStream) -> Result<Item> { |
| 2077 | let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?; |
| 2078 | let lookahead = input.lookahead1(); |
| 2079 | if lookahead.peek(token::Brace) |
| 2080 | || lookahead.peek(Token![:]) |
| 2081 | || lookahead.peek(Token![where]) |
| 2082 | { |
| 2083 | let unsafety = None; |
| 2084 | let auto_token = None; |
| 2085 | parse_rest_of_trait( |
| 2086 | input, |
| 2087 | attrs, |
| 2088 | vis, |
| 2089 | unsafety, |
| 2090 | auto_token, |
| 2091 | trait_token, |
| 2092 | ident, |
| 2093 | generics, |
| 2094 | ) |
| 2095 | .map(Item::Trait) |
| 2096 | } else if lookahead.peek(Token![=]) { |
| 2097 | parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics) |
| 2098 | .map(Item::TraitAlias) |
| 2099 | } else { |
| 2100 | Err(lookahead.error()) |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2105 | impl Parse for ItemTrait { |
| 2106 | fn parse(input: ParseStream) -> Result<Self> { |
| 2107 | let outer_attrs = input.call(Attribute::parse_outer)?; |
| 2108 | let vis: Visibility = input.parse()?; |
| 2109 | let unsafety: Option<Token![unsafe]> = input.parse()?; |
| 2110 | let auto_token: Option<Token![auto]> = input.parse()?; |
| 2111 | let trait_token: Token![trait] = input.parse()?; |
| 2112 | let ident: Ident = input.parse()?; |
| 2113 | let generics: Generics = input.parse()?; |
| 2114 | parse_rest_of_trait( |
| 2115 | input, |
| 2116 | outer_attrs, |
| 2117 | vis, |
| 2118 | unsafety, |
| 2119 | auto_token, |
| 2120 | trait_token, |
| 2121 | ident, |
| 2122 | generics, |
| 2123 | ) |
| 2124 | } |
| 2125 | } |
| 2126 | |
| 2127 | fn parse_rest_of_trait( |
| 2128 | input: ParseStream, |
| 2129 | mut attrs: Vec<Attribute>, |
| 2130 | vis: Visibility, |
| 2131 | unsafety: Option<Token![unsafe]>, |
| 2132 | auto_token: Option<Token![auto]>, |
| 2133 | trait_token: Token![trait], |
| 2134 | ident: Ident, |
| 2135 | mut generics: Generics, |
| 2136 | ) -> Result<ItemTrait> { |
| 2137 | let colon_token: Option<Token![:]> = input.parse()?; |
| 2138 | |
| 2139 | let mut supertraits = Punctuated::new(); |
| 2140 | if colon_token.is_some() { |
| 2141 | loop { |
| 2142 | if input.peek(Token![where]) || input.peek(token::Brace) { |
| 2143 | break; |
| 2144 | } |
| 2145 | supertraits.push_value(input.parse()?); |
| 2146 | if input.peek(Token![where]) || input.peek(token::Brace) { |
| 2147 | break; |
| 2148 | } |
| 2149 | supertraits.push_punct(input.parse()?); |
| 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | generics.where_clause = input.parse()?; |
| 2154 | |
| 2155 | let content; |
| 2156 | let brace_token = braced!(content in input); |
| 2157 | attr::parsing::parse_inner(&content, &mut attrs)?; |
| 2158 | let mut items = Vec::new(); |
| 2159 | while !content.is_empty() { |
| 2160 | items.push(content.parse()?); |
| 2161 | } |
| 2162 | |
| 2163 | Ok(ItemTrait { |
| 2164 | attrs, |
| 2165 | vis, |
| 2166 | unsafety, |
| 2167 | auto_token, |
| 2168 | restriction: None, |
| 2169 | trait_token, |
| 2170 | ident, |
| 2171 | generics, |
| 2172 | colon_token, |
| 2173 | supertraits, |
| 2174 | brace_token, |
| 2175 | items, |
| 2176 | }) |
| 2177 | } |
| 2178 | |
| 2179 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2180 | impl Parse for ItemTraitAlias { |
| 2181 | fn parse(input: ParseStream) -> Result<Self> { |
| 2182 | let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?; |
| 2183 | parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics) |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | fn parse_start_of_trait_alias( |
| 2188 | input: ParseStream, |
| 2189 | ) -> Result<(Vec<Attribute>, Visibility, Token![trait], Ident, Generics)> { |
| 2190 | let attrs = input.call(Attribute::parse_outer)?; |
| 2191 | let vis: Visibility = input.parse()?; |
| 2192 | let trait_token: Token![trait] = input.parse()?; |
| 2193 | let ident: Ident = input.parse()?; |
| 2194 | let generics: Generics = input.parse()?; |
| 2195 | Ok((attrs, vis, trait_token, ident, generics)) |
| 2196 | } |
| 2197 | |
| 2198 | fn parse_rest_of_trait_alias( |
| 2199 | input: ParseStream, |
| 2200 | attrs: Vec<Attribute>, |
| 2201 | vis: Visibility, |
| 2202 | trait_token: Token![trait], |
| 2203 | ident: Ident, |
| 2204 | mut generics: Generics, |
| 2205 | ) -> Result<ItemTraitAlias> { |
| 2206 | let eq_token: Token![=] = input.parse()?; |
| 2207 | |
| 2208 | let mut bounds = Punctuated::new(); |
| 2209 | loop { |
| 2210 | if input.peek(Token![where]) || input.peek(Token![;]) { |
| 2211 | break; |
| 2212 | } |
| 2213 | bounds.push_value(input.parse()?); |
| 2214 | if input.peek(Token![where]) || input.peek(Token![;]) { |
| 2215 | break; |
| 2216 | } |
| 2217 | bounds.push_punct(input.parse()?); |
| 2218 | } |
| 2219 | |
| 2220 | generics.where_clause = input.parse()?; |
| 2221 | let semi_token: Token![;] = input.parse()?; |
| 2222 | |
| 2223 | Ok(ItemTraitAlias { |
| 2224 | attrs, |
| 2225 | vis, |
| 2226 | trait_token, |
| 2227 | ident, |
| 2228 | generics, |
| 2229 | eq_token, |
| 2230 | bounds, |
| 2231 | semi_token, |
| 2232 | }) |
| 2233 | } |
| 2234 | |
| 2235 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2236 | impl Parse for TraitItem { |
| 2237 | fn parse(input: ParseStream) -> Result<Self> { |
| 2238 | let begin = input.fork(); |
| 2239 | let mut attrs = input.call(Attribute::parse_outer)?; |
| 2240 | let vis: Visibility = input.parse()?; |
| 2241 | let defaultness: Option<Token![default]> = input.parse()?; |
| 2242 | let ahead = input.fork(); |
| 2243 | |
| 2244 | let lookahead = ahead.lookahead1(); |
| 2245 | let mut item = if lookahead.peek(Token![fn]) || peek_signature(&ahead) { |
| 2246 | input.parse().map(TraitItem::Fn) |
| 2247 | } else if lookahead.peek(Token![const]) { |
| 2248 | let const_token: Token![const] = ahead.parse()?; |
| 2249 | let lookahead = ahead.lookahead1(); |
| 2250 | if lookahead.peek(Ident) || lookahead.peek(Token![_]) { |
| 2251 | input.advance_to(&ahead); |
| 2252 | let ident = input.call(Ident::parse_any)?; |
| 2253 | let mut generics: Generics = input.parse()?; |
| 2254 | let colon_token: Token![:] = input.parse()?; |
| 2255 | let ty: Type = input.parse()?; |
| 2256 | let default = if let Some(eq_token) = input.parse::<Option<Token![=]>>()? { |
| 2257 | let expr: Expr = input.parse()?; |
| 2258 | Some((eq_token, expr)) |
| 2259 | } else { |
| 2260 | None |
| 2261 | }; |
| 2262 | generics.where_clause = input.parse()?; |
| 2263 | let semi_token: Token![;] = input.parse()?; |
| 2264 | if generics.lt_token.is_none() && generics.where_clause.is_none() { |
| 2265 | Ok(TraitItem::Const(TraitItemConst { |
| 2266 | attrs: Vec::new(), |
| 2267 | const_token, |
| 2268 | ident, |
| 2269 | generics, |
| 2270 | colon_token, |
| 2271 | ty, |
| 2272 | default, |
| 2273 | semi_token, |
| 2274 | })) |
| 2275 | } else { |
| 2276 | return Ok(TraitItem::Verbatim(verbatim::between(&begin, input))); |
| 2277 | } |
| 2278 | } else if lookahead.peek(Token![async]) |
| 2279 | || lookahead.peek(Token![unsafe]) |
| 2280 | || lookahead.peek(Token![extern]) |
| 2281 | || lookahead.peek(Token![fn]) |
| 2282 | { |
| 2283 | input.parse().map(TraitItem::Fn) |
| 2284 | } else { |
| 2285 | Err(lookahead.error()) |
| 2286 | } |
| 2287 | } else if lookahead.peek(Token![type]) { |
| 2288 | parse_trait_item_type(begin.fork(), input) |
| 2289 | } else if vis.is_inherited() |
| 2290 | && defaultness.is_none() |
| 2291 | && (lookahead.peek(Ident) |
| 2292 | || lookahead.peek(Token![self]) |
| 2293 | || lookahead.peek(Token![super]) |
| 2294 | || lookahead.peek(Token![crate]) |
| 2295 | || lookahead.peek(Token![::])) |
| 2296 | { |
| 2297 | input.parse().map(TraitItem::Macro) |
| 2298 | } else { |
| 2299 | Err(lookahead.error()) |
| 2300 | }?; |
| 2301 | |
| 2302 | match (vis, defaultness) { |
| 2303 | (Visibility::Inherited, None) => {} |
| 2304 | _ => return Ok(TraitItem::Verbatim(verbatim::between(&begin, input))), |
| 2305 | } |
| 2306 | |
| 2307 | let item_attrs = match &mut item { |
| 2308 | TraitItem::Const(item) => &mut item.attrs, |
| 2309 | TraitItem::Fn(item) => &mut item.attrs, |
| 2310 | TraitItem::Type(item) => &mut item.attrs, |
| 2311 | TraitItem::Macro(item) => &mut item.attrs, |
| 2312 | TraitItem::Verbatim(_) => unreachable!(), |
| 2313 | }; |
| 2314 | attrs.append(item_attrs); |
| 2315 | *item_attrs = attrs; |
| 2316 | Ok(item) |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2321 | impl Parse for TraitItemConst { |
| 2322 | fn parse(input: ParseStream) -> Result<Self> { |
| 2323 | let attrs = input.call(Attribute::parse_outer)?; |
| 2324 | let const_token: Token![const] = input.parse()?; |
| 2325 | |
| 2326 | let lookahead = input.lookahead1(); |
| 2327 | let ident = if lookahead.peek(Ident) || lookahead.peek(Token![_]) { |
| 2328 | input.call(Ident::parse_any)? |
| 2329 | } else { |
| 2330 | return Err(lookahead.error()); |
| 2331 | }; |
| 2332 | |
| 2333 | let colon_token: Token![:] = input.parse()?; |
| 2334 | let ty: Type = input.parse()?; |
| 2335 | let default = if input.peek(Token![=]) { |
| 2336 | let eq_token: Token![=] = input.parse()?; |
| 2337 | let default: Expr = input.parse()?; |
| 2338 | Some((eq_token, default)) |
| 2339 | } else { |
| 2340 | None |
| 2341 | }; |
| 2342 | let semi_token: Token![;] = input.parse()?; |
| 2343 | |
| 2344 | Ok(TraitItemConst { |
| 2345 | attrs, |
| 2346 | const_token, |
| 2347 | ident, |
| 2348 | generics: Generics::default(), |
| 2349 | colon_token, |
| 2350 | ty, |
| 2351 | default, |
| 2352 | semi_token, |
| 2353 | }) |
| 2354 | } |
| 2355 | } |
| 2356 | |
| 2357 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2358 | impl Parse for TraitItemFn { |
| 2359 | fn parse(input: ParseStream) -> Result<Self> { |
| 2360 | let mut attrs = input.call(Attribute::parse_outer)?; |
| 2361 | let sig: Signature = input.parse()?; |
| 2362 | |
| 2363 | let lookahead = input.lookahead1(); |
| 2364 | let (brace_token, stmts, semi_token) = if lookahead.peek(token::Brace) { |
| 2365 | let content; |
| 2366 | let brace_token = braced!(content in input); |
| 2367 | attr::parsing::parse_inner(&content, &mut attrs)?; |
| 2368 | let stmts = content.call(Block::parse_within)?; |
| 2369 | (Some(brace_token), stmts, None) |
| 2370 | } else if lookahead.peek(Token![;]) { |
| 2371 | let semi_token: Token![;] = input.parse()?; |
| 2372 | (None, Vec::new(), Some(semi_token)) |
| 2373 | } else { |
| 2374 | return Err(lookahead.error()); |
| 2375 | }; |
| 2376 | |
| 2377 | Ok(TraitItemFn { |
| 2378 | attrs, |
| 2379 | sig, |
| 2380 | default: brace_token.map(|brace_token| Block { brace_token, stmts }), |
| 2381 | semi_token, |
| 2382 | }) |
| 2383 | } |
| 2384 | } |
| 2385 | |
| 2386 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2387 | impl Parse for TraitItemType { |
| 2388 | fn parse(input: ParseStream) -> Result<Self> { |
| 2389 | let attrs = input.call(Attribute::parse_outer)?; |
| 2390 | let type_token: Token![type] = input.parse()?; |
| 2391 | let ident: Ident = input.parse()?; |
| 2392 | let mut generics: Generics = input.parse()?; |
| 2393 | let (colon_token, bounds) = FlexibleItemType::parse_optional_bounds(input)?; |
| 2394 | let default = FlexibleItemType::parse_optional_definition(input)?; |
| 2395 | generics.where_clause = input.parse()?; |
| 2396 | let semi_token: Token![;] = input.parse()?; |
| 2397 | Ok(TraitItemType { |
| 2398 | attrs, |
| 2399 | type_token, |
| 2400 | ident, |
| 2401 | generics, |
| 2402 | colon_token, |
| 2403 | bounds, |
| 2404 | default, |
| 2405 | semi_token, |
| 2406 | }) |
| 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | fn parse_trait_item_type(begin: ParseBuffer, input: ParseStream) -> Result<TraitItem> { |
| 2411 | let FlexibleItemType { |
| 2412 | vis, |
| 2413 | defaultness: _, |
| 2414 | type_token, |
| 2415 | ident, |
| 2416 | generics, |
| 2417 | colon_token, |
| 2418 | bounds, |
| 2419 | ty, |
| 2420 | semi_token, |
| 2421 | } = FlexibleItemType::parse( |
| 2422 | input, |
| 2423 | TypeDefaultness::Disallowed, |
| 2424 | WhereClauseLocation::AfterEq, |
| 2425 | )?; |
| 2426 | |
| 2427 | if vis.is_some() { |
| 2428 | Ok(TraitItem::Verbatim(verbatim::between(&begin, input))) |
| 2429 | } else { |
| 2430 | Ok(TraitItem::Type(TraitItemType { |
| 2431 | attrs: Vec::new(), |
| 2432 | type_token, |
| 2433 | ident, |
| 2434 | generics, |
| 2435 | colon_token, |
| 2436 | bounds, |
| 2437 | default: ty, |
| 2438 | semi_token, |
| 2439 | })) |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2444 | impl Parse for TraitItemMacro { |
| 2445 | fn parse(input: ParseStream) -> Result<Self> { |
| 2446 | let attrs = input.call(Attribute::parse_outer)?; |
| 2447 | let mac: Macro = input.parse()?; |
| 2448 | let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() { |
| 2449 | None |
| 2450 | } else { |
| 2451 | Some(input.parse()?) |
| 2452 | }; |
| 2453 | Ok(TraitItemMacro { |
| 2454 | attrs, |
| 2455 | mac, |
| 2456 | semi_token, |
| 2457 | }) |
| 2458 | } |
| 2459 | } |
| 2460 | |
| 2461 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2462 | impl Parse for ItemImpl { |
| 2463 | fn parse(input: ParseStream) -> Result<Self> { |
| 2464 | let allow_verbatim_impl = false; |
| 2465 | parse_impl(input, allow_verbatim_impl).map(Option::unwrap) |
| 2466 | } |
| 2467 | } |
| 2468 | |
| 2469 | fn parse_impl(input: ParseStream, allow_verbatim_impl: bool) -> Result<Option<ItemImpl>> { |
| 2470 | let mut attrs = input.call(Attribute::parse_outer)?; |
| 2471 | let has_visibility = allow_verbatim_impl && input.parse::<Visibility>()?.is_some(); |
| 2472 | let defaultness: Option<Token![default]> = input.parse()?; |
| 2473 | let unsafety: Option<Token![unsafe]> = input.parse()?; |
| 2474 | let impl_token: Token![impl] = input.parse()?; |
| 2475 | |
| 2476 | let has_generics = input.peek(Token![<]) |
| 2477 | && (input.peek2(Token![>]) |
| 2478 | || input.peek2(Token![#]) |
| 2479 | || (input.peek2(Ident) || input.peek2(Lifetime)) |
| 2480 | && (input.peek3(Token![:]) |
| 2481 | || input.peek3(Token![,]) |
| 2482 | || input.peek3(Token![>]) |
| 2483 | || input.peek3(Token![=])) |
| 2484 | || input.peek2(Token![const])); |
| 2485 | let mut generics: Generics = if has_generics { |
| 2486 | input.parse()? |
| 2487 | } else { |
| 2488 | Generics::default() |
| 2489 | }; |
| 2490 | |
| 2491 | let is_const_impl = allow_verbatim_impl |
| 2492 | && (input.peek(Token![const]) || input.peek(Token![?]) && input.peek2(Token![const])); |
| 2493 | if is_const_impl { |
| 2494 | input.parse::<Option<Token![?]>>()?; |
| 2495 | input.parse::<Token![const]>()?; |
| 2496 | } |
| 2497 | |
| 2498 | let begin = input.fork(); |
| 2499 | let polarity = if input.peek(Token![!]) && !input.peek2(token::Brace) { |
| 2500 | Some(input.parse::<Token![!]>()?) |
| 2501 | } else { |
| 2502 | None |
| 2503 | }; |
| 2504 | |
| 2505 | #[cfg (not(feature = "printing" ))] |
| 2506 | let first_ty_span = input.span(); |
| 2507 | let mut first_ty: Type = input.parse()?; |
| 2508 | let self_ty: Type; |
| 2509 | let trait_; |
| 2510 | |
| 2511 | let is_impl_for = input.peek(Token![for]); |
| 2512 | if is_impl_for { |
| 2513 | let for_token: Token![for] = input.parse()?; |
| 2514 | let mut first_ty_ref = &first_ty; |
| 2515 | while let Type::Group(ty) = first_ty_ref { |
| 2516 | first_ty_ref = &ty.elem; |
| 2517 | } |
| 2518 | if let Type::Path(TypePath { qself: None, .. }) = first_ty_ref { |
| 2519 | while let Type::Group(ty) = first_ty { |
| 2520 | first_ty = *ty.elem; |
| 2521 | } |
| 2522 | if let Type::Path(TypePath { qself: None, path }) = first_ty { |
| 2523 | trait_ = Some((polarity, path, for_token)); |
| 2524 | } else { |
| 2525 | unreachable!(); |
| 2526 | } |
| 2527 | } else if !allow_verbatim_impl { |
| 2528 | #[cfg (feature = "printing" )] |
| 2529 | return Err(Error::new_spanned(first_ty_ref, "expected trait path" )); |
| 2530 | #[cfg (not(feature = "printing" ))] |
| 2531 | return Err(Error::new(first_ty_span, "expected trait path" )); |
| 2532 | } else { |
| 2533 | trait_ = None; |
| 2534 | } |
| 2535 | self_ty = input.parse()?; |
| 2536 | } else { |
| 2537 | trait_ = None; |
| 2538 | self_ty = if polarity.is_none() { |
| 2539 | first_ty |
| 2540 | } else { |
| 2541 | Type::Verbatim(verbatim::between(&begin, input)) |
| 2542 | }; |
| 2543 | } |
| 2544 | |
| 2545 | generics.where_clause = input.parse()?; |
| 2546 | |
| 2547 | let content; |
| 2548 | let brace_token = braced!(content in input); |
| 2549 | attr::parsing::parse_inner(&content, &mut attrs)?; |
| 2550 | |
| 2551 | let mut items = Vec::new(); |
| 2552 | while !content.is_empty() { |
| 2553 | items.push(content.parse()?); |
| 2554 | } |
| 2555 | |
| 2556 | if has_visibility || is_const_impl || is_impl_for && trait_.is_none() { |
| 2557 | Ok(None) |
| 2558 | } else { |
| 2559 | Ok(Some(ItemImpl { |
| 2560 | attrs, |
| 2561 | defaultness, |
| 2562 | unsafety, |
| 2563 | impl_token, |
| 2564 | generics, |
| 2565 | trait_, |
| 2566 | self_ty: Box::new(self_ty), |
| 2567 | brace_token, |
| 2568 | items, |
| 2569 | })) |
| 2570 | } |
| 2571 | } |
| 2572 | |
| 2573 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2574 | impl Parse for ImplItem { |
| 2575 | fn parse(input: ParseStream) -> Result<Self> { |
| 2576 | let begin = input.fork(); |
| 2577 | let mut attrs = input.call(Attribute::parse_outer)?; |
| 2578 | let ahead = input.fork(); |
| 2579 | let vis: Visibility = ahead.parse()?; |
| 2580 | |
| 2581 | let mut lookahead = ahead.lookahead1(); |
| 2582 | let defaultness = if lookahead.peek(Token![default]) && !ahead.peek2(Token![!]) { |
| 2583 | let defaultness: Token![default] = ahead.parse()?; |
| 2584 | lookahead = ahead.lookahead1(); |
| 2585 | Some(defaultness) |
| 2586 | } else { |
| 2587 | None |
| 2588 | }; |
| 2589 | |
| 2590 | let mut item = if lookahead.peek(Token![fn]) || peek_signature(&ahead) { |
| 2591 | let allow_omitted_body = true; |
| 2592 | if let Some(item) = parse_impl_item_fn(input, allow_omitted_body)? { |
| 2593 | Ok(ImplItem::Fn(item)) |
| 2594 | } else { |
| 2595 | Ok(ImplItem::Verbatim(verbatim::between(&begin, input))) |
| 2596 | } |
| 2597 | } else if lookahead.peek(Token![const]) { |
| 2598 | input.advance_to(&ahead); |
| 2599 | let const_token: Token![const] = input.parse()?; |
| 2600 | let lookahead = input.lookahead1(); |
| 2601 | let ident = if lookahead.peek(Ident) || lookahead.peek(Token![_]) { |
| 2602 | input.call(Ident::parse_any)? |
| 2603 | } else { |
| 2604 | return Err(lookahead.error()); |
| 2605 | }; |
| 2606 | let mut generics: Generics = input.parse()?; |
| 2607 | let colon_token: Token![:] = input.parse()?; |
| 2608 | let ty: Type = input.parse()?; |
| 2609 | let value = if let Some(eq_token) = input.parse::<Option<Token![=]>>()? { |
| 2610 | let expr: Expr = input.parse()?; |
| 2611 | Some((eq_token, expr)) |
| 2612 | } else { |
| 2613 | None |
| 2614 | }; |
| 2615 | generics.where_clause = input.parse()?; |
| 2616 | let semi_token: Token![;] = input.parse()?; |
| 2617 | return match value { |
| 2618 | Some((eq_token, expr)) |
| 2619 | if generics.lt_token.is_none() && generics.where_clause.is_none() => |
| 2620 | { |
| 2621 | Ok(ImplItem::Const(ImplItemConst { |
| 2622 | attrs, |
| 2623 | vis, |
| 2624 | defaultness, |
| 2625 | const_token, |
| 2626 | ident, |
| 2627 | generics, |
| 2628 | colon_token, |
| 2629 | ty, |
| 2630 | eq_token, |
| 2631 | expr, |
| 2632 | semi_token, |
| 2633 | })) |
| 2634 | } |
| 2635 | _ => Ok(ImplItem::Verbatim(verbatim::between(&begin, input))), |
| 2636 | }; |
| 2637 | } else if lookahead.peek(Token![type]) { |
| 2638 | parse_impl_item_type(begin, input) |
| 2639 | } else if vis.is_inherited() |
| 2640 | && defaultness.is_none() |
| 2641 | && (lookahead.peek(Ident) |
| 2642 | || lookahead.peek(Token![self]) |
| 2643 | || lookahead.peek(Token![super]) |
| 2644 | || lookahead.peek(Token![crate]) |
| 2645 | || lookahead.peek(Token![::])) |
| 2646 | { |
| 2647 | input.parse().map(ImplItem::Macro) |
| 2648 | } else { |
| 2649 | Err(lookahead.error()) |
| 2650 | }?; |
| 2651 | |
| 2652 | { |
| 2653 | let item_attrs = match &mut item { |
| 2654 | ImplItem::Const(item) => &mut item.attrs, |
| 2655 | ImplItem::Fn(item) => &mut item.attrs, |
| 2656 | ImplItem::Type(item) => &mut item.attrs, |
| 2657 | ImplItem::Macro(item) => &mut item.attrs, |
| 2658 | ImplItem::Verbatim(_) => return Ok(item), |
| 2659 | }; |
| 2660 | attrs.append(item_attrs); |
| 2661 | *item_attrs = attrs; |
| 2662 | } |
| 2663 | |
| 2664 | Ok(item) |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2669 | impl Parse for ImplItemConst { |
| 2670 | fn parse(input: ParseStream) -> Result<Self> { |
| 2671 | let attrs = input.call(Attribute::parse_outer)?; |
| 2672 | let vis: Visibility = input.parse()?; |
| 2673 | let defaultness: Option<Token![default]> = input.parse()?; |
| 2674 | let const_token: Token![const] = input.parse()?; |
| 2675 | |
| 2676 | let lookahead = input.lookahead1(); |
| 2677 | let ident = if lookahead.peek(Ident) || lookahead.peek(Token![_]) { |
| 2678 | input.call(Ident::parse_any)? |
| 2679 | } else { |
| 2680 | return Err(lookahead.error()); |
| 2681 | }; |
| 2682 | |
| 2683 | let colon_token: Token![:] = input.parse()?; |
| 2684 | let ty: Type = input.parse()?; |
| 2685 | let eq_token: Token![=] = input.parse()?; |
| 2686 | let expr: Expr = input.parse()?; |
| 2687 | let semi_token: Token![;] = input.parse()?; |
| 2688 | |
| 2689 | Ok(ImplItemConst { |
| 2690 | attrs, |
| 2691 | vis, |
| 2692 | defaultness, |
| 2693 | const_token, |
| 2694 | ident, |
| 2695 | generics: Generics::default(), |
| 2696 | colon_token, |
| 2697 | ty, |
| 2698 | eq_token, |
| 2699 | expr, |
| 2700 | semi_token, |
| 2701 | }) |
| 2702 | } |
| 2703 | } |
| 2704 | |
| 2705 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2706 | impl Parse for ImplItemFn { |
| 2707 | fn parse(input: ParseStream) -> Result<Self> { |
| 2708 | let allow_omitted_body = false; |
| 2709 | parse_impl_item_fn(input, allow_omitted_body).map(Option::unwrap) |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | fn parse_impl_item_fn( |
| 2714 | input: ParseStream, |
| 2715 | allow_omitted_body: bool, |
| 2716 | ) -> Result<Option<ImplItemFn>> { |
| 2717 | let mut attrs = input.call(Attribute::parse_outer)?; |
| 2718 | let vis: Visibility = input.parse()?; |
| 2719 | let defaultness: Option<Token![default]> = input.parse()?; |
| 2720 | let sig: Signature = input.parse()?; |
| 2721 | |
| 2722 | // Accept functions without a body in an impl block because rustc's |
| 2723 | // *parser* does not reject them (the compilation error is emitted later |
| 2724 | // than parsing) and it can be useful for macro DSLs. |
| 2725 | if allow_omitted_body && input.parse::<Option<Token![;]>>()?.is_some() { |
| 2726 | return Ok(None); |
| 2727 | } |
| 2728 | |
| 2729 | let content; |
| 2730 | let brace_token = braced!(content in input); |
| 2731 | attrs.extend(content.call(Attribute::parse_inner)?); |
| 2732 | let block = Block { |
| 2733 | brace_token, |
| 2734 | stmts: content.call(Block::parse_within)?, |
| 2735 | }; |
| 2736 | |
| 2737 | Ok(Some(ImplItemFn { |
| 2738 | attrs, |
| 2739 | vis, |
| 2740 | defaultness, |
| 2741 | sig, |
| 2742 | block, |
| 2743 | })) |
| 2744 | } |
| 2745 | |
| 2746 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2747 | impl Parse for ImplItemType { |
| 2748 | fn parse(input: ParseStream) -> Result<Self> { |
| 2749 | let attrs = input.call(Attribute::parse_outer)?; |
| 2750 | let vis: Visibility = input.parse()?; |
| 2751 | let defaultness: Option<Token![default]> = input.parse()?; |
| 2752 | let type_token: Token![type] = input.parse()?; |
| 2753 | let ident: Ident = input.parse()?; |
| 2754 | let mut generics: Generics = input.parse()?; |
| 2755 | let eq_token: Token![=] = input.parse()?; |
| 2756 | let ty: Type = input.parse()?; |
| 2757 | generics.where_clause = input.parse()?; |
| 2758 | let semi_token: Token![;] = input.parse()?; |
| 2759 | Ok(ImplItemType { |
| 2760 | attrs, |
| 2761 | vis, |
| 2762 | defaultness, |
| 2763 | type_token, |
| 2764 | ident, |
| 2765 | generics, |
| 2766 | eq_token, |
| 2767 | ty, |
| 2768 | semi_token, |
| 2769 | }) |
| 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | fn parse_impl_item_type(begin: ParseBuffer, input: ParseStream) -> Result<ImplItem> { |
| 2774 | let FlexibleItemType { |
| 2775 | vis, |
| 2776 | defaultness, |
| 2777 | type_token, |
| 2778 | ident, |
| 2779 | generics, |
| 2780 | colon_token, |
| 2781 | bounds: _, |
| 2782 | ty, |
| 2783 | semi_token, |
| 2784 | } = FlexibleItemType::parse( |
| 2785 | input, |
| 2786 | TypeDefaultness::Optional, |
| 2787 | WhereClauseLocation::AfterEq, |
| 2788 | )?; |
| 2789 | |
| 2790 | let (eq_token, ty) = match ty { |
| 2791 | Some(ty) if colon_token.is_none() => ty, |
| 2792 | _ => return Ok(ImplItem::Verbatim(verbatim::between(&begin, input))), |
| 2793 | }; |
| 2794 | |
| 2795 | Ok(ImplItem::Type(ImplItemType { |
| 2796 | attrs: Vec::new(), |
| 2797 | vis, |
| 2798 | defaultness, |
| 2799 | type_token, |
| 2800 | ident, |
| 2801 | generics, |
| 2802 | eq_token, |
| 2803 | ty, |
| 2804 | semi_token, |
| 2805 | })) |
| 2806 | } |
| 2807 | |
| 2808 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2809 | impl Parse for ImplItemMacro { |
| 2810 | fn parse(input: ParseStream) -> Result<Self> { |
| 2811 | let attrs = input.call(Attribute::parse_outer)?; |
| 2812 | let mac: Macro = input.parse()?; |
| 2813 | let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() { |
| 2814 | None |
| 2815 | } else { |
| 2816 | Some(input.parse()?) |
| 2817 | }; |
| 2818 | Ok(ImplItemMacro { |
| 2819 | attrs, |
| 2820 | mac, |
| 2821 | semi_token, |
| 2822 | }) |
| 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | impl Visibility { |
| 2827 | fn is_inherited(&self) -> bool { |
| 2828 | match self { |
| 2829 | Visibility::Inherited => true, |
| 2830 | _ => false, |
| 2831 | } |
| 2832 | } |
| 2833 | } |
| 2834 | |
| 2835 | impl MacroDelimiter { |
| 2836 | pub(crate) fn is_brace(&self) -> bool { |
| 2837 | match self { |
| 2838 | MacroDelimiter::Brace(_) => true, |
| 2839 | MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false, |
| 2840 | } |
| 2841 | } |
| 2842 | } |
| 2843 | |
| 2844 | #[cfg_attr (doc_cfg, doc(cfg(feature = "parsing" )))] |
| 2845 | impl Parse for StaticMutability { |
| 2846 | fn parse(input: ParseStream) -> Result<Self> { |
| 2847 | let mut_token: Option<Token![mut]> = input.parse()?; |
| 2848 | Ok(mut_token.map_or(StaticMutability::None, StaticMutability::Mut)) |
| 2849 | } |
| 2850 | } |
| 2851 | } |
| 2852 | |
| 2853 | #[cfg (feature = "printing" )] |
| 2854 | mod printing { |
| 2855 | use super::*; |
| 2856 | use crate::attr::FilterAttrs; |
| 2857 | use crate::print::TokensOrDefault; |
| 2858 | use proc_macro2::TokenStream; |
| 2859 | use quote::{ToTokens, TokenStreamExt}; |
| 2860 | |
| 2861 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 2862 | impl ToTokens for ItemExternCrate { |
| 2863 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 2864 | tokens.append_all(self.attrs.outer()); |
| 2865 | self.vis.to_tokens(tokens); |
| 2866 | self.extern_token.to_tokens(tokens); |
| 2867 | self.crate_token.to_tokens(tokens); |
| 2868 | self.ident.to_tokens(tokens); |
| 2869 | if let Some((as_token, rename)) = &self.rename { |
| 2870 | as_token.to_tokens(tokens); |
| 2871 | rename.to_tokens(tokens); |
| 2872 | } |
| 2873 | self.semi_token.to_tokens(tokens); |
| 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 2878 | impl ToTokens for ItemUse { |
| 2879 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 2880 | tokens.append_all(self.attrs.outer()); |
| 2881 | self.vis.to_tokens(tokens); |
| 2882 | self.use_token.to_tokens(tokens); |
| 2883 | self.leading_colon.to_tokens(tokens); |
| 2884 | self.tree.to_tokens(tokens); |
| 2885 | self.semi_token.to_tokens(tokens); |
| 2886 | } |
| 2887 | } |
| 2888 | |
| 2889 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 2890 | impl ToTokens for ItemStatic { |
| 2891 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 2892 | tokens.append_all(self.attrs.outer()); |
| 2893 | self.vis.to_tokens(tokens); |
| 2894 | self.static_token.to_tokens(tokens); |
| 2895 | self.mutability.to_tokens(tokens); |
| 2896 | self.ident.to_tokens(tokens); |
| 2897 | self.colon_token.to_tokens(tokens); |
| 2898 | self.ty.to_tokens(tokens); |
| 2899 | self.eq_token.to_tokens(tokens); |
| 2900 | self.expr.to_tokens(tokens); |
| 2901 | self.semi_token.to_tokens(tokens); |
| 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 2906 | impl ToTokens for ItemConst { |
| 2907 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 2908 | tokens.append_all(self.attrs.outer()); |
| 2909 | self.vis.to_tokens(tokens); |
| 2910 | self.const_token.to_tokens(tokens); |
| 2911 | self.ident.to_tokens(tokens); |
| 2912 | self.colon_token.to_tokens(tokens); |
| 2913 | self.ty.to_tokens(tokens); |
| 2914 | self.eq_token.to_tokens(tokens); |
| 2915 | self.expr.to_tokens(tokens); |
| 2916 | self.semi_token.to_tokens(tokens); |
| 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 2921 | impl ToTokens for ItemFn { |
| 2922 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 2923 | tokens.append_all(self.attrs.outer()); |
| 2924 | self.vis.to_tokens(tokens); |
| 2925 | self.sig.to_tokens(tokens); |
| 2926 | self.block.brace_token.surround(tokens, |tokens| { |
| 2927 | tokens.append_all(self.attrs.inner()); |
| 2928 | tokens.append_all(&self.block.stmts); |
| 2929 | }); |
| 2930 | } |
| 2931 | } |
| 2932 | |
| 2933 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 2934 | impl ToTokens for ItemMod { |
| 2935 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 2936 | tokens.append_all(self.attrs.outer()); |
| 2937 | self.vis.to_tokens(tokens); |
| 2938 | self.unsafety.to_tokens(tokens); |
| 2939 | self.mod_token.to_tokens(tokens); |
| 2940 | self.ident.to_tokens(tokens); |
| 2941 | if let Some((brace, items)) = &self.content { |
| 2942 | brace.surround(tokens, |tokens| { |
| 2943 | tokens.append_all(self.attrs.inner()); |
| 2944 | tokens.append_all(items); |
| 2945 | }); |
| 2946 | } else { |
| 2947 | TokensOrDefault(&self.semi).to_tokens(tokens); |
| 2948 | } |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 2953 | impl ToTokens for ItemForeignMod { |
| 2954 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 2955 | tokens.append_all(self.attrs.outer()); |
| 2956 | self.unsafety.to_tokens(tokens); |
| 2957 | self.abi.to_tokens(tokens); |
| 2958 | self.brace_token.surround(tokens, |tokens| { |
| 2959 | tokens.append_all(self.attrs.inner()); |
| 2960 | tokens.append_all(&self.items); |
| 2961 | }); |
| 2962 | } |
| 2963 | } |
| 2964 | |
| 2965 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 2966 | impl ToTokens for ItemType { |
| 2967 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 2968 | tokens.append_all(self.attrs.outer()); |
| 2969 | self.vis.to_tokens(tokens); |
| 2970 | self.type_token.to_tokens(tokens); |
| 2971 | self.ident.to_tokens(tokens); |
| 2972 | self.generics.to_tokens(tokens); |
| 2973 | self.generics.where_clause.to_tokens(tokens); |
| 2974 | self.eq_token.to_tokens(tokens); |
| 2975 | self.ty.to_tokens(tokens); |
| 2976 | self.semi_token.to_tokens(tokens); |
| 2977 | } |
| 2978 | } |
| 2979 | |
| 2980 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 2981 | impl ToTokens for ItemEnum { |
| 2982 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 2983 | tokens.append_all(self.attrs.outer()); |
| 2984 | self.vis.to_tokens(tokens); |
| 2985 | self.enum_token.to_tokens(tokens); |
| 2986 | self.ident.to_tokens(tokens); |
| 2987 | self.generics.to_tokens(tokens); |
| 2988 | self.generics.where_clause.to_tokens(tokens); |
| 2989 | self.brace_token.surround(tokens, |tokens| { |
| 2990 | self.variants.to_tokens(tokens); |
| 2991 | }); |
| 2992 | } |
| 2993 | } |
| 2994 | |
| 2995 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 2996 | impl ToTokens for ItemStruct { |
| 2997 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 2998 | tokens.append_all(self.attrs.outer()); |
| 2999 | self.vis.to_tokens(tokens); |
| 3000 | self.struct_token.to_tokens(tokens); |
| 3001 | self.ident.to_tokens(tokens); |
| 3002 | self.generics.to_tokens(tokens); |
| 3003 | match &self.fields { |
| 3004 | Fields::Named(fields) => { |
| 3005 | self.generics.where_clause.to_tokens(tokens); |
| 3006 | fields.to_tokens(tokens); |
| 3007 | } |
| 3008 | Fields::Unnamed(fields) => { |
| 3009 | fields.to_tokens(tokens); |
| 3010 | self.generics.where_clause.to_tokens(tokens); |
| 3011 | TokensOrDefault(&self.semi_token).to_tokens(tokens); |
| 3012 | } |
| 3013 | Fields::Unit => { |
| 3014 | self.generics.where_clause.to_tokens(tokens); |
| 3015 | TokensOrDefault(&self.semi_token).to_tokens(tokens); |
| 3016 | } |
| 3017 | } |
| 3018 | } |
| 3019 | } |
| 3020 | |
| 3021 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3022 | impl ToTokens for ItemUnion { |
| 3023 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3024 | tokens.append_all(self.attrs.outer()); |
| 3025 | self.vis.to_tokens(tokens); |
| 3026 | self.union_token.to_tokens(tokens); |
| 3027 | self.ident.to_tokens(tokens); |
| 3028 | self.generics.to_tokens(tokens); |
| 3029 | self.generics.where_clause.to_tokens(tokens); |
| 3030 | self.fields.to_tokens(tokens); |
| 3031 | } |
| 3032 | } |
| 3033 | |
| 3034 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3035 | impl ToTokens for ItemTrait { |
| 3036 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3037 | tokens.append_all(self.attrs.outer()); |
| 3038 | self.vis.to_tokens(tokens); |
| 3039 | self.unsafety.to_tokens(tokens); |
| 3040 | self.auto_token.to_tokens(tokens); |
| 3041 | self.trait_token.to_tokens(tokens); |
| 3042 | self.ident.to_tokens(tokens); |
| 3043 | self.generics.to_tokens(tokens); |
| 3044 | if !self.supertraits.is_empty() { |
| 3045 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
| 3046 | self.supertraits.to_tokens(tokens); |
| 3047 | } |
| 3048 | self.generics.where_clause.to_tokens(tokens); |
| 3049 | self.brace_token.surround(tokens, |tokens| { |
| 3050 | tokens.append_all(self.attrs.inner()); |
| 3051 | tokens.append_all(&self.items); |
| 3052 | }); |
| 3053 | } |
| 3054 | } |
| 3055 | |
| 3056 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3057 | impl ToTokens for ItemTraitAlias { |
| 3058 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3059 | tokens.append_all(self.attrs.outer()); |
| 3060 | self.vis.to_tokens(tokens); |
| 3061 | self.trait_token.to_tokens(tokens); |
| 3062 | self.ident.to_tokens(tokens); |
| 3063 | self.generics.to_tokens(tokens); |
| 3064 | self.eq_token.to_tokens(tokens); |
| 3065 | self.bounds.to_tokens(tokens); |
| 3066 | self.generics.where_clause.to_tokens(tokens); |
| 3067 | self.semi_token.to_tokens(tokens); |
| 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3072 | impl ToTokens for ItemImpl { |
| 3073 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3074 | tokens.append_all(self.attrs.outer()); |
| 3075 | self.defaultness.to_tokens(tokens); |
| 3076 | self.unsafety.to_tokens(tokens); |
| 3077 | self.impl_token.to_tokens(tokens); |
| 3078 | self.generics.to_tokens(tokens); |
| 3079 | if let Some((polarity, path, for_token)) = &self.trait_ { |
| 3080 | polarity.to_tokens(tokens); |
| 3081 | path.to_tokens(tokens); |
| 3082 | for_token.to_tokens(tokens); |
| 3083 | } |
| 3084 | self.self_ty.to_tokens(tokens); |
| 3085 | self.generics.where_clause.to_tokens(tokens); |
| 3086 | self.brace_token.surround(tokens, |tokens| { |
| 3087 | tokens.append_all(self.attrs.inner()); |
| 3088 | tokens.append_all(&self.items); |
| 3089 | }); |
| 3090 | } |
| 3091 | } |
| 3092 | |
| 3093 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3094 | impl ToTokens for ItemMacro { |
| 3095 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3096 | tokens.append_all(self.attrs.outer()); |
| 3097 | self.mac.path.to_tokens(tokens); |
| 3098 | self.mac.bang_token.to_tokens(tokens); |
| 3099 | self.ident.to_tokens(tokens); |
| 3100 | match &self.mac.delimiter { |
| 3101 | MacroDelimiter::Paren(paren) => { |
| 3102 | paren.surround(tokens, |tokens| self.mac.tokens.to_tokens(tokens)); |
| 3103 | } |
| 3104 | MacroDelimiter::Brace(brace) => { |
| 3105 | brace.surround(tokens, |tokens| self.mac.tokens.to_tokens(tokens)); |
| 3106 | } |
| 3107 | MacroDelimiter::Bracket(bracket) => { |
| 3108 | bracket.surround(tokens, |tokens| self.mac.tokens.to_tokens(tokens)); |
| 3109 | } |
| 3110 | } |
| 3111 | self.semi_token.to_tokens(tokens); |
| 3112 | } |
| 3113 | } |
| 3114 | |
| 3115 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3116 | impl ToTokens for UsePath { |
| 3117 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3118 | self.ident.to_tokens(tokens); |
| 3119 | self.colon2_token.to_tokens(tokens); |
| 3120 | self.tree.to_tokens(tokens); |
| 3121 | } |
| 3122 | } |
| 3123 | |
| 3124 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3125 | impl ToTokens for UseName { |
| 3126 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3127 | self.ident.to_tokens(tokens); |
| 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3132 | impl ToTokens for UseRename { |
| 3133 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3134 | self.ident.to_tokens(tokens); |
| 3135 | self.as_token.to_tokens(tokens); |
| 3136 | self.rename.to_tokens(tokens); |
| 3137 | } |
| 3138 | } |
| 3139 | |
| 3140 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3141 | impl ToTokens for UseGlob { |
| 3142 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3143 | self.star_token.to_tokens(tokens); |
| 3144 | } |
| 3145 | } |
| 3146 | |
| 3147 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3148 | impl ToTokens for UseGroup { |
| 3149 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3150 | self.brace_token.surround(tokens, |tokens| { |
| 3151 | self.items.to_tokens(tokens); |
| 3152 | }); |
| 3153 | } |
| 3154 | } |
| 3155 | |
| 3156 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3157 | impl ToTokens for TraitItemConst { |
| 3158 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3159 | tokens.append_all(self.attrs.outer()); |
| 3160 | self.const_token.to_tokens(tokens); |
| 3161 | self.ident.to_tokens(tokens); |
| 3162 | self.colon_token.to_tokens(tokens); |
| 3163 | self.ty.to_tokens(tokens); |
| 3164 | if let Some((eq_token, default)) = &self.default { |
| 3165 | eq_token.to_tokens(tokens); |
| 3166 | default.to_tokens(tokens); |
| 3167 | } |
| 3168 | self.semi_token.to_tokens(tokens); |
| 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3173 | impl ToTokens for TraitItemFn { |
| 3174 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3175 | tokens.append_all(self.attrs.outer()); |
| 3176 | self.sig.to_tokens(tokens); |
| 3177 | match &self.default { |
| 3178 | Some(block) => { |
| 3179 | block.brace_token.surround(tokens, |tokens| { |
| 3180 | tokens.append_all(self.attrs.inner()); |
| 3181 | tokens.append_all(&block.stmts); |
| 3182 | }); |
| 3183 | } |
| 3184 | None => { |
| 3185 | TokensOrDefault(&self.semi_token).to_tokens(tokens); |
| 3186 | } |
| 3187 | } |
| 3188 | } |
| 3189 | } |
| 3190 | |
| 3191 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3192 | impl ToTokens for TraitItemType { |
| 3193 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3194 | tokens.append_all(self.attrs.outer()); |
| 3195 | self.type_token.to_tokens(tokens); |
| 3196 | self.ident.to_tokens(tokens); |
| 3197 | self.generics.to_tokens(tokens); |
| 3198 | if !self.bounds.is_empty() { |
| 3199 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
| 3200 | self.bounds.to_tokens(tokens); |
| 3201 | } |
| 3202 | if let Some((eq_token, default)) = &self.default { |
| 3203 | eq_token.to_tokens(tokens); |
| 3204 | default.to_tokens(tokens); |
| 3205 | } |
| 3206 | self.generics.where_clause.to_tokens(tokens); |
| 3207 | self.semi_token.to_tokens(tokens); |
| 3208 | } |
| 3209 | } |
| 3210 | |
| 3211 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3212 | impl ToTokens for TraitItemMacro { |
| 3213 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3214 | tokens.append_all(self.attrs.outer()); |
| 3215 | self.mac.to_tokens(tokens); |
| 3216 | self.semi_token.to_tokens(tokens); |
| 3217 | } |
| 3218 | } |
| 3219 | |
| 3220 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3221 | impl ToTokens for ImplItemConst { |
| 3222 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3223 | tokens.append_all(self.attrs.outer()); |
| 3224 | self.vis.to_tokens(tokens); |
| 3225 | self.defaultness.to_tokens(tokens); |
| 3226 | self.const_token.to_tokens(tokens); |
| 3227 | self.ident.to_tokens(tokens); |
| 3228 | self.colon_token.to_tokens(tokens); |
| 3229 | self.ty.to_tokens(tokens); |
| 3230 | self.eq_token.to_tokens(tokens); |
| 3231 | self.expr.to_tokens(tokens); |
| 3232 | self.semi_token.to_tokens(tokens); |
| 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3237 | impl ToTokens for ImplItemFn { |
| 3238 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3239 | tokens.append_all(self.attrs.outer()); |
| 3240 | self.vis.to_tokens(tokens); |
| 3241 | self.defaultness.to_tokens(tokens); |
| 3242 | self.sig.to_tokens(tokens); |
| 3243 | self.block.brace_token.surround(tokens, |tokens| { |
| 3244 | tokens.append_all(self.attrs.inner()); |
| 3245 | tokens.append_all(&self.block.stmts); |
| 3246 | }); |
| 3247 | } |
| 3248 | } |
| 3249 | |
| 3250 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3251 | impl ToTokens for ImplItemType { |
| 3252 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3253 | tokens.append_all(self.attrs.outer()); |
| 3254 | self.vis.to_tokens(tokens); |
| 3255 | self.defaultness.to_tokens(tokens); |
| 3256 | self.type_token.to_tokens(tokens); |
| 3257 | self.ident.to_tokens(tokens); |
| 3258 | self.generics.to_tokens(tokens); |
| 3259 | self.eq_token.to_tokens(tokens); |
| 3260 | self.ty.to_tokens(tokens); |
| 3261 | self.generics.where_clause.to_tokens(tokens); |
| 3262 | self.semi_token.to_tokens(tokens); |
| 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3267 | impl ToTokens for ImplItemMacro { |
| 3268 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3269 | tokens.append_all(self.attrs.outer()); |
| 3270 | self.mac.to_tokens(tokens); |
| 3271 | self.semi_token.to_tokens(tokens); |
| 3272 | } |
| 3273 | } |
| 3274 | |
| 3275 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3276 | impl ToTokens for ForeignItemFn { |
| 3277 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3278 | tokens.append_all(self.attrs.outer()); |
| 3279 | self.vis.to_tokens(tokens); |
| 3280 | self.sig.to_tokens(tokens); |
| 3281 | self.semi_token.to_tokens(tokens); |
| 3282 | } |
| 3283 | } |
| 3284 | |
| 3285 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3286 | impl ToTokens for ForeignItemStatic { |
| 3287 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3288 | tokens.append_all(self.attrs.outer()); |
| 3289 | self.vis.to_tokens(tokens); |
| 3290 | self.static_token.to_tokens(tokens); |
| 3291 | self.mutability.to_tokens(tokens); |
| 3292 | self.ident.to_tokens(tokens); |
| 3293 | self.colon_token.to_tokens(tokens); |
| 3294 | self.ty.to_tokens(tokens); |
| 3295 | self.semi_token.to_tokens(tokens); |
| 3296 | } |
| 3297 | } |
| 3298 | |
| 3299 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3300 | impl ToTokens for ForeignItemType { |
| 3301 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3302 | tokens.append_all(self.attrs.outer()); |
| 3303 | self.vis.to_tokens(tokens); |
| 3304 | self.type_token.to_tokens(tokens); |
| 3305 | self.ident.to_tokens(tokens); |
| 3306 | self.generics.to_tokens(tokens); |
| 3307 | self.generics.where_clause.to_tokens(tokens); |
| 3308 | self.semi_token.to_tokens(tokens); |
| 3309 | } |
| 3310 | } |
| 3311 | |
| 3312 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3313 | impl ToTokens for ForeignItemMacro { |
| 3314 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3315 | tokens.append_all(self.attrs.outer()); |
| 3316 | self.mac.to_tokens(tokens); |
| 3317 | self.semi_token.to_tokens(tokens); |
| 3318 | } |
| 3319 | } |
| 3320 | |
| 3321 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3322 | impl ToTokens for Signature { |
| 3323 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3324 | self.constness.to_tokens(tokens); |
| 3325 | self.asyncness.to_tokens(tokens); |
| 3326 | self.unsafety.to_tokens(tokens); |
| 3327 | self.abi.to_tokens(tokens); |
| 3328 | self.fn_token.to_tokens(tokens); |
| 3329 | self.ident.to_tokens(tokens); |
| 3330 | self.generics.to_tokens(tokens); |
| 3331 | self.paren_token.surround(tokens, |tokens| { |
| 3332 | self.inputs.to_tokens(tokens); |
| 3333 | if let Some(variadic) = &self.variadic { |
| 3334 | if !self.inputs.empty_or_trailing() { |
| 3335 | <Token![,]>::default().to_tokens(tokens); |
| 3336 | } |
| 3337 | variadic.to_tokens(tokens); |
| 3338 | } |
| 3339 | }); |
| 3340 | self.output.to_tokens(tokens); |
| 3341 | self.generics.where_clause.to_tokens(tokens); |
| 3342 | } |
| 3343 | } |
| 3344 | |
| 3345 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3346 | impl ToTokens for Receiver { |
| 3347 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3348 | tokens.append_all(self.attrs.outer()); |
| 3349 | if let Some((ampersand, lifetime)) = &self.reference { |
| 3350 | ampersand.to_tokens(tokens); |
| 3351 | lifetime.to_tokens(tokens); |
| 3352 | } |
| 3353 | self.mutability.to_tokens(tokens); |
| 3354 | self.self_token.to_tokens(tokens); |
| 3355 | if let Some(colon_token) = &self.colon_token { |
| 3356 | colon_token.to_tokens(tokens); |
| 3357 | self.ty.to_tokens(tokens); |
| 3358 | } else { |
| 3359 | let consistent = match (&self.reference, &self.mutability, &*self.ty) { |
| 3360 | (Some(_), mutability, Type::Reference(ty)) => { |
| 3361 | mutability.is_some() == ty.mutability.is_some() |
| 3362 | && match &*ty.elem { |
| 3363 | Type::Path(ty) => ty.qself.is_none() && ty.path.is_ident("Self" ), |
| 3364 | _ => false, |
| 3365 | } |
| 3366 | } |
| 3367 | (None, _, Type::Path(ty)) => ty.qself.is_none() && ty.path.is_ident("Self" ), |
| 3368 | _ => false, |
| 3369 | }; |
| 3370 | if !consistent { |
| 3371 | <Token![:]>::default().to_tokens(tokens); |
| 3372 | self.ty.to_tokens(tokens); |
| 3373 | } |
| 3374 | } |
| 3375 | } |
| 3376 | } |
| 3377 | |
| 3378 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3379 | impl ToTokens for Variadic { |
| 3380 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3381 | tokens.append_all(self.attrs.outer()); |
| 3382 | if let Some((pat, colon)) = &self.pat { |
| 3383 | pat.to_tokens(tokens); |
| 3384 | colon.to_tokens(tokens); |
| 3385 | } |
| 3386 | self.dots.to_tokens(tokens); |
| 3387 | self.comma.to_tokens(tokens); |
| 3388 | } |
| 3389 | } |
| 3390 | |
| 3391 | #[cfg_attr (doc_cfg, doc(cfg(feature = "printing" )))] |
| 3392 | impl ToTokens for StaticMutability { |
| 3393 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3394 | match self { |
| 3395 | StaticMutability::None => {} |
| 3396 | StaticMutability::Mut(mut_token) => mut_token.to_tokens(tokens), |
| 3397 | } |
| 3398 | } |
| 3399 | } |
| 3400 | } |
| 3401 | |