Files
sx/examples/1347-ffi-objc-dsl-07-mangling-table.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

71 lines
3.4 KiB
Plaintext

// Phase 3 step 3.2 — locked-in golden test for the default Obj-C
// selector mangling rule (Phase 3.0). One fixture covers the common
// shapes (niladic, 1-arg through 4-arg, camelCase across pieces, and
// the `#selector(...)` override). The accompanying `.ir` snapshot
// records each resolved selector string as an `OBJC_METH_VAR_NAME_*`
// constant — a change to `deriveObjcSelector` produces ONE diff that
// surfaces every affected case at once.
//
// Per the rule:
// - Niladic (arity 0): name verbatim. `length` → "length".
// - Arity N (1..): split the sx name on `_`; each piece becomes a
// keyword with a trailing `:`. Piece count must equal arity.
// - `#selector("...")` overrides the mangling entirely; the literal
// string is used as the selector. Arity is the user's contract.
#import "modules/std.sx";
#import "modules/compiler.sx";
#import "modules/std/objc.sx";
SxManglingProbe :: #foreign #objc_class("SxManglingProbe") {
length :: (self: *Self) -> s32;
addObject :: (self: *Self, a: s32) -> s32;
combine_and :: (self: *Self, a: s32, b: s32) -> s32;
insert_after_index :: (self: *Self, a: s32, b: s32, c: s32) -> s32;
add_observer_for_event :: (self: *Self, a: s32, b: s32, c: s32, d: s32) -> s32;
initWithFrame_options :: (self: *Self, f: s32, o: s32) -> s32;
custom_name :: (self: *Self) -> s32 #selector("actualSelectorName");
}
universal_imp :: (self: *void, _cmd: *void, a: s32, b: s32, c: s32, d: s32) -> s32 callconv(.c) {
// Returns the arg count's witness; the test doesn't check return
// values, only that dispatch succeeds for each selector shape.
a + b + c + d
}
main :: () -> s32 {
inline if OS == .macos {
ns_object := objc_getClass("NSObject".ptr);
cls := objc_allocateClassPair(ns_object, "SxManglingProbe".ptr, 0);
// Register one IMP per selector we'll dispatch to.
class_addMethod(cls, sel_registerName("length".ptr), xx universal_imp, "i@:".ptr);
class_addMethod(cls, sel_registerName("addObject:".ptr), xx universal_imp, "i@:i".ptr);
class_addMethod(cls, sel_registerName("combine:and:".ptr), xx universal_imp, "i@:ii".ptr);
class_addMethod(cls, sel_registerName("insert:after:index:".ptr), xx universal_imp, "i@:iii".ptr);
class_addMethod(cls, sel_registerName("add:observer:for:event:".ptr), xx universal_imp, "i@:iiii".ptr);
class_addMethod(cls, sel_registerName("initWithFrame:options:".ptr), xx universal_imp, "i@:ii".ptr);
class_addMethod(cls, sel_registerName("actualSelectorName".ptr), xx universal_imp, "i@:".ptr);
objc_registerClassPair(cls);
inst : *SxManglingProbe = xx class_createInstance(cls, 0);
// One call per mangling shape; the IR snapshot pins what
// selector string each sx name resolves to.
_ = inst.length();
_ = inst.addObject(1);
_ = inst.combine_and(1, 2);
_ = inst.insert_after_index(1, 2, 3);
_ = inst.add_observer_for_event(1, 2, 3, 4);
_ = inst.initWithFrame_options(1, 2);
_ = inst.custom_name();
print("mangling table OK\n");
}
inline if OS != .macos {
print("skipped (not macos)\n");
}
0
}