Commit 70763b04

70763b040501Merged

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

Description

Seed string-keyed hash value type from []= writes; widen Hash#delete

Two self-host correctness fixes:

  • A string-key hash READ (@h[s] / @h.fetch(s)) promoted an untyped ({}) ivar/local to str_poly_hash, assuming an unknown (poly) value. When the hash is actually filled by @h[s] = int writes it should be str_int_hash, but the read's str_poly contribution monotonically widened it and never narrowed. Seed the promoted value type from the hash's own []= writes (aset_value_type) so it stays str_int / str_str when those writes are concrete.

  • Hash#delete now also handles str_poly_hash (sp_StrPolyHash_delete exists in the runtime; the dispatch just omitted that variant).

Surfaced compiling the legacy compiler: @mname_id ({} filled by @mname_id[s]=int, read via .fetch) was str_poly_hash, so mname_to_id returned poly. Now str_int_hash -> mname_to_id returns int. Self-host gaps 6 -> 5 (delete resolved).

Gates: 845 test, 57 bench, optcarrot checksum 59662 -- all green.

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

Details

Commit
70763b040501
Parent
f8ad2fae3a36

src/analyze_pass.cmodifiedView file

@@ -37,6 +37,49 @@
3737 return 0;
3838 }
3939
40+/* Unified value type of `recv[k] = v` writes that target the same ivar/local
41+ as `recv`. Lets a hash promoted via a string-key READ inherit the value type
42+ its `[]=` writes establish (e.g. `@h[s] = int` -> str_int_hash) instead of
43+ defaulting to a str_poly slot that can never narrow. TY_UNKNOWN if there is
44+ no such write (caller falls back to poly). */
45+static TyKind aset_value_type(Compiler *c, int recv) {
46+ const NodeTable *nt = c->nt;
47+ const char *rty = nt_type(nt, recv);
48+ if (!rty) return TY_UNKNOWN;
49+ int is_ivar = !strcmp(rty, "InstanceVariableReadNode");
50+ int is_local = !strcmp(rty, "LocalVariableReadNode");
51+ if (!is_ivar && !is_local) return TY_UNKNOWN;
52+ const char *rnm = nt_str(nt, recv, "name");
53+ if (!rnm) return TY_UNKNOWN;
54+ Scope *rsc = comp_scope_of(c, recv);
55+ int rcls = rsc ? rsc->class_id : -1;
56+ TyKind acc = TY_UNKNOWN;
57+ for (int id = 0; id < nt->count; id++) {
58+ if (nt_kind(nt, id) != NK_CallNode) continue;
59+ const char *nm = nt_str(nt, id, "name");
60+ if (!nm || strcmp(nm, "[]=")) continue;
61+ int wrecv = nt_ref(nt, id, "receiver");
62+ if (wrecv < 0) continue;
63+ const char *wn = nt_str(nt, wrecv, "name");
64+ if (!wn || strcmp(wn, rnm)) continue;
65+ if (is_ivar) {
66+ if (nt_kind(nt, wrecv) != NK_InstanceVariableReadNode) continue;
67+ Scope *ws = comp_scope_of(c, wrecv);
68+ if ((ws ? ws->class_id : -1) != rcls) continue;
69+ }
70+ else {
71+ if (nt_kind(nt, wrecv) != NK_LocalVariableReadNode) continue;
72+ if (comp_scope_of(c, wrecv) != rsc) continue;
73+ }
74+ int args = nt_ref(nt, id, "arguments");
75+ int an = 0;
76+ const int *av = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
77+ if (an < 2) continue;
78+ acc = ty_unify(acc, infer_type(c, av[1]));
79+ }
80+ return acc;
81+}
82+
4083 int infer_write_types(Compiler *c) {
4184 const NodeTable *nt = c->nt;
4285 int changed = 0;
@@ -686,7 +729,13 @@
686729 }
687730 kt = infer_type(c, argv[0]);
688731 if (kt == TY_SYMBOL) { vt = TY_INT; /* dummy: sym hash val is always poly */ }
689- else if (kt == TY_STRING) { vt = TY_POLY; /* will map to STR_POLY */ }
732+ else if (kt == TY_STRING) {
733+ /* Seed the value type from the hash's `[]=` writes so an int-valued
734+ string-keyed hash filled by `@h[s] = int` stays str_int_hash
735+ instead of widening to str_poly (which never narrows back). */
736+ TyKind wv = aset_value_type(c, recv);
737+ vt = (wv == TY_INT || wv == TY_STRING) ? wv : TY_POLY;
738+ }
690739 /* An int-key bare read (`x[i]`) is NOT strong hash evidence: arrays
691740 index by int too, and an array-returning method assigned to `x` may
692741 not have settled its element type yet, so promoting here would lock