Skip to content

Commit ff14759

Browse files
Merge pull request #352 from Havvy/attr
General attribute docs improvements
2 parents fdd9a32 + 4016b06 commit ff14759

File tree

6 files changed

+105
-9
lines changed

6 files changed

+105
-9
lines changed

src/attributes.md

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
>    | _MetaItem_\
2222
>    | _MetaItem_ `,` _MetaSeq_
2323
24-
Any [item declaration] or [generic lifetime or type parameter][generics] may
25-
have an attribute applied to it. Attributes are modeled on Attributes in
26-
[ECMA-335], with the syntax coming from [ECMA-334] \(C#). An _attribute_ is a
27-
general, free-form metadatum that is interpreted according to name, convention,
28-
and language and compiler version. Attributes may appear as any of:
24+
An _attribute_ is a general, free-form metadatum that is interpreted according
25+
to name, convention, and language and compiler version. Attributes are modeled
26+
on Attributes in [ECMA-335], with the syntax coming from [ECMA-334] \(C#).
2927

30-
* A single identifier, the attribute name
28+
Attributes may appear as any of:
29+
30+
* A single identifier, the _attribute name_
3131
* An identifier followed by the equals sign '=' and a literal, providing a
3232
key/value pair
3333
* An identifier followed by a parenthesized literal, providing a
@@ -39,7 +39,19 @@ item that the attribute is declared within. _Outer attributes_, written without
3939
the bang after the hash, apply to the item or generic parameter that follow the
4040
attribute.
4141

42-
An example of attributes:
42+
Attributes may be applied to many things in the language:
43+
44+
* All [item declarations] accept outer attributes while [external blocks],
45+
[functions], [implementations], and [modules] accept inner attributes.
46+
* [Statements] accept outer attributes.
47+
* [Block expressions] accept outer and inner attributes, but only when they are
48+
the outer expression of an [expression statement] or the final expression of
49+
another block expression.
50+
* [Enum] variants and [struct] and [union] fields accept outer attributes.
51+
* [Match expression arms][match expressions] accept outer attributes.
52+
* [Generic lifetime or type parameter][generics] accept outer attributes.
53+
54+
Some examples of attributes:
4355

4456
```rust
4557
// General metadata applied to the enclosing module or crate.
@@ -60,8 +72,20 @@ mod bar {
6072
// A lint attribute used to suppress a warning/error
6173
#[allow(non_camel_case_types)]
6274
type int8_t = i8;
75+
76+
// Outer attribute applies to the entire function.
77+
fn some_unused_variables() {
78+
#![allow(unused_variables)]
79+
80+
let x = ();
81+
let y = ();
82+
let z = ();
83+
}
6384
```
6485

86+
The rest of this page describes or links to descriptions of which attribute
87+
names have meaning.
88+
6589
## Crate-only attributes
6690

6791
- `crate_name` - specify the crate's crate name.
@@ -533,11 +557,17 @@ You can implement `derive` for your own type through [procedural macros].
533557
[expression statement]: statements.html#expression-statements
534558
[call expression]: expressions/call-expr.html
535559
[block expression]: expressions/block-expr.html
560+
[block expressions]: expressions/block-expr.html
536561
[`Drop`]: special-types-and-traits.html#drop
537562
[let statement]: statements.html#let-statements
538563
[unstable book plugin]: ../unstable-book/language-features/plugin.html#lint-plugins
539564
[zero-variant enum]: items/enumerations.html#zero-variant-enums
540565
[ECMA-334]: https://www.ecma-international.org/publications/standards/Ecma-334.htm
541566
[ECMA-335]: https://www.ecma-international.org/publications/standards/Ecma-335.htm
542-
[item declaration]: items.html
567+
[item declarations]: items.html
543568
[generics]: items/generics.html
569+
[implementations]: items/implementations.html
570+
[modules]: items/modules.html
571+
[statements]: statements.html
572+
[match expressions]: expressions/match-expr.html
573+
[external blocks]: items/external-blocks.html

src/expressions/block-expr.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,32 @@ unsafe {
5757
let a = unsafe { f() };
5858
```
5959

60+
## Attributes on block expressions
61+
62+
Block expressions allow [outer attributes] and [inner attributes] directly after
63+
the opening brace when the block expression is the outer expression of an
64+
[expression statement] or the final expression of another block expression. The
65+
attributes that have meaning on a block expression are [`cfg`], and [the lint
66+
check attributes].
67+
68+
For example, this function returns `true` on unix platforms and `false` on other
69+
platforms.
70+
71+
```rust
72+
fn is_unix_platform() -> bool {
73+
#[cfg(unix)] { true }
74+
#[cfg(not(unix))] { false }
75+
}
76+
```
77+
6078
[_InnerAttribute_]: attributes.html
6179
[_Statement_]: statements.html
6280
[_Expression_]: expressions.html
6381
[expression]: expressions.html
6482
[statements]: statements.html
6583
[value expressions]: expressions.html#place-expressions-and-value-expressions
84+
[outer attributes]: attributes.html
85+
[inner attributes]: attributes.html
86+
[expression statement]: statements.html#expression-statements
87+
[`cfg`]: attributes.html#conditional-compilation
88+
[the lint check attributes]: attributes.html#lint-check-attributes.html

src/expressions/match-expr.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ let message = match maybe_digit {
175175
};
176176
```
177177

178+
## Attributes on match arms
179+
180+
Outer attributes are allowed on match arms. The only attributes that have
181+
meaning on match arms are [`cfg`], `cold`, and the [lint check attributes].
182+
178183
[_Expression_]: expressions.html
179184
[_BlockExpression_]: expressions/block-expr.html#block-expressions
180185
[place expression]: expressions.html#place-expressions-and-value-expressions
@@ -183,3 +188,5 @@ let message = match maybe_digit {
183188
[numeric types]: types.html#numeric-types
184189
[_InnerAttribute_]: attributes.html
185190
[_OuterAttribute_]: attributes.html
191+
[`cfg`]: attributes.html#conditional-compilation
192+
[lint check attributes]: attributes.html#lint-check-attributes

src/items/functions.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,26 @@ As non-Rust calling conventions do not support unwinding, unwinding past the end
115115
of an extern function will cause the process to abort. In LLVM, this is
116116
implemented by executing an illegal instruction.
117117

118+
## Function attributes
119+
120+
Inner [attributes] on the function's block apply to the function item as a whole.
121+
122+
For example, this function will only be available while running tests.
123+
124+
```
125+
fn test_only() {
126+
#![test]
127+
}
128+
```
129+
130+
> Note: Except for lints, it is idiomatic to only use outer attributes on
131+
> function items.
132+
118133
[external blocks]: items/external-blocks.html
119134
[path]: paths.html
120135
[block]: expressions/block-expr.html
121136
[variables]: variables.html
122137
[type]: types.html
123138
[*function item type*]: types.html#function-item-types
124139
[Trait]: items/traits.html
140+
[attributes]: attributes.html

src/items/implementations.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,17 @@ impl Seq<bool> for u32 {
136136
}
137137
```
138138

139+
## Attributes on Implementations
140+
141+
Implementations may contain outer [attributes] before the `impl` keyword and
142+
inner [attributes] inside the brackets that contain the associated items. Inner
143+
attributes must come before any associated items. That attributes that have
144+
meaning here are [`cfg`], [`deprecated`], [`doc`], and [the lint check
145+
attributes].
139146

140147
[trait]: items/traits.html
148+
[attributes]: attributes.html
149+
[`cfg`]: attributes.html#conditional-compilation
150+
[`deprecated`]: attributes.html#deprecation
151+
[`doc`]: attributes.html#documentation
152+
[the lint check attributes]: attributes.html#lint-check-attributes.html

src/statements.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,19 @@ if true {
9090
};
9191
```
9292

93+
## Attributes on Statements
94+
95+
Statements accept [outer attributes]. The attributes that have meaning on a
96+
statement are [`cfg`], and [the lint check attributes].
97+
9398
[block]: expressions/block-expr.html
9499
[expression]: expressions.html
95100
[function]: items/functions.html
96101
[item]: items.html
97102
[module]: items/modules.html
98103
[canonical path]: paths.html#canonical-paths
99104
[implementations]: items/implementations.html
100-
[variables]: variables.html
105+
[variables]: variables.html
106+
[outer attributes]: attributes.html
107+
[`cfg`]: attributes.html#conditional-compilation
108+
[the lint check attributes]: attributes.html#lint-check-attributes.html

0 commit comments

Comments
 (0)