Commit 43d8d587

43d8d58768fbMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 8 files · +2 added · ~6 modified

Description

Forward a block through ... to the (yielding or &block) target

def foo(...); bar(...); end compiles foo to a real C function, but a block can't reach a yielding target through one: spinel splices a block's AST at the target's yield, and a real function only holds a runtime block with no AST to splice. So a block-bearing call to a pure ... forwarder is now redirected straight to the method the forward eventually reaches, carrying the call-site args and the literal block, which then inlines and splices as usual.

Pieces:

  • codegen: a block call fwd(args){ block } whose target is a pure ... forwarder (whole body is target(...)) retargets to target (following a chain of forwarders to the real yielder/&block owner).
  • inference: a forwarded block -- callee(&b) or the implicit block in a callee(...) forward -- yields the enclosing method's own value, so yield_value_type / method_call_ret resolve through the forward; and a block passed to a ... forwarder is typed from the eventual yielder's args.
  • the inliner now expands a ForwardingArgumentsNode in arg binding and binds keyword params by name (kwh_lookup), so kwargs + block forward correctly -- the latter also fixes a yielding method with kwargs called with a block directly. A blockless <&block>.call on a forwarder's dead path emits nil.

Covers positional, chained, instance-method, explicit-&block-target, and keyword + block forwarding.

Closes #1288

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
43d8d58768fb
Parent
6453c0f3c647

src/analyze_pass.cmodifiedView file

@@ -1856,6 +1856,31 @@
18561856
18571857 int a_proc_params_node(Compiler *c, int create); /* forward decl */
18581858
1859+/* Follow a chain of pure `...` forwarders (a method whose whole body is a
1860+ single `target(...)` call) starting at `mi` until reaching the method that
1861+ actually yields (or owns the &block). Returns that method's index, or -1.
1862+ Lets a block passed to a forwarder be typed from the real yielder's args. */
1863+static int forwarding_yield_target(Compiler *c, int mi, int depth) {
1864+ if (mi < 0 || depth > 16) return -1;
1865+ Scope *m = &c->scopes[mi];
1866+ if (m->yields || (m->blk_param && m->blk_param[0])) return mi;
1867+ int body = m->body;
1868+ if (body < 0 || nt_kind(c->nt, body) != NK_StatementsNode) return -1;
1869+ int n = 0; const int *st = nt_arr(c->nt, body, "body", &n);
1870+ if (n != 1) return -1;
1871+ int call = st[0];
1872+ if (nt_kind(c->nt, call) != NK_CallNode || nt_ref(c->nt, call, "receiver") >= 0) return -1;
1873+ int args = nt_ref(c->nt, call, "arguments");
1874+ int ac = 0; const int *av = args >= 0 ? nt_arr(c->nt, args, "arguments", &ac) : NULL;
1875+ if (ac != 1 || !av || !nt_type(c->nt, av[0]) ||
1876+ strcmp(nt_type(c->nt, av[0]), "ForwardingArgumentsNode")) return -1;
1877+ const char *tn = nt_str(c->nt, call, "name");
1878+ if (!tn) return -1;
1879+ int t = comp_method_index(c, tn);
1880+ if (t < 0 && m->class_id >= 0) t = comp_method_in_chain(c, m->class_id, tn, NULL);
1881+ return forwarding_yield_target(c, t, depth + 1);
1882+}
1883+
18591884 /* Bind block parameter types for supported iteration methods. */
18601885 int infer_block_params(Compiler *c) {
18611886 const NodeTable *nt = c->nt;
@@ -2227,9 +2252,17 @@
22272252 if (cid >= 0) mi = comp_cmethod_in_chain(c, cid, name, NULL);
22282253 }
22292254 }
2230- if (mi >= 0 && c->scopes[mi].yields) {
2231- int yn = first_yield(c, mi);
2232- int ya = yn >= 0 ? nt_ref(nt, yn, "arguments") : first_block_call_args(c, mi);
2255+ /* A block passed to a pure `...` forwarder is really consumed by the
2256+ method the forward eventually reaches; type its params from there. */
2257+ int yld_mi = mi;
2258+ if (mi >= 0 && !c->scopes[mi].yields &&
2259+ !(c->scopes[mi].blk_param && c->scopes[mi].blk_param[0])) {
2260+ int t = forwarding_yield_target(c, mi, 0);
2261+ if (t >= 0) yld_mi = t;
2262+ }
2263+ if (yld_mi >= 0 && c->scopes[yld_mi].yields) {
2264+ int yn = first_yield(c, yld_mi);
2265+ int ya = yn >= 0 ? nt_ref(nt, yn, "arguments") : first_block_call_args(c, yld_mi);
22332266 int yc = 0;
22342267 const int *yargs = ya >= 0 ? nt_arr(nt, ya, "arguments", &yc) : NULL;
22352268 Scope *bs = comp_scope_of(c, block);
@@ -2245,7 +2278,7 @@
22452278 int min_yc = yc;
22462279 for (int _yi = 0; _yi < nt->count; _yi++) {
22472280 if (!nt_type(nt, _yi) || strcmp(nt_type(nt, _yi), "YieldNode")) continue;
2248- if (c->nscope[_yi] != mi) continue;
2281+ if (c->nscope[_yi] != yld_mi) continue;
22492282 int _ya = nt_ref(nt, _yi, "arguments");
22502283 int _yc = 0;
22512284 if (_ya >= 0) nt_arr(nt, _ya, "arguments", &_yc);