Files
sx/examples/1322-ffi-objc-arc-00-allocator-thread.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

88 lines
3.3 KiB
Plaintext

// ffi-objc-arc-00 — M4.0 end-to-end allocator threading regression.
//
// Verifies that the per-instance allocator design from M1.2 A.5 + M4.0
// is actually wired:
// 1. `push Context.{ allocator = xx tracker } { SxFoo.alloc(); }` →
// the state struct is allocated via tracker (not libc).
// 2. The state struct's first field is `__sx_allocator` (captures the
// tracker so -dealloc can free through it).
// 3. `f.release()` drives refcount → 0 → -dealloc fires → reads the
// captured allocator → calls tracker.dealloc(state).
// 4. The alloc/dealloc deltas around the call pair balance to (+1, +1)
// — exactly one sx-defined-class state struct round-trips.
//
// Pre-M4.0 the +alloc IMP used libc `malloc` and -dealloc used libc
// `free`, both bypassing context.allocator — tracker would have
// observed (+0, +0) deltas, missing the leak silently.
//
// Important: capture tracker counters BEFORE and AFTER the
// alloc/release with NOTHING else allocating in between. `print`
// allocates strings (NSString-backed), which would also route through
// tracker and pollute the deltas.
#import "modules/std.sx";
#import "modules/allocators.sx";
#import "modules/std/objc.sx";
#import "modules/compiler.sx";
SxAllocProbe :: #objc_class("SxAllocProbe") {
#extends NSObject;
counter: s32;
alloc :: () -> *SxAllocProbe;
}
main :: () -> s32 {
inline if OS == .macos {
gpa := GPA.init();
tracker := TrackingAllocator.init(xx gpa);
push Context.{ allocator = xx tracker, data = null } {
// Snapshot BEFORE the sx-defined alloc.
alloc_before := tracker.alloc_count;
dealloc_before := tracker.dealloc_count;
f := SxAllocProbe.alloc();
// Snapshot AFTER alloc, BEFORE release.
alloc_after_alloc := tracker.alloc_count;
dealloc_after_alloc := tracker.dealloc_count;
f.release();
// Snapshot AFTER release.
alloc_after_release := tracker.alloc_count;
dealloc_after_release := tracker.dealloc_count;
// Verify deltas (do all asserts before any print).
alloc_delta_a := alloc_after_alloc - alloc_before;
dealloc_delta_a := dealloc_after_alloc - dealloc_before;
alloc_delta_r := alloc_after_release - alloc_after_alloc;
dealloc_delta_r := dealloc_after_release - dealloc_after_alloc;
if alloc_delta_a < 1 {
print("FAIL: alloc didn't fire through tracker; delta={}\n", alloc_delta_a);
return 1;
}
if dealloc_delta_a != 0 {
print("FAIL: dealloc fired prematurely; delta={}\n", dealloc_delta_a);
return 1;
}
if dealloc_delta_r < 1 {
print("FAIL: release→-dealloc didn't route through tracker; delta={}\n", dealloc_delta_r);
return 1;
}
if dealloc_delta_r != alloc_delta_a {
print("FAIL: alloc/dealloc deltas mismatched; alloc={} dealloc={}\n",
alloc_delta_a, dealloc_delta_r);
return 1;
}
}
print("allocator round-trip: ok\n");
}
inline if OS != .macos {
print("skipped (not macos)\n");
}
0
}