Skip to content

Commit 173a829

Browse files
committed
Add identifier syntax to attributes.md
1 parent cf88463 commit 173a829

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

src/attributes.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{{#include attributes-redirect.html}}
22
# Attributes
33

4+
r[attributes]
5+
6+
7+
r[attributes.syntax]
48
> **<sup>Syntax</sup>**\
59
> _InnerAttribute_ :\
610
> &nbsp;&nbsp; `#` `!` `[` _Attr_ `]`
@@ -16,20 +20,24 @@
1620
> &nbsp;&nbsp; &nbsp;&nbsp; [_DelimTokenTree_]\
1721
> &nbsp;&nbsp; | `=` [_Expression_]
1822
23+
r[attributes.general]
1924
An _attribute_ is a general, free-form metadatum that is interpreted according
2025
to name, convention, language, and compiler version. Attributes are modeled
2126
on 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
2430
item that the attribute is declared within. _Outer attributes_, written without
2531
the bang after the hash, apply to the thing that follows the attribute.
2632

33+
r[attributes.input]
2734
The attribute consists of a path to the attribute, followed by an optional
2835
delimited token tree whose interpretation is defined by the attribute.
2936
Attributes other than macro attributes also allow the input to be an equals
3037
sign (`=`) followed by an expression. See the [meta item
3138
syntax](#meta-item-attribute-syntax) below for more details.
3239

40+
r[attributes.safety]
3341
An attribute may be unsafe to apply. To avoid undefined behavior when using
3442
these attributes, certain obligations that cannot be checked by the compiler
3543
must 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]
4453
Attributes 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]
5160
Attributes 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]
103115
A "meta item" is the syntax used for the _Attr_ rule by most [built-in
104116
attributes]. It has the following grammar:
105117

118+
r[attributes.meta.syntax]
106119
> **<sup>Syntax</sup>**\
107120
> _MetaItem_ :\
108121
> &nbsp;&nbsp; &nbsp;&nbsp; [_SimplePath_]\
@@ -116,10 +129,12 @@ attributes]. It has the following grammar:
116129
> &nbsp;&nbsp; &nbsp;&nbsp; _MetaItem_\
117130
> &nbsp;&nbsp; | [_Expression_]
118131
132+
r[attributes.meta.literal-expr]
119133
Expressions in meta items must macro-expand to literal expressions, which must not
120134
include integer or float type suffixes. Expressions which are not literal expressions
121135
will be syntactically accepted (and can be passed to proc-macros), but will be rejected after parsing.
122136

137+
r[attributes.meta.order]
123138
Note that if the attribute appears within another macro, it will be expanded
124139
after 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]
136152
Additionally, 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
143159
fn foo() {}
144160
```
145161

162+
r[attributes.meta.builtin]
146163
Various built-in attributes use different subsets of the meta item syntax to
147164
specify their inputs. The following grammar rules show some commonly used
148165
forms:
@@ -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]
178198
An attribute is either active or inert. During attribute processing, *active
179199
attributes* remove themselves from the thing they are on while *inert attributes*
180200
stay 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]
188211
The compiler may allow attributes for external tools where each tool resides
189212
in its own module in the [tool prelude]. The first segment of the attribute
190213
path is the name of the tool, with one or more additional segments whose
191214
interpretation is up to the tool.
192215

216+
r[attributes.tool.ignored]
193217
When a tool is not in use, the tool's attributes are accepted without a
194218
warning. When the tool is in use, the tool is responsible for processing and
195219
interpretation of its attributes.
196220

221+
r[attributes.tool.prelude]
197222
Tool attributes are not available if the [`no_implicit_prelude`] attribute is
198223
used.
199224

@@ -213,32 +238,45 @@ pub fn f() {}
213238
214239
## Built-in attributes index
215240

241+
r[attributes.builtin]
242+
216243
The 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

Comments
 (0)