Skip to content

Clarify implicit semicolon ambiguity resolution. #183

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
merged 1 commit into from
Dec 29, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ declaration until the end of the enclosing block scope.

An *expression statement* is one that evaluates an [expression] and ignores its
result. As a rule, an expression statement's purpose is to trigger the effects
of evaluating its expression. An expression that consists of only a [block
expression][block] or control flow expression and that does not end a block
can also be used as an expression statement by omitting the trailing semicolon.
of evaluating its expression.

An expression that consists of only a [block expression][block] or control flow
expression, if used in a context where a statement is permitted, can omit the
trailing semicolon. This can cause an ambiguity between it being parsed as a
standalone statement and as a part of another expression; in this case, it is
parsed as a statement.

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

When the trailing semicolon is omitted, the result must be type `()`.

```rust
// bad: the block's type is i32, not ()
// Error: expected `()` because of default return type
// if true {
// 1
// }

// good: the block's type is i32
if true {
1
};
```

[block]: expressions/block-expr.html
[expression]: expressions.html
[function]: items/functions.html
[item]: items.html
[module]: items/modules.html
[canonical path]: path.html#canonical-paths
[implementations]: items/implementations.html
[implementations]: items/implementations.html