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

@@ -219,21 +219,9 @@
219219 emitter = HtmlEmitter.new
220220 content = emitter.emit(parser.tokens)
221221 tmpl = Template.new(@layout)
222- # Hoist fm.get(...) into locals before composing the hash literal.
223- # Inline method calls mixed with local-variable values widen the
224- # hash to str_poly_hash even when every value is statically
225- # string-typed: scan_locals' first pass walks the hash literal
226- # before the local declarations have been merged into scope, so
227- # the LV reads default to int, the call returns "" (unresolved),
228- # and the value-type unification picks poly. With the hoist all
229- # three values are LV reads that pass-2 resolves to string and
230- # the literal types as str_str_hash (matches Template.render's
231- # param signature).
232- title = fm.get("title")
233- author = fm.get("author")
234222 vars = {
235- "title" => title,
236- "author" => author,
223+ "title" => fm.get("title"),
224+ "author" => fm.get("author"),
237225 "content" => content
238226 }
239227 tmpl.render(vars)