Commit 38918e96

38918e9605c0Merged

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

Description

codegen: box an unused (poly) param's argument in virtual dispatch instead of a void temp

A method whose parameter is never used keeps an unresolved (TY_UNKNOWN) param type. The method signature already maps such a param to poly (sp_RbVal), but the virtual-dispatch arg-temp emission did not, so calling that method through a runtime cls_id switch -- with the param fed a value that does not pin its type (nil, or a poly value) -- declared the argument temp void (void _t = 0;), a hard C compile error. This is the dispatch shape behind the tep proxy hooks (forward self-calling an overridden before_forward(req, res) with res unused), and it reduces to a poly receiver calling a two-arg method with a nil second argument.

Fix both dispatch paths to mirror the signature's TY_UNKNOWN -> poly mapping:

  • emit_arg_or_default now treats an unresolved param as poly, so the argument is boxed (a raw 0/value would not fit an sp_RbVal slot).
  • The object/self-receiver dispatch (emit_dispatch) and the ...-forward path coerce the arg-temp's declared type from TY_UNKNOWN to poly and box the forwarded local.
  • The poly-receiver dispatch declares a nil/void/unresolved argument temp as a boxed poly rather than emit_ctype'ing it to void.

Found while resolving #1477 (the override-dispatch fix exposed this as a separate latent bug). Both dispatch paths verified by test/dispatch_unused_param_nil.rb.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
38918e9605c0
Parent
dbc146ebb728

src/codegen_call.cmodifiedView file

@@ -8217,11 +8217,23 @@
82178217 TyKind ret = comp_ntype(c, id);
82188218 int tv = ++g_tmp, tr = ++g_tmp;
82198219 int *atmp = malloc(sizeof(int) * argc);
8220+ TyKind *atmp_ty = malloc(sizeof(TyKind) * argc);
82208221 buf_printf(b, "({ sp_RbVal _t%d = ", tv); emit_expr(c, recv, b); buf_puts(b, "; ");
82218222 for (int a = 0; a < argc; a++) {
82228223 atmp[a] = ++g_tmp;
8223- emit_ctype(c, infer_type(c, argv[a]), b);
8224- buf_printf(b, " _t%d = ", atmp[a]); emit_expr(c, argv[a], b); buf_puts(b, "; ");
8224+ TyKind at = infer_type(c, argv[a]);
8225+ /* A nil/void/unresolved arg has no concrete C storage (emit_ctype would
8226+ print `void`); hold it as a boxed poly so it can flow into a poly
8227+ param slot. */
8228+ if (at == TY_NIL || at == TY_VOID || at == TY_UNKNOWN) {
8229+ atmp_ty[a] = TY_POLY;
8230+ buf_printf(b, "sp_RbVal _t%d = ", atmp[a]); emit_boxed(c, argv[a], b); buf_puts(b, "; ");
8231+ }
8232+ else {
8233+ atmp_ty[a] = at;
8234+ emit_ctype(c, at, b);
8235+ buf_printf(b, " _t%d = ", atmp[a]); emit_expr(c, argv[a], b); buf_puts(b, "; ");
8236+ }
82258237 }
82268238 emit_ctype(c, is_scalar_ret(ret) ? ret : TY_INT, b);
82278239 buf_printf(b, " _t%d = %s; ", tr, is_scalar_ret(ret) ? default_value(ret) : "0");
@@ -8256,7 +8268,7 @@
82568268 if (pv) pt = pv->type;
82578269 buf_puts(&cb, ", ");
82588270 if (a < argc) {
8259- TyKind at = infer_type(c, argv[a]);
8271+ TyKind at = atmp_ty[a]; /* the temp's actual type (poly for a nil/void arg) */
82608272 char tn[32]; snprintf(tn, sizeof tn, "_t%d", atmp[a]);
82618273 if (pt == TY_POLY && at != TY_POLY) emit_boxed_text(c, at, tn, &cb);
82628274 else buf_puts(&cb, tn);
@@ -8377,6 +8389,7 @@
83778389 }
83788390 buf_printf(b, " } _t%d; })", tr);
83798391 free(atmp);
8392+ free(atmp_ty);
83808393 return;
83818394 }
83828395 }