Commit a0ddc1f2

a0ddc1f2df92Merged

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

Description

Void/scalar-result handling in argc>0 poly dispatch

A poly-receiver dispatch with arguments no longer assigns a void method's result to the result temp (it just calls it), and boxes a scalar method result when the dispatch type is poly -- matching the argc==0 path.

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

Details

Commit
a0ddc1f2df92
Parent
39b41092a4f7

src/codegen.cmodifiedView file

@@ -306,6 +306,7 @@
306306 static int emit_inline_expr(Compiler *c, int id, Buf *b);
307307 static void emit_cond(Compiler *c, int id, Buf *b);
308308 static int needs_root(TyKind t);
309+static int method_is_void(Scope *s);
309310 static void emit_index_op_write(Compiler *c, int id, Buf *b, int indent);
310311 static void emit_super(Compiler *c, int id, Buf *b);
311312 static void emit_args_filled(Compiler *c, int callee_idx, int argsNode, const char *lead, Buf *out);
@@ -1920,11 +1921,21 @@
19201921 int defcls = -1;
19211922 int mi = comp_method_in_chain(c, k, name, &defcls);
19221923 if (mi < 0 || c->scopes[mi].nparams != argc) continue;
1923- buf_printf(b, " case %d: _t%d = sp_%s_%s((sp_%s *)_t%d.v.p",
1924- k, tr, c->classes[defcls].name, mc(c->scopes[mi].name),
1925- c->classes[defcls].name, tv);
1926- for (int a = 0; a < argc; a++) buf_printf(b, ", _t%d", atmp[a]);
1927- buf_puts(b, "); break;");
1924+ TyKind mret = c->scopes[mi].ret;
1925+ Buf cb; memset(&cb, 0, sizeof cb);
1926+ buf_printf(&cb, "sp_%s_%s((sp_%s *)_t%d.v.p", c->classes[defcls].name,
1927+ mc(c->scopes[mi].name), c->classes[defcls].name, tv);
1928+ for (int a = 0; a < argc; a++) buf_printf(&cb, ", _t%d", atmp[a]);
1929+ buf_puts(&cb, ")");
1930+ buf_printf(b, " case %d: ", k);
1931+ if (mret == TY_VOID || mret == TY_NIL || method_is_void(&c->scopes[mi])) buf_puts(b, cb.p); /* no usable value */
1932+ else {
1933+ buf_printf(b, "_t%d = ", tr);
1934+ if (ret == TY_POLY && mret != TY_POLY) emit_boxed_text(c, mret, cb.p, b);
1935+ else buf_puts(b, cb.p);
1936+ }
1937+ buf_puts(b, "; break;");
1938+ free(cb.p);
19281939 }
19291940 if (is_index) {
19301941 buf_printf(b, " case SP_BUILTIN_INT_ARRAY: _t%d = sp_IntArray_get((sp_IntArray *)_t%d.v.p, _t%d); break;", tr, tv, atmp[0]);