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).
43 lines
1.2 KiB
Plaintext
43 lines
1.2 KiB
Plaintext
// `xx` cast inside an RHS expression assigned to a struct field takes its
|
|
// target type from the field, not from the enclosing function's return type.
|
|
// Covers if-then-else RHS and binary-op RHS variants.
|
|
|
|
#import "modules/std.sx";
|
|
|
|
Foo :: struct {
|
|
pixel_w: s32;
|
|
dpi: f32;
|
|
last_perf: s64;
|
|
delta_time: f32;
|
|
}
|
|
FC :: struct { a: f32; b: f32; c: s32; d: s32; e: f32; f: f32; }
|
|
|
|
// If-then-else RHS in a function whose return type is not f32.
|
|
calc_bool :: (self: *Foo, wf: f32) -> bool {
|
|
self.dpi = if wf > 0.0 then xx self.pixel_w / wf else 1.0;
|
|
true
|
|
}
|
|
|
|
// Binary-op RHS in a struct-returning function. The xx casts must target f32,
|
|
// not the FC return-struct shape.
|
|
begin :: (self: *Foo, current: s64, freq: s64) -> FC {
|
|
if self.last_perf > 0 {
|
|
self.delta_time = xx (current - self.last_perf) / xx freq;
|
|
}
|
|
FC.{ a = 1.0, b = 2.0, c = 3, d = 4, e = 5.0, f = 6.0 }
|
|
}
|
|
|
|
main :: () -> void {
|
|
f : *Foo = xx malloc(size_of(Foo));
|
|
f.pixel_w = 2880;
|
|
f.dpi = 0.0;
|
|
f.last_perf = 1000;
|
|
f.delta_time = 0.0;
|
|
|
|
_ := calc_bool(f, 1440.0);
|
|
print("dpi={}\n", f.dpi);
|
|
|
|
fc := begin(f, 1500, 1000);
|
|
print("delta={} fc.a={}\n", f.delta_time, fc.a);
|
|
}
|