Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add identifiers to attributes.md and its subchapters #1560

Merged
merged 18 commits into from
Nov 15, 2024
Merged
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
39 changes: 39 additions & 0 deletions src/attributes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{{#include attributes-redirect.html}}
# Attributes

r[attributes.syntax]
> **<sup>Syntax</sup>**\
> _InnerAttribute_ :\
> &nbsp;&nbsp; `#` `!` `[` _Attr_ `]`
Expand All @@ -16,20 +17,24 @@
> &nbsp;&nbsp; &nbsp;&nbsp; [_DelimTokenTree_]\
> &nbsp;&nbsp; | `=` [_Expression_]

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#).

r[attributes.inner]
_Inner attributes_, written with a bang (`!`) after the hash (`#`), apply to the
item that the attribute is declared within. _Outer attributes_, written without
the bang after the hash, apply to the thing that follows the attribute.

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.

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
Expand All @@ -41,13 +46,15 @@ The following attributes are unsafe:
* [`link_section`]
* [`no_mangle`]

r[attributes.kind]
Attributes can be classified into the following kinds:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you say why these blank lines were removed? I think in general, markdown reads a little better when lists have a blank line separating from surrounding paragraphs.

* [Built-in attributes]
* [Macro attributes][attribute macros]
* [Derive macro helper attributes]
* [Tool attributes](#tool-attributes)

r[attributes.allowed-position]
Attributes may be applied to many things in the language:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same about blank line.

* All [item declarations] accept outer attributes while [external blocks],
Expand Down Expand Up @@ -100,9 +107,13 @@ fn some_unused_variables() {

## Meta Item Attribute Syntax

r[attributes.meta]

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:

r[attributes.meta.syntax]
> **<sup>Syntax</sup>**\
> _MetaItem_ :\
> &nbsp;&nbsp; &nbsp;&nbsp; [_SimplePath_]\
Expand All @@ -116,10 +127,12 @@ attributes]. It has the following grammar:
> &nbsp;&nbsp; &nbsp;&nbsp; _MetaItem_\
> &nbsp;&nbsp; | [_Expression_]

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.

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
Expand All @@ -133,6 +146,7 @@ struct Foo {
}
```

r[attributes.meta.order-macro]
Additionally, macros in attributes will be expanded only after all other attributes applied to the item:

```rust ignore
Expand All @@ -143,6 +157,7 @@ Additionally, macros in attributes will be expanded only after all other attribu
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:
Expand Down Expand Up @@ -175,6 +190,9 @@ _MetaListNameValueStr_ | `link(name = "CoreFoundation", kind = "framework")`

## Active and inert attributes

r[attributes.activity]

r[attributes.activity.intro]
An attribute is either active or inert. During attribute processing, *active
attributes* remove themselves from the thing they are on while *inert attributes*
stay on.
Expand All @@ -185,15 +203,20 @@ active. All other attributes are inert.

## Tool attributes

r[attributes.tool]

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.

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.

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

Expand All @@ -213,32 +236,39 @@ pub fn f() {}

## Built-in attributes index

r[attributes.builtin]

The following is an index of all built-in attributes.

- Conditional compilation
- [`cfg`] --- Controls conditional compilation.
- [`cfg_attr`] --- Conditionally includes attributes.

- Testing
- [`test`] --- Marks a function as a test.
- [`ignore`] --- Disables a test function.
- [`should_panic`] --- Indicates a test should generate a panic.

- Derive
- [`derive`] --- Automatic trait implementations.
- [`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.
- [`proc_macro`] --- Defines a function-like macro.
- [`proc_macro_derive`] --- Defines a derive macro.
- [`proc_macro_attribute`] --- Defines an attribute macro.

- Diagnostics
- [`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.

- 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
Expand All @@ -257,35 +287,44 @@ The following is an index of all built-in attributes.
- [`used`] --- Forces the compiler to keep a static item in the output
object file.
- [`crate_name`] --- Specifies the crate name.

- Code generation
- [`inline`] --- Hint to inline code.
- [`cold`] --- Hint that a function is unlikely to be called.
- [`no_builtins`] --- Disables use of certain built-in functions.
- [`target_feature`] --- Configure platform-specific code generation.
- [`track_caller`] - Pass the parent call location to `std::panic::Location::caller()`.
- [`instruction_set`] - Specify the instruction set used to generate a functions code

- Documentation
- `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.
- [`no_implicit_prelude`] --- Disables prelude lookups within a module.

- Modules
- [`path`] --- Specifies the filename for a module.

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

- Runtime
- [`panic_handler`] --- Sets the function to handle panics.
- [`global_allocator`] --- Sets the global memory allocator.
- [`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`.

- Type System
- [`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.
- [`collapse_debuginfo`] --- Controls how macro invocations are encoded in debuginfo.
Expand Down
Loading