Commit e33bd3d8

e33bd3d83e1dMerged

Ryan Seys <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

codegen: initialize an unwritten int ivar to nil

An instance variable that is never written, or whose first write is conditional (@x ||= 5), must read as nil. The compiler already models an int ivar's unset value as SP_INT_NIL — truthiness, ||=/&&=, and .nil? all test the slot against SP_INT_NIL — but the constructors left the slot at its zero bit-pattern ({0}/memset), so an unwritten int ivar read back as a real 0. @x ||= 5 then saw 0 (truthy) and kept it, yielding 0 where CRuby yields 5.

Seed int ivar slots to SP_INT_NIL in every constructor path that previously nil-initialized only poly ivars — the value-type new, the heap and exception-subclass new, and both Class#allocate forms. The five paths duplicated the same per-ivar init loop (and the value-type new path had drifted: it nil-initialized poly ivars nowhere), so factor the loop into a shared emit_ivar_nil_inits helper that handles both a poly ivar (sp_box_nil()) and a scalar nil sentinel (SP_INT_NIL). The value-type new path now nil-initializes poly ivars too, matching the other four. An unconditionally initialized ivar (@k = 0 in initialize) is overwritten as before, so it is unaffected.

Float ivars have the same shape but their nil sentinel interacts with ||=/NaN handling that needs separate work, so they keep the current behavior here.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
e33bd3d83e1d
Parent
3d4efd9a07a7

src/codegen.cmodifiedView file

