Commit dcc77dcb

dcc77dcb65acMerged

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

Description

fix(analyze): port hash-each block-body inference + weak default (#408)

Two coordinated changes:

  1. Port the per-block-body str-concat collector from spinel_codegen.rb to spinel_analyze.rb. The original #408 fix (commit 9ca01d77) added collect_param_each_block_signals + collect_each_block_concat_signals + collect_concat_chain_leaves to spinel_codegen.rb's infer_param_hash_from_writes, but the analyze/codegen split (PR #416) moved the inference passes to spinel_analyze.rb -- the codegen-side copy stopped running, so the body-each hint collection has been silent dead code ever since. Ports the helpers to spinel_analyze.rb's infer_param_hash_from_writes so they actually fire during the analyze fixpoint.

  2. Option B "weak default" from Ori's issue body: when no concrete signals at all (no []= writes, no str-concat hints from the each-block) and the current param type is poly_poly_hash / sym_poly_hash / str_poly_hash, AND the body has at least one pname.each do |k, v| shape on the param, default to str_str_hash. New helper param_has_each_kv? walks for the each-shape presence.

    Unsound in general -- a sym-keyed poly_poly_hash gets mis-narrowed -- but covers the dominant Rails/Tep code shape where a string-keyed hash flows through a cmeth that just iterates without inserting back. The narrowing only fires when no caller has established a non-string-keyed shape via the existing call-site widening, so the fallback applies exactly when the param's poly type was itself a fallback from missing signal rather than a deliberate widening.

The dead-code copy in spinel_codegen.rb is left in place as a "common helper" for now (mirrors other helpers post-#416 that stay duplicated across the two files). A follow-up could delete it; the analyze-side is the one that actually runs.

Out of scope for this fix (still #424): when the each-body calls a sibling cmeth with k or v as an arg, propagating that k/v narrowing into the cmeth's param widening needs a different scope-handling shape than the inference pipeline currently uses (the targeted block-scope push tried for #424 breaks issue207's static-fold convergence). #424 stays open with a comment explaining the residual.

Test hash_param_each_kv_weak_default.rb covers the str-concat hint path (Joiner.encode) and the option-B weak-default path (HashDumper#collect with each|k,v| and no inserts back).

Refs #408

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

Details

Commit
dcc77dcb65ac
Parent
26c49657c625

spinel_analyze.rbmodifiedView file

@@ -20025,7 +20025,29 @@
2002520025 val_types = "".split(",")
2002620026 key_types = "".split(",")
2002720027 collect_param_hash_writes(nid, pname, val_types, key_types)
20028+ # Issue #408: also harvest signals from `pname.each do |k, v|`
20029+ # block bodies. Programs that read-only-iterate the hash never
20030+ # hit the `[]=` collector above, leaving the param widened to
20031+ # whatever poly-ish variant an earlier widening pinned it to.
20032+ collect_param_each_block_signals(nid, pname, val_types, key_types)
2002820033 if val_types.length == 0
20034+ # Issue #408 option B (Ori's "weaker fix"): when no concrete
20035+ # signals at all and the current type is poly-ish AND the
20036+ # body has an `each |k, v|` on the param, default to
20037+ # str_str_hash. This is unsound in general (a sym-keyed
20038+ # poly_poly_hash would mis-narrow), but covers the dominant
20039+ # Rails/Tep shape -- string-keyed hashes flowing through a
20040+ # cmeth body that just dispatches to a sibling-cmeth
20041+ # formatter. The narrowing only fires when no caller has
20042+ # established a non-string-keyed shape via the existing
20043+ # call-site widening, so the fallback applies exactly when
20044+ # the param's poly type is itself a fallback from missing
20045+ # call-site signal rather than a deliberate widening.
20046+ if (cur == "poly_poly_hash" || cur == "sym_poly_hash" || cur == "str_poly_hash") &&
20047+ param_has_each_kv?(nid, pname) == 1
20048+ @needs_str_str_hash = 1
20049+ return "str_str_hash"
20050+ end
2002920051 return ""
2003020052 end
2003120053 # Decide hash variant from the union of observed types. If any
@@ -20037,6 +20059,139 @@
2003720059 return compose_hash_type(new_kt, new_vt)
2003820060 end
2003920061
20062+ # Issue #408: walks `nid` and returns 1 if there is at least
20063+ # one `pname.each do |k, v|` shape on the param (regardless of
20064+ # what the block body does with k / v). Used by the option B
20065+ # weak-default fallback in infer_param_hash_from_writes.
20066+ def param_has_each_kv?(nid, pname)
20067+ if nid < 0
20068+ return 0
20069+ end
20070+ if @nd_type[nid] == "CallNode" && (@nd_name[nid] == "each" || @nd_name[nid] == "each_pair")
20071+ r_each_kv = @nd_receiver[nid]
20072+ if r_each_kv >= 0 && @nd_type[r_each_kv] == "LocalVariableReadNode" && @nd_name[r_each_kv] == pname
20073+ blk_each_kv = @nd_block[nid]
20074+ if blk_each_kv >= 0
20075+ # 2-arity block?
20076+ if get_block_param(nid, 1) != ""
20077+ return 1
20078+ end
20079+ end
20080+ end
20081+ end
20082+ cs_each_kv = []
20083+ push_child_ids(nid, cs_each_kv)
20084+ k_each_kv = 0
20085+ while k_each_kv < cs_each_kv.length
20086+ if param_has_each_kv?(cs_each_kv[k_each_kv], pname) == 1
20087+ return 1
20088+ end
20089+ k_each_kv = k_each_kv + 1
20090+ end
20091+ 0
20092+ end
20093+
20094+ # Issue #408: walk `nid` for `pname.each do |k, v|` block
20095+ # expressions and harvest type signals from how `k` and `v`
20096+ # participate in `+`-chains in the body. A chain whose
20097+ # transitive leaves include both a string literal and a
20098+ # reference to k_pname / v_pname is treated as evidence the
20099+ # corresponding side is string-typed.
20100+ def collect_param_each_block_signals(nid, pname, val_types, key_types)
20101+ if nid < 0
20102+ return
20103+ end
20104+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "each"
20105+ r_408 = @nd_receiver[nid]
20106+ if r_408 >= 0 && @nd_type[r_408] == "LocalVariableReadNode" && @nd_name[r_408] == pname
20107+ blk_408 = @nd_block[nid]
20108+ if blk_408 >= 0
20109+ k_pname_408 = get_block_param(nid, 0)
20110+ v_pname_408 = get_block_param(nid, 1)
20111+ body_408 = @nd_body[blk_408]
20112+ if body_408 >= 0
20113+ collect_each_block_concat_signals(body_408, k_pname_408, v_pname_408, val_types, key_types)
20114+ end
20115+ end
20116+ end
20117+ end
20118+ cs_408 = []
20119+ push_child_ids(nid, cs_408)
20120+ k_408 = 0
20121+ while k_408 < cs_408.length
20122+ collect_param_each_block_signals(cs_408[k_408], pname, val_types, key_types)
20123+ k_408 = k_408 + 1
20124+ end
20125+ end
20126+
20127+ def collect_each_block_concat_signals(nid, k_pname, v_pname, val_types, key_types)
20128+ if nid < 0
20129+ return
20130+ end
20131+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "+"
20132+ leaves_408 = []
20133+ collect_concat_chain_leaves(nid, leaves_408)
20134+ has_str_lit_408 = 0
20135+ has_k_408 = 0
20136+ has_v_408 = 0
20137+ li_408 = 0
20138+ while li_408 < leaves_408.length
20139+ lt_408 = @nd_type[leaves_408[li_408]]
20140+ if lt_408 == "StringNode" || lt_408 == "InterpolatedStringNode"
20141+ has_str_lit_408 = 1
20142+ end
20143+ if lt_408 == "LocalVariableReadNode"
20144+ ln_408 = @nd_name[leaves_408[li_408]]
20145+ if ln_408 == k_pname && k_pname != ""
20146+ has_k_408 = 1
20147+ end
20148+ if ln_408 == v_pname && v_pname != ""
20149+ has_v_408 = 1
20150+ end
20151+ end
20152+ li_408 = li_408 + 1
20153+ end
20154+ if has_str_lit_408 == 1
20155+ if has_k_408 == 1 && not_in("string", key_types) == 1
20156+ key_types.push("string")
20157+ end
20158+ if has_v_408 == 1 && not_in("string", val_types) == 1
20159+ val_types.push("string")
20160+ end
20161+ end
20162+ end
20163+ cs_408b = []
20164+ push_child_ids(nid, cs_408b)
20165+ k_408b = 0
20166+ while k_408b < cs_408b.length
20167+ collect_each_block_concat_signals(cs_408b[k_408b], k_pname, v_pname, val_types, key_types)
20168+ k_408b = k_408b + 1
20169+ end
20170+ end
20171+
20172+ def collect_concat_chain_leaves(nid, leaves)
20173+ if nid < 0
20174+ return
20175+ end
20176+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "+"
20177+ r_chain = @nd_receiver[nid]
20178+ if r_chain >= 0
20179+ collect_concat_chain_leaves(r_chain, leaves)
20180+ end
20181+ args_id_chain = @nd_arguments[nid]
20182+ if args_id_chain >= 0
20183+ args_chain = get_args(args_id_chain)
20184+ ai_chain = 0
20185+ while ai_chain < args_chain.length
20186+ collect_concat_chain_leaves(args_chain[ai_chain], leaves)
20187+ ai_chain = ai_chain + 1
20188+ end
20189+ end
20190+ return
20191+ end
20192+ leaves.push(nid)
20193+ end
20194+
2004020195 def collect_param_hash_writes(nid, pname, val_types, key_types)
2004120196 if nid < 0
2004220197 return