Commit 8f300c2c

8f300c2c0491Merged

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

Description

codegen: a block stored into an ivar uses the poly return ABI

A method that stores its &block into an instance variable (@blk = blk) type-erases it into a generic sp_Proc*. A later @blk.call reads the boxed poly return slot (_sp_proc_poly_ret), but the block's proc returned its scalar value directly through sp_proc_call -- so the call site read a stale nil and the value was lost (a stored { 42 } read back as 0).

When the owner method stores its (non-yielded) &block into an ivar, force the block literal's proc to the poly return ABI so it boxes its result into _sp_proc_poly_ret, matching the generic call site. Detection is narrowed to the ivar-store shape (block_stored_in_ivar): a block merely captured into a local proc keeps its concrete return type tracked through that proc's typed .call, so it must not be forced -- forcing it broke the captured-local-proc value flow.

Surfaced once #1436 let such blocks compile; independent of self-capture.

Refs #1436

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

Details

Commit
8f300c2c0491
Parent
ad239b34b987

src/codegen.cmodifiedView file

@@ -841,6 +841,33 @@
841841 return 0;
842842 }
843843
844+/* Does node `id` (a method body subtree) store the &block param `bp` into an
845+ instance variable -- `@x = blk`? Such a block is type-erased into a generic
846+ sp_Proc* ivar; a later `@x.call` reads the boxed _sp_proc_poly_ret, so the
847+ block must use the poly return ABI. (A block merely captured into a local
848+ proc keeps its concrete return type tracked, so it is not forced.) */
849+static int block_stored_in_ivar(Compiler *c, int id, const char *bp) {
850+ if (id < 0) return 0;
851+ const char *ty = nt_type(c->nt, id);
852+ if (!ty) return 0;
853+ if (!strcmp(ty, "InstanceVariableWriteNode") || !strcmp(ty, "InstanceVariableOrWriteNode")) {
854+ int v = nt_ref(c->nt, id, "value");
855+ if (v >= 0) {
856+ const char *vt = nt_type(c->nt, v);
857+ if (vt && !strcmp(vt, "LocalVariableReadNode")) {
858+ const char *vn = nt_str(c->nt, v, "name");
859+ if (vn && !strcmp(vn, bp)) return 1;
860+ }
861+ }
862+ }
863+ int nr = nt_num_refs(c->nt, id);
864+ for (int i = 0; i < nr; i++) if (block_stored_in_ivar(c, nt_ref_at(c->nt, id, i), bp)) return 1;
865+ int na = nt_num_arrs(c->nt, id);
866+ for (int i = 0; i < na; i++) { int n = 0; const int *ids = nt_arr_at(c->nt, id, i, &n);
867+ for (int k = 0; k < n; k++) if (block_stored_in_ivar(c, ids[k], bp)) return 1; }
868+ return 0;
869+}
870+
844871 /* Lower a `proc {}` / `lambda {}` / `Proc.new {}` / `->(){}` literal: emit a
845872 standalone `static mrb_int _proc_N(void *cap, mrb_int argc, mrb_int *args)`
846873 (sp_proc_call's ABI) into g_procs, and emit the boxing `sp_proc_new_meta(...)`
@@ -923,7 +950,15 @@
923950 TyKind ort = comp_ntype(c, orecv);
924951 if (ty_is_object(ort)) mi = comp_method_in_chain(c, ty_object_class(ort), onm, NULL);
925952 }
926- if (mi >= 0 && (TyKind)c->scopes[mi].blk_ret == TY_POLY) ret = TY_POLY;
953+ /* Force the poly ABI when the owner method stores its &block into an ivar
954+ (`@x = blk`): the block becomes a type-erased sp_Proc* called later via
955+ a generic `@x.call` that reads the boxed _sp_proc_poly_ret. A proc
956+ returning its scalar directly would be read as that stale poly slot
957+ (returning nil). */
958+ int escapes = mi >= 0 && c->scopes[mi].blk_param && c->scopes[mi].blk_param[0] &&
959+ !c->scopes[mi].yields &&
960+ block_stored_in_ivar(c, c->scopes[mi].body, c->scopes[mi].blk_param);
961+ if (mi >= 0 && ((TyKind)c->scopes[mi].blk_ret == TY_POLY || escapes)) ret = TY_POLY;
927962 }
928963 }
929964 /* The proc fn returns mrb_int (the ABI); heap-pointer values (strings,