Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/aienux/blob/main/vendor/syn-2.0.119/src/item.rs
# aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`aienux / vendor / syn-2.0.119 / src / item.rs`
3539 lines ยท 122882 bytes Raw Blame History
1 use crate::attr::Attribute;
2 use crate::data::{Fields, FieldsNamed, Variant};
3 use crate::derive::{Data, DataEnum, DataStruct, DataUnion, DeriveInput};
4 use crate::expr::Expr;
5 use crate::generics::{Generics, TypeParamBound};
6 use crate::ident::Ident;
7 use crate::lifetime::Lifetime;
8 use crate::mac::Macro;
9 use crate::pat::{Pat, PatType};
10 use crate::path::Path;
11 use crate::punctuated::Punctuated;
12 use crate::restriction::Visibility;
13 use crate::stmt::Block;
14 use crate::token;
15 use crate::ty::{Abi, ReturnType, Type};
16 use alloc::boxed::Box;
17 use alloc::vec::Vec;
18 #[cfg(feature = "parsing")]
19 use core::mem;
20 use proc_macro2::TokenStream;
21
22 ast_enum_of_structs! {
23 /// Things that can appear directly inside of a module or scope.
24 ///
25 /// # Syntax tree enum
26 ///
27 /// This type is a [syntax tree enum].
28 ///
29 /// [syntax tree enum]: crate::expr::Expr#syntax-tree-enums
30 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
31 #[non_exhaustive]
32 pub enum Item {
33 /// A constant item: `const MAX: u16 = 65535`.
34 Const(ItemConst),
35
36 /// An enum definition: `enum Foo { A(A), B(B) }`.
37 Enum(ItemEnum),
38
39 /// An `extern crate` item: `extern crate serde`.
40 ExternCrate(ItemExternCrate),
41
42 /// A free-standing function: `fn process(n: usize) -> Result<()> { ...
43 /// }`.
44 Fn(ItemFn),
45
46 /// A block of foreign items: `extern "C" { ... }`.
47 ForeignMod(ItemForeignMod),
48
49 /// An impl block providing trait or associated items: `impl Trait
50 /// for Data { ... }`.
51 Impl(ItemImpl),
52
53 /// A macro invocation, which includes `macro_rules!` definitions.
54 Macro(ItemMacro),
55
56 /// A module or module declaration: `mod m` or `mod m { ... }`.
57 Mod(ItemMod),
58
59 /// A static item: `static BIKE: Shed = Shed(42)`.
60 Static(ItemStatic),
61
62 /// A struct definition: `struct Foo { x: A }`.
63 Struct(ItemStruct),
64
65 /// A trait definition: `pub trait Iterator { ... }`.
66 Trait(ItemTrait),
67
68 /// A trait alias: `pub trait SharableIterator = Iterator + Sync`.
69 TraitAlias(ItemTraitAlias),
70
71 /// A type alias: `type Result = core::result::Result`.
72 Type(ItemType),
73
74 /// A union definition: `union Foo { x: A, y: B }`.
75 Union(ItemUnion),
76
77 /// A use declaration: `use alloc::collections::HashMap`.
78 Use(ItemUse),
79
80 /// Tokens forming an item not interpreted by Syn.
81 Verbatim(TokenStream),
82
83 // For testing exhaustiveness in downstream code, use the following idiom:
84 //
85 // match item {
86 // #![cfg_attr(test, deny(non_exhaustive_omitted_patterns))]
87 //
88 // Item::Const(item) => {...}
89 // Item::Enum(item) => {...}
90 // ...
91 // Item::Verbatim(item) => {...}
92 //
93 // _ => { /* some sane fallback */ }
94 // }
95 //
96 // This way we fail your tests but don't break your library when adding
97 // a variant. You will be notified by a test failure when a variant is
98 // added, so that you can add code to handle it, but your library will
99 // continue to compile and work for downstream users in the interim.
100 }
101 }
102
103 ast_struct! {
104 /// A constant item: `const MAX: u16 = 65535`.
105 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
106 pub struct ItemConst {
107 pub attrs: Vec,
108 pub vis: Visibility,
109 pub const_token: Token![const],
110 pub ident: Ident,
111 pub generics: Generics,
112 pub colon_token: Token![:],
113 pub ty: Box,
114 pub eq_token: Token![=],
115 pub expr: Box,
116 pub semi_token: Token![;],
117 }
118 }
119
120 ast_struct! {
121 /// An enum definition: `enum Foo { A(A), B(B) }`.
122 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
123 pub struct ItemEnum {
124 pub attrs: Vec,
125 pub vis: Visibility,
126 pub enum_token: Token![enum],
127 pub ident: Ident,
128 pub generics: Generics,
129 pub brace_token: token::Brace,
130 pub variants: Punctuated,
131 }
132 }
133
134 ast_struct! {
135 /// An `extern crate` item: `extern crate serde`.
136 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
137 pub struct ItemExternCrate {
138 pub attrs: Vec,
139 pub vis: Visibility,
140 pub extern_token: Token![extern],
141 pub crate_token: Token![crate],
142 pub ident: Ident,
143 pub rename: Option<(Token![as], Ident)>,
144 pub semi_token: Token![;],
145 }
146 }
147
148 ast_struct! {
149 /// A free-standing function: `fn process(n: usize) -> Result<()> { ... }`.
150 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
151 pub struct ItemFn {
152 pub attrs: Vec,
153 pub vis: Visibility,
154 pub sig: Signature,
155 pub block: Box,
156 }
157 }
158
159 ast_struct! {
160 /// A block of foreign items: `extern "C" { ... }`.
161 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
162 pub struct ItemForeignMod {
163 pub attrs: Vec,
164 pub unsafety: Option,
165 pub abi: Abi,
166 pub brace_token: token::Brace,
167 pub items: Vec,
168 }
169 }
170
171 ast_struct! {
172 /// An impl block providing trait or associated items: `impl Trait
173 /// for Data { ... }`.
174 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
175 pub struct ItemImpl {
176 pub attrs: Vec,
177 pub defaultness: Option,
178 pub unsafety: Option,
179 pub impl_token: Token![impl],
180 pub generics: Generics,
181 /// Trait this impl implements.
182 pub trait_: Option<(Option, Path, Token![for])>,
183 /// The Self type of the impl.
184 pub self_ty: Box,
185 pub brace_token: token::Brace,
186 pub items: Vec,
187 }
188 }
189
190 ast_struct! {
191 /// A macro invocation, which includes `macro_rules!` definitions.
192 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
193 pub struct ItemMacro {
194 pub attrs: Vec,
195 /// The `example` in `macro_rules! example { ... }`.
196 pub ident: Option,
197 pub mac: Macro,
198 pub semi_token: Option,
199 }
200 }
201
202 ast_struct! {
203 /// A module or module declaration: `mod m` or `mod m { ... }`.
204 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
205 pub struct ItemMod {
206 pub attrs: Vec,
207 pub vis: Visibility,
208 pub unsafety: Option,
209 pub mod_token: Token![mod],
210 pub ident: Ident,
211 pub content: Option<(token::Brace, Vec- )>,
212 pub semi: Option,
213 }
214 }
215
216 ast_struct! {
217 /// A static item: `static BIKE: Shed = Shed(42)`.
218 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
219 pub struct ItemStatic {
220 pub attrs: Vec,
221 pub vis: Visibility,
222 pub static_token: Token![static],
223 pub mutability: StaticMutability,
224 pub ident: Ident,
225 pub colon_token: Token![:],
226 pub ty: Box,
227 pub eq_token: Token![=],
228 pub expr: Box,
229 pub semi_token: Token![;],
230 }
231 }
232
233 ast_struct! {
234 /// A struct definition: `struct Foo { x: A }`.
235 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
236 pub struct ItemStruct {
237 pub attrs: Vec,
238 pub vis: Visibility,
239 pub struct_token: Token![struct],
240 pub ident: Ident,
241 pub generics: Generics,
242 pub fields: Fields,
243 pub semi_token: Option,
244 }
245 }
246
247 ast_struct! {
248 /// A trait definition: `pub trait Iterator { ... }`.
249 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
250 pub struct ItemTrait {
251 pub attrs: Vec,
252 pub vis: Visibility,
253 pub unsafety: Option,
254 pub auto_token: Option,
255 pub restriction: Option,
256 pub trait_token: Token![trait],
257 pub ident: Ident,
258 pub generics: Generics,
259 pub colon_token: Option,
260 pub supertraits: Punctuated,
261 pub brace_token: token::Brace,
262 pub items: Vec,
263 }
264 }
265
266 ast_struct! {
267 /// A trait alias: `pub trait SharableIterator = Iterator + Sync`.
268 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
269 pub struct ItemTraitAlias {
270 pub attrs: Vec,
271 pub vis: Visibility,
272 pub trait_token: Token![trait],
273 pub ident: Ident,
274 pub generics: Generics,
275 pub eq_token: Token![=],
276 pub bounds: Punctuated,
277 pub semi_token: Token![;],
278 }
279 }
280
281 ast_struct! {
282 /// A type alias: `type Result = core::result::Result`.
283 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
284 pub struct ItemType {
285 pub attrs: Vec,
286 pub vis: Visibility,
287 pub type_token: Token![type],
288 pub ident: Ident,
289 pub generics: Generics,
290 pub eq_token: Token![=],
291 pub ty: Box,
292 pub semi_token: Token![;],
293 }
294 }
295
296 ast_struct! {
297 /// A union definition: `union Foo { x: A, y: B }`.
298 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
299 pub struct ItemUnion {
300 pub attrs: Vec,
301 pub vis: Visibility,
302 pub union_token: Token![union],
303 pub ident: Ident,
304 pub generics: Generics,
305 pub fields: FieldsNamed,
306 }
307 }
308
309 ast_struct! {
310 /// A use declaration: `use alloc::collections::HashMap`.
311 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
312 pub struct ItemUse {
313 pub attrs: Vec,
314 pub vis: Visibility,
315 pub use_token: Token![use],
316 pub leading_colon: Option,
317 pub tree: UseTree,
318 pub semi_token: Token![;],
319 }
320 }
321
322 impl Item {
323 #[cfg(feature = "parsing")]
324 pub(crate) fn replace_attrs(&mut self, new: Vec) -> Vec {
325 match self {
326 Item::Const(ItemConst { attrs, .. })
327 | Item::Enum(ItemEnum { attrs, .. })
328 | Item::ExternCrate(ItemExternCrate { attrs, .. })
329 | Item::Fn(ItemFn { attrs, .. })
330 | Item::ForeignMod(ItemForeignMod { attrs, .. })
331 | Item::Impl(ItemImpl { attrs, .. })
332 | Item::Macro(ItemMacro { attrs, .. })
333 | Item::Mod(ItemMod { attrs, .. })
334 | Item::Static(ItemStatic { attrs, .. })
335 | Item::Struct(ItemStruct { attrs, .. })
336 | Item::Trait(ItemTrait { attrs, .. })
337 | Item::TraitAlias(ItemTraitAlias { attrs, .. })
338 | Item::Type(ItemType { attrs, .. })
339 | Item::Union(ItemUnion { attrs, .. })
340 | Item::Use(ItemUse { attrs, .. }) => mem::replace(attrs, new),
341 Item::Verbatim(_) => Vec::new(),
342 }
343 }
344 }
345
346 impl From for Item {
347 fn from(input: DeriveInput) -> Item {
348 match input.data {
349 Data::Struct(data) => Item::Struct(ItemStruct {
350 attrs: input.attrs,
351 vis: input.vis,
352 struct_token: data.struct_token,
353 ident: input.ident,
354 generics: input.generics,
355 fields: data.fields,
356 semi_token: data.semi_token,
357 }),
358 Data::Enum(data) => Item::Enum(ItemEnum {
359 attrs: input.attrs,
360 vis: input.vis,
361 enum_token: data.enum_token,
362 ident: input.ident,
363 generics: input.generics,
364 brace_token: data.brace_token,
365 variants: data.variants,
366 }),
367 Data::Union(data) => Item::Union(ItemUnion {
368 attrs: input.attrs,
369 vis: input.vis,
370 union_token: data.union_token,
371 ident: input.ident,
372 generics: input.generics,
373 fields: data.fields,
374 }),
375 }
376 }
377 }
378
379 impl From for DeriveInput {
380 fn from(input: ItemStruct) -> DeriveInput {
381 DeriveInput {
382 attrs: input.attrs,
383 vis: input.vis,
384 ident: input.ident,
385 generics: input.generics,
386 data: Data::Struct(DataStruct {
387 struct_token: input.struct_token,
388 fields: input.fields,
389 semi_token: input.semi_token,
390 }),
391 }
392 }
393 }
394
395 impl From for DeriveInput {
396 fn from(input: ItemEnum) -> DeriveInput {
397 DeriveInput {
398 attrs: input.attrs,
399 vis: input.vis,
400 ident: input.ident,
401 generics: input.generics,
402 data: Data::Enum(DataEnum {
403 enum_token: input.enum_token,
404 brace_token: input.brace_token,
405 variants: input.variants,
406 }),
407 }
408 }
409 }
410
411 impl From for DeriveInput {
412 fn from(input: ItemUnion) -> DeriveInput {
413 DeriveInput {
414 attrs: input.attrs,
415 vis: input.vis,
416 ident: input.ident,
417 generics: input.generics,
418 data: Data::Union(DataUnion {
419 union_token: input.union_token,
420 fields: input.fields,
421 }),
422 }
423 }
424 }
425
426 ast_enum_of_structs! {
427 /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
428 ///
429 /// # Syntax tree enum
430 ///
431 /// This type is a [syntax tree enum].
432 ///
433 /// [syntax tree enum]: crate::expr::Expr#syntax-tree-enums
434 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
435 pub enum UseTree {
436 /// A path prefix of imports in a `use` item: `core::...`.
437 Path(UsePath),
438
439 /// An identifier imported by a `use` item: `HashMap`.
440 Name(UseName),
441
442 /// An renamed identifier imported by a `use` item: `HashMap as Map`.
443 Rename(UseRename),
444
445 /// A glob import in a `use` item: `*`.
446 Glob(UseGlob),
447
448 /// A braced group of imports in a `use` item: `{A, B, C}`.
449 Group(UseGroup),
450 }
451 }
452
453 ast_struct! {
454 /// A path prefix of imports in a `use` item: `core::...`.
455 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
456 pub struct UsePath {
457 pub ident: Ident,
458 pub colon2_token: Token![::],
459 pub tree: Box,
460 }
461 }
462
463 ast_struct! {
464 /// An identifier imported by a `use` item: `HashMap`.
465 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
466 pub struct UseName {
467 pub ident: Ident,
468 }
469 }
470
471 ast_struct! {
472 /// An renamed identifier imported by a `use` item: `HashMap as Map`.
473 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
474 pub struct UseRename {
475 pub ident: Ident,
476 pub as_token: Token![as],
477 pub rename: Ident,
478 }
479 }
480
481 ast_struct! {
482 /// A glob import in a `use` item: `*`.
483 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
484 pub struct UseGlob {
485 pub star_token: Token![*],
486 }
487 }
488
489 ast_struct! {
490 /// A braced group of imports in a `use` item: `{A, B, C}`.
491 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
492 pub struct UseGroup {
493 pub brace_token: token::Brace,
494 pub items: Punctuated,
495 }
496 }
497
498 ast_enum_of_structs! {
499 /// An item within an `extern` block.
500 ///
501 /// # Syntax tree enum
502 ///
503 /// This type is a [syntax tree enum].
504 ///
505 /// [syntax tree enum]: crate::expr::Expr#syntax-tree-enums
506 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
507 #[non_exhaustive]
508 pub enum ForeignItem {
509 /// A foreign function in an `extern` block.
510 Fn(ForeignItemFn),
511
512 /// A foreign static item in an `extern` block: `static ext: u8`.
513 Static(ForeignItemStatic),
514
515 /// A foreign type in an `extern` block: `type void`.
516 Type(ForeignItemType),
517
518 /// A macro invocation within an extern block.
519 Macro(ForeignItemMacro),
520
521 /// Tokens in an `extern` block not interpreted by Syn.
522 Verbatim(TokenStream),
523
524 // For testing exhaustiveness in downstream code, use the following idiom:
525 //
526 // match item {
527 // #![cfg_attr(test, deny(non_exhaustive_omitted_patterns))]
528 //
529 // ForeignItem::Fn(item) => {...}
530 // ForeignItem::Static(item) => {...}
531 // ...
532 // ForeignItem::Verbatim(item) => {...}
533 //
534 // _ => { /* some sane fallback */ }
535 // }
536 //
537 // This way we fail your tests but don't break your library when adding
538 // a variant. You will be notified by a test failure when a variant is
539 // added, so that you can add code to handle it, but your library will
540 // continue to compile and work for downstream users in the interim.
541 }
542 }
543
544 ast_struct! {
545 /// A foreign function in an `extern` block.
546 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
547 pub struct ForeignItemFn {
548 pub attrs: Vec,
549 pub vis: Visibility,
550 pub sig: Signature,
551 pub semi_token: Token![;],
552 }
553 }
554
555 ast_struct! {
556 /// A foreign static item in an `extern` block: `static ext: u8`.
557 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
558 pub struct ForeignItemStatic {
559 pub attrs: Vec,
560 pub vis: Visibility,
561 pub static_token: Token![static],
562 pub mutability: StaticMutability,
563 pub ident: Ident,
564 pub colon_token: Token![:],
565 pub ty: Box,
566 pub semi_token: Token![;],
567 }
568 }
569
570 ast_struct! {
571 /// A foreign type in an `extern` block: `type void`.
572 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
573 pub struct ForeignItemType {
574 pub attrs: Vec,
575 pub vis: Visibility,
576 pub type_token: Token![type],
577 pub ident: Ident,
578 pub generics: Generics,
579 pub semi_token: Token![;],
580 }
581 }
582
583 ast_struct! {
584 /// A macro invocation within an extern block.
585 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
586 pub struct ForeignItemMacro {
587 pub attrs: Vec,
588 pub mac: Macro,
589 pub semi_token: Option,
590 }
591 }
592
593 ast_enum_of_structs! {
594 /// An item declaration within the definition of a trait.
595 ///
596 /// # Syntax tree enum
597 ///
598 /// This type is a [syntax tree enum].
599 ///
600 /// [syntax tree enum]: crate::expr::Expr#syntax-tree-enums
601 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
602 #[non_exhaustive]
603 pub enum TraitItem {
604 /// An associated constant within the definition of a trait.
605 Const(TraitItemConst),
606
607 /// An associated function within the definition of a trait.
608 Fn(TraitItemFn),
609
610 /// An associated type within the definition of a trait.
611 Type(TraitItemType),
612
613 /// A macro invocation within the definition of a trait.
614 Macro(TraitItemMacro),
615
616 /// Tokens within the definition of a trait not interpreted by Syn.
617 Verbatim(TokenStream),
618
619 // For testing exhaustiveness in downstream code, use the following idiom:
620 //
621 // match item {
622 // #![cfg_attr(test, deny(non_exhaustive_omitted_patterns))]
623 //
624 // TraitItem::Const(item) => {...}
625 // TraitItem::Fn(item) => {...}
626 // ...
627 // TraitItem::Verbatim(item) => {...}
628 //
629 // _ => { /* some sane fallback */ }
630 // }
631 //
632 // This way we fail your tests but don't break your library when adding
633 // a variant. You will be notified by a test failure when a variant is
634 // added, so that you can add code to handle it, but your library will
635 // continue to compile and work for downstream users in the interim.
636 }
637 }
638
639 ast_struct! {
640 /// An associated constant within the definition of a trait.
641 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
642 pub struct TraitItemConst {
643 pub attrs: Vec,
644 pub const_token: Token![const],
645 pub ident: Ident,
646 pub generics: Generics,
647 pub colon_token: Token![:],
648 pub ty: Type,
649 pub default: Option<(Token![=], Expr)>,
650 pub semi_token: Token![;],
651 }
652 }
653
654 ast_struct! {
655 /// An associated function within the definition of a trait.
656 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
657 pub struct TraitItemFn {
658 pub attrs: Vec,
659 pub sig: Signature,
660 pub default: Option,
661 pub semi_token: Option,
662 }
663 }
664
665 ast_struct! {
666 /// An associated type within the definition of a trait.
667 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
668 pub struct TraitItemType {
669 pub attrs: Vec,
670 pub type_token: Token![type],
671 pub ident: Ident,
672 pub generics: Generics,
673 pub colon_token: Option,
674 pub bounds: Punctuated,
675 pub default: Option<(Token![=], Type)>,
676 pub semi_token: Token![;],
677 }
678 }
679
680 ast_struct! {
681 /// A macro invocation within the definition of a trait.
682 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
683 pub struct TraitItemMacro {
684 pub attrs: Vec,
685 pub mac: Macro,
686 pub semi_token: Option,
687 }
688 }
689
690 ast_enum_of_structs! {
691 /// An item within an impl block.
692 ///
693 /// # Syntax tree enum
694 ///
695 /// This type is a [syntax tree enum].
696 ///
697 /// [syntax tree enum]: crate::expr::Expr#syntax-tree-enums
698 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
699 #[non_exhaustive]
700 pub enum ImplItem {
701 /// An associated constant within an impl block.
702 Const(ImplItemConst),
703
704 /// An associated function within an impl block.
705 Fn(ImplItemFn),
706
707 /// An associated type within an impl block.
708 Type(ImplItemType),
709
710 /// A macro invocation within an impl block.
711 Macro(ImplItemMacro),
712
713 /// Tokens within an impl block not interpreted by Syn.
714 Verbatim(TokenStream),
715
716 // For testing exhaustiveness in downstream code, use the following idiom:
717 //
718 // match item {
719 // #![cfg_attr(test, deny(non_exhaustive_omitted_patterns))]
720 //
721 // ImplItem::Const(item) => {...}
722 // ImplItem::Fn(item) => {...}
723 // ...
724 // ImplItem::Verbatim(item) => {...}
725 //
726 // _ => { /* some sane fallback */ }
727 // }
728 //
729 // This way we fail your tests but don't break your library when adding
730 // a variant. You will be notified by a test failure when a variant is
731 // added, so that you can add code to handle it, but your library will
732 // continue to compile and work for downstream users in the interim.
733 }
734 }
735
736 ast_struct! {
737 /// An associated constant within an impl block.
738 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
739 pub struct ImplItemConst {
740 pub attrs: Vec,
741 pub vis: Visibility,
742 pub defaultness: Option,
743 pub const_token: Token![const],
744 pub ident: Ident,
745 pub generics: Generics,
746 pub colon_token: Token![:],
747 pub ty: Type,
748 pub eq_token: Token![=],
749 pub expr: Expr,
750 pub semi_token: Token![;],
751 }
752 }
753
754 ast_struct! {
755 /// An associated function within an impl block.
756 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
757 pub struct ImplItemFn {
758 pub attrs: Vec,
759 pub vis: Visibility,
760 pub defaultness: Option,
761 pub sig: Signature,
762 pub block: Block,
763 }
764 }
765
766 ast_struct! {
767 /// An associated type within an impl block.
768 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
769 pub struct ImplItemType {
770 pub attrs: Vec,
771 pub vis: Visibility,
772 pub defaultness: Option,
773 pub type_token: Token![type],
774 pub ident: Ident,
775 pub generics: Generics,
776 pub eq_token: Token![=],
777 pub ty: Type,
778 pub semi_token: Token![;],
779 }
780 }
781
782 ast_struct! {
783 /// A macro invocation within an impl block.
784 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
785 pub struct ImplItemMacro {
786 pub attrs: Vec,
787 pub mac: Macro,
788 pub semi_token: Option,
789 }
790 }
791
792 ast_struct! {
793 /// A function signature in a trait or implementation: `unsafe fn
794 /// initialize(&self)`.
795 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
796 pub struct Signature {
797 pub constness: Option,
798 pub asyncness: Option,
799 pub unsafety: Option,
800 pub abi: Option,
801 pub fn_token: Token![fn],
802 pub ident: Ident,
803 pub generics: Generics,
804 pub paren_token: token::Paren,
805 pub inputs: Punctuated,
806 pub variadic: Option,
807 pub output: ReturnType,
808 }
809 }
810
811 impl Signature {
812 /// A method's `self` receiver, such as `&self` or `self: Box`.
813 pub fn receiver(&self) -> Option<&Receiver> {
814 let arg = self.inputs.first()?;
815 match arg {
816 FnArg::Receiver(receiver) => Some(receiver),
817 FnArg::Typed(_) => None,
818 }
819 }
820 }
821
822 ast_enum_of_structs! {
823 /// An argument in a function signature: the `n: usize` in `fn f(n: usize)`.
824 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
825 pub enum FnArg {
826 /// The `self` argument of an associated method.
827 Receiver(Receiver),
828
829 /// A function argument accepted by pattern and type.
830 Typed(PatType),
831 }
832 }
833
834 ast_struct! {
835 /// The `self` argument of an associated method.
836 ///
837 /// If `colon_token` is present, the receiver is written with an explicit
838 /// type such as `self: Box`. If `colon_token` is absent, the receiver
839 /// is written in shorthand such as `self` or `&self` or `&mut self`. In the
840 /// shorthand case, the type in `ty` is reconstructed as one of `Self`,
841 /// `&Self`, or `&mut Self`.
842 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
843 pub struct Receiver {
844 pub attrs: Vec,
845 pub reference: Option<(Token![&], Option)>,
846 pub mutability: Option,
847 pub self_token: Token![self],
848 pub colon_token: Option,
849 pub ty: Box,
850 }
851 }
852
853 impl Receiver {
854 pub fn lifetime(&self) -> Option<&Lifetime> {
855 self.reference.as_ref()?.1.as_ref()
856 }
857 }
858
859 ast_struct! {
860 /// The variadic argument of a foreign function.
861 ///
862 /// ```rust
863 /// # struct c_char;
864 /// # struct c_int;
865 /// #
866 /// extern "C" {
867 /// fn printf(format: *const c_char, ...) -> c_int;
868 /// // ^^^
869 /// }
870 /// ```
871 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
872 pub struct Variadic {
873 pub attrs: Vec,
874 pub pat: Option<(Box, Token![:])>,
875 pub dots: Token![...],
876 pub comma: Option,
877 }
878 }
879
880 ast_enum! {
881 /// The mutability of an `Item::Static` or `ForeignItem::Static`.
882 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
883 #[non_exhaustive]
884 pub enum StaticMutability {
885 Mut(Token![mut]),
886 None,
887 }
888 }
889
890 ast_enum! {
891 /// Unused, but reserved for RFC 3323 restrictions.
892 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
893 #[non_exhaustive]
894 pub enum ImplRestriction {}
895
896
897 // TODO: https://rust-lang.github.io/rfcs/3323-restrictions.html
898 //
899 // pub struct ImplRestriction {
900 // pub impl_token: Token![impl],
901 // pub paren_token: token::Paren,
902 // pub in_token: Option,
903 // pub path: Box,
904 // }
905 }
906
907 #[cfg(feature = "parsing")]
908 pub(crate) mod parsing {
909 use crate::attr::{self, Attribute};
910 use crate::buffer::Cursor;
911 use crate::derive;
912 use crate::error::{Error, Result};
913 use crate::expr::Expr;
914 use crate::ext::IdentExt as _;
915 use crate::generics::{self, Generics, TypeParamBound};
916 use crate::ident::Ident;
917 use crate::item::{
918 FnArg, ForeignItem, ForeignItemFn, ForeignItemMacro, ForeignItemStatic, ForeignItemType,
919 ImplItem, ImplItemConst, ImplItemFn, ImplItemMacro, ImplItemType, Item, ItemConst,
920 ItemEnum, ItemExternCrate, ItemFn, ItemForeignMod, ItemImpl, ItemMacro, ItemMod,
921 ItemStatic, ItemStruct, ItemTrait, ItemTraitAlias, ItemType, ItemUnion, ItemUse, Receiver,
922 Signature, StaticMutability, TraitItem, TraitItemConst, TraitItemFn, TraitItemMacro,
923 TraitItemType, UseGlob, UseGroup, UseName, UsePath, UseRename, UseTree, Variadic,
924 };
925 use crate::lifetime::Lifetime;
926 use crate::lit::LitStr;
927 use crate::mac::{self, Macro};
928 use crate::parse::discouraged::Speculative as _;
929 use crate::parse::{Parse, ParseStream};
930 use crate::pat::{Pat, PatType, PatWild};
931 use crate::path::Path;
932 use crate::punctuated::Punctuated;
933 use crate::restriction::Visibility;
934 use crate::stmt::Block;
935 use crate::token;
936 use crate::ty::{Abi, ReturnType, Type, TypePath, TypeReference};
937 use crate::verbatim;
938 use alloc::boxed::Box;
939 use alloc::vec::Vec;
940 use proc_macro2::TokenStream;
941
942 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
943 impl Parse for Item {
944 fn parse(input: ParseStream) -> Result {
945 let begin = input.cursor();
946 let attrs = input.call(Attribute::parse_outer)?;
947 parse_rest_of_item(begin, attrs, input)
948 }
949 }
950
951 pub(crate) fn parse_rest_of_item(
952 begin: Cursor,
953 mut attrs: Vec,
954 input: ParseStream,
955 ) -> Result
- {
956 let ahead = input.fork();
957 let vis: Visibility = ahead.parse()?;
958
959 let lookahead = ahead.lookahead1();
960 let allow_safe = false;
961 let mut item = if lookahead.peek(Token![fn]) || peek_signature(&ahead, allow_safe) {
962 let vis: Visibility = input.parse()?;
963 let sig: Signature = input.parse()?;
964 if input.peek(Token![;]) {
965 input.parse::()?;
966 Ok(Item::Verbatim(verbatim::between(begin, input.cursor())))
967 } else {
968 parse_rest_of_fn(input, Vec::new(), vis, sig).map(Item::Fn)
969 }
970 } else if lookahead.peek(Token![extern]) {
971 ahead.parse::()?;
972 let lookahead = ahead.lookahead1();
973 if lookahead.peek(Token![crate]) {
974 input.parse().map(Item::ExternCrate)
975 } else if lookahead.peek(token::Brace) {
976 input.parse().map(Item::ForeignMod)
977 } else if lookahead.peek(LitStr) {
978 ahead.parse::()?;
979 let lookahead = ahead.lookahead1();
980 if lookahead.peek(token::Brace) {
981 input.parse().map(Item::ForeignMod)
982 } else {
983 Err(lookahead.error())
984 }
985 } else {
986 Err(lookahead.error())
987 }
988 } else if lookahead.peek(Token![use]) {
989 let allow_crate_root_in_path = true;
990 match parse_item_use(input, allow_crate_root_in_path)? {
991 Some(item_use) => Ok(Item::Use(item_use)),
992 None => Ok(Item::Verbatim(verbatim::between(begin, input.cursor()))),
993 }
994 } else if lookahead.peek(Token![static]) {
995 let vis = input.parse()?;
996 let static_token = input.parse()?;
997 let mutability = input.parse()?;
998 let ident = input.parse()?;
999 if input.peek(Token![=]) {
1000 input.parse::()?;
1001 input.parse::()?;
1002 input.parse::()?;
1003 Ok(Item::Verbatim(verbatim::between(begin, input.cursor())))
1004 } else {
1005 let colon_token = input.parse()?;
1006 let ty = input.parse()?;
1007 if input.peek(Token![;]) {
1008 input.parse::()?;
1009 Ok(Item::Verbatim(verbatim::between(begin, input.cursor())))
1010 } else {
1011 Ok(Item::Static(ItemStatic {
1012 attrs: Vec::new(),
1013 vis,
1014 static_token,
1015 mutability,
1016 ident,
1017 colon_token,
1018 ty,
1019 eq_token: input.parse()?,
1020 expr: input.parse()?,
1021 semi_token: input.parse()?,
1022 }))
1023 }
1024 }
1025 } else if lookahead.peek(Token![const]) {
1026 let vis = input.parse()?;
1027 let const_token: Token![const] = input.parse()?;
1028 let lookahead = input.lookahead1();
1029 let ident = if lookahead.peek(Ident) || lookahead.peek(Token![_]) {
1030 input.call(Ident::parse_any)?
1031 } else {
1032 return Err(lookahead.error());
1033 };
1034 let mut generics: Generics = input.parse()?;
1035 let colon_token = input.parse()?;
1036 let ty = input.parse()?;
1037 let value = if let Some(eq_token) = input.parse::