Skip to content

Commit 55bf168

Browse files
committed
Update attribute documentation.
1 parent c3dd948 commit 55bf168

5 files changed

+113
-69
lines changed

src/attributes.md

+75-43
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,39 @@
11
# Attributes
22

33
> **<sup>Syntax</sup>**\
4-
> _Attribute_ :\
5-
> &nbsp;&nbsp; _InnerAttribute_ | _OuterAttribute_
6-
>
74
> _InnerAttribute_ :\
8-
> &nbsp;&nbsp; `#![` MetaItem `]`
5+
> &nbsp;&nbsp; `#` `!` `[` _Attr_ `]`
96
>
107
> _OuterAttribute_ :\
11-
> &nbsp;&nbsp; `#[` MetaItem `]`
8+
> &nbsp;&nbsp; `#` `[` _Attr_ `]`
129
>
13-
> _MetaItem_ :\
14-
> &nbsp;&nbsp; &nbsp;&nbsp; [_SimplePath_]\
15-
> &nbsp;&nbsp; | [_SimplePath_] `=` [_LiteralExpression_]<sub>_without suffix_</sub>\
16-
> &nbsp;&nbsp; | [_SimplePath_] `(` _MetaSeq_<sup>?</sup> `)`
10+
> _Attr_ :\
11+
> &nbsp;&nbsp; [_SimplePath_] _AttrInput_<sup>?</sup>
1712
>
18-
> _MetaSeq_ :\
19-
> &nbsp;&nbsp; _MetaItemInner_ ( `,` MetaItemInner )<sup>\*</sup> `,`<sup>?</sup>
20-
>
21-
> _MetaItemInner_ :\
22-
> &nbsp;&nbsp; &nbsp;&nbsp; _MetaItem_\
23-
> &nbsp;&nbsp; | [_LiteralExpression_]<sub>_without suffix_</sub>
13+
> _AttrInput_ :\
14+
> &nbsp;&nbsp; &nbsp;&nbsp; [_DelimTokenTree_]\
15+
> &nbsp;&nbsp; | `=` [_LiteralExpression_]<sub>_without suffix_</sub>
2416
2517
An _attribute_ is a general, free-form metadatum that is interpreted according
2618
to name, convention, and language and compiler version. Attributes are modeled
2719
on Attributes in [ECMA-335], with the syntax coming from [ECMA-334] \(C#).
2820

29-
Attributes may appear as any of:
21+
_Inner attributes_, written with a bang (`!`) after the hash (`#`), apply to the
22+
item that the attribute is declared within. _Outer attributes_, written without
23+
the bang after the hash, apply to the thing that follows the attribute.
3024

31-
* A single identifier, the _attribute name_
32-
* An identifier followed by the equals sign '=' and a literal, providing a
33-
key/value pair
34-
* An identifier followed by a parenthesized list of sub-attribute arguments
35-
which include literals
25+
The attribute consists of a path to the attribute, followed by an optional
26+
delimited token tree whose interpretation is defined by the attribute.
27+
Attributes other than macro attributes also allow the input to be an equals
28+
sign (`=`) followed by a literal expression. See the [meta item
29+
syntax](#meta-item-attribute-syntax) below for more details.
3630

37-
Literal values must not include integer or float type suffixes.
31+
Attributes can be classified into the following kinds:
3832

39-
_Inner attributes_, written with a bang ("!") after the hash ("#"), apply to the
40-
item that the attribute is declared within. _Outer attributes_, written without
41-
the bang after the hash, apply to the thing that follows the attribute.
33+
* Built-in attributes
34+
* [Macro attributes][attribute macro]
35+
* [Derive mode helper attributes]
36+
* [Tool attributes](#tool-attributes)
4237

4338
Attributes may be applied to many things in the language:
4439

@@ -87,11 +82,33 @@ fn some_unused_variables() {
8782
}
8883
```
8984

90-
There are three kinds of attributes:
85+
## Meta Item Attribute Syntax
9186

92-
* Built-in attributes
93-
* Macro attributes
94-
* Derive mode helper attributes
87+
A "meta item" is the syntax used for the _Attr_ rule by built-in attributes
88+
and the [`meta` macro fragment specifier]. It has the following grammar:
89+
90+
> **<sup>Syntax</sup>**\
91+
> _MetaItem_ :\
92+
> &nbsp;&nbsp; &nbsp;&nbsp; [_SimplePath_]\
93+
> &nbsp;&nbsp; | [_SimplePath_] `=` [_LiteralExpression_]<sub>_without suffix_</sub>\
94+
> &nbsp;&nbsp; | [_SimplePath_] `(` _MetaSeq_<sup>?</sup> `)`
95+
>
96+
> _MetaSeq_ :\
97+
> &nbsp;&nbsp; _MetaItemInner_ ( `,` MetaItemInner )<sup>\*</sup> `,`<sup>?</sup>
98+
>
99+
> _MetaItemInner_ :\
100+
> &nbsp;&nbsp; &nbsp;&nbsp; _MetaItem_\
101+
> &nbsp;&nbsp; | [_LiteralExpression_]<sub>_without suffix_</sub>
102+
103+
Literal expressions in meta items must not include integer or float type
104+
suffixes.
105+
106+
Some examples of meta items are:
107+
- `no_std`
108+
- `doc = "example"`
109+
- `cfg(any())`
110+
- `deprecated(since = "1.2.0", note = "text")`
111+
- `repr(align(32))`
95112

96113
## Active and inert attributes
97114

@@ -139,28 +156,18 @@ names have meaning.
139156

140157
On an `extern` block, the following attributes are interpreted:
141158

142-
- `link_args` - specify arguments to the linker, rather than just the library
143-
name and type. This is feature gated and the exact behavior is
144-
implementation-defined (due to variety of linker invocation syntax).
145159
- `link` - indicate that a native library should be linked to for the
146160
declarations in this block to be linked correctly. `link` supports an optional
147161
`kind` key with three possible values: `dylib`, `static`, and `framework`. See
148162
[external blocks](items/external-blocks.html) for more about external blocks.
149163
Two examples: `#[link(name = "readline")]` and
150164
`#[link(name = "CoreFoundation", kind = "framework")]`.
151-
- `linked_from` - indicates what native library this block of FFI items is
152-
coming from. This attribute is of the form `#[linked_from = "foo"]` where
153-
`foo` is the name of a library in either `#[link]` or a `-l` flag. This
154-
attribute is currently required to export symbols from a Rust dynamic library
155-
on Windows, and it is feature gated behind the `linked_from` feature.
156165

157166
On declarations inside an `extern` block, the following attributes are
158167
interpreted:
159168

160169
- `link_name` - the name of the symbol that this function or static should be
161170
imported as.
162-
- `linkage` - on a static, this specifies the [linkage
163-
type](http://llvm.org/docs/LangRef.html#linkage-types).
164171

165172
See [type layout](type-layout.html) for documentation on the `repr` attribute
166173
which can be used to control type layout.
@@ -175,8 +182,6 @@ which can be used to control type layout.
175182
macros named. The `extern crate` must appear at the crate root, not inside
176183
`mod`, which ensures proper function of the `$crate` macro variable.
177184

178-
- `macro_reexport` on an `extern crate` — re-export the named macros.
179-
180185
- `macro_export` - export a `macro_rules` macro for cross-crate usage.
181186

182187
- `no_link` on an `extern crate` — even if we load this crate for macros, don't
@@ -371,8 +376,7 @@ They only get checked when the associated tool is active, so if you try to use a
371376

372377
Otherwise, they work just like regular lint attributes:
373378

374-
375-
```rust,ignore
379+
```rust
376380
// set the entire `pedantic` clippy lint group to warn
377381
#![warn(clippy::pedantic)]
378382
// silence warnings from the `filter_map` clippy lint
@@ -552,6 +556,32 @@ impl<T: PartialEq> PartialEq for Foo<T> {
552556

553557
You can implement `derive` for your own traits through [procedural macros].
554558

559+
## Tool attributes
560+
561+
The compiler may allow attributes for external tools where each tool resides
562+
in its own namespace. The first segment of the attribute path is the name of
563+
the tool, with one or more additional segments whose interpretation is up to
564+
the tool.
565+
566+
When a tool is not in use, the tool's attributes are accepted without a
567+
warning. When the tool is in use, the tool is responsible for processing and
568+
interpretation of its attributes.
569+
570+
Tool attributes are not available if the [`no_implicit_prelude`] attribute is
571+
used.
572+
573+
```rust
574+
// Tells the rustfmt tool to not format the following element.
575+
#[rustfmt::skip]
576+
struct S {
577+
}
578+
579+
// Controls the "cyclomatic complexity" threshold for the cLippy tool.
580+
#[clippy::cyclomatic_complexity = "100"]
581+
pub fn f() {}
582+
```
583+
584+
[_DelimTokenTree_]: macros.html
555585
[_LiteralExpression_]: expressions/literal-expr.html
556586
[_SimplePath_]: paths.html#simple-paths
557587
[`no_implicit_prelude`]: items/modules.html#prelude-items
@@ -585,6 +615,7 @@ You can implement `derive` for your own traits through [procedural macros].
585615
[external blocks]: items/external-blocks.html
586616
[items]: items.html
587617
[attribute macro]: procedural-macros.html#attribute-macros
618+
[derive mode helper attributes]: procedural-macros.html#derive-mode-helper-attributes
588619
[function-like macro]: procedural-macros.html#function-like-procedural-macros
589620
[conditional compilation]: conditional-compilation.html
590621
[derive mode macro]: procedural-macros.html#derive-mode-macros
@@ -594,3 +625,4 @@ You can implement `derive` for your own traits through [procedural macros].
594625
[where clause]: items/where-clauses.html
595626
[trait or lifetime bounds]: trait-bounds.html
596627
[Expression Attributes]: expressions.html#expression-attributes
628+
[`meta` macro fragment specifier]: macros-by-example.html

src/conditional-compilation.md

+22-10
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Example values:
104104
* `"android"`
105105
* `"freebsd"`
106106
* `"dragonfly"`
107-
* `"bitrig"`
107+
* `"bitrig"`
108108
* `"openbsd"`
109109
* `"netbsd"`
110110

@@ -151,7 +151,7 @@ Key-value option set once with the target's pointer width in bits. For example,
151151
for targets with 32-bit pointers, this is set to `"32"`. Likewise, it is set
152152
to `"64"` for targets with 64-bit pointers.
153153

154-
<!-- Are there targets that have a different bit number? -->
154+
<!-- Are there targets that have a different bit number? -->
155155

156156
### `target_has_atomic`
157157

@@ -249,16 +249,16 @@ generic parameters.
249249

250250
> **<sup>Syntax</sup>**\
251251
> _CfgAttrAttribute_ :\
252-
> &nbsp;&nbsp; `cfg_attr` `(` _ConfigurationPredicate_ `,` [_MetaItem_] `,`<sup>?</sup> `)`
252+
> &nbsp;&nbsp; `cfg_attr` `(` _ConfigurationPredicate_ `,` _CfgAttrs_<sup>?</sup> `)`
253+
>
254+
> _CfgAttrs_ :\
255+
> &nbsp;&nbsp; [_Attr_]&nbsp;(`,` [_Attr_])<sup>\*</sup> `,`<sup>?</sup>
253256
254257
The `cfg_attr` [attribute] conditionally includes [attributes] based on a
255258
configuration predicate.
256259

257-
It is written as `cfg_attr` followed by `(`, a configuration predicate, a
258-
[metaitem], an optional `,`, and finally a `)`.
259-
260-
When the configuration predicate is true, this attribute expands out to be an
261-
attribute of the attribute metaitem. For example, the following module will
260+
When the configuration predicate is true, this attribute expands out to the
261+
attributes listed after the predicate. For example, the following module will
262262
either be found at `linux.rs` or `windows.rs` based on the target.
263263

264264
```rust,ignore
@@ -267,6 +267,19 @@ either be found at `linux.rs` or `windows.rs` based on the target.
267267
mod os;
268268
```
269269

270+
Zero, one, or more attributes may be listed. Multiple attributes will each be
271+
expanded into separate attributes. For example:
272+
273+
```rust,ignore
274+
#[cfg_attr(feature = "magic", sparkles, crackles)]
275+
fn bewitched() {}
276+
277+
// When the `magic` feature flag is enabled, the above will expand to:
278+
#[sparkles]
279+
#[crackles]
280+
fn bewitched() {}
281+
```
282+
270283
> **Note**: The `cfg_attr` can expand to another `cfg_attr`. For example,
271284
> `#[cfg_attr(linux, cfg_attr(feature = "multithreaded", some_other_attribute))`
272285
> is valid. This example would be equivalent to
@@ -298,7 +311,7 @@ println!("I'm running on a {} machine!", machine_kind);
298311
[IDENTIFIER]: identifiers.html
299312
[RAW_STRING_LITERAL]: tokens.html#raw-string-literals
300313
[STRING_LITERAL]: tokens.html#string-literals
301-
[_MetaItem_]: attributes.html
314+
[_Attr_]: attributes.html
302315
[`--cfg`]: ../rustc/command-line-arguments.html#a--cfg-configure-the-compilation-environment
303316
[`--test`]: ../rustc/command-line-arguments.html#a--test-build-a-test-harness
304317
[`cfg`]: #the-cfg-attribute
@@ -310,4 +323,3 @@ println!("I'm running on a {} machine!", machine_kind);
310323
[crate type]: linkage.html
311324
[expressions]: expressions.html
312325
[items]: items.html
313-
[metaitem]: attributes.html

src/macros-by-example.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ syntax named by _designator_. Valid designators are:
8080
[_Expression_]: expressions.html
8181
[_Item_]: items.html
8282
[_LiteralExpression_]: expressions/literal-expr.html
83-
[_MetaItem_]: attributes.html
83+
[_MetaItem_]: attributes.html#meta-item-attribute-syntax
8484
[_Pattern_]: patterns.html
8585
[_Statement_]: statements.html
8686
[_TokenTree_]: macros.html#macro-invocation

src/procedural-macros.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ These macros are defined by a [public]&#32;[function] with the `proc_macro`
7777
[`TokenStream`] is what is inside the delimiters of the macro invocation and the
7878
output [`TokenStream`] replaces the entire macro invocation. It may contain an
7979
arbitrary number of [items]. These macros cannot expand to syntax that defines
80-
new `macro_rule` style macros.
80+
new `macro_rules` style macros.
8181
8282
For example, the following macro definition ignores its input and outputs a
8383
function `answer` into its scope.
@@ -195,14 +195,14 @@ struct Struct {
195195
*Attribute macros* define new [attributes] which can be attached to [items].
196196

197197
Attribute macros are defined by a [public]&#32;[function] with the
198-
`proc_macro_attribute` [attribute] that a signature of
199-
`(TokenStream, TokenStream) -> TokenStream`. The first [`TokenStream`] is the
200-
attribute's metaitems, not including the delimiters. If the attribute is written
201-
without a metaitem, the attribute [`TokenStream`] is empty. The second
202-
[`TokenStream`] is of the rest of the [item] including other [attributes] on the
203-
[item]. The returned [`TokenStream`] replaces the [item] with an arbitrary
204-
number of [items]. These macros cannot expand to syntax that defines new
205-
`macro_rule` style macros.
198+
`proc_macro_attribute` [attribute] that a signature of `(TokenStream,
199+
TokenStream) -> TokenStream`. The first [`TokenStream`] is the delimited token
200+
tree following the attribute's name, not including the outer delimiters. If
201+
the attribute is written as a bare attribute name, the attribute
202+
[`TokenStream`] is empty. The second [`TokenStream`] is the rest of the [item]
203+
including other [attributes] on the [item]. The returned [`TokenStream`]
204+
replaces the [item] with an arbitrary number of [items]. These macros cannot
205+
expand to syntax that defines new `macro_rules` style macros.
206206

207207
For example, this attribute macro takes the input stream and returns it as is,
208208
effectively being the no-op of attributes.
@@ -247,16 +247,16 @@ fn invoke1() {}
247247
// out: attr: ""
248248
// out: item: "fn invoke1() { }"
249249
250-
// Example: Attribute has a metaitem
250+
// Example: Attribute with input
251251
#[show_streams(bar)]
252252
fn invoke2() {}
253253
// out: attr: "bar"
254254
// out: item: "fn invoke2() {}"
255255
256-
// Example: Multiple words in metaitem
257-
#[show_streams(multiple words)]
256+
// Example: Multiple tokens in the input
257+
#[show_streams(multiple => tokens)]
258258
fn invoke3() {}
259-
// out: attr: "multiple words"
259+
// out: attr: "multiple => tokens"
260260
// out: item: "fn invoke3() {}"
261261
262262
// Example:

src/type-layout.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ The possible representations for a type are the default representation, `C`, the
116116
primitive representations, and `packed`. Multiple representations can be applied
117117
to a single type.
118118

119-
The representation of a type can be changed by applying the [`repr` attribute]
119+
The representation of a type can be changed by applying the `repr` attribute
120120
to it. The following example shows a struct with a `C` representation.
121121

122122
```
@@ -332,4 +332,4 @@ used with any other representation.
332332
[zero-variant enumerations]: items/enumerations.html#zero-variant-enums
333333
[undefined behavior]: behavior-considered-undefined.html
334334
[27060]: https://github.com/rust-lang/rust/issues/27060
335-
[`PhantomData<T>`]: special-types-and-traits.html#phantomdatat
335+
[`PhantomData<T>`]: special-types-and-traits.html#phantomdatat

0 commit comments

Comments
 (0)