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