Skip to content

Commit

Permalink
Document extended key-value attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 committed May 22, 2021
1 parent f34a622 commit dc4ecf1
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
>
> _AttrInput_ :\
>       [_DelimTokenTree_]\
> &nbsp;&nbsp; | `=` [_LiteralExpression_]<sub>_without suffix_</sub>
> &nbsp;&nbsp; | `=` [_Expression_]
An _attribute_ is a general, free-form metadatum that is interpreted according
to name, convention, language, and compiler version. Attributes are modeled
Expand All @@ -26,7 +26,7 @@ the bang after the hash, apply to the thing that follows the attribute.
The attribute consists of a path to the attribute, followed by an optional
delimited token tree whose interpretation is defined by the attribute.
Attributes other than macro attributes also allow the input to be an equals
sign (`=`) followed by a literal expression. See the [meta item
sign (`=`) followed by a expression. See the [meta item
syntax](#meta-item-attribute-syntax) below for more details.

Attributes can be classified into the following kinds:
Expand Down Expand Up @@ -94,18 +94,42 @@ attributes]. It has the following grammar:
> **<sup>Syntax</sup>**\
> _MetaItem_ :\
> &nbsp;&nbsp; &nbsp;&nbsp; [_SimplePath_]\
> &nbsp;&nbsp; | [_SimplePath_] `=` [_LiteralExpression_]<sub>_without suffix_</sub>\
> &nbsp;&nbsp; | [_SimplePath_] `=` [_Expression_]\
> &nbsp;&nbsp; | [_SimplePath_] `(` _MetaSeq_<sup>?</sup> `)`
>
> _MetaSeq_ :\
> &nbsp;&nbsp; _MetaItemInner_ ( `,` MetaItemInner )<sup>\*</sup> `,`<sup>?</sup>
>
> _MetaItemInner_ :\
> &nbsp;&nbsp; &nbsp;&nbsp; _MetaItem_\
> &nbsp;&nbsp; | [_LiteralExpression_]<sub>_without suffix_</sub>
> &nbsp;&nbsp; | [_Expression_]
Expressions in meta items must macro-expand to literal expressions, which must not
include integer or float type suffixes. Expressions which are not literal expressions
will be syntactically accepted (and can be passed to proc-macros), but will be rejected after parsing.

Note that if the attribute appears within another macro, it will be expanded
after that outer macro. For example, the following code will expand the
`Serialize` proc-macro first, which must preserve the `include_str!` call in
order for it to be expanded:

```rust ignore
#[derive(Serialize)]
struct Foo {
#[doc = include_str!("x.md")]
x: u32
}
```

Literal expressions in meta items must not include integer or float type
suffixes.
Additionally, macros in attributes will be expanded only after all other attributes applied to the item:

```rust ignore
#[macro_attr1] // expanded first
#[doc = mac!()] // `mac!` is expanded fourth.
#[macro_attr2] // expanded second
#[derive(MacroDerive1, MacroDerive2)] // expanded third
fn foo() {}
```

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

0 comments on commit dc4ecf1

Please sign in to comment.