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).
52 lines
1.9 KiB
Plaintext
52 lines
1.9 KiB
Plaintext
// Phase 1 step 1.7 (PLAN-FFI.md): struct returns through
|
||
// `#objc_call`. emit_llvm's `objc_msg_send` arm hands the IR
|
||
// struct type straight to LLVMBuildCall2; the AArch64 / SysV
|
||
// AMD64 backend handles the register-pair / HFA / byval+sret
|
||
// lowering as long as the function type at the call site is
|
||
// the precise IR struct type.
|
||
//
|
||
// Obj-C runtime contract: `[nil structMethod]` returns a
|
||
// zero-initialized struct of the return type. Lets us pin the
|
||
// ABI without constructing a real object graph.
|
||
|
||
#import "modules/std.sx";
|
||
#import "modules/compiler.sx";
|
||
|
||
// 16 B HFA (Apple ARM64 — 2×f64 stays in v0/v1, SysV AMD64 — in xmm0/xmm1).
|
||
NSPoint :: struct { x: f64; y: f64; }
|
||
|
||
// 16 B integer aggregate (Apple ARM64 — x0/x1 register pair, coerced
|
||
// via `[2 x i64]` in our foreign-decl path; same trip-up that
|
||
// issue-0036 surfaced).
|
||
NSRange :: struct { location: u64; length: u64; }
|
||
|
||
// 32 B HFA (Apple ARM64 — 4×f64 stays in v0..v3). NSRect / CGRect
|
||
// shape. The plan singles this out because >16 B is the sret cliff
|
||
// for *integer* aggregates, but HFAs of any size up to v0..v3 stay
|
||
// register-resident; that distinction is what we want to lock in.
|
||
NSRect :: struct {
|
||
x: f64; y: f64; width: f64; height: f64;
|
||
}
|
||
|
||
main :: () -> s32 {
|
||
inline if OS == .macos {
|
||
// 16 B HFA — both fields zero.
|
||
p := #objc_call(NSPoint)(null, "pointValue");
|
||
print("point = ({}, {})\n", p.x, p.y);
|
||
|
||
// 16 B integer — both fields zero.
|
||
r := #objc_call(NSRange)(null, "rangeValue");
|
||
print("range = ({}, {})\n", r.location, r.length);
|
||
|
||
// 32 B HFA — all four fields zero.
|
||
rect := #objc_call(NSRect)(null, "rectValue");
|
||
print("rect = ({}, {}, {}, {})\n", rect.x, rect.y, rect.width, rect.height);
|
||
|
||
// >16 B non-HFA struct returns (sret path) land in Phase 1.8.
|
||
}
|
||
inline if OS != .macos {
|
||
print("skipped (not macos)\n");
|
||
}
|
||
0
|
||
}
|