Commit 906671d8

906671d83bebMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): hash param each|k,v| body harvests narrow signals (#408)

Followup to #397. The existing narrow_param_hash_types_from_body_writes pass collected only pname[k] = v write-back patterns -- a method that read-only iterates the param via pname.each do |k, v| ... end got no signal at all, so an upstream call site that pinned the param to a poly hash variant left the body's k/v references at sp_RbVal, tripping subsequent string-concat sites with passing 'sp_RbVal' to const char *.

Extends the same pass with a parallel each block walker:

  • collect_param_each_block_signals finds CallNodes whose recv is a LocalVariableReadNode named pname and whose mname is each, then descends into the block body.

  • collect_each_block_concat_signals looks for +-chains in the block body and flattens them via collect_concat_chain_leaves. A chain whose transitive leaves include both a string literal (StringNode / InterpolatedStringNode) and a reference to k_pname / v_pname contributes "string" to the corresponding key_types or val_types accumulator. The compose_hash_type call already in infer_param_hash_from_writes then narrows the variant.

Heuristic-only by design -- interpreting arbitrary callee signatures inside the block would be unsound during inference (the callees themselves are mid-typing). The transitive-leaf check covers the canonical formatter shapes (out + k + "=" + v, out + k + ":" + v + "\n", etc.) without overcommitting.

Test hash_param_each_kv_inference.rb: module class method (canonical Tep::Json shape) and class instance method, each with multiple call sites that exercise the iterative inference loop's fixpoint over the new collector.

Refs #408

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

Details

Commit
906671d83beb
Parent
940dc2986209

spinel_codegen.rbmodifiedView file

@@ -5085,6 +5085,11 @@
50855085 val_types = "".split(",")
50865086 key_types = "".split(",")
50875087 collect_param_hash_writes(nid, pname, val_types, key_types)
5088+ # Issue #408: also harvest signals from `pname.each do |k, v|`
5089+ # block bodies. Programs that read-only-iterate the hash never
5090+ # hit the `[]=` collector above, leaving the param widened to
5091+ # whatever poly-ish variant an upstream call site picked.
5092+ collect_param_each_block_signals(nid, pname, val_types, key_types)
50885093 if val_types.length == 0
50895094 return ""
50905095 end
@@ -5129,6 +5134,119 @@
51295134 end
51305135 end
51315136
5137+ # Issue #408 (followup to #397). Walk `nid` for `pname.each do |k, v|`
5138+ # block expressions and harvest type signals from how `k` and `v` are
5139+ # used inside the block. The existing `[]=` collector above only fires
5140+ # for write-back patterns (`pname[k] = v`); a method that *reads* the
5141+ # hash via `each` -- the dominant Tep::Json shape -- contributes
5142+ # nothing through that path, so the param's hash variant stays at
5143+ # whichever poly-ish form an earlier widening pinned it to.
5144+ #
5145+ # Heuristic: a `+`-chain whose transitive leaves include both a
5146+ # string literal and a reference to k_pname / v_pname is treated as
5147+ # evidence that the corresponding side is string-typed. This catches
5148+ # the canonical `out + k + "=" + v` formatter without needing to
5149+ # interpret arbitrary callee signatures (which would themselves be
5150+ # mid-inference and unreliable).
5151+ def collect_param_each_block_signals(nid, pname, val_types, key_types)
5152+ if nid < 0
5153+ return
5154+ end
5155+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "each"
5156+ r_408 = @nd_receiver[nid]
5157+ if r_408 >= 0 && @nd_type[r_408] == "LocalVariableReadNode" && @nd_name[r_408] == pname
5158+ blk_408 = @nd_block[nid]
5159+ if blk_408 >= 0
5160+ k_pname_408 = get_block_param(nid, 0)
5161+ v_pname_408 = get_block_param(nid, 1)
5162+ body_408 = @nd_body[blk_408]
5163+ if body_408 >= 0
5164+ collect_each_block_concat_signals(body_408, k_pname_408, v_pname_408, val_types, key_types)
5165+ end
5166+ end
5167+ end
5168+ end
5169+ cs_408 = []
5170+ push_child_ids(nid, cs_408)
5171+ k_408 = 0
5172+ while k_408 < cs_408.length
5173+ collect_param_each_block_signals(cs_408[k_408], pname, val_types, key_types)
5174+ k_408 = k_408 + 1
5175+ end
5176+ end
5177+
5178+ def collect_each_block_concat_signals(nid, k_pname, v_pname, val_types, key_types)
5179+ if nid < 0
5180+ return
5181+ end
5182+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "+"
5183+ leaves_408 = []
5184+ collect_concat_chain_leaves(nid, leaves_408)
5185+ has_str_lit_408 = 0
5186+ has_k_408 = 0
5187+ has_v_408 = 0
5188+ li_408 = 0
5189+ while li_408 < leaves_408.length
5190+ lt_408 = @nd_type[leaves_408[li_408]]
5191+ if lt_408 == "StringNode" || lt_408 == "InterpolatedStringNode"
5192+ has_str_lit_408 = 1
5193+ end
5194+ if lt_408 == "LocalVariableReadNode"
5195+ ln_408 = @nd_name[leaves_408[li_408]]
5196+ if ln_408 == k_pname && k_pname != ""
5197+ has_k_408 = 1
5198+ end
5199+ if ln_408 == v_pname && v_pname != ""
5200+ has_v_408 = 1
5201+ end
5202+ end
5203+ li_408 = li_408 + 1
5204+ end
5205+ if has_str_lit_408 == 1
5206+ if has_k_408 == 1 && not_in("string", key_types) == 1
5207+ key_types.push("string")
5208+ end
5209+ if has_v_408 == 1 && not_in("string", val_types) == 1
5210+ val_types.push("string")
5211+ end
5212+ end
5213+ end
5214+ cs_408b = []
5215+ push_child_ids(nid, cs_408b)
5216+ k_408b = 0
5217+ while k_408b < cs_408b.length
5218+ collect_each_block_concat_signals(cs_408b[k_408b], k_pname, v_pname, val_types, key_types)
5219+ k_408b = k_408b + 1
5220+ end
5221+ end
5222+
5223+ # Flatten a left-associative `+` chain into its leaves. `out + k + "="
5224+ # + v` parses as `((out + k) + "=") + v`; this returns
5225+ # `[out, k, "=", v]` regardless of nesting depth so the caller can
5226+ # check membership without recursing through CallNode shells.
5227+ def collect_concat_chain_leaves(nid, leaves)
5228+ if nid < 0
5229+ return
5230+ end
5231+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "+"
5232+ r_chain = @nd_receiver[nid]
5233+ if r_chain >= 0
5234+ collect_concat_chain_leaves(r_chain, leaves)
5235+ end
5236+ args_id_chain = @nd_arguments[nid]
5237+ if args_id_chain >= 0
5238+ args_chain = get_args(args_id_chain)
5239+ ai_chain = 0
5240+ while ai_chain < args_chain.length
5241+ collect_concat_chain_leaves(args_chain[ai_chain], leaves)
5242+ ai_chain = ai_chain + 1
5243+ end
5244+ end
5245+ return
5246+ end
5247+ leaves.push(nid)
5248+ end
5249+
51325250 def hash_key_part(t)
51335251 if t == "str_int_hash" || t == "str_str_hash" || t == "str_poly_hash"
51345252 return "str"