11# Attributes
22
33> ** <sup >Syntax</sup >** \
4- > _ Attribute_ :\
5- >   ;  ; _ InnerAttribute_ | _ OuterAttribute_
6- >
74> _ InnerAttribute_ :\
8- >   ;  ; ` #! [ ` MetaItem ` ] `
5+ >   ;  ; ` # ` ` ! ` ` [ ` _ Attr _ ` ] `
96>
107> _ OuterAttribute_ :\
11- >   ;  ; ` #[ ` MetaItem ` ] `
12- >
13- > _ MetaItem_ :\
14- >   ;  ;   ;  ; [ _ SimplePath_ ] \
15- >   ;  ; | [ _ SimplePath_ ] ` = ` [ _ LiteralExpression_ ] <sub >_ without suffix_ </sub >\
16- >   ;  ; | [ _ SimplePath_ ] ` ( ` _ MetaSeq_ <sup >?</sup > ` ) `
8+ >   ;  ; ` # ` ` [ ` _ Attr_ ` ] `
179>
18- > _ MetaSeq _ :\
19- >   ;  ; _ MetaItemInner _ ( ` , ` MetaItemInner )< sup > \* </ sup > ` , ` <sup >?</sup >
10+ > _ Attr _ :\
11+ >   ;  ; [ _ SimplePath _ ] _ AttrInput _ <sup >?</sup >
2012>
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 macro helper attributes]
36+ * [ Tool attributes] ( #tool-attributes )
4237
4338Attributes may be applied to many things in the language:
4439
@@ -87,11 +82,34 @@ 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 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)) `
95113
96114## Active and inert attributes
97115
@@ -139,28 +157,18 @@ names have meaning.
139157
140158On an ` extern ` block, the following attributes are interpreted:
141159
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).
145160- ` link ` - indicate that a native library should be linked to for the
146161 declarations in this block to be linked correctly. ` link ` supports an optional
147162 ` kind ` key with three possible values: ` dylib ` , ` static ` , and ` framework ` . See
148163 [ external blocks] ( items/external-blocks.html ) for more about external blocks.
149164 Two examples: ` #[link(name = "readline")] ` and
150165 ` #[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.
156166
157167On declarations inside an ` extern ` block, the following attributes are
158168interpreted:
159169
160170- ` link_name ` - the name of the symbol that this function or static should be
161171 imported as.
162- - ` linkage ` - on a static, this specifies the [ linkage
163- type] ( http://llvm.org/docs/LangRef.html#linkage-types ) .
164172
165173See [ type layout] ( type-layout.html ) for documentation on the ` repr ` attribute
166174which can be used to control type layout.
@@ -175,8 +183,6 @@ which can be used to control type layout.
175183 macros named. The ` extern crate ` must appear at the crate root, not inside
176184 ` mod ` , which ensures proper function of the ` $crate ` macro variable.
177185
178- - ` macro_reexport ` on an ` extern crate ` — re-export the named macros.
179-
180186- ` macro_export ` - export a ` macro_rules ` macro for cross-crate usage.
181187
182188- ` 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
371377
372378Otherwise, they work just like regular lint attributes:
373379
374-
375- ``` rust,ignore
380+ ``` rust
376381// set the entire `pedantic` clippy lint group to warn
377382#![warn(clippy:: pedantic)]
378383// silence warnings from the `filter_map` clippy lint
@@ -552,6 +557,34 @@ impl<T: PartialEq> PartialEq for Foo<T> {
552557
553558You can implement ` derive ` for your own traits through [ procedural macros] .
554559
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
555588[ _LiteralExpression_ ] : expressions/literal-expr.html
556589[ _SimplePath_ ] : paths.html#simple-paths
557590[ `no_implicit_prelude` ] : items/modules.html#prelude-items
@@ -585,6 +618,7 @@ You can implement `derive` for your own traits through [procedural macros].
585618[ external blocks ] : items/external-blocks.html
586619[ items ] : items.html
587620[ attribute macro ] : procedural-macros.html#attribute-macros
621+ [ derive macro helper attributes ] : procedural-macros.html#derive-macro-helper-attributes
588622[ function-like macro ] : procedural-macros.html#function-like-procedural-macros
589623[ conditional compilation ] : conditional-compilation.html
590624[ derive macro ] : procedural-macros.html#derive-macros
@@ -594,3 +628,4 @@ You can implement `derive` for your own traits through [procedural macros].
594628[ where clause ] : items/where-clauses.html
595629[ trait or lifetime bounds ] : trait-bounds.html
596630[ Expression Attributes ] : expressions.html#expression-attributes
631+ [ `meta` macro fragment specifier ] : macros-by-example.html
0 commit comments