@@ -1485,6 +1485,36 @@
14851485 buf_puts(b, "}\n");
14861486 }
14871487
1488+/* An int ivar's nil default differs from its zero bit-pattern: its nil is
1489+ SP_INT_NIL, not 0. The compiler already reads an unwritten int ivar as that
1490+ sentinel (truthiness, `@x ||= v`, `.nil?`), so the constructor must seed it
1491+ explicitly -- a memset/{0} slot reads back as a real 0 and makes `@x ||= 5`
1492+ keep 0. Returns NULL for types whose zero-init already reads as nil
1493+ (string->NULL) or which are nil-initialized separately (poly). */
1494+static const char *ivar_scalar_nil_init(TyKind t) {
1495+ if (t == TY_INT) return "SP_INT_NIL";
1496+ return NULL;
1497+}
1498+
1499+/* Seed every ivar whose zero bit-pattern is not nil. A poly ivar's zero
1500+ pattern has tag 0, not SP_TAG_NIL, so it must be set to sp_box_nil(); an int
1501+ ivar's nil is SP_INT_NIL, not 0. `lv` is the receiver-and-accessor prefix
1502+ ("self.", "self->", "_t3.", "_t3->"); each assignment is bracketed by `lead`
1503+ (indentation) and `term` (`;\n` for a statement, `;` inside a compound expr).
1504+ A string ivar's NULL zero-pattern already reads as nil, so it is skipped. */
1505+static void emit_ivar_nil_inits(Buf *b, ClassInfo *ci, const char *lv,
1506+ const char *lead, const char *term) {
1507+ for (int i = 0; i < ci->nivars; i++) {
1508+ const char *name = ci->ivars[i] + 1; /* skip leading '@' */
1509+ if (ci->ivar_types[i] == TY_POLY)
1510+ buf_printf(b, "%s%siv_%s = sp_box_nil()%s", lead, lv, name, term);
1511+ else {
1512+ const char *nv = ivar_scalar_nil_init(ci->ivar_types[i]);
1513+ if (nv) buf_printf(b, "%s%siv_%s = %s%s", lead, lv, name, nv, term);
1514+ }
1515+ }
1516+}
1517+
14881518 void emit_class_new(Compiler *c, ClassInfo *ci, Buf *b) {
14891519 int cid = comp_class_index(c, ci->name);
14901520 if (ci->is_struct) {
@@ -1526,6 +1556,7 @@
15261556 }
15271557 else buf_puts(b, "void");
15281558 buf_printf(b, ") {\n sp_%s self = {0};\n self.cls_id = %d;\n", ci->name, cid);
1559+ emit_ivar_nil_inits(b, ci, "self.", " ", ";\n");
15291560 if (init >= 0 && c->scopes[init].reachable && !c->scopes[init].yields) {
15301561 buf_printf(b, " sp_%s_initialize(&self", c->classes[initcls].name);
15311562 Scope *s = &c->scopes[init];
@@ -1576,9 +1607,7 @@
15761607 buf_printf(b, " self->parent_cls_name = \"%s\";\n", par);
15771608 buf_puts(b, " self->msg = (&(\"\\xff\")[1]);\n");
15781609 buf_printf(b, " SP_GC_ROOT(self);\n");
1579- for (int i = 0; i < ci->nivars; i++)
1580- if (ci->ivar_types[i] == TY_POLY)
1581- buf_printf(b, " self->iv_%s = sp_box_nil();\n", ci->ivars[i] + 1);
1610+ emit_ivar_nil_inits(b, ci, "self->", " ", ";\n");
15821611 }
15831612 }
15841613 else {
@@ -1589,12 +1618,10 @@
15891618 buf_puts(b, " memset(self, 0, sizeof(*self));\n"); /* recycled slots are not zeroed */
15901619 buf_printf(b, " SP_GC_ROOT(self);\n");
15911620 buf_printf(b, " self->cls_id = %d;\n", cid);
1592- /* calloc zero-inits fields; a poly (boxed) ivar's zero pattern is not nil,
1593- so set poly ivars to boxed-nil before initialize runs (read-only ivars
1594- stay nil; written ones are overwritten). */
1595- for (int i = 0; i < ci->nivars; i++)
1596- if (ci->ivar_types[i] == TY_POLY)
1597- buf_printf(b, " self->iv_%s = sp_box_nil();\n", ci->ivars[i] + 1);
1621+ /* memset zero-inits fields, but a poly ivar's zero pattern is not nil and an
1622+ int ivar's nil is SP_INT_NIL, so seed them before initialize runs
1623+ (read-only ivars stay nil; written ones are overwritten). */
1624+ emit_ivar_nil_inits(b, ci, "self->", " ", ";\n");
15981625 } /* close else (non-exception subclass allocation) */
15991626 if (init >= 0 && c->scopes[init].reachable && !c->scopes[init].yields) {
16001627 buf_printf(b, " sp_%s_initialize(", c->classes[initcls].name);
@@ -1618,9 +1645,8 @@
16181645 int t = ++g_tmp;
16191646 if (is_val) {
16201647 buf_printf(b, "({ sp_%s _t%d = {0}; _t%d.cls_id = %d;", ci->name, t, t, cid);
1621- for (int i = 0; i < ci->nivars; i++)
1622- if (ci->ivar_types[i] == TY_POLY)
1623- buf_printf(b, " _t%d.iv_%s = sp_box_nil();", t, ci->ivars[i] + 1);
1648+ char lv[32]; snprintf(lv, sizeof lv, "_t%d.", t);
1649+ emit_ivar_nil_inits(b, ci, lv, " ", ";");
16241650 buf_printf(b, " _t%d; })", t);
16251651 }
16261652 else {
@@ -1633,9 +1659,8 @@
16331659 ci->name, t, ci->name,
16341660 class_needs_scan(ci) ? "sp_" : "", class_needs_scan(ci) ? ci->name : "NULL",
16351661 class_needs_scan(ci) ? "_scan" : "", t, t, t, cid);
1636- for (int i = 0; i < ci->nivars; i++)
1637- if (ci->ivar_types[i] == TY_POLY)
1638- buf_printf(b, " _t%d->iv_%s = sp_box_nil();", t, ci->ivars[i] + 1);
1662+ char lv[32]; snprintf(lv, sizeof lv, "_t%d->", t);
1663+ emit_ivar_nil_inits(b, ci, lv, " ", ";");
16391664 buf_printf(b, " _t%d; })", t);
16401665 }
16411666 }