13
13
>
14
14
> _ AttrInput_ :\
15
15
>   ;  ;   ;  ; [ _ DelimTokenTree_ ] \
16
- >   ;  ; | ` = ` [ _ LiteralExpression _ ] < sub > _ without suffix _ </ sub >
16
+ >   ;  ; | ` = ` [ _ Expression _ ]
17
17
18
18
An _ attribute_ is a general, free-form metadatum that is interpreted according
19
19
to name, convention, language, and compiler version. Attributes are modeled
@@ -26,7 +26,7 @@ the bang after the hash, apply to the thing that follows the attribute.
26
26
The attribute consists of a path to the attribute, followed by an optional
27
27
delimited token tree whose interpretation is defined by the attribute.
28
28
Attributes other than macro attributes also allow the input to be an equals
29
- sign (` = ` ) followed by a literal expression. See the [ meta item
29
+ sign (` = ` ) followed by a expression. See the [ meta item
30
30
syntax] ( #meta-item-attribute-syntax ) below for more details.
31
31
32
32
Attributes can be classified into the following kinds:
@@ -94,18 +94,42 @@ attributes]. It has the following grammar:
94
94
> ** <sup >Syntax</sup >** \
95
95
> _ MetaItem_ :\
96
96
>   ;  ;   ;  ; [ _ SimplePath_ ] \
97
- >   ;  ; | [ _ SimplePath_ ] ` = ` [ _ LiteralExpression _ ] < sub > _ without suffix _ </ sub > \
97
+ >   ;  ; | [ _ SimplePath_ ] ` = ` [ _ Expression _ ] \
98
98
>   ;  ; | [ _ SimplePath_ ] ` ( ` _ MetaSeq_ <sup >?</sup > ` ) `
99
99
>
100
100
> _ MetaSeq_ :\
101
101
>   ;  ; _ MetaItemInner_ ( ` , ` MetaItemInner )<sup >\* </sup > ` , ` <sup >?</sup >
102
102
>
103
103
> _ MetaItemInner_ :\
104
104
>   ;  ;   ;  ; _ MetaItem_ \
105
- >   ;  ; | [ _ LiteralExpression_ ] <sub >_ without suffix_ </sub >
105
+ >   ;  ; | [ _ Expression_ ]
106
+
107
+ Expressions in meta items must macro-expand to literal expressions, which must not
108
+ include integer or float type suffixes. Expressions which are not literal expressions
109
+ will be syntactically accepted (and can be passed to proc-macros), but will be rejected after parsing.
110
+
111
+ Note that if the attribute appears within another macro, it will be expanded
112
+ after that outer macro. For example, the following code will expand the
113
+ ` Serialize ` proc-macro first, which must preserve the ` include_str! ` call in
114
+ order for it to be expanded:
115
+
116
+ ``` rust ignore
117
+ #[derive(Serialize )]
118
+ struct Foo {
119
+ #[doc = include_str! (" x.md" )]
120
+ x : u32
121
+ }
122
+ ```
106
123
107
- Literal expressions in meta items must not include integer or float type
108
- suffixes.
124
+ Additionally, macros in attributes will be expanded only after all other attributes applied to the item:
125
+
126
+ ``` rust ignore
127
+ #[macro_attr1] // expanded first
128
+ #[doc = mac! ()] // `mac!` is expanded fourth.
129
+ #[macro_attr2] // expanded second
130
+ #[derive(MacroDerive1 , MacroDerive2 )] // expanded third
131
+ fn foo () {}
132
+ ```
109
133
110
134
Various built-in attributes use different subsets of the meta item syntax to
111
135
specify their inputs. The following grammar rules show some commonly used
0 commit comments