Commit 03eb2769

03eb2769b6aaMerged

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

Description

codegen: capture self in fiber bodies that access instance state

Fiber bodies inside instance methods that access ivars or dispatch implicit self-calls (e.g. Fiber.new { @n += 1 } or Fiber.new { main_loop }) previously referenced an undeclared self because the fiber body function had no access to the enclosing method's receiver.

Fix: detect self-usage (ivar nodes, SelfNode, no-receiver CallNode) and add a self_ptr field to the capture struct, GC-marked by the scan function. The fiber body unpacks it as the correct class pointer.

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

Details

Commit
03eb2769b6aa
Parent
b5240d24a56f

src/codegen.cmodifiedView file

@@ -16543,6 +16543,31 @@
1654316543 t == TY_MATCHDATA || t == TY_REGEX || t == TY_TIME;
1654416544 }
1654516545
16546+/* Returns 1 if the fiber body accesses instance state (ivars, self, or
16547+ implicit self-dispatch calls) without crossing into nested blocks/lambdas. */
16548+static int fiber_body_uses_self(Compiler *c, int id) {
16549+ if (id < 0) return 0;
16550+ const char *ty = nt_type(c->nt, id);
16551+ if (!ty) return 0;
16552+ if (!strcmp(ty, "InstanceVariableReadNode") || !strcmp(ty, "InstanceVariableWriteNode") ||
16553+ !strcmp(ty, "InstanceVariableOperatorWriteNode") ||
16554+ !strcmp(ty, "InstanceVariableOrWriteNode") || !strcmp(ty, "InstanceVariableAndWriteNode") ||
16555+ !strcmp(ty, "SelfNode")) return 1;
16556+ if (!strcmp(ty, "CallNode") && nt_ref(c->nt, id, "receiver") < 0) return 1;
16557+ int nr = nt_num_refs(c->nt, id);
16558+ for (int i = 0; i < nr; i++) {
16559+ int ch = nt_ref_at(c->nt, id, i);
16560+ if (ch >= 0 && !is_nested_block(nt_type(c->nt, ch)) && fiber_body_uses_self(c, ch)) return 1;
16561+ }
16562+ int na = nt_num_arrs(c->nt, id);
16563+ for (int i = 0; i < na; i++) {
16564+ int n = 0; const int *ids = nt_arr_at(c->nt, id, i, &n);
16565+ for (int k = 0; k < n; k++)
16566+ if (ids[k] >= 0 && !is_nested_block(nt_type(c->nt, ids[k])) && fiber_body_uses_self(c, ids[k])) return 1;
16567+ }
16568+ return 0;
16569+}
16570+
1654616571 static void emit_fiber_new(Compiler *c, int id, Buf *b) {
1654716572 const NodeTable *nt = c->nt;
1654816573 int blk = nt_ref(nt, id, "block");
@@ -16589,9 +16614,18 @@
1658916614
1659016615 int ncap = caps.n;
1659116616
16592- /* Emit capture struct + GC scan function when there are captured vars */
16593- if (ncap > 0) {
16617+ /* Capture self if the body accesses ivars or dispatches to self implicitly */
16618+ int cap_self = 0;
16619+ const char *cap_self_class = NULL;
16620+ if (encl && encl->class_id >= 0 && !encl->is_cmethod && body >= 0 && fiber_body_uses_self(c, body)) {
16621+ cap_self = 1;
16622+ cap_self_class = c->classes[encl->class_id].name;
16623+ }
16624+
16625+ /* Emit capture struct + GC scan function when there are captured vars or self */
16626+ if (ncap > 0 || cap_self) {
1659416627 buf_printf(&g_proc_protos, "typedef struct {");
16628+ if (cap_self) buf_printf(&g_proc_protos, " sp_%s *self_ptr;", cap_self_class);
1659516629 for (int i = 0; i < ncap; i++) {
1659616630 LocalVar *lv = encl ? scope_local(encl, caps.v[i]) : NULL;
1659716631 TyKind ct = lv ? lv->type : TY_POLY;
@@ -16602,6 +16636,7 @@
1660216636 buf_printf(&g_proc_protos, "static void _fib_cap_scan_%d(void *p) {\n", fid);
1660316637 buf_printf(&g_proc_protos, " sp_gc_mark(p);\n");
1660416638 buf_printf(&g_proc_protos, " _fib_cap_%d *_c = (_fib_cap_%d *)p;\n", fid, fid);
16639+ if (cap_self) buf_printf(&g_proc_protos, " if (_c->self_ptr) sp_gc_mark((void *)_c->self_ptr);\n");
1660516640 for (int i = 0; i < ncap; i++) {
1660616641 LocalVar *lv = encl ? scope_local(encl, caps.v[i]) : NULL;
1660716642 TyKind ct = lv ? lv->type : TY_POLY;
@@ -16630,8 +16665,13 @@
1663016665 g_ret_type = TY_POLY; g_result_poly = 0; g_result_var = NULL;
1663116666
1663216667 /* Unpack capture struct */
16633- if (ncap > 0) {
16668+ if (ncap > 0 || cap_self) {
1663416669 buf_printf(pb, " _fib_cap_%d *_fc = (_fib_cap_%d *)_fb->user_data;\n", fid, fid);
16670+ if (cap_self) {
16671+ const char *svar = sv_self ? sv_self : "self";
16672+ buf_printf(pb, " sp_%s *%s = _fc->self_ptr;\n", cap_self_class, svar);
16673+ buf_printf(pb, " SP_GC_ROOT(%s);\n", svar);
16674+ }
1663516675 for (int i = 0; i < ncap; i++) {
1663616676 LocalVar *lv = encl ? scope_local(encl, caps.v[i]) : NULL;
1663716677 TyKind ct = lv ? lv->type : TY_POLY;
@@ -16720,7 +16760,7 @@
1672016760 If there are captures, allocate a GC-managed capture struct, fill it,
1672116761 assign to fiber->user_data, then return the fiber.
1672216762 Without captures: just sp_Fiber_new(fname). */
16723- if (ncap > 0) {
16763+ if (ncap > 0 || cap_self) {
1672416764 int tf = ++g_tmp, tc = ++g_tmp;
1672516765 emit_indent(g_pre, g_indent);
1672616766 buf_printf(g_pre, "sp_Fiber *_t%d = sp_Fiber_new(%s);\n", tf, fname);
@@ -16729,6 +16769,10 @@
1672916769 emit_indent(g_pre, g_indent);
1673016770 buf_printf(g_pre, "_fib_cap_%d *_t%d = (_fib_cap_%d *)sp_gc_alloc(sizeof(_fib_cap_%d), NULL, _fib_cap_scan_%d);\n",
1673116771 fid, tc, fid, fid, fid);
16772+ if (cap_self) {
16773+ emit_indent(g_pre, g_indent);
16774+ buf_printf(g_pre, "_t%d->self_ptr = %s;\n", tc, sv_self ? sv_self : "self");
16775+ }
1673216776 for (int i = 0; i < ncap; i++) {
1673316777 const char *rn = rename_local(caps.v[i]);
1673416778 emit_indent(g_pre, g_indent);