-
Notifications
You must be signed in to change notification settings - Fork 532
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
Document inert vs active attributes #1110
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
946c58d
Document inert vs active attributes
Aaron1011 27029e6
Update src/attributes.md
Noratrieb a21ee89
Update src/attributes.md
Noratrieb ce455cb
Update src/attributes.md
Noratrieb 78e6a64
Update src/attributes.md
Noratrieb 069e271
Update src/attributes.md
Noratrieb bc3c874
Update src/test-implementation.md
Noratrieb b118f53
Update src/attributes.md
Noratrieb d1f30ba
Merge branch 'master' into attr-doc
Noratrieb 1966dad
Update src/test-implementation.md
Noratrieb c90a04e
Update src/test-implementation.md
Noratrieb 17b4cec
Update src/test-implementation.md
Noratrieb 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
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Attributes | ||
|
||
Attributes come in two types - 'builtin'/'inert' attributes, and 'non-builtin'/'active' attributes: | ||
Noratrieb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## 'Builtin'/'inert' attributes | ||
Noratrieb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
These attributes are defined in the compiler itself, in | ||
[`compiler/rustc_feature/src/builtin_attrs.rs`][builtin_attrs] | ||
|
||
Example include `#[allow]` and `#[macro_use]` | ||
Noratrieb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[builtin_attrs]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_feature/builtin_attrs/index.html | ||
|
||
These attributes have several important characteristics: | ||
* They are always in scope, and do not participate in typical path-based resolution. | ||
* They cannot be renamed. For example, `use allow as foo` will compile, but writing `#[foo]` will | ||
produce an error. | ||
* They are 'inert', meaning they are left as-is by the macro expansion code. | ||
As a result, any behavior comes as a result of the compiler explicitly checking for their presence | ||
(for example, lint-related code explicitly checks for `#[allow]`, `#[warn]`, `#[deny]`, and | ||
`#[forbid]`) | ||
Noratrieb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## 'Non-builtin'/'active' attributes | ||
|
||
These attributes are defined by a crate - either the standard library, or a proc-macro crate. | ||
**Important**: Many non-builtin attributes, such as `#[derive]`, are still considered part of the | ||
Noratrieb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
core Rust language. However, they are **not** called 'builtin attributes', since they have a | ||
corresponding definition in the standard library. | ||
|
||
Definitions of non-builtin attributes take two forms: | ||
|
||
1. Proc-macro attributes, defined via a function annotated with `#[proc_macro_attribute]` in a | ||
proc-macro crate. | ||
2. Ast-based attributes, defined in the standard library. These attributes have special 'stub' | ||
macros defined in places like [`library/core/src/macros/mod.rs`][core_macros] | ||
Noratrieb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[core_macros]: https://github.com/rust-lang/rust/blob/master/library/core/src/macros/mod.rs | ||
|
||
These definitions exist to allow the macros to participate in typical path-based resolution - they | ||
can be imported, re-exported, and renamed just like any other item definition. However, the body of | ||
the definition is empty. Instead, the macro is annotated with the `#[rustc_builtin_macro]` | ||
attribute, which tells the compiler to run a corresponding function in `rustc_builtin_macros`. | ||
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. Could you add a link to the crate's API docs? That way it is easy for people to see more about it, and it will make our CI fail if the name ever changes. |
||
|
||
All non-builtin attributes have the following characteristics: | ||
* Like all other definitions (e.g. structs), they must be brought into scope via an import. | ||
Many standard library attributes are included in the prelude - this is why writing `#[derive]` | ||
works without an import. | ||
* They participate in macro expansion. The implementation of the macro may leave the attribute | ||
target unchanged, modify the target, produce new AST nodes, or remove the target entirely. |
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
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.
I feel like the single quotes here and in the headings make the text a bit harder to read; are you okay with removing them?
This one seems fine to keep, but I feel that the others make it harder to read.