1
1
{{#include attributes-redirect.html}}
2
2
# Attributes
3
3
4
+ r[ attributes]
5
+
6
+
7
+ r[ attributes.syntax]
4
8
> ** <sup >Syntax</sup >** \
5
9
> _ InnerAttribute_ :\
6
10
>   ;  ; ` # ` ` ! ` ` [ ` _ Attr_ ` ] `
16
20
>   ;  ;   ;  ; [ _ DelimTokenTree_ ] \
17
21
>   ;  ; | ` = ` [ _ Expression_ ]
18
22
23
+ r[ attributes.general]
19
24
An _ attribute_ is a general, free-form metadatum that is interpreted according
20
25
to name, convention, language, and compiler version. Attributes are modeled
21
26
on Attributes in [ ECMA-335] , with the syntax coming from [ ECMA-334] \( C#).
22
27
28
+ r[ attributes.inner]
23
29
_ Inner attributes_ , written with a bang (` ! ` ) after the hash (` # ` ), apply to the
24
30
item that the attribute is declared within. _ Outer attributes_ , written without
25
31
the bang after the hash, apply to the thing that follows the attribute.
26
32
33
+ r[ attributes.input]
27
34
The attribute consists of a path to the attribute, followed by an optional
28
35
delimited token tree whose interpretation is defined by the attribute.
29
36
Attributes other than macro attributes also allow the input to be an equals
30
37
sign (` = ` ) followed by an expression. See the [ meta item
31
38
syntax] ( #meta-item-attribute-syntax ) below for more details.
32
39
40
+ r[ attributes.safety]
33
41
An attribute may be unsafe to apply. To avoid undefined behavior when using
34
42
these attributes, certain obligations that cannot be checked by the compiler
35
43
must be met. To assert these have been, the attribute is wrapped in
@@ -41,15 +49,15 @@ The following attributes are unsafe:
41
49
* [ ` link_section ` ]
42
50
* [ ` no_mangle ` ]
43
51
52
+ r[ attributes.kind]
44
53
Attributes can be classified into the following kinds:
45
-
46
54
* [ Built-in attributes]
47
55
* [ Macro attributes] [ attribute macros ]
48
56
* [ Derive macro helper attributes]
49
57
* [ Tool attributes] ( #tool-attributes )
50
58
59
+ r[ attributes.application]
51
60
Attributes may be applied to many things in the language:
52
-
53
61
* All [ item declarations] accept outer attributes while [ external blocks] ,
54
62
[ functions] , [ implementations] , and [ modules] accept inner attributes.
55
63
* Most [ statements] accept outer attributes (see [ Expression Attributes] for
@@ -100,9 +108,14 @@ fn some_unused_variables() {
100
108
101
109
## Meta Item Attribute Syntax
102
110
111
+ r[ attributes.meta]
112
+
113
+
114
+ r[ attributes.meta.general]
103
115
A "meta item" is the syntax used for the _ Attr_ rule by most [ built-in
104
116
attributes] . It has the following grammar:
105
117
118
+ r[ attributes.meta.syntax]
106
119
> ** <sup >Syntax</sup >** \
107
120
> _ MetaItem_ :\
108
121
>   ;  ;   ;  ; [ _ SimplePath_ ] \
@@ -116,10 +129,12 @@ attributes]. It has the following grammar:
116
129
>   ;  ;   ;  ; _ MetaItem_ \
117
130
>   ;  ; | [ _ Expression_ ]
118
131
132
+ r[ attributes.meta.literal-expr]
119
133
Expressions in meta items must macro-expand to literal expressions, which must not
120
134
include integer or float type suffixes. Expressions which are not literal expressions
121
135
will be syntactically accepted (and can be passed to proc-macros), but will be rejected after parsing.
122
136
137
+ r[ attributes.meta.order]
123
138
Note that if the attribute appears within another macro, it will be expanded
124
139
after that outer macro. For example, the following code will expand the
125
140
` Serialize ` proc-macro first, which must preserve the ` include_str! ` call in
@@ -133,6 +148,7 @@ struct Foo {
133
148
}
134
149
```
135
150
151
+ r[ attributes.meta.order-macro]
136
152
Additionally, macros in attributes will be expanded only after all other attributes applied to the item:
137
153
138
154
``` rust ignore
@@ -143,6 +159,7 @@ Additionally, macros in attributes will be expanded only after all other attribu
143
159
fn foo () {}
144
160
```
145
161
162
+ r[ attributes.meta.builtin]
146
163
Various built-in attributes use different subsets of the meta item syntax to
147
164
specify their inputs. The following grammar rules show some commonly used
148
165
forms:
@@ -175,6 +192,9 @@ _MetaListNameValueStr_ | `link(name = "CoreFoundation", kind = "framework")`
175
192
176
193
## Active and inert attributes
177
194
195
+ r[ attributes.activity]
196
+
197
+ r[ attributes.activity.general]
178
198
An attribute is either active or inert. During attribute processing, * active
179
199
attributes* remove themselves from the thing they are on while * inert attributes*
180
200
stay on.
@@ -185,15 +205,20 @@ active. All other attributes are inert.
185
205
186
206
## Tool attributes
187
207
208
+ r[ attributes.tool]
209
+
210
+ r[ attributes.tool.general]
188
211
The compiler may allow attributes for external tools where each tool resides
189
212
in its own module in the [ tool prelude] . The first segment of the attribute
190
213
path is the name of the tool, with one or more additional segments whose
191
214
interpretation is up to the tool.
192
215
216
+ r[ attributes.tool.ignored]
193
217
When a tool is not in use, the tool's attributes are accepted without a
194
218
warning. When the tool is in use, the tool is responsible for processing and
195
219
interpretation of its attributes.
196
220
221
+ r[ attributes.tool.prelude]
197
222
Tool attributes are not available if the [ ` no_implicit_prelude ` ] attribute is
198
223
used.
199
224
@@ -213,32 +238,45 @@ pub fn f() {}
213
238
214
239
## Built-in attributes index
215
240
241
+ r[ attributes.builtin]
242
+
216
243
The following is an index of all built-in attributes.
217
244
245
+ r[ attributes.builtin.cfg]
218
246
- Conditional compilation
219
247
- [ ` cfg ` ] --- Controls conditional compilation.
220
248
- [ ` cfg_attr ` ] --- Conditionally includes attributes.
249
+
250
+ r[ attributes.builtin.testing]
221
251
- Testing
222
252
- [ ` test ` ] --- Marks a function as a test.
223
253
- [ ` ignore ` ] --- Disables a test function.
224
254
- [ ` should_panic ` ] --- Indicates a test should generate a panic.
255
+
256
+ r[ attributes.builtin.derive]
225
257
- Derive
226
258
- [ ` derive ` ] --- Automatic trait implementations.
227
259
- [ ` automatically_derived ` ] --- Marker for implementations created by
228
260
` derive ` .
261
+
262
+ r[ attributes.builtin.macros]
229
263
- Macros
230
264
- [ ` macro_export ` ] --- Exports a ` macro_rules ` macro for cross-crate usage.
231
265
- [ ` macro_use ` ] --- Expands macro visibility, or imports macros from other
232
266
crates.
233
267
- [ ` proc_macro ` ] --- Defines a function-like macro.
234
268
- [ ` proc_macro_derive ` ] --- Defines a derive macro.
235
269
- [ ` proc_macro_attribute ` ] --- Defines an attribute macro.
270
+
271
+ r[ attributes.builtin.diagnostic]
236
272
- Diagnostics
237
273
- [ ` allow ` ] , [ ` expect ` ] , [ ` warn ` ] , [ ` deny ` ] , [ ` forbid ` ] --- Alters the default lint level.
238
274
- [ ` deprecated ` ] --- Generates deprecation notices.
239
275
- [ ` must_use ` ] --- Generates a lint for unused values.
240
276
- [ ` diagnostic::on_unimplemented ` ] --- Hints the compiler to emit a certain error
241
277
message if a trait is not implemented.
278
+
279
+ r[ attributes.builtin.linkage]
242
280
- ABI, linking, symbols, and FFI
243
281
- [ ` link ` ] --- Specifies a native library to link with an ` extern ` block.
244
282
- [ ` 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.
257
295
- [ ` used ` ] --- Forces the compiler to keep a static item in the output
258
296
object file.
259
297
- [ ` crate_name ` ] --- Specifies the crate name.
298
+
299
+ r[ attributes.builtin.codegen]
260
300
- Code generation
261
301
- [ ` inline ` ] --- Hint to inline code.
262
302
- [ ` cold ` ] --- Hint that a function is unlikely to be called.
263
303
- [ ` no_builtins ` ] --- Disables use of certain built-in functions.
264
304
- [ ` target_feature ` ] --- Configure platform-specific code generation.
265
305
- [ ` track_caller ` ] - Pass the parent call location to ` std::panic::Location::caller() ` .
266
306
- [ ` instruction_set ` ] - Specify the instruction set used to generate a functions code
307
+
308
+ r[ attributes.builtin.doc]
267
309
- Documentation
268
310
- ` doc ` --- Specifies documentation. See [ The Rustdoc Book] for more
269
311
information. [ Doc comments] are transformed into ` doc ` attributes.
312
+
313
+ r[ attributes.builtin.prelude]
270
314
- Preludes
271
315
- [ ` no_std ` ] --- Removes std from the prelude.
272
316
- [ ` no_implicit_prelude ` ] --- Disables prelude lookups within a module.
317
+
318
+ r[ attributes.builtin.module]
273
319
- Modules
274
320
- [ ` path ` ] --- Specifies the filename for a module.
321
+
322
+ r[ attributes.builtin.limits]
275
323
- Limits
276
324
- [ ` recursion_limit ` ] --- Sets the maximum recursion limit for certain
277
325
compile-time operations.
278
326
- [ ` type_length_limit ` ] --- Sets the maximum size of a polymorphic type.
327
+
328
+ r[ attributes.builtin.rt]
279
329
- Runtime
280
330
- [ ` panic_handler ` ] --- Sets the function to handle panics.
281
331
- [ ` global_allocator ` ] --- Sets the global memory allocator.
282
332
- [ ` windows_subsystem ` ] --- Specifies the windows subsystem to link with.
333
+
334
+ r[ attributes.builtin.unstable]
283
335
- Features
284
336
- ` feature ` --- Used to enable unstable or experimental compiler features. See
285
337
[ The Unstable Book] for features implemented in ` rustc ` .
338
+
339
+ r[ attributes.builtin.typesystem]
286
340
- Type System
287
341
- [ ` non_exhaustive ` ] --- Indicate that a type will have more fields/variants
288
342
added in future.
343
+
344
+ r[ attributes.builtin.debugging]
289
345
- Debugger
290
346
- [ ` debugger_visualizer ` ] --- Embeds a file that specifies debugger output for a type.
291
347
- [ ` collapse_debuginfo ` ] --- Controls how macro invocations are encoded in debuginfo.
0 commit comments