Commit 6749983e

6749983efd8aMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Merge PR #1519: read an unwritten int instance variable as nil

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

Int ivar slots are now seeded to SP_INT_NIL in every constructor path that previously nil-initialized only poly ivars (value-type new, heap and exception-subclass new, both Class#allocate forms), via a shared emit_ivar_nil_inits / ivar_scalar_nil_init helper. An unconditionally initialized ivar is overwritten as before. Float ivars keep current behavior (their nil sentinel interacts with ||=/NaN handling needing separate work). Adds test/ivar_conditional_nil.rb.

Combined-state gates: test 1004/0/0, bench 57/57, optcarrot checksum 59662.

Closes #1519

Co-authored-by: Ryan Seys [email protected] Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
6749983efd8a
Parent
87579127d3d6
Merge parent
e33bd3d83e1d

src/codegen.cmodifiedView file

@@ -1497,6 +1497,36 @@
14971497 buf_puts(b, "}\n");
14981498 }
14991499
1500+/* An int ivar's nil default differs from its zero bit-pattern: its nil is
1501+ SP_INT_NIL, not 0. The compiler already reads an unwritten int ivar as that
1502+ sentinel (truthiness, `@x ||= v`, `.nil?`), so the constructor must seed it
1503+ explicitly -- a memset/{0} slot reads back as a real 0 and makes `@x ||= 5`
1504+ keep 0. Returns NULL for types whose zero-init already reads as nil
1505+ (string->NULL) or which are nil-initialized separately (poly). */
1506+static const char *ivar_scalar_nil_init(TyKind t) {
1507+ if (t == TY_INT) return "SP_INT_NIL";
1508+ return NULL;
1509+}
1510+
1511+/* Seed every ivar whose zero bit-pattern is not nil. A poly ivar's zero
1512+ pattern has tag 0, not SP_TAG_NIL, so it must be set to sp_box_nil(); an int
1513+ ivar's nil is SP_INT_NIL, not 0. `lv` is the receiver-and-accessor prefix
1514+ ("self.", "self->", "_t3.", "_t3->"); each assignment is bracketed by `lead`
1515+ (indentation) and `term` (`;\n` for a statement, `;` inside a compound expr).
1516+ A string ivar's NULL zero-pattern already reads as nil, so it is skipped. */
1517+static void emit_ivar_nil_inits(Buf *b, ClassInfo *ci, const char *lv,
1518+ const char *lead, const char *term) {
1519+ for (int i = 0; i < ci->nivars; i++) {
1520+ const char *name = ci->ivars[i] + 1; /* skip leading '@' */
1521+ if (ci->ivar_types[i] == TY_POLY)
1522+ buf_printf(b, "%s%siv_%s = sp_box_nil()%s", lead, lv, name, term);
1523+ else {
1524+ const char *nv = ivar_scalar_nil_init(ci->ivar_types[i]);
1525+ if (nv) buf_printf(b, "%s%siv_%s = %s%s", lead, lv, name, nv, term);
1526+ }
1527+ }
1528+}
1529+
15001530 void emit_class_new(Compiler *c, ClassInfo *ci, Buf *b) {
15011531 int cid = comp_class_index(c, ci->name);
15021532 if (ci->is_struct) {
@@ -1538,6 +1568,7 @@
15381568 }
15391569 else buf_puts(b, "void");
15401570 buf_printf(b, ") {\n sp_%s self = {0};\n self.cls_id = %d;\n", ci->name, cid);
1571+ emit_ivar_nil_inits(b, ci, "self.", " ", ";\n");
15411572 if (init >= 0 && c->scopes[init].reachable && !c->scopes[init].yields) {
15421573 buf_printf(b, " sp_%s_initialize(&self", c->classes[initcls].name);
15431574 Scope *s = &c->scopes[init];
@@ -1588,9 +1619,7 @@
15881619 buf_printf(b, " self->parent_cls_name = \"%s\";\n", par);
15891620 buf_puts(b, " self->msg = (&(\"\\xff\")[1]);\n");
15901621 buf_printf(b, " SP_GC_ROOT(self);\n");
1591- for (int i = 0; i < ci->nivars; i++)
1592- if (ci->ivar_types[i] == TY_POLY)
1593- buf_printf(b, " self->iv_%s = sp_box_nil();\n", ci->ivars[i] + 1);
1622+ emit_ivar_nil_inits(b, ci, "self->", " ", ";\n");
15941623 }
15951624 }
15961625 else {
@@ -1601,12 +1630,10 @@
16011630 buf_puts(b, " memset(self, 0, sizeof(*self));\n"); /* recycled slots are not zeroed */
16021631 buf_printf(b, " SP_GC_ROOT(self);\n");
16031632 buf_printf(b, " self->cls_id = %d;\n", cid);
1604- /* calloc zero-inits fields; a poly (boxed) ivar's zero pattern is not nil,
1605- so set poly ivars to boxed-nil before initialize runs (read-only ivars
1606- stay nil; written ones are overwritten). */
1607- for (int i = 0; i < ci->nivars; i++)
1608- if (ci->ivar_types[i] == TY_POLY)
1609- buf_printf(b, " self->iv_%s = sp_box_nil();\n", ci->ivars[i] + 1);
1633+ /* memset zero-inits fields, but a poly ivar's zero pattern is not nil and an
1634+ int ivar's nil is SP_INT_NIL, so seed them before initialize runs
1635+ (read-only ivars stay nil; written ones are overwritten). */
1636+ emit_ivar_nil_inits(b, ci, "self->", " ", ";\n");
16101637 } /* close else (non-exception subclass allocation) */
16111638 if (init >= 0 && c->scopes[init].reachable && !c->scopes[init].yields) {
16121639 buf_printf(b, " sp_%s_initialize(", c->classes[initcls].name);
@@ -1630,9 +1657,8 @@
16301657 int t = ++g_tmp;
16311658 if (is_val) {
16321659 buf_printf(b, "({ sp_%s _t%d = {0}; _t%d.cls_id = %d;", ci->name, t, t, cid);
1633- for (int i = 0; i < ci->nivars; i++)
1634- if (ci->ivar_types[i] == TY_POLY)
1635- buf_printf(b, " _t%d.iv_%s = sp_box_nil();", t, ci->ivars[i] + 1);
1660+ char lv[32]; snprintf(lv, sizeof lv, "_t%d.", t);
1661+ emit_ivar_nil_inits(b, ci, lv, " ", ";");
16361662 buf_printf(b, " _t%d; })", t);
16371663 }
16381664 else {
@@ -1645,9 +1671,8 @@
16451671 ci->name, t, ci->name,
16461672 class_needs_scan(ci) ? "sp_" : "", class_needs_scan(ci) ? ci->name : "NULL",
16471673 class_needs_scan(ci) ? "_scan" : "", t, t, t, cid);
1648- for (int i = 0; i < ci->nivars; i++)
1649- if (ci->ivar_types[i] == TY_POLY)
1650- buf_printf(b, " _t%d->iv_%s = sp_box_nil();", t, ci->ivars[i] + 1);
1674+ char lv[32]; snprintf(lv, sizeof lv, "_t%d->", t);
1675+ emit_ivar_nil_inits(b, ci, lv, " ", ";");
16511676 buf_printf(b, " _t%d; })", t);
16521677 }
16531678 }