feat(lang): block value requires no trailing ; (Rust-style)
A block's value is now its last statement ONLY when that statement is a trailing expression with no `;`. A trailing `;` discards the value, leaving the block void. This makes value-vs-statement explicit and lets the compiler reject "this block was supposed to produce a value". Compiler: - Parser records `Block.produces_value` (last stmt is a no-`;` trailing expression) + `Block.discarded_semi` (the `;` that discarded a value), via `expectSemicolonAfter`. A trailing expression before `}` may now omit its `;` (previously a parse error). Match-arm and else-arm bodies are built value-producing regardless of the arm `;` (arms are exempt — the `;` is an arm terminator). - Lowering: `lowerBlockValue` / the block-expr path / `inferExprType` respect `produces_value`. A value-position block that discards its value is a hard error (`lowerValueBody` for function bodies; the value-context `.block` path for if/else branches, `catch` bodies, value bindings, match arms). Pure-failable `-> !` bodies (value rides the error channel) and a value-if whose branches are void are handled without false errors. - `defer`/`onfail` cleanup bodies lower as statements (void), so a trailing `;` there is fine. Migration (behavior-preserving — output unchanged): - stdlib + ~210 examples: dropped the trailing `;` on value-position last expressions. `format` now ends with an explicit `#insert "return result;"` (it relied on `#insert`-as-block-value, which `;` discards). - Two `main :: () -> s32` examples that relied on the old silent default-return got an explicit trailing `0`. - Rejection snapshots 0412 / 1013 regenerated (their quoted source lines lost a `;`); the diagnostics themselves are unchanged. Docs/tests: specs.md "Block values" section; examples 0040 (rules) + 0041 (rejection); 3 parser unit tests. Filed issue 0066 (pre-existing match-arm negated-literal phi-width quirk, surfaced not caused here). Gates: zig build, zig build test, run_examples.sh -> 343 passed, cross_compile.sh -> 7 passed (also refreshed its stale example names).
This commit is contained in:
51
specs.md
51
specs.md
@@ -1230,8 +1230,9 @@ name :: (params) -> return_type {
|
||||
|
||||
- Parameters: `name: type` separated by commas
|
||||
- Return type: `-> type` (omit for void). A multi-value return is a tuple: `-> (T1, T2)`.
|
||||
- Body: block of statements; last expression is the implicit return value
|
||||
- No `return` keyword needed (last expression = return value)
|
||||
- Body: a block whose **value** is its last statement when that statement is a
|
||||
trailing expression with **no** `;` (see [Block values](#block-values)). That
|
||||
value is the implicit return; an explicit `return` works too.
|
||||
|
||||
A trailing `!` in the return type marks the function **failable** — it adds a
|
||||
separate error channel alongside the normal returns (`-> (T, !)`, `-> !`,
|
||||
@@ -1241,19 +1242,57 @@ return slot. See [§12 Error Handling](#12-error-handling).
|
||||
Examples:
|
||||
```sx
|
||||
compute :: (x: s32) -> s32 {
|
||||
x * x;
|
||||
x * x // trailing expression, no `;` → the return value
|
||||
}
|
||||
|
||||
square :: (x: s32) -> s32 {
|
||||
return x * x; // explicit return is equivalent
|
||||
}
|
||||
|
||||
main :: () {
|
||||
// void return, no -> annotation
|
||||
}
|
||||
```
|
||||
|
||||
// No-arg void function:
|
||||
main :: () {
|
||||
// ...
|
||||
#### Block values
|
||||
|
||||
A block's **value** is its last statement, but only when that statement is a
|
||||
trailing expression with **no** trailing `;`. A trailing `;` discards the value,
|
||||
leaving the block void. This applies uniformly to every block used in value
|
||||
position: function bodies, `if` / `else` branches, value-bound blocks
|
||||
(`x := { … }`), and `catch` bodies.
|
||||
|
||||
```sx
|
||||
a := { f(); g() }; // value is g()
|
||||
b := { f(); g(); }; // void — the `;` discards g()'s value
|
||||
```
|
||||
|
||||
A block in **value position** that produces no value is a compile error (rather
|
||||
than silently returning a zero default):
|
||||
|
||||
```sx
|
||||
double :: (n: s32) -> s32 {
|
||||
n * 2; // error: value discarded by `;` — drop it, or use `return`
|
||||
}
|
||||
```
|
||||
|
||||
**Match arms are exempt.** In `case .x: expr;` the `;` is an arm terminator, not
|
||||
a value-discard, so the arm still yields `expr`. Only an explicit inner braced
|
||||
block inside an arm follows the rule:
|
||||
|
||||
```sx
|
||||
classify :: (n: s32) -> s32 {
|
||||
if n == {
|
||||
case 0: 100; // arm value is 100 (the `;` is just the separator)
|
||||
case 1: { x := 5; x*2 } // braced block, no trailing `;` → value 10
|
||||
else: 7;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
A `defer` / `onfail` cleanup body and loop bodies are statement (void) contexts,
|
||||
so a trailing `;` there is fine and changes nothing.
|
||||
|
||||
#### Default Parameter Values
|
||||
|
||||
A parameter can declare a default value with `name: type = expr`. When a
|
||||
|
||||
Reference in New Issue
Block a user