Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 29 additions & 74 deletions src/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ AttrInput ->
```

r[attributes.intro]
An _attribute_ is a general, free-form metadatum that is interpreted according
to name, convention, language, and compiler version. Attributes are modeled
on Attributes in [ECMA-335], with the syntax coming from [ECMA-334] \(C#).
An _attribute_ is a general, free-form metadatum that is interpreted according to name, convention, language, and compiler version. Attributes are modeled on Attributes in [ECMA-335], with the syntax coming from [ECMA-334] \(C#).

r[attributes.inner]
_Inner attributes_, written with a bang (`!`) after the hash (`#`), apply to the form that the attribute is declared within.
Expand Down Expand Up @@ -62,17 +60,10 @@ _Outer attributes_, written without the bang after the hash, apply to the form t
> ```

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

r[attributes.safety]
An attribute may be unsafe to apply. To avoid undefined behavior when using
these attributes, certain obligations that cannot be checked by the compiler
must be met. To assert these have been, the attribute is wrapped in
`unsafe(..)`, e.g. `#[unsafe(no_mangle)]`.
An attribute may be unsafe to apply. To avoid undefined behavior when using these attributes, certain obligations that cannot be checked by the compiler must be met. To assert these have been, the attribute is wrapped in `unsafe(..)`, e.g. `#[unsafe(no_mangle)]`.

The following attributes are unsafe:

Expand All @@ -92,29 +83,21 @@ Attributes can be classified into the following kinds:
r[attributes.allowed-position]
Attributes may be applied to many forms in the language:

* All [item declarations] accept outer attributes while [external blocks],
[functions], [implementations], and [modules] accept inner attributes.
* Most [statements] accept outer attributes (see [Expression Attributes] for
limitations on expression statements).
* [Block expressions] accept outer and inner attributes, but only when they are
the outer expression of an [expression statement] or the final expression of
another block expression.
* All [item declarations] accept outer attributes while [external blocks], [functions], [implementations], and [modules] accept inner attributes.
* Most [statements] accept outer attributes (see [Expression Attributes] for limitations on expression statements).
* [Block expressions] accept outer and inner attributes, but only when they are the outer expression of an [expression statement] or the final expression of another block expression.
* [Enum] variants and [struct] and [union] fields accept outer attributes.
* [Match expression arms][match expressions] accept outer attributes.
* [Generic lifetime or type parameter][generics] accept outer attributes.
* Expressions accept outer attributes in limited situations, see [Expression
Attributes] for details.
* [Function][functions], [closure] and [function pointer]
parameters accept outer attributes. This includes attributes on variadic parameters
denoted with `...` in function pointers and [external blocks][variadic functions].
* Expressions accept outer attributes in limited situations, see [Expression Attributes] for details.
* [Function][functions], [closure] and [function pointer] parameters accept outer attributes. This includes attributes on variadic parameters denoted with `...` in function pointers and [external blocks][variadic functions].
* [Inline assembly] template strings and operands accept outer attributes. Only certain attributes are accepted semantically; for details, see [asm.attributes.supported-attributes].

r[attributes.meta]
## Meta item attribute syntax

r[attributes.meta.intro]
A "meta item" is the syntax used for the [Attr] rule by most [built-in
attributes]. It has the following grammar:
A "meta item" is the syntax used for the [Attr] rule by most [built-in attributes]. It has the following grammar:

r[attributes.meta.syntax]
```grammar,attributes
Expand All @@ -132,15 +115,10 @@ MetaItemInner ->
```

r[attributes.meta.literal-expr]
Expressions in meta items must macro-expand to literal expressions, which must not
include integer or float type suffixes. Expressions which are not literal expressions
will be syntactically accepted (and can be passed to proc-macros), but will be rejected after parsing.
Expressions in meta items must macro-expand to literal expressions, which must not include integer or float type suffixes. Expressions which are not literal expressions will be syntactically accepted (and can be passed to proc-macros), but will be rejected after parsing.

r[attributes.meta.order]
Note that if the attribute appears within another macro, it will be expanded
after that outer macro. For example, the following code will expand the
`Serialize` proc-macro first, which must preserve the `include_str!` call in
order for it to be expanded:
Note that if the attribute appears within another macro, it will be expanded after that outer macro. For example, the following code will expand the `Serialize` proc-macro first, which must preserve the `include_str!` call in order for it to be expanded:

```rust ignore
#[derive(Serialize)]
Expand All @@ -162,9 +140,7 @@ fn foo() {}
```

r[attributes.meta.builtin]
Various built-in attributes use different subsets of the meta item syntax to
specify their inputs. The following grammar rules show some commonly used
forms:
Various built-in attributes use different subsets of the meta item syntax to specify their inputs. The following grammar rules show some commonly used forms:

