Skip to content

Commit 41ccb0e

Browse files
authored
Merge pull request #1560 from chorman0773/spec-add-identifiers-attributes
Add identifiers to attributes.md and its subchapters
2 parents 60fb3cd + 4ef5f5d commit 41ccb0e

File tree

8 files changed

+252
-15
lines changed

8 files changed

+252
-15
lines changed

src/attributes.md

+39
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{{#include attributes-redirect.html}}
22
# Attributes
33

4+
r[attributes.syntax]
45
> **<sup>Syntax</sup>**\
56
> _InnerAttribute_ :\
67
> &nbsp;&nbsp; `#` `!` `[` _Attr_ `]`
@@ -16,20 +17,24 @@
1617
> &nbsp;&nbsp; &nbsp;&nbsp; [_DelimTokenTree_]\
1718
> &nbsp;&nbsp; | `=` [_Expression_]
1819
20+
r[attributes.intro]
1921
An _attribute_ is a general, free-form metadatum that is interpreted according
2022
to name, convention, language, and compiler version. Attributes are modeled
2123
on 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
2427
item that the attribute is declared within. _Outer attributes_, written without
2528
the bang after the hash, apply to the thing that follows the attribute.
2629

30+
r[attributes.input]
2731
The attribute consists of a path to the attribute, followed by an optional
2832
delimited token tree whose interpretation is defined by the attribute.
2933
Attributes other than macro attributes also allow the input to be an equals
3034
sign (`=`) followed by an expression. See the [meta item
3135
syntax](#meta-item-attribute-syntax) below for more details.
3236

37+
r[attributes.safety]
3338
An attribute may be unsafe to apply. To avoid undefined behavior when using
3439
these attributes, certain obligations that cannot be checked by the compiler
3540
must 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]
4450
Attributes 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]
5158
Attributes 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]
103113
A "meta item" is the syntax used for the _Attr_ rule by most [built-in
104114
attributes]. It has the following grammar:
105115

116+
r[attributes.meta.syntax]
106117
> **<sup>Syntax</sup>**\
107118
> _MetaItem_ :\
108119
> &nbsp;&nbsp; &nbsp;&nbsp; [_SimplePath_]\
@@ -116,10 +127,12 @@ attributes]. It has the following grammar:
116127
> &nbsp;&nbsp; &nbsp;&nbsp; _MetaItem_\
117128
> &nbsp;&nbsp; | [_Expression_]
118129
130+
r[attributes.meta.literal-expr]
119131
Expressions in meta items must macro-expand to literal expressions, which must not
120132
include integer or float type suffixes. Expressions which are not literal expressions
121133
will be syntactically accepted (and can be passed to proc-macros), but will be rejected after parsing.
122134

135+
r[attributes.meta.order]
123136
Note that if the attribute appears within another macro, it will be expanded
124137
after 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]
136150
Additionally, 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
143157
fn foo() {}
144158
```
145159

160+
r[attributes.meta.builtin]
146161
Various built-in attributes use different subsets of the meta item syntax to
147162
specify their inputs. The following grammar rules show some commonly used
148163
forms:
@@ -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]
178196
An attribute is either active or inert. During attribute processing, *active
179197
attributes* remove themselves from the thing they are on while *inert attributes*
180198
stay 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]
188209
The compiler may allow attributes for external tools where each tool resides
189210
in its own module in the [tool prelude]. The first segment of the attribute
190211
path is the name of the tool, with one or more additional segments whose
191212
interpretation is up to the tool.
192213

214+
r[attributes.tool.ignored]
193215
When a tool is not in use, the tool's attributes are accepted without a
194216
warning. When the tool is in use, the tool is responsible for processing and
195217
interpretation of its attributes.
196218

219+
r[attributes.tool.prelude]
197220
Tool attributes are not available if the [`no_implicit_prelude`] attribute is
198221
used.
199222

@@ -213,32 +236,39 @@ pub fn f() {}
213236
214237
## Built-in attributes index
215238

239+
r[attributes.builtin]
240+
216241
The 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

Comments
 (0)