Commit 2822b6a9
2822b6a9d830Merged
Ryan Seys <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): bind missing instance_exec block params to typed nil
The direct instance_exec splice bound a positional or numbered block param
that has no matching call-site argument with the literal 0. For a poly
(sp_RbVal) slot that is invalid C under -Werror and semantically wrong, and
it is wrong for any non-int slot type.
Bind the surplus param through emit_ie_param_default, which returns a valid
assignment rvalue for the slot type: default_value() for scalars, pointers,
and the compound-literal value types (Range/Time/Complex/Rational), and a zero
compound literal (sp_Name){0} for a value-type object, whose C type is a
struct and for which default_value would yield the unassignable NULL. A poly
slot becomes sp_box_nil(), matching CRuby's lenient block arity where a param
with no argument is nil.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 2822b6a9d830
- Parent
- 34f8cd5461a2
src/codegen_call.cmodifiedView file
return 0;}/* Emit a valid assignment rvalue for an instance_exec block param the caller omitted, given its slot type. default_value() is rvalue-safe for scalars, pointers, and the compound-literal value types (Range/Time/Complex/Rational), but returns "NULL" for an object type -- correct for a heap object, invalid for a value-type object whose C type is a struct (`lv = NULL` won't compile). Emit a zero compound literal `(sp_Name){0}` in that case. */static void emit_ie_param_default(Compiler *c, TyKind t, Buf *b) { if (ty_is_object(t)) { int cid = ty_object_class(t); if (cid >= 0 && cid < c->nclasses && c->classes[cid].is_value_type) { buf_printf(b, "(sp_%s){0}", c->classes[cid].name); return; } } buf_puts(b, default_value(t));}void emit_call(Compiler *c, int id, Buf *b) { const NodeTable *nt = c->nt; if (emit_partition_expr(c, id, b)) return; char pn[8]; snprintf(pn, sizeof pn, "_%d", p + 1); emit_indent(g_pre, g_indent); buf_printf(g_pre, "lv_%s = ", rename_local(pn)); if (is_exec) { if (p < iac) emit_expr(c, iav[p], g_pre); else buf_puts(g_pre, "0"); } if (is_exec) { if (p < iac) emit_expr(c, iav[p], g_pre); else { LocalVar *plv = scope_local(comp_scope_of(c, id), pn); emit_ie_param_default(c, plv ? plv->type : TY_POLY, g_pre); } } else buf_printf(g_pre, "_t%d", tr); buf_puts(g_pre, ";\n"); } if (an >= 0) emit_expr(c, an, &eb); else buf_puts(&eb, "0"); buf_puts(g_pre, eb.p ? eb.p : "0"); free(eb.p); } else if (is_exec) { if (p < iac) emit_expr(c, iav[p], g_pre); else buf_puts(g_pre, "0"); } else if (is_exec) { if (p < iac) emit_expr(c, iav[p], g_pre); else { LocalVar *plv = scope_local(comp_scope_of(c, id), pn); emit_ie_param_default(c, plv ? plv->type : TY_POLY, g_pre); } } else buf_printf(g_pre, "_t%d", tr); /* instance_eval yields self */ buf_puts(g_pre, ";\n"); }test/instance_exec_lenient_arity.rbaddedView file
Loading diff…
test/instance_exec_lenient_arity.rb.expectedaddedView file
Loading diff…