Skip to content

Commit 480069d

Browse files
committed
Document extended key-value attributes
1 parent f34a622 commit 480069d

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

src/attributes.md

+30-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
>
1414
> _AttrInput_ :\
1515
>       [_DelimTokenTree_]\
16-
> &nbsp;&nbsp; | `=` [_LiteralExpression_]<sub>_without suffix_</sub>
16+
> &nbsp;&nbsp; | `=` [_Expression_]
1717
1818
An _attribute_ is a general, free-form metadatum that is interpreted according
1919
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.
2626
The attribute consists of a path to the attribute, followed by an optional
2727
delimited token tree whose interpretation is defined by the attribute.
2828
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
3030
syntax](#meta-item-attribute-syntax) below for more details.
3131

3232
Attributes can be classified into the following kinds:
@@ -94,18 +94,42 @@ attributes]. It has the following grammar:
9494
> **<sup>Syntax</sup>**\
9595
> _MetaItem_ :\
9696
> &nbsp;&nbsp; &nbsp;&nbsp; [_SimplePath_]\
97-
> &nbsp;&nbsp; | [_SimplePath_] `=` [_LiteralExpression_]<sub>_without suffix_</sub>\
97+
> &nbsp;&nbsp; | [_SimplePath_] `=` [_Expression_]\
9898
> &nbsp;&nbsp; | [_SimplePath_] `(` _MetaSeq_<sup>?</sup> `)`
9999
>
100100
> _MetaSeq_ :\
101101
> &nbsp;&nbsp; _MetaItemInner_ ( `,` MetaItemInner )<sup>\*</sup> `,`<sup>?</sup>
102102
>
103103
> _MetaItemInner_ :\
104104
> &nbsp;&nbsp; &nbsp;&nbsp; _MetaItem_\
105-
> &nbsp;&nbsp; | [_LiteralExpression_]<sub>_without suffix_</sub>
105+
> &nbsp;&nbsp; | [_Expression_]
106106
107-
Literal expressions in meta items must not include integer or float type
108-
suffixes.
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
117+
#[derive(Serialize)]
118+
struct Foo {
119+
#[doc = include_str!("x.md")]
120+
x: u32
121+
}
122+
```
123+
124+
Additionally, macros in attributes will be expanded only after all other attributes applied to the item:
125+
126+
```rust
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+
```
109133

110134
Various built-in attributes use different subsets of the meta item syntax to
111135
specify their inputs. The following grammar rules show some commonly used

0 commit comments

Comments
 (0)