Commit c91f9c07

c91f9c071b32Merged

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

Description

fix(analyze): pass-3 rescan in refine_method_body_locals (#445)

Sibling to #443. `h[pp[1..]] = ap` where pp / ap are intermediate body-locals (themselves split-result elements) had the hash typed as str_int_hash instead of str_str_hash, breaking the value-side narrowing #443 was supposed to fix. Other slice forms (closed range, [start, length], .upcase, str concat) worked fine. Only open-end range and similar nested-call-on-an- intermediate-local shapes regressed.

Root cause: refine_method_body_locals's pass-1 scan saw pp's RHS `pa[i]` resolve to "int" because pa wasn't declared yet (it's also a body local). Pass 2 with pa declared as str_array resolved `pa[i]` → string and pushed pp's correct type into the fresh ln2/lt2, but the merge happened AFTER pass-2 scan_locals finished. During pass-2 scan, infer_type(pp) read pp's pass-1 stale "int" from global scope, so a downstream `h[pp[1..]] = ap` promoted the empty hash to int_str_hash. Pass-2 ln2/lt2 captured the wrong hash variant; merge saw nothing new to correct.

Fix: add a third scan_locals pass after the pass-2 merge has already corrected intermediate-local types in scope. The third pass re-walks the body with pp etc. now declared as their merged types, infer_type(pp[1..]) resolves to "string", and the empty-hash promotion picks str_str_hash. Re-applies the existing hash-variant merge rules to fold the third-pass results back into ltypes.

The direct-element shape from #443 already worked because pa is declared once between passes — only the intermediate-local shape needed the extra pass.

Closes #445

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

Details

Commit
c91f9c071b32
Parent
87fd506580c2

spinel_analyze.rbmodifiedView file

@@ -20578,7 +20578,38 @@
2057820578 end
2057920579 j = j + 1
2058020580 end
20581- # Pass 3: lambda upgrade
20581+ # Pass 3: re-scan with the merged types now reflected in scope.
20582+ # Intermediate locals (e.g. `pp = pa[i]` where pa is itself a
20583+ # body local) had their pass-1 type fall back to "int" because
20584+ # pa wasn't declared yet on pass 1. Pass 2 pushed pp's correct
20585+ # type into the fresh ln2/lt2 but pp's *global* scope entry
20586+ # still carried the pass-1 stale type, so a downstream
20587+ # `h[pp[1..]] = ap` resolved pp via infer_type → "int" and the
20588+ # empty-hash promotion picked int_str_hash. The merge above
20589+ # set pp's scope to the merged type; redo scan_locals so the
20590+ # `[]=` arm now sees pp resolved to its real type and re-picks
20591+ # the right hash variant. Mirror the merge rules from pass 2.
20592+ lnames3 = "".split(",")
20593+ ltypes3 = "".split(",")
20594+ scan_locals(bid, lnames3, ltypes3, params)
20595+ j = 0
20596+ while j < lnames3.length
20597+ k = 0
20598+ while k < lnames.length
20599+ if lnames[k] == lnames3[j]
20600+ if ltypes[k] == "str_int_hash" && ltypes3[j] == "str_str_hash"
20601+ ltypes[k] = ltypes3[j]
20602+ set_var_type(lnames[k], ltypes3[j])
20603+ elsif ltypes[k] == "int_str_hash" && (ltypes3[j] == "str_str_hash" || ltypes3[j] == "sym_str_hash" || ltypes3[j] == "str_int_hash" || ltypes3[j] == "sym_int_hash")
20604+ ltypes[k] = ltypes3[j]
20605+ set_var_type(lnames[k], ltypes3[j])
20606+ end
20607+ end
20608+ k = k + 1
20609+ end
20610+ j = j + 1
20611+ end
20612+ # Pass 4: lambda upgrade
2058220613 j = 0
2058320614 while j < lnames.length
2058420615 if ltypes[j] == "int"