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).
79 lines
2.8 KiB
Plaintext
79 lines
2.8 KiB
Plaintext
// ffi-objc-arc-02 — #property(strong) on sx-defined class.
|
|
//
|
|
// Strong contract:
|
|
// - setter retains the new value, releases the old.
|
|
// - -dealloc releases each strong property ivar before freeing state.
|
|
//
|
|
// Observation: a child instance assigned to a parent's strong property
|
|
// stays alive after we drop our own reference. Without the strong
|
|
// setter, the child's refcount drops to 0 immediately when we release,
|
|
// and the parent ends up with a dangling pointer.
|
|
//
|
|
// We observe via TrackingAllocator. The KEY check is the dealloc count
|
|
// AT THE MIDPOINT — between dropping our child reference and releasing
|
|
// the parent. With strong+dealloc-cleanup: child stays alive across
|
|
// midpoint (no extra dealloc). Without: child is dead at midpoint.
|
|
|
|
#import "modules/std.sx";
|
|
#import "modules/allocators.sx";
|
|
#import "modules/std/objc.sx";
|
|
#import "modules/compiler.sx";
|
|
|
|
SxStrongChild :: #objc_class("SxStrongChild") {
|
|
#extends NSObject;
|
|
tag: s32;
|
|
alloc :: () -> *SxStrongChild;
|
|
}
|
|
|
|
SxStrongParent :: #objc_class("SxStrongParent") {
|
|
#extends NSObject;
|
|
child: *SxStrongChild #property(strong);
|
|
alloc :: () -> *SxStrongParent;
|
|
}
|
|
|
|
main :: () -> s32 {
|
|
inline if OS == .macos {
|
|
gpa := GPA.init();
|
|
tracker := TrackingAllocator.init(xx gpa);
|
|
|
|
push Context.{ allocator = xx tracker, data = null } {
|
|
d0 := tracker.dealloc_count;
|
|
a0 := tracker.alloc_count;
|
|
|
|
parent := SxStrongParent.alloc();
|
|
child := SxStrongChild.alloc();
|
|
parent.child = child; // dispatches setChild: via M2.2 property machinery
|
|
child.release();
|
|
|
|
// Midpoint: with strong setter, child is retained by parent;
|
|
// tracker.dealloc_count should NOT have advanced beyond d0.
|
|
// Without strong setter, child auto-dealloc'd on release.
|
|
d_mid := tracker.dealloc_count;
|
|
if d_mid != d0 {
|
|
print("FAIL: child dealloc'd at midpoint (strong setter not retaining); delta={}\n",
|
|
d_mid - d0);
|
|
return 1;
|
|
}
|
|
|
|
parent.release();
|
|
|
|
// After parent.release: parent.dealloc fires, releases the
|
|
// strong child ivar, child deallocs, state freed.
|
|
d_end := tracker.dealloc_count;
|
|
// Net: 2 allocs (parent + child state), 2 deallocs (both freed).
|
|
// Balance check: dealloc delta == alloc delta.
|
|
if d_end - d0 != tracker.alloc_count - a0 {
|
|
print("FAIL: unbalanced; alloc={} dealloc={}\n",
|
|
tracker.alloc_count - a0, d_end - d0);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
print("strong property: ok\n");
|
|
}
|
|
inline if OS != .macos {
|
|
print("skipped (not macos)\n");
|
|
}
|
|
0
|
|
}
|