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 ` ] `
12
- >
13
- > _ MetaItem_ :\
14
- >   ;  ;   ;  ; [ _ SimplePath_ ] \
15
- >   ;  ; | [ _ SimplePath_ ] ` = ` [ _ LiteralExpression_ ] <sub >_ without suffix_ </sub >\
16
- >   ;  ; | [ _ SimplePath_ ] ` ( ` _ MetaSeq_ <sup >?</sup > ` ) `
8
+ >   ;  ; ` # ` ` [ ` _ Attr_ ` ] `
17
9
>
18
- > _ MetaSeq _ :\
19
- >   ;  ; _ MetaItemInner _ ( ` , ` MetaItemInner )< sup > \* </ sup > ` , ` <sup >?</sup >
10
+ > _ Attr _ :\
11
+ >   ;  ; [ _ SimplePath _ ] _ AttrInput _ <sup >?</sup >
20
12
>
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 macro 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,34 @@ 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 macro helper attributes
87
+ A "meta item" is the syntax used for the _ Attr_ rule by most built-in
88
+ attributes and the [ ` meta ` macro fragment specifier] . It has the following
89
+ grammar:
90
+
91
+ > ** <sup >Syntax</sup >** \
92
+ > _ MetaItem_ :\
93
+ >   ;  ;   ;  ; [ _ SimplePath_ ] \
94
+ >   ;  ; | [ _ SimplePath_ ] ` = ` [ _ LiteralExpression_ ] <sub >_ without suffix_ </sub >\
95
+ >   ;  ; | [ _ SimplePath_ ] ` ( ` _ MetaSeq_ <sup >?</sup > ` ) `
96
+ >
97
+ > _ MetaSeq_ :\
98
+ >   ;  ; _ MetaItemInner_ ( ` , ` MetaItemInner )<sup >\* </sup > ` , ` <sup >?</sup >
99
+ >
100
+ > _ MetaItemInner_ :\
101
+ >   ;  ;   ;  ; _ MetaItem_ \
102
+ >   ;  ; | [ _ LiteralExpression_ ] <sub >_ without suffix_ </sub >
103
+
104
+ Literal expressions in meta items must not include integer or float type
105
+ suffixes.
106
+
107
+ Some examples of meta items are:
108
+ - ` no_std `
109
+ - ` doc = "example" `
110
+ - ` cfg(any()) `
111
+ - ` deprecated(since = "1.2.0", note = "text") `
112
+ - ` repr(align(32)) `
95
113
96
114
## Active and inert attributes
97
115
@@ -139,28 +157,18 @@ names have meaning.
139
157
140
158
On an ` extern ` block, the following attributes are interpreted:
141
159
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
160
- ` link ` - indicate that a native library should be linked to for the
146
161
declarations in this block to be linked correctly. ` link ` supports an optional
147
162
` kind ` key with three possible values: ` dylib ` , ` static ` , and ` framework ` . See
148
163
[ external blocks] ( items/external-blocks.html ) for more about external blocks.
149
164
Two examples: ` #[link(name = "readline")] ` and
150
165
` #[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
166
157
167
On declarations inside an ` extern ` block, the following attributes are
158
168
interpreted:
159
169
160
170
- ` link_name ` - the name of the symbol that this function or static should be
161
171
imported as.
162
- - ` linkage ` - on a static, this specifies the [ linkage
163
- type] ( http://llvm.org/docs/LangRef.html#linkage-types ) .
164
172
165
173
See [ type layout] ( type-layout.html ) for documentation on the ` repr ` attribute
166
174
which can be used to control type layout.
@@ -175,8 +183,6 @@ which can be used to control type layout.
175
183
macros named. The ` extern crate ` must appear at the crate root, not inside
176
184
` mod ` , which ensures proper function of the ` $crate ` macro variable.
177
185
178
- - ` macro_reexport ` on an ` extern crate ` — re-export the named macros.
179
-
180
186
- ` macro_export ` - export a ` macro_rules ` macro for cross-crate usage.
181
187
182
188
- ` no_link ` on an ` extern crate ` — even if we load this crate for macros, don't
@@ -371,8 +377,7 @@ They only get checked when the associated tool is active, so if you try to use a
371
377
372
378
Otherwise, they work just like regular lint attributes:
373
379
374
-
375
- ``` rust,ignore
380
+ ``` rust
376
381
// set the entire `pedantic` clippy lint group to warn
377
382
#![warn(clippy:: pedantic)]
378
383
// silence warnings from the `filter_map` clippy lint
@@ -552,6 +557,34 @@ impl<T: PartialEq> PartialEq for Foo<T> {
552
557
553
558
You can implement ` derive ` for your own traits through [ procedural macros] .
554
559
560
+ ## Tool attributes
561
+
562
+ The compiler may allow attributes for external tools where each tool resides
563
+ in its own namespace. The first segment of the attribute path is the name of
564
+ the tool, with one or more additional segments whose interpretation is up to
565
+ the tool.
566
+
567
+ When a tool is not in use, the tool's attributes are accepted without a
568
+ warning. When the tool is in use, the tool is responsible for processing and
569
+ interpretation of its attributes.
570
+
571
+ Tool attributes are not available if the [ ` no_implicit_prelude ` ] attribute is
572
+ used.
573
+
574
+ ``` rust
575
+ // Tells the rustfmt tool to not format the following element.
576
+ #[rustfmt:: skip]
577
+ struct S {
578
+ }
579
+
580
+ // Controls the "cyclomatic complexity" threshold for the clippy tool.
581
+ #[clippy:: cyclomatic_complexity = " 100" ]
582
+ pub fn f () {}
583
+ ```
584
+
585
+ > Note: ` rustc ` currently recognizes the tools "clippy" and "rustfmt".
586
+
587
+ [ _DelimTokenTree_ ] : macros.html
555
588
[ _LiteralExpression_ ] : expressions/literal-expr.html
556
589
[ _SimplePath_ ] : paths.html#simple-paths
557
590
[ `no_implicit_prelude` ] : items/modules.html#prelude-items
@@ -585,6 +618,7 @@ You can implement `derive` for your own traits through [procedural macros].
585
618
[ external blocks ] : items/external-blocks.html
586
619
[ items ] : items.html
587
620
[ attribute macro ] : procedural-macros.html#attribute-macros
621
+ [ derive macro helper attributes ] : procedural-macros.html#derive-macro-helper-attributes
588
622
[ function-like macro ] : procedural-macros.html#function-like-procedural-macros
589
623
[ conditional compilation ] : conditional-compilation.html
590
624
[ derive macro ] : procedural-macros.html#derive-macros
@@ -594,3 +628,4 @@ You can implement `derive` for your own traits through [procedural macros].
594
628
[ where clause ] : items/where-clauses.html
595
629
[ trait or lifetime bounds ] : trait-bounds.html
596
630
[ Expression Attributes ] : expressions.html#expression-attributes
631
+ [ `meta` macro fragment specifier ] : macros-by-example.html
0 commit comments