11{{#include attributes-redirect.html}}
22# Attributes
33
4+ r[ attributes.syntax]
45> ** <sup >Syntax</sup >** \
56> _ InnerAttribute_ :\
67>   ;  ; ` # ` ` ! ` ` [ ` _ Attr_ ` ] `
1617>   ;  ;   ;  ; [ _ DelimTokenTree_ ] \
1718>   ;  ; | ` = ` [ _ Expression_ ]
1819
20+ r[ attributes.intro]
1921An _ attribute_ is a general, free-form metadatum that is interpreted according
2022to name, convention, language, and compiler version. Attributes are modeled
2123on Attributes in [ ECMA-335] , with the syntax coming from [ ECMA-334] \( C#).
2224
25+ r[ attributes.inner]
2326_ Inner attributes_ , written with a bang (` ! ` ) after the hash (` # ` ), apply to the
2427item that the attribute is declared within. _ Outer attributes_ , written without
2528the bang after the hash, apply to the thing that follows the attribute.
2629
30+ r[ attributes.input]
2731The attribute consists of a path to the attribute, followed by an optional
2832delimited token tree whose interpretation is defined by the attribute.
2933Attributes other than macro attributes also allow the input to be an equals
3034sign (` = ` ) followed by an expression. See the [ meta item
3135syntax] ( #meta-item-attribute-syntax ) below for more details.
3236
37+ r[ attributes.safety]
3338An attribute may be unsafe to apply. To avoid undefined behavior when using
3439these attributes, certain obligations that cannot be checked by the compiler
3540must be met. To assert these have been, the attribute is wrapped in
@@ -41,13 +46,15 @@ The following attributes are unsafe:
4146* [ ` link_section ` ]
4247* [ ` no_mangle ` ]
4348
49+ r[ attributes.kind]
4450Attributes can be classified into the following kinds:
4551
4652* [ Built-in attributes]
4753* [ Proc macro attributes] [ attribute macros ]
4854* [ Derive macro helper attributes]
4955* [ Tool attributes] ( #tool-attributes )
5056
57+ r[ attributes.allowed-position]
5158Attributes may be applied to many things in the language:
5259
5360* All [ item declarations] accept outer attributes while [ external blocks] ,
@@ -100,9 +107,13 @@ fn some_unused_variables() {
100107
101108## Meta Item Attribute Syntax
102109
110+ r[ attributes.meta]
111+
112+ r[ attributes.meta.intro]
103113A "meta item" is the syntax used for the _ Attr_ rule by most [ built-in
104114attributes] . It has the following grammar:
105115
116+ r[ attributes.meta.syntax]
106117> ** <sup >Syntax</sup >** \
107118> _ MetaItem_ :\
108119>   ;  ;   ;  ; [ _ SimplePath_ ] \
@@ -116,10 +127,12 @@ attributes]. It has the following grammar:
116127>   ;  ;   ;  ; _ MetaItem_ \
117128>   ;  ; | [ _ Expression_ ]
118129
130+ r[ attributes.meta.literal-expr]
119131Expressions in meta items must macro-expand to literal expressions, which must not
120132include integer or float type suffixes. Expressions which are not literal expressions
121133will be syntactically accepted (and can be passed to proc-macros), but will be rejected after parsing.
122134
135+ r[ attributes.meta.order]
123136Note that if the attribute appears within another macro, it will be expanded
124137after that outer macro. For example, the following code will expand the
125138` Serialize ` proc-macro first, which must preserve the ` include_str! ` call in
@@ -133,6 +146,7 @@ struct Foo {
133146}
134147```
135148
149+ r[ attributes.meta.order-macro]
136150Additionally, macros in attributes will be expanded only after all other attributes applied to the item:
137151
138152``` rust ignore
@@ -143,6 +157,7 @@ Additionally, macros in attributes will be expanded only after all other attribu
143157fn foo () {}
144158```
145159
160+ r[ attributes.meta.builtin]
146161Various built-in attributes use different subsets of the meta item syntax to
147162specify their inputs. The following grammar rules show some commonly used
148163forms:
@@ -175,6 +190,9 @@ _MetaListNameValueStr_ | `link(name = "CoreFoundation", kind = "framework")`
175190
176191## Active and inert attributes
177192
193+ r[ attributes.activity]
194+
195+ r[ attributes.activity.intro]
178196An attribute is either active or inert. During attribute processing, * active
179197attributes* remove themselves from the thing they are on while * inert attributes*
180198stay on.
@@ -185,15 +203,20 @@ active. All other attributes are inert.
185203
186204## Tool attributes
187205
206+ r[ attributes.tool]
207+
208+ r[ attributes.tool.intro]
188209The compiler may allow attributes for external tools where each tool resides
189210in its own module in the [ tool prelude] . The first segment of the attribute
190211path is the name of the tool, with one or more additional segments whose
191212interpretation is up to the tool.
192213
214+ r[ attributes.tool.ignored]
193215When a tool is not in use, the tool's attributes are accepted without a
194216warning. When the tool is in use, the tool is responsible for processing and
195217interpretation of its attributes.
196218
219+ r[ attributes.tool.prelude]
197220Tool attributes are not available if the [ ` no_implicit_prelude ` ] attribute is
198221used.
199222
@@ -213,32 +236,39 @@ pub fn f() {}
213236
214237## Built-in attributes index
215238
239+ r[ attributes.builtin]
240+
216241The following is an index of all built-in attributes.
217242
218243- Conditional compilation
219244 - [ ` cfg ` ] --- Controls conditional compilation.
220245 - [ ` cfg_attr ` ] --- Conditionally includes attributes.
246+
221247- Testing
222248 - [ ` test ` ] --- Marks a function as a test.
223249 - [ ` ignore ` ] --- Disables a test function.
224250 - [ ` should_panic ` ] --- Indicates a test should generate a panic.
251+
225252- Derive
226253 - [ ` derive ` ] --- Automatic trait implementations.
227254 - [ ` automatically_derived ` ] --- Marker for implementations created by
228255 ` derive ` .
256+
229257- Macros
230258 - [ ` macro_export ` ] --- Exports a ` macro_rules ` macro for cross-crate usage.
231259 - [ ` macro_use ` ] --- Expands macro visibility, or imports macros from other
232260 crates.
233261 - [ ` proc_macro ` ] --- Defines a function-like macro.
234262 - [ ` proc_macro_derive ` ] --- Defines a derive macro.
235263 - [ ` proc_macro_attribute ` ] --- Defines an attribute macro.
264+
236265- Diagnostics
237266 - [ ` allow ` ] , [ ` expect ` ] , [ ` warn ` ] , [ ` deny ` ] , [ ` forbid ` ] --- Alters the default lint level.
238267 - [ ` deprecated ` ] --- Generates deprecation notices.
239268 - [ ` must_use ` ] --- Generates a lint for unused values.
240269 - [ ` diagnostic::on_unimplemented ` ] --- Hints the compiler to emit a certain error
241270 message if a trait is not implemented.
271+
242272- ABI, linking, symbols, and FFI
243273 - [ ` link ` ] --- Specifies a native library to link with an ` extern ` block.
244274 - [ ` link_name ` ] --- Specifies the name of the symbol for functions or statics
@@ -257,35 +287,44 @@ The following is an index of all built-in attributes.
257287 - [ ` used ` ] --- Forces the compiler to keep a static item in the output
258288 object file.
259289 - [ ` crate_name ` ] --- Specifies the crate name.
290+
260291- Code generation
261292 - [ ` inline ` ] --- Hint to inline code.
262293 - [ ` cold ` ] --- Hint that a function is unlikely to be called.
263294 - [ ` no_builtins ` ] --- Disables use of certain built-in functions.
264295 - [ ` target_feature ` ] --- Configure platform-specific code generation.
265296 - [ ` track_caller ` ] - Pass the parent call location to ` std::panic::Location::caller() ` .
266297 - [ ` instruction_set ` ] - Specify the instruction set used to generate a functions code
298+
267299- Documentation
268300 - ` doc ` --- Specifies documentation. See [ The Rustdoc Book] for more
269301 information. [ Doc comments] are transformed into ` doc ` attributes.
302+
270303- Preludes
271304 - [ ` no_std ` ] --- Removes std from the prelude.
272305 - [ ` no_implicit_prelude ` ] --- Disables prelude lookups within a module.
306+
273307- Modules
274308 - [ ` path ` ] --- Specifies the filename for a module.
309+
275310- Limits
276311 - [ ` recursion_limit ` ] --- Sets the maximum recursion limit for certain
277312 compile-time operations.
278313 - [ ` type_length_limit ` ] --- Sets the maximum size of a polymorphic type.
314+
279315- Runtime
280316 - [ ` panic_handler ` ] --- Sets the function to handle panics.
281317 - [ ` global_allocator ` ] --- Sets the global memory allocator.
282318 - [ ` windows_subsystem ` ] --- Specifies the windows subsystem to link with.
319+
283320- Features
284321 - ` feature ` --- Used to enable unstable or experimental compiler features. See
285322 [ The Unstable Book] for features implemented in ` rustc ` .
323+
286324- Type System
287325 - [ ` non_exhaustive ` ] --- Indicate that a type will have more fields/variants
288326 added in future.
327+
289328- Debugger
290329 - [ ` debugger_visualizer ` ] --- Embeds a file that specifies debugger output for a type.
291330 - [ ` collapse_debuginfo ` ] --- Controls how macro invocations are encoded in debuginfo.
0 commit comments