Skip to content

Commit c524481

Browse files
authored
Clarify implicit semicolon ambiguity resolution.
See https://play.rust-lang.org/?gist=59090cd71dfbd84cbbdc611b94a0a9c2&version=stable for an example that shows that the rule explained in the current docs is incorrect. It implies that the parse ambiguity is resolved depending on the type of the control flow expression, but that's not true.
1 parent 36fc52c commit c524481

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/statements.md

+23-4
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ declaration until the end of the enclosing block scope.
5353

5454
An *expression statement* is one that evaluates an [expression] and ignores its
5555
result. As a rule, an expression statement's purpose is to trigger the effects
56-
of evaluating its expression. An expression that consists of only a [block
57-
expression][block] or control flow expression and that does not end a block
58-
can also be used as an expression statement by omitting the trailing semicolon.
56+
of evaluating its expression.
57+
58+
An expression that consists of only a [block expression][block] or control flow
59+
expression, if used in a context where a statement is permitted, can omit the
60+
trailing semicolon. This can cause an ambiguity between it being parsed as a
61+
standalone statement and as a part of another expression; in this case, it is
62+
parsed as a statement.
5963

6064
```rust
6165
# let mut v = vec![1, 2, 3];
@@ -68,10 +72,25 @@ if v.is_empty() {
6872
[1]; // Separate expression statement, not an indexing expression.
6973
```
7074

75+
When the trailing semicolon is omitted, the result must be type `()`.
76+
77+
```rust
78+
// bad: the block's type is i32, not ()
79+
// Error: expected `()` because of default return type
80+
// if true {
81+
// 1
82+
// }
83+
84+
// good: the block's type is i32
85+
if true {
86+
1
87+
};
88+
```
89+
7190
[block]: expressions/block-expr.html
7291
[expression]: expressions.html
7392
[function]: items/functions.html
7493
[item]: items.html
7594
[module]: items/modules.html
7695
[canonical path]: path.html#canonical-paths
77-
[implementations]: items/implementations.html
96+
[implementations]: items/implementations.html

0 commit comments

Comments
 (0)