Commit 5a9c9717

5a9c9717155cMerged

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

Description

Fix &block param capture by nested procs + proc poly-ret undeclared

analyze.c:

  • collect_def_params: register named &block params as LocalVar (type=TY_PROC, is_param=1) so mark_proc_captures can detect capture by nested proc bodies and set is_cell=1
  • finalize_analysis: before the yields=1 escape check, build an inproc_m bitmap for proc bodies nested in the method; a blk_param read inside a proc body is a real escape (the proc captures it in a heap cell) so set escapes=1, preventing spurious yields=1 inlining
  • infer_block_params: for non-inlined methods (yields=0) with a named &block param, scan for blk_param.call(args) in the method body and use those arg types to infer the block params at the call site

codegen.c:

  • emit_cond: move the blk_param/yields=1 check before the type-based dispatch so TY_PROC blk_param in a yielding (inlined) method still emits 0/1 instead of lv_ (which would be undeclared at an inline site)
  • emit_dot_call_expr: when a proc .call site needs _sp_proc_poly_ret (e.g. ivar-stored proc whose proc_ret is unknown → TY_POLY at analysis), ensure the file-level declaration is emitted into g_proc_protos even when no proc body with TY_POLY return triggered emit_proc_fn

Fixes: captured_block_call_value, proc_anon_dot_call, toplevel_kwrest_block_param, bundle_classd_26 (and no regressions)

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
5a9c9717155c
Parent
3b5109d7c695

src/analyze.cmodifiedView file

@@ -2252,6 +2252,13 @@
22522252 if (bp >= 0 && nt_type(c->nt, bp) && !strcmp(nt_type(c->nt, bp), "BlockParameterNode")) {
22532253 const char *bn = nt_str(c->nt, bp, "name");
22542254 s->blk_param = strdup(bn ? bn : "");
2255+ /* Register the &block param as a local so mark_proc_captures can find it
2256+ and mark it is_cell when a nested proc body captures it. */
2257+ if (bn && bn[0]) {
2258+ LocalVar *blv = scope_local_intern(s, bn);
2259+ blv->is_param = 1;
2260+ blv->type = TY_PROC;
2261+ }
22552262 }
22562263 }
22572264
@@ -4825,6 +4832,40 @@
48254832 }
48264833 continue;
48274834 }
4835+ /* Method with a named &block param (not inlined): blk_param.call(args)
4836+ inside the method body determines the arg types for the call-site block. */
4837+ if (mi >= 0 && !c->scopes[mi].yields &&
4838+ c->scopes[mi].blk_param && c->scopes[mi].blk_param[0]) {
4839+ const char *bpname = c->scopes[mi].blk_param;
4840+ Scope *bs = comp_scope_of(c, block);
4841+ for (int bid = 0; bid < nt->count; bid++) {
4842+ const char *bty2 = nt_type(nt, bid);
4843+ if (!bty2 || strcmp(bty2, "CallNode")) continue;
4844+ const char *bcn = nt_str(nt, bid, "name");
4845+ if (!bcn || strcmp(bcn, "call")) continue;
4846+ int brecv = nt_ref(nt, bid, "receiver");
4847+ if (brecv < 0) continue;
4848+ const char *brecvty = nt_type(nt, brecv);
4849+ if (!brecvty || strcmp(brecvty, "LocalVariableReadNode")) continue;
4850+ const char *brecvnm = nt_str(nt, brecv, "name");
4851+ if (!brecvnm || strcmp(brecvnm, bpname)) continue;
4852+ if (comp_scope_of(c, bid) != &c->scopes[mi]) continue;
4853+ int ba = nt_ref(nt, bid, "arguments");
4854+ int barc = 0; const int *barg = NULL;
4855+ if (ba >= 0) barg = nt_arr(nt, ba, "arguments", &barc);
4856+ if (barc == 0) continue;
4857+ for (int k = 0; k < barc; k++) {
4858+ const char *bp = block_param_name(c, block, k);
4859+ if (!bp) continue;
4860+ LocalVar *lv = scope_local_intern(bs, bp); lv->is_block_param = 1;
4861+ TyKind at = infer_type(c, barg[k]);
4862+ if (at == TY_UNKNOWN || at == lv->type) continue;
4863+ TyKind merged = ty_unify(lv->type, at);
4864+ if (merged != lv->type) { lv->type = merged; changed = 1; }
4865+ }
4866+ }
4867+ continue;
4868+ }
48284869 }
48294870
48304871 if (recv < 0) continue;
@@ -5835,6 +5876,18 @@
58355876 /* Anonymous `&`: nameless, so it can only be forwarded -- always safe
58365877 to inline (there is no escaping read to worry about). */
58375878 if (!m->blk_param[0]) { m->yields = 1; continue; }
5879+ /* Mark nodes inside proc/lambda bodies nested within this method.
5880+ A blk_param read there is a real capture-escape: the proc runs
5881+ independently and needs blk to live in a heap cell. */
5882+ char *inproc_m = (char *)calloc((size_t)c->nt->count, 1);
5883+ if (inproc_m) {
5884+ for (int id = 0; id < c->nt->count; id++) {
5885+ if (!is_proc_create(c, id)) continue;
5886+ if (comp_scope_of(c, id) != m) continue;
5887+ int body = a_proc_body(c, id);
5888+ if (body >= 0) a_mark_subtree(c, body, inproc_m);
5889+ }
5890+ }
58385891 int escapes = 0, uses = 0;
58395892 for (int id = 0; id < c->nt->count && !escapes; id++) {
58405893 const char *ty = nt_type(c->nt, id);
@@ -5842,6 +5895,9 @@
58425895 if (comp_scope_of(c, id) != m) continue;
58435896 const char *nm = nt_str(c->nt, id, "name");
58445897 if (!nm || strcmp(nm, m->blk_param)) continue;
5898+ /* A read inside a nested proc body is a capture-escape: the proc
5899+ holds a reference to blk independently of the call site. */
5900+ if (inproc_m && inproc_m[id]) { escapes = 1; break; }
58455901 uses++;
58465902 /* approved: receiver of a `.call`, or expression of a `&block` arg */
58475903 int ok = 0;
@@ -5856,6 +5912,7 @@
58565912 }
58575913 if (!ok) escapes = 1;
58585914 }
5915+ free(inproc_m);
58595916 if (!escapes && uses > 0) {
58605917 /* Don't mark yields=1 if the method has an explicit return: emit_inlined_call
58615918 would reject inlining anyway (scope_has_return), but the method would then