Skip to content

Commit 49f5c9a

Browse files
committed
Fix statements and add some missing attributes.
1 parent bf8965b commit 49f5c9a

File tree

8 files changed

+97
-25
lines changed

8 files changed

+97
-25
lines changed

src/expressions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ a few specific cases:
262262
https://github.com/rust-lang/rust/issues/15701
263263
-->
264264
* The tail expression of [block expressions].
265+
<!-- Keep list in sync with block-expr.md -->
265266

266267
They are never allowed before:
267268

src/expressions/array-expr.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
> **<sup>Syntax</sup>**\
66
> _ArrayExpression_ :\
7-
> &nbsp;&nbsp; &nbsp;&nbsp; `[` `]`\
8-
> &nbsp;&nbsp; | `[` [_Expression_] ( `,` [_Expression_] )<sup>\*</sup> `,`<sup>?</sup> `]`\
9-
> &nbsp;&nbsp; | `[` [_Expression_] `;` [_Expression_] `]`
7+
> &nbsp;&nbsp; `[` [_InnerAttribute_]<sup>\*</sup> _ArrayElements_<sup>?</sup> `]`
8+
>
9+
> _ArrayElements_ :\
10+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] ( `,` [_Expression_] )<sup>\*</sup> `,`<sup>?</sup>\
11+
> &nbsp;&nbsp; | [_Expression_] `;` [_Expression_]
1012
1113
An _[array](types.html#array-and-slice-types) expression_ can be written by
1214
enclosing zero or more comma-separated expressions of uniform type in square
@@ -30,6 +32,12 @@ greater than 1 then this requires that the type of `a` is
3032
[[1, 0, 0], [0, 1, 0], [0, 0, 1]]; // 2D array
3133
```
3234

35+
### Array expression attributes
36+
37+
[Inner attributes] are allowed directly after the opening bracket of an array
38+
expression in the same expression contexts as [attributes on block
39+
expressions].
40+
3341
## Array and slice indexing expressions
3442

3543
> **<sup>Syntax</sup>**\
@@ -73,8 +81,11 @@ arr[10]; // warning: index out of bounds
7381
The array index expression can be implemented for types other than arrays and slices
7482
by implementing the [Index] and [IndexMut] traits.
7583

76-
[_Expression_]: expressions.html
77-
[memory location]: expressions.html#place-expressions-and-value-expressions
78-
[Index]: ../std/ops/trait.Index.html
7984
[IndexMut]: ../std/ops/trait.IndexMut.html
85+
[Index]: ../std/ops/trait.Index.html
86+
[Inner attributes]: attributes.html
87+
[_Expression_]: expressions.html
88+
[_InnerAttribute_]: attributes.html
89+
[attributes on block expressions]: expressions/block-expr.html#attributes-on-block-expressions
8090
[constant expression]: const_eval.html#constant-expressions
91+
[memory location]: expressions.html#place-expressions-and-value-expressions

src/expressions/block-expr.md

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
> _BlockExpression_ :\
55
> &nbsp;&nbsp; `{`\
66
> &nbsp;&nbsp; &nbsp;&nbsp; [_InnerAttribute_]<sup>\*</sup>\
7-
> &nbsp;&nbsp; &nbsp;&nbsp; [_Statement_]<sup>\*</sup>\
8-
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_]<sup>?</sup>\
7+
> &nbsp;&nbsp; &nbsp;&nbsp; _Statements_<sup>?</sup>\
98
> &nbsp;&nbsp; `}`
9+
>
10+
> _Statements_ :\
11+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Statement_]<sup>\+</sup>\
12+
> &nbsp;&nbsp; | [_Statement_]<sup>\+</sup> [_ExpressionWithoutBlock_]\
13+
> &nbsp;&nbsp; | [_ExpressionWithoutBlock_]
1014
1115
A *block expression*, or *block*, is a control flow expression and anonymous
1216
namespace scope for items and variable declarations. As a control flow
@@ -101,11 +105,19 @@ let a = unsafe { an_unsafe_fn() };
101105

102106
## Attributes on block expressions
103107

104-
Block expressions allow [outer attributes] and [inner attributes] directly after
105-
the opening brace when the block expression is the outer expression of an
106-
[expression statement] or the final expression of another block expression. The
107-
attributes that have meaning on a block expression are [`cfg`] and [the lint
108-
check attributes].
108+
[Inner attributes] are allowed directly after the opening brace of a block
109+
expression in the following situations:
110+
111+
* [Function] and [method] bodies.
112+
* Loop bodies ([`loop`], [`while`], [`while let`], and [`for`]).
113+
* Block expressions used as a [statement].
114+
* Block expressions as elements of [array expressions], [tuple expressions],
115+
[call expressions], tuple-style [struct] and [enum variant] expressions.
116+
* A block expression as the tail expression of another block expression.
117+
<!-- Keep list in sync with expressions.md -->
118+
119+
The attributes that have meaning on a block expression are [`cfg`] and [the
120+
lint check attributes].
109121

110122
For example, this function returns `true` on unix platforms and `false` on other
111123
platforms.
@@ -117,15 +129,26 @@ fn is_unix_platform() -> bool {
117129
}
118130
```
119131

132+
[_ExpressionWithoutBlock_]: expressions.html
120133
[_InnerAttribute_]: attributes.html
121134
[_Statement_]: statements.html
122-
[_Expression_]: expressions.html
135+
[`cfg`]: conditional-compilation.html
136+
[`for`]: expressions/loop-expr.html#iterator-loops
137+
[`loop`]: expressions/loop-expr.html#infinite-loops
138+
[`while let`]: expressions/loop-expr.html#predicate-pattern-loops
139+
[`while`]: expressions/loop-expr.html#predicate-loops
140+
[array expressions]: expressions/array-expr.html
141+
[call expressions]: expressions/call-expr.html
142+
[enum variant]: expressions/enum-variant-expr.html
143+
[expression attributes]: expressions.html#expression-attributes
123144
[expression]: expressions.html
124-
[statements]: statements.html
125-
[value expressions]: expressions.html#place-expressions-and-value-expressions
126-
[outer attributes]: attributes.html
145+
[function]: items/functions.html
127146
[inner attributes]: attributes.html
128-
[expression statement]: statements.html#expression-statements
129-
[`cfg`]: conditional-compilation.html
147+
[method]: items/associated-items.html#methods
148+
[statement]: statements.html
149+
[statements]: statements.html
150+
[struct]: expressions/struct-expr.html
130151
[the lint check attributes]: attributes.html#lint-check-attributes
152+
[tuple expressions]: expressions/tuple-expr.html
131153
[unsafe operations]: unsafety.html
154+
[value expressions]: expressions.html#place-expressions-and-value-expressions

src/expressions/grouped-expr.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> **<sup>Syntax</sup>**\
44
> _GroupedExpression_ :\
5-
> &nbsp;&nbsp; `(` [_Expression_] `)`
5+
> &nbsp;&nbsp; `(` [_InnerAttribute_]<sup>\*</sup> [_Expression_] `)`
66
77
An expression enclosed in parentheses evaluates to the result of the enclosed
88
expression. Parentheses can be used to explicitly specify evaluation order
@@ -35,4 +35,13 @@ assert_eq!( a.f (), "The method f");
3535
assert_eq!((a.f)(), "The field f");
3636
```
3737

38+
## Group expression attributes
39+
40+
[Inner attributes] are allowed directly after the opening parenthesis of a
41+
group expression in the same expression contexts as [attributes on block
42+
expressions].
43+
44+
[Inner attributes]: attributes.html
3845
[_Expression_]: expressions.html
46+
[_InnerAttribute_]: attributes.html
47+
[attributes on block expressions]: expressions/block-expr.html#attributes-on-block-expressions

src/expressions/match-expr.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ let message = match maybe_digit {
120120
Outer attributes are allowed on match arms. The only attributes that have
121121
meaning on match arms are [`cfg`], `cold`, and the [lint check attributes].
122122
123+
[Inner attributes] are allowed directly after the opening brace of the match
124+
expression in the same expression contexts as [attributes on block
125+
expressions].
126+
123127
[_Expression_]: expressions.html
124128
[_BlockExpression_]: expressions/block-expr.html#block-expressions
125129
[place expression]: expressions.html#place-expressions-and-value-expressions
@@ -133,8 +137,10 @@ meaning on match arms are [`cfg`], `cold`, and the [lint check attributes].
133137
[_Pattern_]: patterns.html
134138
[pattern]: patterns.html
135139
[Identifier Patterns]: patterns.html#identifier-patterns
140+
[Inner attributes]: attributes.html
136141
[Struct Patterns]: patterns.html#struct-patterns
137142
[Tuple Struct Patterns]: patterns.html#tuplestruct-patterns
138143
[Tuple Patterns]: patterns.html#tuple-patterns
139144
[Range Pattern]: patterns.html#range-patterns
145+
[attributes on block expressions]: expressions/block-expr.html#attributes-on-block-expressions
140146
[binding mode]: patterns.html#binding-modes

src/expressions/struct-expr.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
> &nbsp;&nbsp; | _StructExprUnit_
88
>
99
> _StructExprStruct_ :\
10-
> &nbsp;&nbsp; [_PathInExpression_] `{` (_StructExprFields_ | _StructBase_)<sup>?</sup> `}`
10+
> &nbsp;&nbsp; [_PathInExpression_] `{` [_InnerAttribute_]<sup>\*</sup> (_StructExprFields_ | _StructBase_)<sup>?</sup> `}`
1111
>
1212
> _StructExprFields_ :\
1313
> &nbsp;&nbsp; _StructExprField_ (`,` _StructExprField_)<sup>\*</sup> (`,` _StructBase_ | `,`<sup>?</sup>)
@@ -16,10 +16,12 @@
1616
> &nbsp;&nbsp; &nbsp;&nbsp; [IDENTIFIER]\
1717
> &nbsp;&nbsp; | ([IDENTIFIER] | [TUPLE_INDEX]) `:` [_Expression_]
1818
>
19-
> _StructBase_ : `..` [_Expression_]
19+
> _StructBase_ :\
20+
> &nbsp;&nbsp; `..` [_Expression_]
2021
>
2122
> _StructExprTuple_ :\
2223
> &nbsp;&nbsp; [_PathInExpression_] `(`\
24+
> &nbsp;&nbsp; &nbsp;&nbsp; [_InnerAttribute_]<sup>\*</sup>\
2325
> &nbsp;&nbsp; &nbsp;&nbsp; ( [_Expression_] (`,` [_Expression_])<sup>\*</sup> `,`<sup>?</sup> )<sup>?</sup>\
2426
> &nbsp;&nbsp; `)`
2527
>
@@ -126,11 +128,19 @@ let a = Gamma; // Gamma unit value.
126128
let b = Gamma{}; // Exact same value as `a`.
127129
```
128130

131+
## Struct expression attributes
132+
133+
[Inner attributes] are allowed directly after the opening brace or parenthesis
134+
of a struct expression in the same expression contexts as [attributes on block
135+
expressions].
129136

130137
[IDENTIFIER]: identifiers.html
138+
[Inner attributes]: attributes.html
131139
[TUPLE_INDEX]: tokens.html#integer-literals
132140
[_Expression_]: expressions.html
141+
[_InnerAttribute_]: attributes.html
133142
[_PathInExpression_]: paths.html#paths-in-expressions
143+
[attributes on block expressions]: expressions/block-expr.html#attributes-on-block-expressions
134144
[call expression]: expressions/call-expr.html
135145
[if let]: expressions/if-expr.html#if-let-expressions
136146
[if]: expressions/if-expr.html#if-expressions

src/expressions/tuple-expr.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
> **<sup>Syntax</sup>**\
66
> _TupleExpression_ :\
7-
> &nbsp;&nbsp; &nbsp;&nbsp; `(` `)`\
8-
> &nbsp;&nbsp; | `(` ( [_Expression_] `,` )<sup>+</sup> [_Expression_]<sup>?</sup> `)`
7+
> &nbsp;&nbsp; `(` [_InnerAttribute_]<sup>\*</sup> _TupleElements_<sup>?</sup> `)`
8+
>
9+
> _TupleElements_ :\
10+
> &nbsp;&nbsp; ( [_Expression_] `,` )<sup>+</sup> [_Expression_]<sup>?</sup>
911
1012
Tuples are written by enclosing zero or more comma-separated expressions in
1113
parentheses. They are used to create [tuple-typed](types.html#tuple-types)
@@ -25,6 +27,12 @@ comma:
2527
(0); // zero in parentheses
2628
```
2729

30+
### Tuple expression attributes
31+
32+
[Inner attributes] are allowed directly after the opening parenthesis of a
33+
tuple expression in the same expression contexts as [attributes on block
34+
expressions].
35+
2836
## Tuple indexing expressions
2937

3038
> **<sup>Syntax</sup>**\
@@ -46,5 +54,8 @@ let unit_x = Point(1.0, 0.0);
4654
assert_eq!(unit_x.0, 1.0);
4755
```
4856

57+
[Inner attributes]: attributes.html
4958
[TUPLE_INDEX]: tokens.html#integer-literals
5059
[_Expression_]: expressions.html
60+
[_InnerAttribute_]: attributes.html
61+
[attributes on block expressions]: expressions/block-expr.html#attributes-on-block-expressions

src/statements.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ An expression that consists of only a [block expression][block] or control flow
7474
expression, if used in a context where a statement is permitted, can omit the
7575
trailing semicolon. This can cause an ambiguity between it being parsed as a
7676
standalone statement and as a part of another expression; in this case, it is
77-
parsed as a statement.
77+
parsed as a statement. The type of [_ExpressionWithBlock_][expression]
78+
expressions when used as statements must be the unit type.
7879

7980
```rust
8081
# let mut v = vec![1, 2, 3];

0 commit comments

Comments
 (0)