Commit b3750af6

b3750af68fbdMerged

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

Description

Re-pick empty-hash ivar variant from converged key/value types

A regex match-capture used as the key of an instance-variable hash inside a loop (@h[m[1]] = ...) was typed int_str_hash, so the String key was stored unboxed: Hash#keys came back as raw pointers and a lookup by a String literal returned 0/nil. The afm and i18n-country-translations gems hit this with their "parse lines -> build an ivar lookup table" pattern.

Root cause is a fixpoint-ordering lock. The one-shot empty-hash promotion in scan_writer_calls fires during the inference fixpoint, before block-local keys converge: a capture m[1] whose receiver m is assigned from a block param (line.match(...)) still reads as the scan-time int default, so promote_empty_hash_for(int, string) picks int_str_hash, and the cur == str_int_hash guard then prevents any later re-promotion. The local-hash path escapes this because its type is re-derived post-convergence in walk_and_cache (where block params are declared live); the ivar slot, baked into the struct, does not.

Add refine_class_ivar_hash_types, a post-fixpoint pass that, for every empty-{}-initialized class instance-variable hash, re-collects the key/value types observed at its []= writes with each method's locals resolved to their converged types (via refine_locals_multi_pass_full, the same scope discipline walk_and_cache uses), then re-picks the hash variant. Gated on the empty-{} init so genuinely-typed hash literals are untouched, and placed before the post-fixpoint infer_all_returns so a def keys; @h.keys; end reader re-derives its return type against the corrected slot. Verified to change no compiler-source ivar (clean self-host bootstrap).

Fixes #1308

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

Details

Commit
b3750af68fbd
Parent
e4842fddb148

spinel_analyze.rbmodifiedView file

@@ -14183,6 +14183,120 @@
1418314183 end
1418414184 end
1418514185
14186+ # Does class `ci` initialize instance variable `iname` with an empty
14187+ # `{}` hash literal anywhere in its method bodies? Gates the
14188+ # empty-hash re-pick so genuinely-typed hash slots (`@h = {"a" => 1}`)
14189+ # are never re-typed from their declared variant.
14190+ def class_ivar_init_is_empty_hash(ci, iname)
14191+ bodies = @cls_meth_bodies[ci].split(";", -1)
14192+ mj = 0
14193+ while mj < bodies.length
14194+ bid = bodies[mj].to_i
14195+ if bid >= 0 && node_has_empty_hash_ivar_init(bid, iname) == 1
14196+ return 1
14197+ end
14198+ mj = mj + 1
14199+ end
14200+ 0
14201+ end
14202+
14203+ def node_has_empty_hash_ivar_init(nid, iname)
14204+ if nid < 0
14205+ return 0
14206+ end
14207+ if @nd_type[nid] == "InstanceVariableWriteNode" && @nd_name[nid] == iname
14208+ if is_empty_hash_literal(@nd_expression[nid]) == 1
14209+ return 1
14210+ end
14211+ end
14212+ cs = []
14213+ push_child_ids(nid, cs)
14214+ k = 0
14215+ while k < cs.length
14216+ if node_has_empty_hash_ivar_init(cs[k], iname) == 1
14217+ return 1
14218+ end
14219+ k = k + 1
14220+ end
14221+ 0
14222+ end
14223+
14224+ # Re-pick the hash variant of every empty-`{}`-initialized class
14225+ # instance-variable hash from the key/value types observed at its
14226+ # `@ivar[k] = v` writes. The one-shot promotion in scan_writer_calls
14227+ # fires during the fixpoint, before block-local keys converge (a
14228+ # match-capture `m[1]` still reads as the scan-time `int` default),
14229+ # and its `cur == str_int_hash` guard then locks the slot. Here, run
14230+ # post-fixpoint with each method's locals resolved to their converged
14231+ # types via refine_locals_multi_pass_full (the same scope discipline
14232+ # walk_and_cache / codegen use), so `m[1]` reads as `string` and the
14233+ # variant is picked correctly. Placed before the post-fixpoint
14234+ # infer_all_returns so a `def keys; @h.keys; end` reader re-derives
14235+ # its return type against the corrected slot.
14236+ def refine_class_ivar_hash_types
14237+ ci = 0
14238+ while ci < @cls_names.length
14239+ inames = @cls_ivar_names[ci].split(";", -1)
14240+ itypes = @cls_ivar_types[ci].split(";", -1)
14241+ eligible = "".split(",", -1)
14242+ key_sets = []
14243+ val_sets = []
14244+ ii = 0
14245+ while ii < inames.length
14246+ if inames[ii] != "" && ii < itypes.length && is_hash_type(itypes[ii]) == 1 && class_ivar_init_is_empty_hash(ci, inames[ii]) == 1
14247+ eligible.push(inames[ii])
14248+ key_sets.push("".split(",", -1))
14249+ val_sets.push("".split(",", -1))
14250+ end
14251+ ii = ii + 1
14252+ end
14253+ if eligible.length > 0
14254+ bodies = @cls_meth_bodies[ci].split(";", -1)
14255+ mj = 0
14256+ while mj < bodies.length
14257+ bid = bodies[mj].to_i
14258+ if bid >= 0
14259+ push_scope
14260+ pnames = cls_meth_pnames_get(ci, mj)
14261+ ptypes = cls_meth_ptypes_get(ci, mj)
14262+ pk = 0
14263+ while pk < pnames.length
14264+ pt = "int"
14265+ if pk < ptypes.length
14266+ pt = ptypes[pk]
14267+ end
14268+ declare_var(pnames[pk], pt)
14269+ pk = pk + 1
14270+ end
14271+ saved_ci = @current_class_idx
14272+ @current_class_idx = ci
14273+ lnames = "".split(",", -1)
14274+ ltypes = "".split(",", -1)
14275+ refine_locals_multi_pass_full(get_stmts(bid), lnames, ltypes, pnames, 0, 0)
14276+ ei = 0
14277+ while ei < eligible.length
14278+ scan_module_ivar_writes(bid, eligible[ei], key_sets[ei], val_sets[ei])
14279+ ei = ei + 1
14280+ end
14281+ @current_class_idx = saved_ci
14282+ pop_scope
14283+ end
14284+ mj = mj + 1
14285+ end
14286+ ei = 0
14287+ while ei < eligible.length
14288+ picked = pick_hash_class(key_sets[ei], val_sets[ei])
14289+ if picked != "" && picked != cls_ivar_type(ci, eligible[ei])
14290+ replace_ivar_type(ci, eligible[ei], picked)
14291+ mark_hash_needs(picked)
14292+ end
14293+ ei = ei + 1
14294+ end
14295+ end
14296+ ci = ci + 1
14297+ end
14298+ end
14299+
1418614300 def refine_module_ivar_types(mname, body_stmts)
1418714301 body_stmts.each { |sid|
1418814302 # Two shapes both refine the same `@const_types` slot:
@@ -26724,6 +26838,7 @@
2672426838 # `def self.add(s); @items << s; end` whose return is now
2672526839 # `sp_StrArray *` rather than the placeholder `sp_IntArray *`).
2672626840 refine_all_module_ivar_types
26841+ refine_class_ivar_hash_types
2672726842 infer_all_returns
2672826843 infer_function_body_call_types
2672926844 infer_class_body_call_types