Skip to content

Commit be09487

Browse files
committed
Attributes: Docs about what's allowed where for block exprs, impls
1 parent 8c1cb09 commit be09487

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

src/attributes.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
>    | _MetaItem_
2222
>    | _MetaItem_ `,` _MetaSeq_
2323
24-
An _attribute_ is a
25-
general, free-form metadatum that is interpreted according to name, convention,
26-
and language and compiler version. Attributes are modeled on Attributes in
27-
[ECMA-335], with the syntax coming from [ECMA-334] \(C#).
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#).
2827

2928
Attributes may appear as any of:
3029

@@ -45,6 +44,9 @@ Attributes may be applied to many things in the language:
4544
* All [item declarations] accept outer attributes while [external blocks],
4645
[functions], [implementations], and [modules] accept inner attributes.
4746
* [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.
4850
* [Enum] variants and [struct] and [union] fields accept outer attributes.
4951
* [Match expression arms][match expressions] accept outer attributes.
5052
* [Generic lifetime or type parameter][generics] accept outer attributes.
@@ -557,6 +559,7 @@ You can implement `derive` for your own type through [procedural macros].
557559
[expression statement]: statements.html#expression-statements
558560
[call expression]: expressions/call-expr.html
559561
[block expression]: expressions/block-expr.html
562+
[block expressions]: expressions/block-expr.html
560563
[`Drop`]: special-types-and-traits.html#drop
561564
[let statement]: statements.html#let-statements
562565
[unstable book plugin]: ../unstable-book/language-features/plugin.html#lint-plugins
@@ -569,4 +572,4 @@ You can implement `derive` for your own type through [procedural macros].
569572
[modules]: items/modules.html
570573
[statements]: statements.html
571574
[match expressions]: expressions/match-expr.html
572-
[external blocks]: items/external-blocks.html
575+
[external blocks]: items/external-blocks.html

src/expressions/block-expr.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,30 @@ 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() {
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
65-
[value expressions]: expressions.html#place-expressions-and-value-expressions
83+
[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

src/items/implementations.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,17 @@ impl Seq<bool> for u32 {
136136
}
137137
```
138138

139-
## Attributes in Implementations
140-
141-
Implementations may contain inner [attributes] inside the brackets that contain
142-
the associated items. They must come before the associated items.
139+
## Attributes on Implementations
143140

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].
144146

145147
[trait]: items/traits.html
146148
[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

0 commit comments

Comments
 (0)