From 768c2475b1663dc570d9b7cf0338ae17dcdb8780 Mon Sep 17 00:00:00 2001 From: Alexander Satretdinov Date: Mon, 16 Nov 2026 21:25:18 +0200 Subject: [PATCH] a block without a value drops its last expression, so the ';' after it is optional in for, while, defer and bare blocks --- docs/src/language/conditionals.md | 8 ++++---- pkg/rune/parse_stmt.go | 4 ---- test/block/loop_tail.rn | 27 +++++++++++++++++++++++++-- test/block/loop_tail.stderr | 6 ------ 4 files changed, 29 insertions(+), 16 deletions(-) delete mode 100644 test/block/loop_tail.stderr diff --git a/docs/src/language/conditionals.md b/docs/src/language/conditionals.md index d17543c..61a51ba 100644 --- a/docs/src/language/conditionals.md +++ b/docs/src/language/conditionals.md @@ -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] ``` diff --git a/pkg/rune/parse_stmt.go b/pkg/rune/parse_stmt.go index 9e2fcc9..0d3511d 100644 --- a/pkg/rune/parse_stmt.go +++ b/pkg/rune/parse_stmt.go @@ -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()) diff --git a/test/block/loop_tail.rn b/test/block/loop_tail.rn index 042e544..e62ff80 100644 --- a/test/block/loop_tail.rn +++ b/test/block/loop_tail.rn @@ -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 diff --git a/test/block/loop_tail.stderr b/test/block/loop_tail.stderr deleted file mode 100644 index 619fb68..0000000 --- a/test/block/loop_tail.stderr +++ /dev/null @@ -1,6 +0,0 @@ -error: Expect ';' after expression. - --> loop_tail.rn:3:6 - | -2 | for i in 0..2 { -3 | i - | ^