Commit 26f137a9

26f137a906aeMerged

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

Description

codegen: fix three method-call / super / yield bugs

super() with omitted optional params: use emit_args_filled to fill defaults for the parent method's missing parameters, matching the behavior of ordinary call dispatch (initialize_override_param_inference).

Default expressions evaluated in receiver context: in emit_dispatch, temporarily set g_self = selfptr when evaluating a missing-arg default so ivar references (e.g. @base * 10) resolve to the receiver, not the enclosing caller (default_args).

Nested yield in inlined block body: add g_yield_block_fallback global threaded through emit_inline_call_or_stmt; emit_block_invoke sets g_block_id to the fallback so a yield inside the block's body chains to the outermost caller's block. Also keep the rename table active for the block body (nested inlines append, they don't overwrite) and apply rename_local to block param names so renamed inlined locals (x → _y3_x) are used correctly (nested_yield_with_block_param).

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

Details

Commit
26f137a906ae
Parent
b7be1300eead

src/codegen.cmodifiedView file

@@ -59,6 +59,10 @@
5959 static char g_ren_to[MAX_RENAME][112];
6060 static int g_nren = 0;
6161 static int g_block_id = -1;
62+/* When a yielding method is inlined, g_yield_block_fallback holds the block
63+ that was active in the CALLER's context so nested `yield`s inside the
64+ passed block can chain back to the outermost caller's block. */
65+static int g_yield_block_fallback = -1;
6266 /* Name of the `&block` parameter of the method currently being inlined, so
6367 `<blk>.call(args)` inside it expands the active block like `yield args`. */
6468 static const char *g_block_param_name = NULL;
@@ -1638,11 +1642,17 @@
16381642 /* evaluate each param value (provided arg or default) into a temp so the
16391643 virtual-dispatch cases reuse them without re-evaluating */
16401644 int *atmp = np ? malloc(sizeof(int) * np) : NULL;
1645+ const char *saved_self = g_self;
16411646 for (int k = 0; k < np; k++) {
16421647 atmp[k] = ++g_tmp;
16431648 Buf ab; memset(&ab, 0, sizeof ab);
16441649 int kv = (m && kwh_d >= 0) ? kwh_lookup(nt, kwh_d, m->pnames[k]) : -1;
1645- emit_arg_or_default(c, m, k, kv >= 0 ? kv : (k < pos_argc_d ? argv[k] : -1), &ab);
1650+ int provided = kv >= 0 ? kv : (k < pos_argc_d ? argv[k] : -1);
1651+ /* Default expressions (e.g. `@ivar * 10`) reference the callee's self,
1652+ not the caller's. Temporarily point g_self at the receiver. */
1653+ if (provided < 0) g_self = selfptr;
1654+ emit_arg_or_default(c, m, k, provided, &ab);
1655+ g_self = saved_self;
16461656 LocalVar *p = scope_local(m, m->pnames[k]);
16471657 emit_indent(g_pre, g_indent);
16481658 emit_ctype(c, p ? p->type : comp_ntype(c, k < argc ? argv[k] : -1), g_pre);
@@ -4986,7 +4996,11 @@
49864996 int saved_nren = g_nren, saved_block = g_block_id;
49874997 const char *saved_self = g_self;
49884998 const char *saved_bpn = g_block_param_name;
4999+ int saved_yfb = g_yield_block_fallback;
49895000 static char selfbuf[64];
5001+ /* Nested `yield` inside the block body should chain to the block that was
5002+ active before this inline, not to the inner block. */
5003+ g_yield_block_fallback = saved_block;
49905004 g_block_id = block;
49915005 g_block_param_name = m->blk_param;
49925006
@@ -5053,6 +5067,7 @@
50535067 g_block_id = saved_block;
50545068 g_self = saved_self;
50555069 g_block_param_name = saved_bpn;
5070+ g_yield_block_fallback = saved_yfb;
50565071 return 1;
50575072 }
50585073
@@ -5087,8 +5102,11 @@
50875102 for (int k = 0; ; k++) {
50885103 const char *bp = block_param_name(c, blk, k);
50895104 if (!bp) break;
5105+ /* When inside an inlined method, block params may be renamed (e.g. x →
5106+ _y3_x); apply the rename table so we write the right C variable. */
5107+ const char *bpr = rename_local(bp);
50905108 if (!as_expr) emit_indent(b, indent);
5091- buf_printf(b, "lv_%s = ", bp);
5109+ buf_printf(b, "lv_%s = ", bpr);
50925110 if (k < yc) emit_expr(c, yargs[k], b);
50935111 else {
50945112 LocalVar *bl = scope_local(bsc, bp);
@@ -5097,24 +5115,16 @@
50975115 }
50985116 buf_puts(b, as_expr ? "; " : ";\n");
50995117 }
5100- /* Save the outer rename table (entries 0..sv-1) before clearing, because
5101- any nested inline inside the block body writes at index 0 and up,
5102- corrupting the outer entries. */
5103- int sv = g_nren;
5104- char sv_ren_from[MAX_RENAME][96], sv_ren_to[MAX_RENAME][112];
5105- for (int _ri = 0; _ri < sv; _ri++) {
5106- memcpy(sv_ren_from[_ri], g_ren_from[_ri], sizeof g_ren_from[0]);
5107- memcpy(sv_ren_to[_ri], g_ren_to[_ri], sizeof g_ren_to[0]);
5108- }
5109- g_nren = 0;
5110- int svb = g_block_id; g_block_id = -1;
5118+ /* Keep the rename table active for the block body: the block's variable
5119+ references are in the same lexical scope as the surrounding inlined
5120+ method, so renames like x → _y3_x must stay visible. Nested inlines
5121+ inside the block body append at the current g_nren and self-restore.
5122+ Set g_block_id to the fallback (the block active before the enclosing
5123+ inline started) so that a nested `yield` inside the block chains to
5124+ the outermost caller's block rather than going dead. */
5125+ int svb = g_block_id; g_block_id = g_yield_block_fallback;
51115126 const char *svbpn = g_block_param_name; g_block_param_name = NULL;
51125127 emit_stmts(c, bbody, b, as_expr ? 0 : indent);
5113- g_nren = sv;
5114- for (int _ri = 0; _ri < sv; _ri++) {
5115- memcpy(g_ren_from[_ri], sv_ren_from[_ri], sizeof g_ren_from[0]);
5116- memcpy(g_ren_to[_ri], sv_ren_to[_ri], sizeof g_ren_to[0]);
5117- }
51185128 g_block_id = svb; g_block_param_name = svbpn;
51195129 if (as_expr) buf_puts(b, "})");
51205130 }
@@ -8337,10 +8347,9 @@
83378347 for (int i = 0; i < s->nparams; i++) buf_printf(b, ", lv_%s", s->pnames[i]);
83388348 }
83398349 else {
8340- int args = nt_ref(c->nt, id, "arguments");
8341- int argc = 0; const int *argv = NULL;
8342- if (args >= 0) argv = nt_arr(c->nt, args, "arguments", &argc);
8343- for (int k = 0; k < argc; k++) { buf_puts(b, ", "); emit_expr(c, argv[k], b); }
8350+ /* SuperNode: fill in defaults for any omitted trailing params, exactly
8351+ like a normal call through emit_args_filled. */
8352+ emit_args_filled(c, mi, nt_ref(c->nt, id, "arguments"), ", ", b);
83448353 }
83458354 buf_puts(b, ")");
83468355 }