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

@@ -149,6 +149,23 @@
149149 return 0;
150150 }
151151
152+/* Emit a valid assignment rvalue for an instance_exec block param the caller
153+ omitted, given its slot type. default_value() is rvalue-safe for scalars,
154+ pointers, and the compound-literal value types (Range/Time/Complex/Rational),
155+ but returns "NULL" for an object type -- correct for a heap object, invalid
156+ for a value-type object whose C type is a struct (`lv = NULL` won't compile).
157+ Emit a zero compound literal `(sp_Name){0}` in that case. */
158+static void emit_ie_param_default(Compiler *c, TyKind t, Buf *b) {
159+ if (ty_is_object(t)) {
160+ int cid = ty_object_class(t);
161+ if (cid >= 0 && cid < c->nclasses && c->classes[cid].is_value_type) {
162+ buf_printf(b, "(sp_%s){0}", c->classes[cid].name);
163+ return;
164+ }
165+ }
166+ buf_puts(b, default_value(t));
167+}
168+
152169 void emit_call(Compiler *c, int id, Buf *b) {
153170 const NodeTable *nt = c->nt;
154171 if (emit_partition_expr(c, id, b)) return;
@@ -1811,7 +1828,10 @@
18111828 char pn[8]; snprintf(pn, sizeof pn, "_%d", p + 1);
18121829 emit_indent(g_pre, g_indent);
18131830 buf_printf(g_pre, "lv_%s = ", rename_local(pn));
1814- if (is_exec) { if (p < iac) emit_expr(c, iav[p], g_pre); else buf_puts(g_pre, "0"); }
1831+ if (is_exec) {
1832+ if (p < iac) emit_expr(c, iav[p], g_pre);
1833+ else { LocalVar *plv = scope_local(comp_scope_of(c, id), pn); emit_ie_param_default(c, plv ? plv->type : TY_POLY, g_pre); }
1834+ }
18151835 else buf_printf(g_pre, "_t%d", tr);
18161836 buf_puts(g_pre, ";\n");
18171837 }
@@ -1849,7 +1869,10 @@
18491869 if (an >= 0) emit_expr(c, an, &eb); else buf_puts(&eb, "0");
18501870 buf_puts(g_pre, eb.p ? eb.p : "0"); free(eb.p);
18511871 }
1852- else if (is_exec) { if (p < iac) emit_expr(c, iav[p], g_pre); else buf_puts(g_pre, "0"); }
1872+ else if (is_exec) {
1873+ if (p < iac) emit_expr(c, iav[p], g_pre);
1874+ else { LocalVar *plv = scope_local(comp_scope_of(c, id), pn); emit_ie_param_default(c, plv ? plv->type : TY_POLY, g_pre); }
1875+ }
18531876 else buf_printf(g_pre, "_t%d", tr); /* instance_eval yields self */
18541877 buf_puts(g_pre, ";\n");
18551878 }