,
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 ///
722 ///
723 /// Important: see [Compatibility notes][crate#verbatim-variants].
724 ///
725 ///
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,
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,
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,
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,
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,
779 pub mac: Macro,
780 pub semi_token: Option,
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 ///
810 ///
811 /// Important: see [Compatibility notes][crate#verbatim-variants].
812 ///
813 ///
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,
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,
840 /// (Non-exhaustive) Additional optional information about a function.
841 pub modifiers: FnModifiers,
842 pub sig: Signature,
843 pub default: Option,
844 pub semi_token: Option,
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,
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,
859 pub bounds: Punctuated,
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,
870 pub mac: Macro,
871 pub semi_token: Option,
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 ///
901 ///
902 /// Important: see [Compatibility notes][crate#verbatim-variants].
903 ///
904 ///
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,
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,
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,
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,
963 pub mac: Macro,
964 pub semi_token: Option,
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,
974 pub asyncness: Option,
975 pub safety: Safety,
976 pub abi: Option,
977 pub fn_token: Token![fn],
978 pub ident: Ident,
979 pub generics: Generics,
980 pub paren_token: token::Paren,
981 pub inputs: Punctuated,
982 pub variadic: Option,
983 pub output: ReturnType,
984 }
985 }
986
987 impl Signature {
988 /// A method's `self` receiver, such as `&self` or `self: Box`.
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,
1034 pub mutability: Option,
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, Option),
1049 /// `self: Box`
1050 Typed(Token![:], Box),
1051
1052 // TODO: https://github.com/rust-lang/rust/issues/123076
1053 // Pin(Token![&], Option, 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,
1072 pub pat: Option<(Box, Token![:])>,
1073 pub dots: Token![...],
1074 pub comma: Option,
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 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 where T: 'static = T; }
1101 /// | ^^^^^^^^^^^^^^^^
1102 /// = note: see issue #89122 for more information
1103 /// = note: `#[warn(deprecated_where_clause_location)]` on by default
1104 /// ```
1105 Early,
1106
1107 /// `type Ty = 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 where T: 'static;
1116 /// | ^^^^^^^^^^^^^^^^
1117 /// = note: see issue #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 {
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,
1181 input: ParseStream,
1182 ) -> Result- {
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::()?;
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::()?;
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::()?;
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::()?;
1228 input.parse::()?;
1229 input.parse::()?;
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::()?;
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::