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