-
Notifications
You must be signed in to change notification settings - Fork 498
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
ehuss
merged 18 commits into
rust-lang:master
from
chorman0773:spec-add-identifiers-attributes
Nov 15, 2024
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
173a829
Add identifier syntax to attributes.md
chorman0773 e5acdc2
Add identifier syntax to attributes.codegen
chorman0773 41fe211
Add identifier syntax to attributes.debugger
chorman0773 3b6ebe1
Add identifier syntax to attributes.derive
chorman0773 42fbf9b
Add identifier syntax to attributes.diagnostics
chorman0773 8f980cb
Add identifier syntax to attributes.limits
chorman0773 0657657
Add identifier syntax to attributes.testing
chorman0773 2b5d973
Add identifier syntax to attributes.type_system
chorman0773 043c99f
Fix style in modified chapters
chorman0773 b1a26cc
Appease link-checker
chorman0773 aa93e94
Apply suggestions from PR review
chorman0773 d3ef277
Replace uses of `constraint` and `restriction` in attributes chapters.
chorman0773 ccbc70d
Fix various spelling and format errors
ehuss e0f31a0
Add back some newlines that were removed
ehuss da65af0
Remove some double blank lines
ehuss b62fbd2
Remove annotations from the attribute index
ehuss d1232b4
Change a few rule names
ehuss 4ef5f5d
Use plural `diagnostics` for diagnostics chapter
ehuss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ :\ | ||
> `#` `!` `[` _Attr_ `]` | ||
|
@@ -16,20 +17,24 @@ | |
> [_DelimTokenTree_]\ | ||
> | `=` [_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 | ||
|
@@ -41,13 +46,15 @@ The following attributes are unsafe: | |
* [`link_section`] | ||
* [`no_mangle`] | ||
|
||
r[attributes.kind] | ||
Attributes can be classified into the following kinds: | ||
|
||
* [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: | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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], | ||
|
@@ -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_ :\ | ||
> [_SimplePath_]\ | ||
|
@@ -116,10 +127,12 @@ attributes]. It has the following grammar: | |
> _MetaItem_\ | ||
> | [_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 | ||
|
@@ -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 | ||
|
@@ -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: | ||
|
@@ -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. | ||
|
@@ -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. | ||
|
||
|
@@ -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 | ||
|
@@ -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. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.