11# Attributes
22
33> ** <sup >Syntax</sup >** \
4- > _ Attribute_ :\
5- >   ;  ; _ InnerAttribute_ | _ OuterAttribute_
6- >
74> _ InnerAttribute_ :\
8- >   ;  ; ` #! [ ` MetaItem ` ] `
5+ >   ;  ; ` # ` ` ! ` ` [ ` _ Attr _ ` ] `
96>
107> _ OuterAttribute_ :\
11- >   ;  ; ` #[ ` MetaItem ` ] `
8+ >   ;  ; ` # ` ` [ ` _ Attr _ ` ] `
129>
13- > _ MetaItem_ :\
14- >   ;  ;   ;  ; [ _ SimplePath_ ] \
15- >   ;  ; | [ _ SimplePath_ ] ` = ` [ _ LiteralExpression_ ] <sub >_ without suffix_ </sub >\
16- >   ;  ; | [ _ SimplePath_ ] ` ( ` _ MetaSeq_ <sup >?</sup > ` ) `
10+ > _ Attr_ :\
11+ >   ;  ; [ _ SimplePath_ ] _ AttrInput_ <sup >?</sup >
1712>
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 >
2416
2517An _ attribute_ is a general, free-form metadatum that is interpreted according
2618to name, convention, and language and compiler version. Attributes are modeled
2719on 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
4338Attributes 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+ >   ;  ;   ;  ; [ _ 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)) `
95112
96113## Active and inert attributes
97114
@@ -139,28 +156,18 @@ names have meaning.
139156
140157On 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
157166On declarations inside an ` extern ` block, the following attributes are
158167interpreted:
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
165172See [ type layout] ( type-layout.html ) for documentation on the ` repr ` attribute
166173which 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
372377Otherwise, 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
553557You 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
0 commit comments