ffi M5.A revert: drop compiler synthesis, require explicit Into(Block) impls

Reconsidered the M5.A.2 cleanup. The compiler-synthesised trampoline
path was hidden behaviour — a user reading their code couldn't tell
how `xx my_closure : Block` worked without reading lower.zig. That's
exactly the kind of magic sx's design has been pushing against.

New design (strict mode):

1. Stdlib's modules/std/objc_block.sx hand-rolls
   `__block_invoke_void` + `Into(Block) for Closure() -> void` and
   the same pair for `Closure(bool) -> void` (restored from M5.A.2).
   These are readable reference implementations of the bridge ABI.

2. The compiler intercept fires NO synthesis — instead, when
   `tryUserConversion` can't find a reachable `Into(Block)` impl for
   the closure's signature, it emits a focused diagnostic:
     "no `Into(Block) for <Closure-sig>` impl — add a per-signature
      `__block_invoke_<sig>` trampoline + Into impl alongside the
      existing ones in modules/std/objc_block.sx, or declare it in
      your own code"

3. Per-signature declarations live in stdlib (for common signatures)
   or in user code (for app-specific ones). 96-objc-block-multi-arg
   now demonstrates the user-side pattern in-file — it declares its
   own `__block_invoke_void_s32_p` + `Into(Block) for Closure(s32,
   *void) -> void` impl alongside its main().

Net effect:
- Every block bridge is source-visible. No hidden compiler magic.
- Users see exactly how the Apple ABI shape is constructed in sx
  source — stdlib serves as the reference implementation.
- Compiler enforces the discipline: missing impl → clear diagnostic
  pointing at the template.
- Coverage for arbitrary signatures requires conscious user opt-in,
  not silent fallthrough.

Removed from lower.zig: `tryClosureToBlockConversion`,
`emitBlockInvokeTrampoline`, `mangleClosureSigForBlock`,
`mangleTypeForBlock`, and the `block_invoke_trampolines` dedup
state field. Net: the synthesis machinery is gone; only the
detection helper `isClosureToBlockCast` remains, used by the
diagnostic.

190/190 example tests pass; chess on iOS-sim green.
This commit is contained in:
agra
2026-05-27 00:34:26 +03:00
parent 26329fe7ba
commit 07f25689ff
3 changed files with 131 additions and 272 deletions

View File

@@ -52,17 +52,58 @@ __sx_block_descriptor : BlockDescriptor = .{
size = 48,
};
// M5.A — `xx closure : Block` casts are handled by the compiler.
// For every closure signature seen at a cast site, the compiler
// synthesises:
// 1. A C-ABI trampoline `__block_invoke_<sig>` matching Apple's
// `__block_literal.invoke` calling convention.
// 2. Inline Block-struct construction at the cast site, with
// `invoke` pointing at the synthesised trampoline and
// `sx_env`/`sx_fn` taken from the closure value.
// Per-signature invoke trampolines. Each one reads sx_env + sx_fn from
// its block_self argument and tail-calls the closure through a typed
// fn-ptr cast. One per Apple block signature we support.
//
// User-facing surface: `xx my_closure : Block` for ANY closure
// signature. No per-signature stdlib boilerplate. The shared
// infrastructure above (`Block`, `BlockDescriptor`,
// `_NSConcreteStackBlock`, `__sx_block_descriptor`) is referenced
// by the synthesised code; users only need to `#import` this module.
// Adding a new signature: write a `__block_invoke_<sig>` trampoline
// matching the closure's calling convention and an
// `impl Into(Block) for Closure(<sig>)` that points its `invoke`
// field at the trampoline. The `xx closure : Block` cast finds the
// impl via `Into` protocol dispatch.
//
// Signature: `void (^)(void)` — no args, no return. The single most
// common Apple block shape (UIView animation bodies, dispatch_async, etc).
__block_invoke_void :: (block_self: *Block) callconv(.c) {
// `sx_fn` is the closure trampoline — an sx-side function with the
// implicit __sx_ctx at slot 0 and env at slot 1. We're a callconv(.c)
// entry, so the call site needs ctx prepended; the typed fn-pointer
// type stays default-conv to enable that.
typed_fn : (*void) -> void = xx block_self.sx_fn;
typed_fn(block_self.sx_env);
}
impl Into(Block) for Closure() -> void {
convert :: (self: Closure() -> void) -> Block {
.{
isa = @_NSConcreteStackBlock,
flags = 0,
reserved = 0,
invoke = xx @__block_invoke_void,
descriptor = xx @__sx_block_descriptor,
sx_env = self.env,
sx_fn = self.fn_ptr,
};
}
}
// Signature: `void (^)(BOOL)` — UIView animation completion handlers and
// similar one-arg-bool callbacks.
__block_invoke_bool :: (block_self: *Block, arg0: bool) callconv(.c) {
typed_fn : (*void, bool) -> void = xx block_self.sx_fn;
typed_fn(block_self.sx_env, arg0);
}
impl Into(Block) for Closure(bool) -> void {
convert :: (self: Closure(bool) -> void) -> Block {
.{
isa = @_NSConcreteStackBlock,
flags = 0,
reserved = 0,
invoke = xx @__block_invoke_bool,
descriptor = xx @__sx_block_descriptor,
sx_env = self.env,
sx_fn = self.fn_ptr,
};
}
}