Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 605
Update attribute documentation.#528
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,44 +1,39 @@ | ||
| # Attributes | ||
| > **<sup>Syntax</sup>**\ | ||
| > _Attribute_ :\ | ||
| > _InnerAttribute_ | _OuterAttribute_ | ||
| > | ||
| > _InnerAttribute_ :\ | ||
| > `#![` MetaItem `]` | ||
| > `#` `!` `[` _Attr_ `]` | ||
| > | ||
| > _OuterAttribute_ :\ | ||
| > `#[` MetaItem `]` | ||
| > | ||
| > _MetaItem_ :\ | ||
| > [_SimplePath_]\ | ||
| > | [_SimplePath_] `=` [_LiteralExpression_]<sub>_without suffix_</sub>\ | ||
| > | [_SimplePath_] `(` _MetaSeq_<sup>?</sup> `)` | ||
| > `#` `[` _Attr_ `]` | ||
| > | ||
| > _MetaSeq_ :\ | ||
| > _MetaItemInner_ ( `,` MetaItemInner )<sup>\*</sup> `,`<sup>?</sup> | ||
| > _Attr_ :\ | ||
| > [_SimplePath_] _AttrInput_<sup>?</sup> | ||
| > | ||
| > _MetaItemInner_ :\ | ||
| > _MetaItem_\ | ||
| > | [_LiteralExpression_]<sub>_without suffix_</sub> | ||
| > _AttrInput_ :\ | ||
| > [_DelimTokenTree_]\ | ||
| > | `=` [_LiteralExpression_]<sub>_without suffix_</sub> | ||
| An _attribute_ is a general, free-form metadatum that is interpreted according | ||
| to name, convention, and language and compiler version. Attributes are modeled | ||
| on Attributes in [ECMA-335], with the syntax coming from [ECMA-334] \(C#). | ||
| Attributes may appear as any of: | ||
| _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. | ||
| * A single identifier, the _attribute name_ | ||
| * An identifier followed by the equals sign '=' and a literal, providing a | ||
| key/value pair | ||
| * An identifier followed by a parenthesized list of sub-attribute arguments | ||
| which include literals | ||
| 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 a literal expression. See the [meta item | ||
| syntax](#meta-item-attribute-syntax) below for more details. | ||
| Literal values must not include integer or float type suffixes. | ||
| Attributes can be classified into the following kinds: | ||
| _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. | ||
| * Built-in attributes | ||
| * [Macro attributes][attribute macro] | ||
| * [Derive macro helper attributes] | ||
| * [Tool attributes](#tool-attributes) | ||
| Attributes may be applied to many things in the language: | ||
| @@ -87,11 +82,34 @@ fn some_unused_variables() { | ||
| } | ||
| ``` | ||
| There are three kinds of attributes: | ||
| ## Meta Item Attribute Syntax | ||
| * Built-in attributes | ||
| * Macro attributes | ||
| * Derive macro helper attributes | ||
| A "meta item" is the syntax used for the _Attr_ rule by most built-in | ||
| attributes and the [`meta` macro fragment specifier]. It has the following | ||
| grammar: | ||
| > **<sup>Syntax</sup>**\ | ||
| > _MetaItem_ :\ | ||
| > [_SimplePath_]\ | ||
| > | [_SimplePath_] `=` [_LiteralExpression_]<sub>_without suffix_</sub>\ | ||
| > | [_SimplePath_] `(` _MetaSeq_<sup>?</sup> `)` | ||
| > | ||
| > _MetaSeq_ :\ | ||
| > _MetaItemInner_ ( `,` MetaItemInner )<sup>\*</sup> `,`<sup>?</sup> | ||
| > | ||
| > _MetaItemInner_ :\ | ||
| > _MetaItem_\ | ||
| > | [_LiteralExpression_]<sub>_without suffix_</sub> | ||
| Literal expressions in meta items must not include integer or float type | ||
| suffixes. | ||
| Some examples of meta items are: | ||
| - `no_std` | ||
| - `doc = "example"` | ||
| - `cfg(any())` | ||
| - `deprecated(since = "1.2.0", note = "text")` | ||
| - `repr(align(32))` | ||
| ## Active and inert attributes | ||
| @@ -139,28 +157,18 @@ names have meaning. | ||
| On an `extern` block, the following attributes are interpreted: | ||
| - `link_args` - specify arguments to the linker, rather than just the library | ||
| name and type. This is feature gated and the exact behavior is | ||
| implementation-defined (due to variety of linker invocation syntax). | ||
| - `link` - indicate that a native library should be linked to for the | ||
| declarations in this block to be linked correctly. `link` supports an optional | ||
| `kind` key with three possible values: `dylib`, `static`, and `framework`. See | ||
| [external blocks](items/external-blocks.html) for more about external blocks. | ||
| Two examples: `#[link(name = "readline")]` and | ||
| `#[link(name = "CoreFoundation", kind = "framework")]`. | ||
| - `linked_from` - indicates what native library this block of FFI items is | ||
| coming from. This attribute is of the form `#[linked_from = "foo"]` where | ||
| `foo` is the name of a library in either `#[link]` or a `-l` flag. This | ||
| attribute is currently required to export symbols from a Rust dynamic library | ||
| on Windows, and it is feature gated behind the `linked_from` feature. | ||
| On declarations inside an `extern` block, the following attributes are | ||
| interpreted: | ||
| - `link_name` - the name of the symbol that this function or static should be | ||
| imported as. | ||
| - `linkage` - on a static, this specifies the [linkage | ||
| type](http://llvm.org/docs/LangRef.html#linkage-types). | ||
| See [type layout](type-layout.html) for documentation on the `repr` attribute | ||
| which can be used to control type layout. | ||
| @@ -175,8 +183,6 @@ which can be used to control type layout. | ||
| macros named. The `extern crate` must appear at the crate root, not inside | ||
| `mod`, which ensures proper function of the `$crate` macro variable. | ||
| - `macro_reexport` on an `extern crate` — re-export the named macros. | ||
| - `macro_export` - export a `macro_rules` macro for cross-crate usage. | ||
| - `no_link` on an `extern crate` — even if we load this crate for macros, don't | ||
| @@ -371,8 +377,7 @@ They only get checked when the associated tool is active, so if you try to use a | ||
| Otherwise, they work just like regular lint attributes: | ||
| ```rust,ignore | ||
| ```rust | ||
| // set the entire `pedantic` clippy lint group to warn | ||
| #![warn(clippy::pedantic)] | ||
| // silence warnings from the `filter_map` clippy lint | ||
| @@ -552,6 +557,34 @@ impl<T: PartialEq> PartialEq for Foo<T> { | ||
| You can implement `derive` for your own traits through [procedural macros]. | ||
| ## Tool attributes | ||
| The compiler may allow attributes for external tools where each tool resides | ||
| in its own namespace. 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. | ||
| 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. | ||
| Tool attributes are not available if the [`no_implicit_prelude`] attribute is | ||
| used. | ||
| ```rust | ||
| // Tells the rustfmt tool to not format the following element. | ||
| #[rustfmt::skip] | ||
| struct S { | ||
| } | ||
| // Controls the "cyclomatic complexity" threshold for the clippy tool. | ||
| #[clippy::cyclomatic_complexity = "100"] | ||
| pub fn f() {} | ||
| ``` | ||
| > Note: `rustc` currently recognizes the tools "clippy" and "rustfmt". | ||
ehuss marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| [_DelimTokenTree_]: macros.html | ||
| [_LiteralExpression_]: expressions/literal-expr.html | ||
| [_SimplePath_]: paths.html#simple-paths | ||
| [`no_implicit_prelude`]: items/modules.html#prelude-items | ||
| @@ -585,6 +618,7 @@ You can implement `derive` for your own traits through [procedural macros]. | ||
| [external blocks]: items/external-blocks.html | ||
| [items]: items.html | ||
| [attribute macro]: procedural-macros.html#attribute-macros | ||
| [derive macro helper attributes]: procedural-macros.html#derive-macro-helper-attributes | ||
| [function-like macro]: procedural-macros.html#function-like-procedural-macros | ||
| [conditional compilation]: conditional-compilation.html | ||
| [derive macro]: procedural-macros.html#derive-macros | ||
| @@ -594,3 +628,4 @@ You can implement `derive` for your own traits through [procedural macros]. | ||
| [where clause]: items/where-clauses.html | ||
| [trait or lifetime bounds]: trait-bounds.html | ||
| [Expression Attributes]: expressions.html#expression-attributes | ||
| [`meta` macro fragment specifier]: macros-by-example.html | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
It probably makes sense to say that only two "tool modules" are currently available -
rustfmtandclippy, and both are hard-coded into the compiler, and there's no way to introduce a custom tool module right now.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 thought about that, but I was reluctant to enshrine "clippy" and "rustfmt" as part of the language. I don't want to be too pedantic, but maybe the added note is ok? To me it's a question of the balance of the reference targeting the language vs the rustc implementation.