Commit f896e1b4
f896e1b484b5Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~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_signalsfinds CallNodes whose recv is a LocalVariableReadNode named pname and whose mname iseach, then descends into the block body. -
collect_each_block_concat_signalslooks for+-chains in the block body and flattens them viacollect_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 ininfer_param_hash_from_writesthen 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
- f896e1b484b5
- Parent
- c465f83ed5c2
spinel_codegen.rbmodifiedView file
val_types = "".split(",") key_types = "".split(",") collect_param_hash_writes(nid, pname, val_types, key_types) # Issue #408: also harvest signals from `pname.each do |k, v|` # block bodies. Programs that read-only-iterate the hash never # hit the `[]=` collector above, leaving the param widened to # whatever poly-ish variant an upstream call site picked. collect_param_each_block_signals(nid, pname, val_types, key_types) if val_types.length == 0 return "" end end end # Issue #408 (followup to #397). Walk `nid` for `pname.each do |k, v|` # block expressions and harvest type signals from how `k` and `v` are # used inside the block. The existing `[]=` collector above only fires # for write-back patterns (`pname[k] = v`); a method that *reads* the # hash via `each` -- the dominant Tep::Json shape -- contributes # nothing through that path, so the param's hash variant stays at # whichever poly-ish form an earlier widening pinned it to. # # Heuristic: a `+`-chain whose transitive leaves include both a # string literal and a reference to k_pname / v_pname is treated as # evidence that the corresponding side is string-typed. This catches # the canonical `out + k + "=" + v` formatter without needing to # interpret arbitrary callee signatures (which would themselves be # mid-inference and unreliable). def collect_param_each_block_signals(nid, pname, val_types, key_types) if nid < 0 return end if @nd_type[nid] == "CallNode" && @nd_name[nid] == "each" r_408 = @nd_receiver[nid] if r_408 >= 0 && @nd_type[r_408] == "LocalVariableReadNode" && @nd_name[r_408] == pname blk_408 = @nd_block[nid] if blk_408 >= 0 k_pname_408 = get_block_param(nid, 0) v_pname_408 = get_block_param(nid, 1) body_408 = @nd_body[blk_408] if body_408 >= 0 collect_each_block_concat_signals(body_408, k_pname_408, v_pname_408, val_types, key_types) end end end end cs_408 = [] push_child_ids(nid, cs_408) k_408 = 0 while k_408 < cs_408.length collect_param_each_block_signals(cs_408[k_408], pname, val_types, key_types) k_408 = k_408 + 1 end end def collect_each_block_concat_signals(nid, k_pname, v_pname, val_types, key_types) if nid < 0 return end if @nd_type[nid] == "CallNode" && @nd_name[nid] == "+" leaves_408 = [] collect_concat_chain_leaves(nid, leaves_408) has_str_lit_408 = 0 has_k_408 = 0 has_v_408 = 0 li_408 = 0 while li_408 < leaves_408.length lt_408 = @nd_type[leaves_408[li_408]] if lt_408 == "StringNode" || lt_408 == "InterpolatedStringNode" has_str_lit_408 = 1 end if lt_408 == "LocalVariableReadNode" ln_408 = @nd_name[leaves_408[li_408]] if ln_408 == k_pname && k_pname != "" has_k_408 = 1 end if ln_408 == v_pname && v_pname != "" has_v_408 = 1 end end li_408 = li_408 + 1 end if has_str_lit_408 == 1 if has_k_408 == 1 && not_in("string", key_types) == 1 key_types.push("string") end if has_v_408 == 1 && not_in("string", val_types) == 1 val_types.push("string") end end end cs_408b = [] push_child_ids(nid, cs_408b) k_408b = 0 while k_408b < cs_408b.length collect_each_block_concat_signals(cs_408b[k_408b], k_pname, v_pname, val_types, key_types) k_408b = k_408b + 1 end end # Flatten a left-associative `+` chain into its leaves. `out + k + "=" # + v` parses as `((out + k) + "=") + v`; this returns # `[out, k, "=", v]` regardless of nesting depth so the caller can # check membership without recursing through CallNode shells. def collect_concat_chain_leaves(nid, leaves) if nid < 0 return end if @nd_type[nid] == "CallNode" && @nd_name[nid] == "+" r_chain = @nd_receiver[nid] if r_chain >= 0 collect_concat_chain_leaves(r_chain, leaves) end args_id_chain = @nd_arguments[nid] if args_id_chain >= 0 args_chain = get_args(args_id_chain) ai_chain = 0 while ai_chain < args_chain.length collect_concat_chain_leaves(args_chain[ai_chain], leaves) ai_chain = ai_chain + 1 end end return end leaves.push(nid) end def hash_key_part(t) if t == "str_int_hash" || t == "str_str_hash" || t == "str_poly_hash" return "str"test/hash_param_each_kv_inference.rbaddedView file
Loading diff…
test/hash_param_each_kv_inference.rb.expectedaddedView file
Loading diff…