Commit 6a4941cd

6a4941cd5d8bMerged

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

Description

analyze: widen an ivar from an attr-writer call on a poly receiver (#1508)

infer_ivar_types already attributes obj.attr = v to obj's class when the receiver has a concrete object type, but bailed when the receiver is poly -- e.g. @cells.each_value { |cell| cell.neighbours = ... }, where cell is read from a poly hash. So an ivar seeded empty in initialize (@neighbours = [] -> int-array) was never widened by the external writer that gives it real elements, and a later neighbours.count(&:alive) resolved alive against Integer (the Conway's Game of Life failure once its two codegen bugs were fixed).

When the receiver is poly/unknown, attribute the write to the class that defines this attr-writer if exactly one does (the runtime object must be that class); skip when zero or several classes share the attr name, so an unrelated same-named ivar is never widened on a guess. ty_unify only widens, so this is monotonic. The full Game of Life now compiles and runs.

Test 985 (gcc+clang), optcarrot 59662, bench 57 -- the widening doesn't cascade-regress the suite.

Closes #1508.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
6a4941cd5d8b
Parent
d5d961783af1

src/analyze_scope.cmodifiedView file

@@ -2285,24 +2285,52 @@
22852285 int recv = nt_ref(nt, id, "receiver");
22862286 size_t ln = nm ? strlen(nm) : 0;
22872287 if (!nm || recv < 0 || ln < 2 || nm[ln - 1] != '=') continue;
2288- TyKind rt = infer_type(c, recv);
2289- if (!ty_is_object(rt)) continue;
2290- ClassInfo *ci = &c->classes[ty_object_class(rt)];
2288+ /* not a comparison that happens to end in '=' (==, !=, <=, >=) */
2289+ if (nm[ln - 2] == '=' || nm[ln - 2] == '!' || nm[ln - 2] == '<' || nm[ln - 2] == '>') continue;
22912290 char base[256];
22922291 if (ln - 1 >= sizeof base) continue;
22932292 memcpy(base, nm, ln - 1); base[ln - 1] = '\0';
2294- if (!comp_is_writer(ci, base)) continue;
22952293 int args = nt_ref(nt, id, "arguments");
22962294 int an = 0;
22972295 const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
22982296 if (an < 1) continue;
2297+ TyKind vt = infer_type(c, argv[0]);
2298+ if (vt == TY_NIL) continue; /* a nil write doesn't pin the ivar type */
22992299 char ivname[256];
23002300 snprintf(ivname, sizeof ivname, "@%s", base);
2301- int iv = comp_ivar_index(ci, ivname);
2302- if (iv < 0 || class_ivar_pinned(ci, ivname)) continue;
2303- TyKind vt = infer_type(c, argv[0]);
2304- TyKind merged = ty_unify(ci->ivar_types[iv], vt);
2305- if (merged != ci->ivar_types[iv]) { ci->ivar_types[iv] = merged; changed = 1; }
2301+ TyKind rt = infer_type(c, recv);
2302+ if (ty_is_object(rt)) {
2303+ /* concrete receiver: attribute to its class. */
2304+ ClassInfo *ci = &c->classes[ty_object_class(rt)];
2305+ if (!comp_is_writer(ci, base)) continue;
2306+ int iv = comp_ivar_index(ci, ivname);
2307+ if (iv < 0 || class_ivar_pinned(ci, ivname)) continue;
2308+ TyKind merged = ty_unify(ci->ivar_types[iv], vt);
2309+ if (merged != ci->ivar_types[iv]) { ci->ivar_types[iv] = merged; changed = 1; }
2310+ }
2311+ else {
2312+ /* Poly/unknown receiver -- e.g. `cell` read from a poly array/hash
2313+ (`@cells.each_value { |cell| cell.neighbours = ... }`). The static
2314+ class is unknown, but if exactly ONE class defines this attr-writer
2315+ with a matching ivar, the runtime object must be of that class, so
2316+ attribute the write to it. (Skip when ambiguous: zero or several
2317+ classes share the attr name -- over-widening an unrelated same-named
2318+ ivar would be unsound to attribute.) ty_unify only widens. */
2319+ int only = -1;
2320+ for (int ci2 = 0; ci2 < c->nclasses; ci2++) {
2321+ if (comp_is_writer(&c->classes[ci2], base) &&
2322+ comp_ivar_index(&c->classes[ci2], ivname) >= 0) {
2323+ if (only >= 0) { only = -2; break; } /* ambiguous */
2324+ only = ci2;
2325+ }
2326+ }
2327+ if (only < 0) continue;
2328+ ClassInfo *ci = &c->classes[only];
2329+ int iv = comp_ivar_index(ci, ivname);
2330+ if (iv < 0 || class_ivar_pinned(ci, ivname)) continue;
2331+ TyKind merged = ty_unify(ci->ivar_types[iv], vt);
2332+ if (merged != ci->ivar_types[iv]) { ci->ivar_types[iv] = merged; changed = 1; }
2333+ }
23062334 }
23072335 }
23082336 return changed;