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:
agra
2026-06-02 09:23:50 +03:00
parent 634cf9bc7f
commit bdd0e96d78
265 changed files with 1070 additions and 761 deletions

View File

@@ -24,7 +24,7 @@ impl View for PaddingModifier {
Size.{
width = child_size.width + self.insets.horizontal(),
height = child_size.height + self.insets.vertical()
};
}
}
layout :: (self: *PaddingModifier, bounds: Frame) {
@@ -37,7 +37,7 @@ impl View for PaddingModifier {
}
handle_event :: (self: *PaddingModifier, event: *Event, frame: Frame) -> bool {
self.child.view.handle_event(event, self.child.computed_frame);
self.child.view.handle_event(event, self.child.computed_frame)
}
}
@@ -58,7 +58,7 @@ impl View for FrameModifier {
Size.{
width = self.width ?? child_size.width,
height = self.height ?? child_size.height
};
}
}
layout :: (self: *FrameModifier, bounds: Frame) {
@@ -78,7 +78,7 @@ impl View for FrameModifier {
}
handle_event :: (self: *FrameModifier, event: *Event, frame: Frame) -> bool {
self.child.view.handle_event(event, self.child.computed_frame);
self.child.view.handle_event(event, self.child.computed_frame)
}
}
@@ -92,7 +92,7 @@ BackgroundModifier :: struct {
impl View for BackgroundModifier {
size_that_fits :: (self: *BackgroundModifier, proposal: ProposedSize) -> Size {
self.child.view.size_that_fits(proposal);
self.child.view.size_that_fits(proposal)
}
layout :: (self: *BackgroundModifier, bounds: Frame) {
@@ -110,7 +110,7 @@ impl View for BackgroundModifier {
}
handle_event :: (self: *BackgroundModifier, event: *Event, frame: Frame) -> bool {
self.child.view.handle_event(event, self.child.computed_frame);
self.child.view.handle_event(event, self.child.computed_frame)
}
}
@@ -123,7 +123,7 @@ OpacityModifier :: struct {
impl View for OpacityModifier {
size_that_fits :: (self: *OpacityModifier, proposal: ProposedSize) -> Size {
self.child.view.size_that_fits(proposal);
self.child.view.size_that_fits(proposal)
}
layout :: (self: *OpacityModifier, bounds: Frame) {
@@ -139,7 +139,7 @@ impl View for OpacityModifier {
}
handle_event :: (self: *OpacityModifier, event: *Event, frame: Frame) -> bool {
self.child.view.handle_event(event, self.child.computed_frame);
self.child.view.handle_event(event, self.child.computed_frame)
}
}
@@ -152,7 +152,7 @@ ClipModifier :: struct {
impl View for ClipModifier {
size_that_fits :: (self: *ClipModifier, proposal: ProposedSize) -> Size {
self.child.view.size_that_fits(proposal);
self.child.view.size_that_fits(proposal)
}
layout :: (self: *ClipModifier, bounds: Frame) {
@@ -167,7 +167,7 @@ impl View for ClipModifier {
}
handle_event :: (self: *ClipModifier, event: *Event, frame: Frame) -> bool {
self.child.view.handle_event(event, self.child.computed_frame);
self.child.view.handle_event(event, self.child.computed_frame)
}
}
@@ -181,7 +181,7 @@ HiddenModifier :: struct {
impl View for HiddenModifier {
size_that_fits :: (self: *HiddenModifier, proposal: ProposedSize) -> Size {
if self.is_hidden { return Size.zero(); }
self.child.view.size_that_fits(proposal);
self.child.view.size_that_fits(proposal)
}
layout :: (self: *HiddenModifier, bounds: Frame) {
@@ -197,7 +197,7 @@ impl View for HiddenModifier {
handle_event :: (self: *HiddenModifier, event: *Event, frame: Frame) -> bool {
if self.is_hidden { return false; }
self.child.view.handle_event(event, self.child.computed_frame);
self.child.view.handle_event(event, self.child.computed_frame)
}
}
@@ -210,7 +210,7 @@ TapGestureModifier :: struct {
impl View for TapGestureModifier {
size_that_fits :: (self: *TapGestureModifier, proposal: ProposedSize) -> Size {
self.child.view.size_that_fits(proposal);
self.child.view.size_that_fits(proposal)
}
layout :: (self: *TapGestureModifier, bounds: Frame) {
@@ -224,7 +224,7 @@ impl View for TapGestureModifier {
handle_event :: (self: *TapGestureModifier, event: *Event, frame: Frame) -> bool {
if self.gesture.handle_event(event, frame) { return true; }
self.child.view.handle_event(event, self.child.computed_frame);
self.child.view.handle_event(event, self.child.computed_frame)
}
}
@@ -237,7 +237,7 @@ DragGestureModifier :: struct {
impl View for DragGestureModifier {
size_that_fits :: (self: *DragGestureModifier, proposal: ProposedSize) -> Size {
self.child.view.size_that_fits(proposal);
self.child.view.size_that_fits(proposal)
}
layout :: (self: *DragGestureModifier, bounds: Frame) {
@@ -251,46 +251,46 @@ impl View for DragGestureModifier {
handle_event :: (self: *DragGestureModifier, event: *Event, frame: Frame) -> bool {
if self.gesture.handle_event(event, frame) { return true; }
self.child.view.handle_event(event, self.child.computed_frame);
self.child.view.handle_event(event, self.child.computed_frame)
}
}
// --- Convenience functions ---
padding :: (view: View, insets: EdgeInsets) -> PaddingModifier {
PaddingModifier.{ child = .{ view = view }, insets = insets };
PaddingModifier.{ child = .{ view = view }, insets = insets }
}
fixed_frame :: (view: View, width: ?f32, height: ?f32) -> FrameModifier {
FrameModifier.{ child = .{ view = view }, width = width, height = height };
FrameModifier.{ child = .{ view = view }, width = width, height = height }
}
background :: (view: View, color: Color, corner_radius: f32) -> BackgroundModifier {
BackgroundModifier.{ child = .{ view = view }, color = color, corner_radius = corner_radius };
BackgroundModifier.{ child = .{ view = view }, color = color, corner_radius = corner_radius }
}
with_opacity :: (view: View, alpha: f32) -> OpacityModifier {
OpacityModifier.{ child = .{ view = view }, alpha = alpha };
OpacityModifier.{ child = .{ view = view }, alpha = alpha }
}
clip :: (view: View, corner_radius: f32) -> ClipModifier {
ClipModifier.{ child = .{ view = view }, corner_radius = corner_radius };
ClipModifier.{ child = .{ view = view }, corner_radius = corner_radius }
}
hidden :: (view: View, is_hidden: bool) -> HiddenModifier {
HiddenModifier.{ child = .{ view = view }, is_hidden = is_hidden };
HiddenModifier.{ child = .{ view = view }, is_hidden = is_hidden }
}
on_tap :: (view: View, handler: Closure()) -> TapGestureModifier {
TapGestureModifier.{
child = .{ view = view },
gesture = TapGesture.{ count = 1, on_tap = handler }
};
}
}
on_drag :: (view: View, on_changed: ?Closure(DragValue), on_ended: ?Closure(DragValue)) -> DragGestureModifier {
DragGestureModifier.{
child = .{ view = view },
gesture = DragGesture.{ min_distance = 10.0, on_changed = on_changed, on_ended = on_ended }
};
}
}