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
int recv = nt_ref(nt, id, "receiver"); size_t ln = nm ? strlen(nm) : 0; if (!nm || recv < 0 || ln < 2 || nm[ln - 1] != '=') continue; TyKind rt = infer_type(c, recv); if (!ty_is_object(rt)) continue; ClassInfo *ci = &c->classes[ty_object_class(rt)]; /* not a comparison that happens to end in '=' (==, !=, <=, >=) */ if (nm[ln - 2] == '=' || nm[ln - 2] == '!' || nm[ln - 2] == '<' || nm[ln - 2] == '>') continue; char base[256]; if (ln - 1 >= sizeof base) continue; memcpy(base, nm, ln - 1); base[ln - 1] = '\0'; if (!comp_is_writer(ci, base)) continue; int args = nt_ref(nt, id, "arguments"); int an = 0; const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL; if (an < 1) continue; TyKind vt = infer_type(c, argv[0]); if (vt == TY_NIL) continue; /* a nil write doesn't pin the ivar type */ char ivname[256]; snprintf(ivname, sizeof ivname, "@%s", base); int iv = comp_ivar_index(ci, ivname); if (iv < 0 || class_ivar_pinned(ci, ivname)) continue; TyKind vt = infer_type(c, argv[0]); TyKind merged = ty_unify(ci->ivar_types[iv], vt); if (merged != ci->ivar_types[iv]) { ci->ivar_types[iv] = merged; changed = 1; } TyKind rt = infer_type(c, recv); if (ty_is_object(rt)) { /* concrete receiver: attribute to its class. */ ClassInfo *ci = &c->classes[ty_object_class(rt)]; if (!comp_is_writer(ci, base)) continue; int iv = comp_ivar_index(ci, ivname); if (iv < 0 || class_ivar_pinned(ci, ivname)) continue; TyKind merged = ty_unify(ci->ivar_types[iv], vt); if (merged != ci->ivar_types[iv]) { ci->ivar_types[iv] = merged; changed = 1; } } else { /* Poly/unknown receiver -- e.g. `cell` read from a poly array/hash (`@cells.each_value { |cell| cell.neighbours = ... }`). The static class is unknown, but if exactly ONE class defines this attr-writer with a matching ivar, the runtime object must be of that class, so attribute the write to it. (Skip when ambiguous: zero or several classes share the attr name -- over-widening an unrelated same-named ivar would be unsound to attribute.) ty_unify only widens. */ int only = -1; for (int ci2 = 0; ci2 < c->nclasses; ci2++) { if (comp_is_writer(&c->classes[ci2], base) && comp_ivar_index(&c->classes[ci2], ivname) >= 0) { if (only >= 0) { only = -2; break; } /* ambiguous */ only = ci2; } } if (only < 0) continue; ClassInfo *ci = &c->classes[only]; int iv = comp_ivar_index(ci, ivname); if (iv < 0 || class_ivar_pinned(ci, ivname)) continue; TyKind merged = ty_unify(ci->ivar_types[iv], vt); if (merged != ci->ivar_types[iv]) { ci->ivar_types[iv] = merged; changed = 1; } } } } return changed;test/ivar_widen_poly_writer.rbaddedView file
Loading diff…
test/ivar_widen_poly_writer.rb.expectedaddedView file
Loading diff…