11{{#include attributes-redirect.html}}
22# Attributes
33
4+ r[ attributes]
5+
6+
7+ r[ attributes.syntax]
48> ** <sup >Syntax</sup >** \
59> _ InnerAttribute_ :\
610>   ;  ; ` # ` ` ! ` ` [ ` _ Attr_ ` ] `
1620>   ;  ;   ;  ; [ _ DelimTokenTree_ ] \
1721>   ;  ; | ` = ` [ _ Expression_ ]
1822
23+ r[ attributes.general]
1924An _ attribute_ is a general, free-form metadatum that is interpreted according
2025to name, convention, language, and compiler version. Attributes are modeled
2126on Attributes in [ ECMA-335] , with the syntax coming from [ ECMA-334] \( C#).
2227
28+ r[ attributes.inner]
2329_ Inner attributes_ , written with a bang (` ! ` ) after the hash (` # ` ), apply to the
2430item that the attribute is declared within. _ Outer attributes_ , written without
2531the bang after the hash, apply to the thing that follows the attribute.
2632
33+ r[ attributes.input]
2734The attribute consists of a path to the attribute, followed by an optional
2835delimited token tree whose interpretation is defined by the attribute.
2936Attributes other than macro attributes also allow the input to be an equals
3037sign (` = ` ) followed by an expression. See the [ meta item
3138syntax] ( #meta-item-attribute-syntax ) below for more details.
3239
40+ r[ attributes.safety]
3341An attribute may be unsafe to apply. To avoid undefined behavior when using
3442these attributes, certain obligations that cannot be checked by the compiler
3543must be met. To assert these have been, the attribute is wrapped in
@@ -41,15 +49,15 @@ The following attributes are unsafe:
4149* [ ` link_section ` ]
4250* [ ` no_mangle ` ]
4351
52+ r[ attributes.kind]
4453Attributes can be classified into the following kinds:
45-
4654* [ Built-in attributes]
4755* [ Macro attributes] [ attribute macros ]
4856* [ Derive macro helper attributes]
4957* [ Tool attributes] ( #tool-attributes )
5058
59+ r[ attributes.application]
5160Attributes may be applied to many things in the language:
52-
5361* All [ item declarations] accept outer attributes while [ external blocks] ,
5462 [ functions] , [ implementations] , and [ modules] accept inner attributes.
5563* Most [ statements] accept outer attributes (see [ Expression Attributes] for
@@ -100,9 +108,14 @@ fn some_unused_variables() {
100108
101109## Meta Item Attribute Syntax
102110
111+ r[ attributes.meta]
112+
113+
114+ r[ attributes.meta.general]
103115A "meta item" is the syntax used for the _ Attr_ rule by most [ built-in
104116attributes] . It has the following grammar:
105117
118+ r[ attributes.meta.syntax]
106119> ** <sup >Syntax</sup >** \
107120> _ MetaItem_ :\
108121>   ;  ;   ;  ; [ _ SimplePath_ ] \
@@ -116,10 +129,12 @@ attributes]. It has the following grammar:
116129>   ;  ;   ;  ; _ MetaItem_ \
117130>   ;  ; | [ _ Expression_ ]
118131
132+ r[ attributes.meta.literal-expr]
119133Expressions in meta items must macro-expand to literal expressions, which must not
120134include integer or float type suffixes. Expressions which are not literal expressions
121135will be syntactically accepted (and can be passed to proc-macros), but will be rejected after parsing.
122136
137+ r[ attributes.meta.order]
123138Note that if the attribute appears within another macro, it will be expanded
124139after that outer macro. For example, the following code will expand the
125140` Serialize ` proc-macro first, which must preserve the ` include_str! ` call in
@@ -133,6 +148,7 @@ struct Foo {
133148}
134149```
135150
151+ r[ attributes.meta.order-macro]
136152Additionally, macros in attributes will be expanded only after all other attributes applied to the item:
137153
138154``` rust ignore
@@ -143,6 +159,7 @@ Additionally, macros in attributes will be expanded only after all other attribu
143159fn foo () {}
144160```
145161
162+ r[ attributes.meta.builtin]
146163Various built-in attributes use different subsets of the meta item syntax to
147164specify their inputs. The following grammar rules show some commonly used
148165forms:
@@ -175,6 +192,9 @@ _MetaListNameValueStr_ | `link(name = "CoreFoundation", kind = "framework")`
175192
176193## Active and inert attributes
177194
195+ r[ attributes.activity]
196+
197+ r[ attributes.activity.general]
178198An attribute is either active or inert. During attribute processing, * active
179199attributes* remove themselves from the thing they are on while * inert attributes*
180200stay on.
@@ -185,15 +205,20 @@ active. All other attributes are inert.
185205
186206## Tool attributes
187207
208+ r[ attributes.tool]
209+
210+ r[ attributes.tool.general]
188211The compiler may allow attributes for external tools where each tool resides
189212in its own module in the [ tool prelude] . The first segment of the attribute
190213path is the name of the tool, with one or more additional segments whose
191214interpretation is up to the tool.
192215
216+ r[ attributes.tool.ignored]
193217When a tool is not in use, the tool's attributes are accepted without a
194218warning. When the tool is in use, the tool is responsible for processing and
195219interpretation of its attributes.
196220
221+ r[ attributes.tool.prelude]
197222Tool attributes are not available if the [ ` no_implicit_prelude ` ] attribute is
198223used.
199224
@@ -213,32 +238,45 @@ pub fn f() {}
213238
214239## Built-in attributes index
215240
241+ r[ attributes.builtin]
242+
216243The following is an index of all built-in attributes.
217244
245+ r[ attributes.builtin.cfg]
218246- Conditional compilation
219247 - [ ` cfg ` ] --- Controls conditional compilation.
220248 - [ ` cfg_attr ` ] --- Conditionally includes attributes.
249+
250+ r[ attributes.builtin.testing]
221251- Testing
222252 - [ ` test ` ] --- Marks a function as a test.
223253 - [ ` ignore ` ] --- Disables a test function.
224254 - [ ` should_panic ` ] --- Indicates a test should generate a panic.
255+
256+ r[ attributes.builtin.derive]
225257- Derive
226258 - [ ` derive ` ] --- Automatic trait implementations.
227259 - [ ` automatically_derived ` ] --- Marker for implementations created by
228260 ` derive ` .
261+
262+ r[ attributes.builtin.macros]
229263- Macros
230264 - [ ` macro_export ` ] --- Exports a ` macro_rules ` macro for cross-crate usage.
231265 - [ ` macro_use ` ] --- Expands macro visibility, or imports macros from other
232266 crates.
233267 - [ ` proc_macro ` ] --- Defines a function-like macro.
234268 - [ ` proc_macro_derive ` ] --- Defines a derive macro.
235269 - [ ` proc_macro_attribute ` ] --- Defines an attribute macro.
270+
271+ r[ attributes.builtin.diagnostic]
236272- Diagnostics
237273 - [ ` allow ` ] , [ ` expect ` ] , [ ` warn ` ] , [ ` deny ` ] , [ ` forbid ` ] --- Alters the default lint level.
238274 - [ ` deprecated ` ] --- Generates deprecation notices.
239275 - [ ` must_use ` ] --- Generates a lint for unused values.
240276 - [ ` diagnostic::on_unimplemented ` ] --- Hints the compiler to emit a certain error
241277 message if a trait is not implemented.
278+
279+ r[ attributes.builtin.linkage]
242280- ABI, linking, symbols, and FFI
243281 - [ ` link ` ] --- Specifies a native library to link with an ` extern ` block.
244282 - [ ` link_name ` ] --- Specifies the name of the symbol for functions or statics
@@ -257,35 +295,53 @@ The following is an index of all built-in attributes.
257295 - [ ` used ` ] --- Forces the compiler to keep a static item in the output
258296 object file.
259297 - [ ` crate_name ` ] --- Specifies the crate name.
298+
299+ r[ attributes.builtin.codegen]
260300- Code generation
261301 - [ ` inline ` ] --- Hint to inline code.
262302 - [ ` cold ` ] --- Hint that a function is unlikely to be called.
263303 - [ ` no_builtins ` ] --- Disables use of certain built-in functions.
264304 - [ ` target_feature ` ] --- Configure platform-specific code generation.
265305 - [ ` track_caller ` ] - Pass the parent call location to ` std::panic::Location::caller() ` .
266306 - [ ` instruction_set ` ] - Specify the instruction set used to generate a functions code
307+
308+ r[ attributes.builtin.doc]
267309- Documentation
268310 - ` doc ` --- Specifies documentation. See [ The Rustdoc Book] for more
269311 information. [ Doc comments] are transformed into ` doc ` attributes.
312+
313+ r[ attributes.builtin.prelude]
270314- Preludes
271315 - [ ` no_std ` ] --- Removes std from the prelude.
272316 - [ ` no_implicit_prelude ` ] --- Disables prelude lookups within a module.
317+
318+ r[ attributes.builtin.module]
273319- Modules
274320 - [ ` path ` ] --- Specifies the filename for a module.
321+
322+ r[ attributes.builtin.limits]
275323- Limits
276324 - [ ` recursion_limit ` ] --- Sets the maximum recursion limit for certain
277325 compile-time operations.
278326 - [ ` type_length_limit ` ] --- Sets the maximum size of a polymorphic type.
327+
328+ r[ attributes.builtin.rt]
279329- Runtime
280330 - [ ` panic_handler ` ] --- Sets the function to handle panics.
281331 - [ ` global_allocator ` ] --- Sets the global memory allocator.
282332 - [ ` windows_subsystem ` ] --- Specifies the windows subsystem to link with.
333+
334+ r[ attributes.builtin.unstable]
283335- Features
284336 - ` feature ` --- Used to enable unstable or experimental compiler features. See
285337 [ The Unstable Book] for features implemented in ` rustc ` .
338+
339+ r[ attributes.builtin.typesystem]
286340- Type System
287341 - [ ` non_exhaustive ` ] --- Indicate that a type will have more fields/variants
288342 added in future.
343+
344+ r[ attributes.builtin.debugging]
289345- Debugger
290346 - [ ` debugger_visualizer ` ] --- Embeds a file that specifies debugger output for a type.
291347 - [ ` collapse_debuginfo ` ] --- Controls how macro invocations are encoded in debuginfo.
0 commit comments