Commit 99e8dee6
99e8dee66559Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Fix void result temp for a nil RHS in poly attr-write
recv.attr = nil on a polymorphic receiver emitted a cls_id dispatch
that cached the RHS in a temp declared with the RHS's type. For a nil
literal that type is void, so the generated C was void _tN = 0; which
does not compile (bm_splay: @root.right = nil).
Treat a nil RHS as a boxed-nil sp_RbVal: cache it as sp_RbVal _tN = sp_box_nil() and drive the per-class slot assignment with an effective
type of TY_POLY (raw store into poly slots, unbox into scalar slots).
bm_splay now compiles; make test stays green (845 pass).
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 99e8dee66559
- Parent
- 2b53087cb7f0
src/codegen_stmt.cmodifiedView file
@@ -1858,24 +1858,32 @@
18581858
if (comp_is_writer(&c->classes[k], base)) ncand++;18591859
if (an >= 1 && ncand > 0) {18601860
TyKind at = comp_ntype(c, argv[0]);1861+
/* A nil literal RHS has void type; cache it as a boxed-nil1862+
sp_RbVal rather than declaring an (illegal) `void` temp. */1863+
int nil_rhs = (at == TY_NIL || at == TY_VOID);1864+
TyKind at_eff = nil_rhs ? TY_POLY : at;18611865
int tv = ++g_tmp, tval = ++g_tmp;18621866
emit_indent(b, indent);18631867
buf_printf(b, "{ sp_RbVal _t%d = ", tv); emit_expr(c, recv, b); buf_puts(b, "; ");1864-
emit_ctype(c, at, b); buf_printf(b, " _t%d = ", tval); emit_expr(c, argv[0], b); buf_puts(b, ";");1868+
if (nil_rhs) {1869+
buf_printf(b, "sp_RbVal _t%d = sp_box_nil();", tval);1870+
} else {1871+
emit_ctype(c, at, b); buf_printf(b, " _t%d = ", tval); emit_expr(c, argv[0], b); buf_puts(b, ";");1872+
}18651873
buf_printf(b, " switch (_t%d.cls_id) {", tv);18661874
char src[32]; snprintf(src, sizeof src, "_t%d", tval);18671875
for (int k = 0; k < c->nclasses; k++) {18681876
if (!comp_is_writer(&c->classes[k], base)) continue;18691877
char ivn[256]; snprintf(ivn, sizeof ivn, "@%s", base);18701878
int iv = comp_ivar_index(&c->classes[k], ivn);1871-
TyKind ivt = iv >= 0 ? c->classes[k].ivar_types[iv] : at;1879+
TyKind ivt = iv >= 0 ? c->classes[k].ivar_types[iv] : at_eff;18721880
/* skip a class whose slot can't hold this concrete rhs (the18731881
runtime object isn't that class anyway): a raw assignment18741882
between mismatched C types would not compile */1875-
if (at != ivt && at != TY_POLY && ivt != TY_POLY) continue;1883+
if (at_eff != ivt && at_eff != TY_POLY && ivt != TY_POLY) continue;18761884
buf_printf(b, " case %d: ((sp_%s *)_t%d.v.p)->iv_%s = ", k, c->classes[k].name, tv, base);1877-
if (ivt == TY_POLY && at != TY_POLY) emit_boxed_text(c, at, src, b);1878-
else if (at == TY_POLY && ivt != TY_POLY) emit_unbox_text(c, ivt, src, b);1885+
if (ivt == TY_POLY && at_eff != TY_POLY) emit_boxed_text(c, at_eff, src, b);1886+
else if (at_eff == TY_POLY && ivt != TY_POLY) emit_unbox_text(c, ivt, src, b);18791887
else buf_puts(b, src);18801888
buf_puts(b, "; break;");18811889
}