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).
109 lines
3.7 KiB
Plaintext
109 lines
3.7 KiB
Plaintext
// `xx self` inside a BOOL-returning `#objc_class` method must
|
|
// resolve to the full receiver pointer at a foreign-class method
|
|
// call site, not get truncated to i8 by the enclosing function's
|
|
// BOOL return type. Regression locks in the
|
|
// `resolveCallParamTypes` fix that threads foreign-class method
|
|
// param types correctly even when the receiver is a `#foreign
|
|
// #objc_class` alias. Every probe round-trips the receiver pointer
|
|
// — a regression would read only the low byte and the observer
|
|
// pointer would appear as e.g. 0xC0 / 0x20 instead of its real
|
|
// 64-bit value.
|
|
#import "modules/std.sx";
|
|
#import "modules/std/objc.sx";
|
|
#import "modules/compiler.sx";
|
|
|
|
g_observer : *void = null;
|
|
|
|
// Stand-in for NSNotificationCenter — we just need a foreign-class
|
|
// method with several *void args so the call site's arg-target-type
|
|
// resolution exercises the same path as uikit.sx's keyboard observer.
|
|
SxIssue44Bus :: #foreign #objc_class("NSNotificationCenter") {
|
|
defaultCenter :: () -> *SxIssue44Bus;
|
|
addObserver_selector_name_object :: (self: *Self, observer: *void, sel: *void, name: *void, obj: *void);
|
|
}
|
|
|
|
SxIssue44Foo :: #objc_class("SxIssue44Foo") {
|
|
counter: s32;
|
|
sentinel: *void;
|
|
|
|
alloc :: () -> *SxIssue44Foo;
|
|
|
|
bump :: (this: *Self) {
|
|
this.counter += 1;
|
|
}
|
|
|
|
get :: (this: *Self) -> s32 {
|
|
return this.counter;
|
|
}
|
|
|
|
// Return the receiver — direct `xx this` round-trip.
|
|
me :: (this: *Self) -> *void {
|
|
return xx this;
|
|
}
|
|
|
|
// SxAppDelegate-shape: BOOL return + 2 extra *void args. Pre-fix,
|
|
// the call to addObserver:... would receive `xx this` truncated to
|
|
// its low byte (because resolveCallParamTypes returned `&.{}` for
|
|
// foreign-class receivers and `self.target_type` leaked the BOOL
|
|
// return type into the call's args).
|
|
appDelegate_options :: (this: *Self, app: *void, opts: *void) -> BOOL {
|
|
bus := SxIssue44Bus.defaultCenter();
|
|
bus.addObserver_selector_name_object(
|
|
xx this,
|
|
xx 0,
|
|
xx 0,
|
|
null);
|
|
return 1;
|
|
}
|
|
|
|
// Same shape but captures the observer-equivalent value to a global
|
|
// so we can read it back without going through NSNotificationCenter
|
|
// (which would crash with a real observer != NSObject subclass).
|
|
captureSelf_options :: (this: *Self, app: *void, opts: *void) -> BOOL {
|
|
capture_observer(xx this);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
capture_observer :: (p: *void) {
|
|
g_observer = p;
|
|
}
|
|
|
|
main :: () -> s32 {
|
|
inline if OS == .macos {
|
|
f := SxIssue44Foo.alloc();
|
|
if f == null { print("FAIL: alloc returned null\n"); return 1; }
|
|
|
|
f.bump();
|
|
f.bump();
|
|
f.bump();
|
|
print("counter: {}\n", f.get());
|
|
|
|
// Direct `xx this` round-trip (worked pre-fix).
|
|
f_void : *void = xx f;
|
|
if f.me() == f_void {
|
|
print("me: ok\n");
|
|
} else {
|
|
print("me: WRONG\n");
|
|
}
|
|
|
|
// The actual repro: BOOL return + foreign-class method call.
|
|
// Pre-fix: `xx this` truncated to i8, capture_observer receives
|
|
// (low_byte_of_f) cast back to *void, which won't equal f_void.
|
|
g_observer = null;
|
|
_ = f.captureSelf_options(xx 0, xx 0);
|
|
if g_observer == f_void {
|
|
print("captureSelf-from-BOOL: ok\n");
|
|
} else {
|
|
print("captureSelf-from-BOOL: WRONG\n");
|
|
}
|
|
|
|
// Also exercise the compile-only path — appDelegate_options' IR
|
|
// must pass `ptr` args to objc_msgSend (not `i8`). We don't
|
|
// dispatch this for real (would crash inside NSNotificationCenter),
|
|
// but the build pulling cleanly through is half the point.
|
|
}
|
|
inline if OS != .macos { print("skipped (not macos)\n"); }
|
|
0
|
|
}
|