Commit d7ac9a9f
d7ac9a9f0fffMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #1405 from ryanseys/rs-iexec-lenient-arity-nil
fix(codegen): bind missing instance_exec block params to typed nil
Details
- Commit
- d7ac9a9f0fff
- Parent
- d97111bb178c
- Merge parent
- 2822b6a9d830
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 caller153+
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, invalid156+
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
/* `require` / `require_relative` is a compile-time directive: top-level ones@@ -1828,7 +1845,10 @@
18281845
char pn[8]; snprintf(pn, sizeof pn, "_%d", p + 1);18291846
emit_indent(g_pre, g_indent);18301847
buf_printf(g_pre, "lv_%s = ", rename_local(pn));1831-
if (is_exec) { if (p < iac) emit_expr(c, iav[p], g_pre); else buf_puts(g_pre, "0"); }1848+
if (is_exec) {1849+
if (p < iac) emit_expr(c, iav[p], g_pre);1850+
else { LocalVar *plv = scope_local(comp_scope_of(c, id), pn); emit_ie_param_default(c, plv ? plv->type : TY_POLY, g_pre); }1851+
}18321852
else buf_printf(g_pre, "_t%d", tr);18331853
buf_puts(g_pre, ";\n");18341854
}@@ -1866,7 +1886,10 @@
18661886
if (an >= 0) emit_expr(c, an, &eb); else buf_puts(&eb, "0");18671887
buf_puts(g_pre, eb.p ? eb.p : "0"); free(eb.p);18681888
}1869-
else if (is_exec) { if (p < iac) emit_expr(c, iav[p], g_pre); else buf_puts(g_pre, "0"); }1889+
else if (is_exec) {1890+
if (p < iac) emit_expr(c, iav[p], g_pre);1891+
else { LocalVar *plv = scope_local(comp_scope_of(c, id), pn); emit_ie_param_default(c, plv ? plv->type : TY_POLY, g_pre); }1892+
}18701893
else buf_printf(g_pre, "_t%d", tr); /* instance_eval yields self */18711894
buf_puts(g_pre, ";\n");18721895
}test/instance_exec_lenient_arity.rbaddedView file
Loading diff…
test/instance_exec_lenient_arity.rb.expectedaddedView file
Loading diff…