Files
sx/examples/1212-ffi-04-fp-struct.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

50 lines
2.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Phase 0 baseline (PLAN-FFI.md step 0.4): focused FP-aggregate (HFA)
// FFI test. All-float / all-double aggregates of ≤4 fields stay as
// struct values in LLVM and pass through the float register file
// (AAPCS64 v0..v3, SysV AMD64 xmm0..xmm7). Distinct from the int
// register-coercion paths (i64 / [2 x i64]).
//
// FQuad — 16 B, four f32 (same slot as ffi-02's Vec4f)
// DQuad — 32 B, four f64 (UIEdgeInsets-shape HFA — the
// f32-vs-f64 landmine from this session)
//
// Already nominally covered by ffi-02's Vec4f, but pinning it as a
// focused single-file test means a future ABI rule change that
// breaks the FP path fails *this* test directly without a noisy
// drag-in from the multi-shape baseline.
#import "modules/std.sx";
#import c {
#source "1212-ffi-04-fp-struct.c";
};
FQuad :: struct { a: f32; b: f32; c: f32; d: f32; }
DQuad :: struct { a: f64; b: f64; c: f64; d: f64; }
ffi_fquad_make :: (a: f32, b: f32, c: f32, d: f32) -> FQuad #foreign;
ffi_fquad_reverse :: (v: FQuad) -> FQuad #foreign;
ffi_fquad_sum :: (v: FQuad) -> f32 #foreign;
ffi_dquad_make :: (a: f64, b: f64, c: f64, d: f64) -> DQuad #foreign;
ffi_dquad_reverse :: (v: DQuad) -> DQuad #foreign;
ffi_dquad_sum :: (v: DQuad) -> f64 #foreign;
main :: () -> s32 {
// ── FQuad (16 B, 4×f32 HFA) ────────────────────────────────────
f := ffi_fquad_make(1.0, 2.0, 3.0, 4.0);
print("fquad make = ({}, {}, {}, {})\n", f.a, f.b, f.c, f.d);
g := ffi_fquad_reverse(f);
print("fquad rev = ({}, {}, {}, {})\n", g.a, g.b, g.c, g.d);
print("fquad sum = {}\n", ffi_fquad_sum(f));
// ── DQuad (32 B, 4×f64 HFA — UIEdgeInsets-shape) ──────────────
d := ffi_dquad_make(1.5, 2.5, 3.5, 4.5);
print("dquad make = ({}, {}, {}, {})\n", d.a, d.b, d.c, d.d);
e := ffi_dquad_reverse(d);
print("dquad rev = ({}, {}, {}, {})\n", e.a, e.b, e.c, e.d);
print("dquad sum = {}\n", ffi_dquad_sum(d));
0
}