1
1
# Attributes
2
2
3
3
> ** <sup >Syntax</sup >** \
4
- > _ Attribute_ :\
5
- >   ;  ; _ InnerAttribute_ | _ OuterAttribute_
6
- >
7
4
> _ InnerAttribute_ :\
8
- >   ;  ; ` #! [ ` MetaItem ` ] `
5
+ >   ;  ; ` # ` ` ! ` ` [ ` _ Attr _ ` ] `
9
6
>
10
7
> _ OuterAttribute_ :\
11
- >   ;  ; ` #[ ` MetaItem ` ] `
8
+ >   ;  ; ` # ` ` [ ` _ Attr _ ` ] `
12
9
>
13
- > _ MetaItem_ :\
14
- >   ;  ;   ;  ; [ _ SimplePath_ ] \
15
- >   ;  ; | [ _ SimplePath_ ] ` = ` [ _ LiteralExpression_ ] <sub >_ without suffix_ </sub >\
16
- >   ;  ; | [ _ SimplePath_ ] ` ( ` _ MetaSeq_ <sup >?</sup > ` ) `
10
+ > _ Attr_ :\
11
+ >   ;  ; [ _ SimplePath_ ] _ AttrInput_ <sup >?</sup >
17
12
>
18
- > _ MetaSeq_ :\
19
- >   ;  ; _ MetaItemInner_ ( ` , ` MetaItemInner )<sup >\* </sup > ` , ` <sup >?</sup >
20
- >
21
- > _ MetaItemInner_ :\
22
- >   ;  ;   ;  ; _ MetaItem_ \
23
- >   ;  ; | [ _ LiteralExpression_ ] <sub >_ without suffix_ </sub >
13
+ > _ AttrInput_ :\
14
+ >   ;  ;   ;  ; [ _ DelimTokenTree_ ] \
15
+ >   ;  ; | ` = ` [ _ LiteralExpression_ ] <sub >_ without suffix_ </sub >
24
16
25
17
An _ attribute_ is a general, free-form metadatum that is interpreted according
26
18
to name, convention, and language and compiler version. Attributes are modeled
27
19
on Attributes in [ ECMA-335] , with the syntax coming from [ ECMA-334] \( C#).
28
20
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.
30
24
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.
36
30
37
- Literal values must not include integer or float type suffixes.
31
+ Attributes can be classified into the following kinds:
38
32
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 )
42
37
43
38
Attributes may be applied to many things in the language:
44
39
@@ -87,11 +82,33 @@ fn some_unused_variables() {
87
82
}
88
83
```
89
84
90
- There are three kinds of attributes:
85
+ ## Meta Item Attribute Syntax
91
86
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
+ >   ;  ;   ;  ; [ _ SimplePath_ ] \
93
+ >   ;  ; | [ _ SimplePath_ ] ` = ` [ _ LiteralExpression_ ] <sub >_ without suffix_ </sub >\
94
+ >   ;  ; | [ _ SimplePath_ ] ` ( ` _ MetaSeq_ <sup >?</sup > ` ) `
95
+ >
96
+ > _ MetaSeq_ :\
97
+ >   ;  ; _ MetaItemInner_ ( ` , ` MetaItemInner )<sup >\* </sup > ` , ` <sup >?</sup >
98
+ >
99
+ > _ MetaItemInner_ :\
100
+ >   ;  ;   ;  ; _ MetaItem_ \
101
+ >   ;  ; | [ _ 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)) `
95
112
96
113
## Active and inert attributes
97
114
@@ -139,28 +156,18 @@ names have meaning.
139
156
140
157
On an ` extern ` block, the following attributes are interpreted:
141
158
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).
145
159
- ` link ` - indicate that a native library should be linked to for the
146
160
declarations in this block to be linked correctly. ` link ` supports an optional
147
161
` kind ` key with three possible values: ` dylib ` , ` static ` , and ` framework ` . See
148
162
[ external blocks] ( items/external-blocks.html ) for more about external blocks.
149
163
Two examples: ` #[link(name = "readline")] ` and
150
164
` #[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.
156
165
157
166
On declarations inside an ` extern ` block, the following attributes are
158
167
interpreted:
159
168
160
169
- ` link_name ` - the name of the symbol that this function or static should be
161
170
imported as.
162
- - ` linkage ` - on a static, this specifies the [ linkage
163
- type] ( http://llvm.org/docs/LangRef.html#linkage-types ) .
164
171
165
172
See [ type layout] ( type-layout.html ) for documentation on the ` repr ` attribute
166
173
which can be used to control type layout.
@@ -175,8 +182,6 @@ which can be used to control type layout.
175
182
macros named. The ` extern crate ` must appear at the crate root, not inside
176
183
` mod ` , which ensures proper function of the ` $crate ` macro variable.
177
184
178
- - ` macro_reexport ` on an ` extern crate ` — re-export the named macros.
179
-
180
185
- ` macro_export ` - export a ` macro_rules ` macro for cross-crate usage.
181
186
182
187
- ` 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
371
376
372
377
Otherwise, they work just like regular lint attributes:
373
378
374
-
375
- ``` rust,ignore
379
+ ``` rust
376
380
// set the entire `pedantic` clippy lint group to warn
377
381
#![warn(clippy:: pedantic)]
378
382
// silence warnings from the `filter_map` clippy lint
@@ -552,6 +556,32 @@ impl<T: PartialEq> PartialEq for Foo<T> {
552
556
553
557
You can implement ` derive ` for your own traits through [ procedural macros] .
554
558
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
555
585
[ _LiteralExpression_ ] : expressions/literal-expr.html
556
586
[ _SimplePath_ ] : paths.html#simple-paths
557
587
[ `no_implicit_prelude` ] : items/modules.html#prelude-items
@@ -585,6 +615,7 @@ You can implement `derive` for your own traits through [procedural macros].
585
615
[ external blocks ] : items/external-blocks.html
586
616
[ items ] : items.html
587
617
[ attribute macro ] : procedural-macros.html#attribute-macros
618
+ [ derive mode helper attributes ] : procedural-macros.html#derive-mode-helper-attributes
588
619
[ function-like macro ] : procedural-macros.html#function-like-procedural-macros
589
620
[ conditional compilation ] : conditional-compilation.html
590
621
[ derive mode macro ] : procedural-macros.html#derive-mode-macros
@@ -594,3 +625,4 @@ You can implement `derive` for your own traits through [procedural macros].
594
625
[ where clause ] : items/where-clauses.html
595
626
[ trait or lifetime bounds ] : trait-bounds.html
596
627
[ Expression Attributes ] : expressions.html#expression-attributes
628
+ [ `meta` macro fragment specifier ] : macros-by-example.html
0 commit comments