Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions docs/src/language/conditionals.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,16 @@ let response = if ok { ({ code: 200 }) } else { ({ code: 500 }) };
print(response); // {code: 200}
```

The bodies of `while`, `for` and `defer` give no value, so every statement
in them ends with `;`. The exception is `return`, `break` and `continue`
right before a `}`: they leave the block, so they need no `;` anywhere:
The bodies of `while`, `for` and `defer` give no value: their last
expression is dropped, so the `;` after it is optional. `return`, `break`
and `continue` right before a `}` need no `;` in any block:

```rune
let mut paid = [];
for amount in [20, -5, 0, 40, 999] {
if amount <= 0 { continue }
if amount > 500 { break }
paid.push(amount);
paid.push(amount)
}
print(paid); // [20, 40]
```
Expand Down
4 changes: 0 additions & 4 deletions pkg/rune/parse_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,6 @@ func (s *Parser) blockOf(valued bool) (*ast.BlockStmt, error) {

switch st := (*last).(type) {
case *ast.ExprStmt:
if !valued {
s.errors = append(s.errors, foreignKeyword(st.Expr, errors.NewMissingError(s.previous(), s.peek(), "Expect ';' after expression.")))
}

b.Valued = valued
case ast.ValuedStmt:
b.Valued = valued && hasValue(st.Values())
Expand Down
27 changes: 25 additions & 2 deletions test/block/loop_tail.rn
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
// [line: 4] Error at '}': Expect ';' after expression.
// A block without a value drops its last expression; the ';' after it is optional.
for i in 0..2 {
i
print(i)
}
// expect: 0
// expect: 1

let mut n = 0;
while n < 1 {
n += 1
}
print(n); // expect: 1

{
print("bare")
}
// expect: bare

fun f() {
defer {
print("deferred");
print("twice")
}
}
f();
// expect: deferred
// expect: twice
6 changes: 0 additions & 6 deletions test/block/loop_tail.stderr

This file was deleted.

Loading