Files
sx/examples/0901-optionals-match-optional-arms.sx
agra bdd0e96d78 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).
2026-06-02 09:23:50 +03:00

86 lines
2.8 KiB
Plaintext

// Match expression with both `null` arms and concrete struct value arms
// produces an optional type (?T) and correctly wraps non-null values.
#import "modules/std.sx";
HAlignment :: enum { leading; center; trailing; }
VAlignment :: enum { top; center; bottom; }
Alignment :: struct { h: HAlignment; v: VAlignment; }
ALIGN_CENTER :: Alignment.{ h = .center, v = .center };
ALIGN_TOP :: Alignment.{ h = .center, v = .top };
ALIGN_BOTTOM :: Alignment.{ h = .center, v = .bottom };
ALIGN_LEADING :: Alignment.{ h = .leading, v = .center };
ALIGN_TRAILING :: Alignment.{ h = .trailing, v = .center };
ALIGN_TOP_LEADING :: Alignment.{ h = .leading, v = .top };
ALIGN_TOP_TRAILING :: Alignment.{ h = .trailing, v = .top };
ALIGN_BOTTOM_LEADING :: Alignment.{ h = .leading, v = .bottom };
ALIGN_BOTTOM_TRAILING :: Alignment.{ h = .trailing, v = .bottom };
Zone :: enum {
floating;
fill;
center;
top;
bottom;
left;
right;
top_left;
top_right;
bottom_left;
bottom_right;
}
// Match expression as implicit return with mixed null/concrete arms
zone_to_alignment :: (zone: Zone) -> ?Alignment {
if zone == {
case .floating: null;
case .fill: ALIGN_CENTER;
case .center: ALIGN_CENTER;
case .top: ALIGN_TOP;
case .bottom: ALIGN_BOTTOM;
case .left: ALIGN_LEADING;
case .right: ALIGN_TRAILING;
case .top_left: ALIGN_TOP_LEADING;
case .top_right: ALIGN_TOP_TRAILING;
case .bottom_left: ALIGN_BOTTOM_LEADING;
case .bottom_right: ALIGN_BOTTOM_TRAILING;
}
}
// Side-effect match inside a function returning bool — must NOT be
// affected by optional inference (no null arms here)
NodeType :: enum { rect; text; image; }
process_node :: (t: NodeType) -> bool {
if t == {
case .rect: { out("rect\n"); }
case .text: { out("text\n"); }
case .image: { out("image\n"); }
}
true
}
main :: () -> void {
// Test null arm
r0 := zone_to_alignment(.floating);
if a := r0 { print("BUG: floating should be null, got h={}\n", xx a.h); }
else { out("ok: floating is null\n"); }
// Test concrete arms
r1 := zone_to_alignment(.left);
if a := r1 { print("ok: left h={}\n", xx a.h); }
else { out("BUG: left returned null\n"); }
r2 := zone_to_alignment(.center);
if a := r2 { print("ok: center h={}\n", xx a.h); }
else { out("BUG: center returned null\n"); }
r3 := zone_to_alignment(.top_right);
if a := r3 { print("ok: top_right h={} v={}\n", xx a.h, xx a.v); }
else { out("BUG: top_right returned null\n"); }
// Test side-effect match (no null arms) still works
process_node(.rect);
process_node(.text);
}