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
end j = j + 1 end # Pass 3: lambda upgrade # Pass 3: re-scan with the merged types now reflected in scope. # Intermediate locals (e.g. `pp = pa[i]` where pa is itself a # body local) had their pass-1 type fall back to "int" because # pa wasn't declared yet on pass 1. Pass 2 pushed pp's correct # type into the fresh ln2/lt2 but pp's *global* scope entry # still carried the pass-1 stale type, so a downstream # `h[pp[1..]] = ap` resolved pp via infer_type → "int" and the # empty-hash promotion picked int_str_hash. The merge above # set pp's scope to the merged type; redo scan_locals so the # `[]=` arm now sees pp resolved to its real type and re-picks # the right hash variant. Mirror the merge rules from pass 2. lnames3 = "".split(",") ltypes3 = "".split(",") scan_locals(bid, lnames3, ltypes3, params) j = 0 while j < lnames3.length k = 0 while k < lnames.length if lnames[k] == lnames3[j] if ltypes[k] == "str_int_hash" && ltypes3[j] == "str_str_hash" ltypes[k] = ltypes3[j] set_var_type(lnames[k], ltypes3[j]) 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") ltypes[k] = ltypes3[j] set_var_type(lnames[k], ltypes3[j]) end end k = k + 1 end j = j + 1 end # Pass 4: lambda upgrade j = 0 while j < lnames.length if ltypes[j] == "int"test/hash_key_via_intermediate_local.rbaddedView file
Loading diff…
test/hash_key_via_intermediate_local.rb.expectedaddedView file
Loading diff…