r[attributes.meta.builtin.syntax]
```grammar,attributes
Expand Down Expand Up @@ -198,30 +174,21 @@ r[attributes.activity]
## Active and inert attributes

r[attributes.activity.intro]
An attribute is either active or inert. During attribute processing, *active
attributes* remove themselves from the form they are on while *inert attributes*
stay on.
An attribute is either active or inert. During attribute processing, *active attributes* remove themselves from the form they are on while *inert attributes* stay on.

The [`cfg`] and [`cfg_attr`] attributes are active.
[Attribute macros] are active. All other attributes are inert.
The [`cfg`] and [`cfg_attr`] attributes are active. [Attribute macros] are active. All other attributes are inert.

r[attributes.tool]
## Tool attributes

r[attributes.tool.intro]
The compiler may allow attributes for external tools where each tool resides
in its own module in the [tool prelude]. The first segment of the attribute
path is the name of the tool, with one or more additional segments whose
interpretation is up to the tool.
The compiler may allow attributes for external tools where each tool resides in its own module in the [tool prelude]. The first segment of the attribute path is the name of the tool, with one or more additional segments whose interpretation is up to the tool.

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

r[attributes.tool.prelude]
Tool attributes are not available if the [`no_implicit_prelude`] attribute is
used.
Tool attributes are not available if the [`no_implicit_prelude`] attribute is used.

```rust
// Tells the rustfmt tool to not format the following element.
Expand Down Expand Up @@ -253,13 +220,11 @@ The following is an index of all built-in attributes.

- Derive
- [`derive`] --- Automatic trait implementations.
- [`automatically_derived`] --- Marker for implementations created by
`derive`.
- [`automatically_derived`] --- Marker for implementations created by `derive`.

- Macros
- [`macro_export`] --- Exports a `macro_rules` macro for cross-crate usage.
- [`macro_use`] --- Expands macro visibility, or imports macros from other
crates.
- [`macro_use`] --- Expands macro visibility, or imports macros from other crates.
- [`proc_macro`] --- Defines a function-like macro.
- [`proc_macro_derive`] --- Defines a derive macro.
- [`proc_macro_attribute`] --- Defines an attribute macro.
Expand All @@ -268,27 +233,21 @@ The following is an index of all built-in attributes.
- [`allow`], [`expect`], [`warn`], [`deny`], [`forbid`] --- Alters the default lint level.
- [`deprecated`] --- Generates deprecation notices.
- [`must_use`] --- Generates a lint for unused values.
- [`diagnostic::on_unimplemented`] --- Hints the compiler to emit a certain error
message if a trait is not implemented.
- [`diagnostic::on_unimplemented`] --- Hints the compiler to emit a certain error message if a trait is not implemented.
- [`diagnostic::do_not_recommend`] --- Hints the compiler to not show a certain trait impl in error messages.

- ABI, linking, symbols, and FFI
- [`link`] --- Specifies a native library to link with an `extern` block.
- [`link_name`] --- Specifies the name of the symbol for functions or statics
in an `extern` block.
- [`link_ordinal`] --- Specifies the ordinal of the symbol for functions or
statics in an `extern` block.
- [`link_name`] --- Specifies the name of the symbol for functions or statics in an `extern` block.
- [`link_ordinal`] --- Specifies the ordinal of the symbol for functions or statics in an `extern` block.
- [`no_link`] --- Prevents linking an extern crate.
- [`repr`] --- Controls type layout.
- [`crate_type`] --- Specifies the type of crate (library, executable, etc.).
- [`no_main`] --- Disables emitting the `main` symbol.
- [`export_name`] --- Specifies the exported symbol name for a function or
static.
- [`link_section`] --- Specifies the section of an object file to use for a
function or static.
- [`export_name`] --- Specifies the exported symbol name for a function or static.
- [`link_section`] --- Specifies the section of an object file to use for a function or static.
- [`no_mangle`] --- Disables symbol name encoding.
- [`used`] --- Forces the compiler to keep a static item in the output
object file.
- [`used`] --- Forces the compiler to keep a static item in the output object file.
- [`crate_name`] --- Specifies the crate name.

- Code generation
Expand All @@ -301,8 +260,7 @@ The following is an index of all built-in attributes.
- [`instruction_set`] --- Specify the instruction set used to generate a function's code.

- Documentation
- `doc` --- Specifies documentation. See [The Rustdoc Book] for more
information. [Doc comments] are transformed into `doc` attributes.
- `doc` --- Specifies documentation. See [The Rustdoc Book] for more information. [Doc comments] are transformed into `doc` attributes.

- Preludes
- [`no_std`] --- Removes std from the prelude.
Expand All @@ -312,8 +270,7 @@ The following is an index of all built-in attributes.
- [`path`] --- Specifies the filename for a module.

- Limits
- [`recursion_limit`] --- Sets the maximum recursion limit for certain
compile-time operations.
- [`recursion_limit`] --- Sets the maximum recursion limit for certain compile-time operations.
- [`type_length_limit`] --- Sets the maximum size of a polymorphic type.

- Runtime
Expand All @@ -322,12 +279,10 @@ The following is an index of all built-in attributes.
- [`windows_subsystem`] --- Specifies the windows subsystem to link with.

- Features
- `feature` --- Used to enable unstable or experimental compiler features. See
[The Unstable Book] for features implemented in `rustc`.
- `feature` --- Used to enable unstable or experimental compiler features. See [The Unstable Book] for features implemented in `rustc`.

- Type System
- [`non_exhaustive`] --- Indicate that a type will have more fields/variants
added in future.
- [`non_exhaustive`] --- Indicate that a type will have more fields/variants added in future.

- Debugger
- [`debugger_visualizer`] --- Embeds a file that specifies debugger output for a type.
Expand Down
Loading