Commit 09556c2b
09556c2b9d1fMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 modified
Description
fix(analyze): pass-2 hash refinement accepts poly_hash -> concrete
Proposal C from the post-#463 inference plan. A hash literal like
vars = { "a" => fm.get, "b" => content } where content is a
String-typed local declared earlier in the same body got typed
as str_poly_hash even though every value is statically string.
Root: scan_locals' pass 1 builds the names/types arrays as it
walks but doesn't declare them into scope until after the walk.
When the hash literal vars = {...} is visited, infer_type on
the LV read content falls back to int (the local isn't yet in
scope). The hash literal's value-type unify then mixes the
fm.get return ("") with content's int and picks str_poly_hash
via infer_hash_val_type_raw's mixed-types branch.
Pass 2 has all locals declared, so content resolves to string
and the value-type unify picks str_str_hash correctly. The
existing pass-2 merge rules accepted str_int_hash -> str_str_hash
(value refinement) but rejected str_poly_hash -> str_str_hash
(the same refinement from a wider starting point).
Fix: extend merge_refined_local_type with str_poly_hash -> str_str_hash / str_int_hash and sym_poly_hash -> sym_str_hash / sym_int_hash. Symmetric to the int_str_hash and str_int_hash refinements already there. The earlier "if new_t == str_poly_hash, take it" rule still fires for cur -> wider transitions; this only handles the inverse where pass-2 has more info than pass-1.
Side benefit: bm_jekyll_lite no longer needs the hoist-fm.get-into-locals workaround documented inline. Removed the workaround and the explanatory comment; the inline-call hash literal now compiles to str_str_hash directly.
Validation: tests 452 pass, bench 56/56, bootstrap 4-way OK.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 09556c2b9d1f
- Parent
- 8de7725ed79c
benchmark/bm_jekyll_lite.rbmodifiedView file
emitter = HtmlEmitter.new content = emitter.emit(parser.tokens) tmpl = Template.new(@layout) # Hoist fm.get(...) into locals before composing the hash literal. # Inline method calls mixed with local-variable values widen the # hash to str_poly_hash even when every value is statically # string-typed: scan_locals' first pass walks the hash literal # before the local declarations have been merged into scope, so # the LV reads default to int, the call returns "" (unresolved), # and the value-type unification picks poly. With the hoist all # three values are LV reads that pass-2 resolves to string and # the literal types as str_str_hash (matches Template.render's # param signature). title = fm.get("title") author = fm.get("author") vars = { "title" => title, "author" => author, "title" => fm.get("title"), "author" => fm.get("author"), "content" => content } tmpl.render(vars)spinel_analyze.rbmodifiedView file
Loading diff…