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
TyKind ret = comp_ntype(c, id); int tv = ++g_tmp, tr = ++g_tmp; int *atmp = malloc(sizeof(int) * argc); TyKind *atmp_ty = malloc(sizeof(TyKind) * argc); buf_printf(b, "({ sp_RbVal _t%d = ", tv); emit_expr(c, recv, b); buf_puts(b, "; "); for (int a = 0; a < argc; a++) { atmp[a] = ++g_tmp; emit_ctype(c, infer_type(c, argv[a]), b); buf_printf(b, " _t%d = ", atmp[a]); emit_expr(c, argv[a], b); buf_puts(b, "; "); TyKind at = infer_type(c, argv[a]); /* A nil/void/unresolved arg has no concrete C storage (emit_ctype would print `void`); hold it as a boxed poly so it can flow into a poly param slot. */ if (at == TY_NIL || at == TY_VOID || at == TY_UNKNOWN) { atmp_ty[a] = TY_POLY; buf_printf(b, "sp_RbVal _t%d = ", atmp[a]); emit_boxed(c, argv[a], b); buf_puts(b, "; "); } else { atmp_ty[a] = at; emit_ctype(c, at, b); buf_printf(b, " _t%d = ", atmp[a]); emit_expr(c, argv[a], b); buf_puts(b, "; "); } } emit_ctype(c, is_scalar_ret(ret) ? ret : TY_INT, b); buf_printf(b, " _t%d = %s; ", tr, is_scalar_ret(ret) ? default_value(ret) : "0"); if (pv) pt = pv->type; buf_puts(&cb, ", "); if (a < argc) { TyKind at = infer_type(c, argv[a]); TyKind at = atmp_ty[a]; /* the temp's actual type (poly for a nil/void arg) */ char tn[32]; snprintf(tn, sizeof tn, "_t%d", atmp[a]); if (pt == TY_POLY && at != TY_POLY) emit_boxed_text(c, at, tn, &cb); else buf_puts(&cb, tn); } buf_printf(b, " } _t%d; })", tr); free(atmp); free(atmp_ty); return; } }src/codegen_fold.cmodifiedView file
Loading diff…
test/dispatch_unused_param_nil.rbaddedView file
Loading diff…
test/dispatch_unused_param_nil.rb.expectedaddedView file
Loading diff…