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_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
f896e1b484b5
Parent
c465f83ed5c2

spinel_codegen.rbmodifiedView file

@@ -12428,6 +12428,11 @@
1242812428 val_types = "".split(",")
1242912429 key_types = "".split(",")
1243012430 collect_param_hash_writes(nid, pname, val_types, key_types)
12431+ # Issue #408: also harvest signals from `pname.each do |k, v|`
12432+ # block bodies. Programs that read-only-iterate the hash never
12433+ # hit the `[]=` collector above, leaving the param widened to
12434+ # whatever poly-ish variant an upstream call site picked.
12435+ collect_param_each_block_signals(nid, pname, val_types, key_types)
1243112436 if val_types.length == 0
1243212437 return ""
1243312438 end
@@ -12472,6 +12477,119 @@
1247212477 end
1247312478 end
1247412479
12480+ # Issue #408 (followup to #397). Walk `nid` for `pname.each do |k, v|`
12481+ # block expressions and harvest type signals from how `k` and `v` are
12482+ # used inside the block. The existing `[]=` collector above only fires
12483+ # for write-back patterns (`pname[k] = v`); a method that *reads* the
12484+ # hash via `each` -- the dominant Tep::Json shape -- contributes
12485+ # nothing through that path, so the param's hash variant stays at
12486+ # whichever poly-ish form an earlier widening pinned it to.
12487+ #
12488+ # Heuristic: a `+`-chain whose transitive leaves include both a
12489+ # string literal and a reference to k_pname / v_pname is treated as
12490+ # evidence that the corresponding side is string-typed. This catches
12491+ # the canonical `out + k + "=" + v` formatter without needing to
12492+ # interpret arbitrary callee signatures (which would themselves be
12493+ # mid-inference and unreliable).
12494+ def collect_param_each_block_signals(nid, pname, val_types, key_types)
12495+ if nid < 0
12496+ return
12497+ end
12498+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "each"
12499+ r_408 = @nd_receiver[nid]
12500+ if r_408 >= 0 && @nd_type[r_408] == "LocalVariableReadNode" && @nd_name[r_408] == pname
12501+ blk_408 = @nd_block[nid]
12502+ if blk_408 >= 0
12503+ k_pname_408 = get_block_param(nid, 0)
12504+ v_pname_408 = get_block_param(nid, 1)
12505+ body_408 = @nd_body[blk_408]
12506+ if body_408 >= 0
12507+ collect_each_block_concat_signals(body_408, k_pname_408, v_pname_408, val_types, key_types)
12508+ end
12509+ end
12510+ end
12511+ end
12512+ cs_408 = []
12513+ push_child_ids(nid, cs_408)
12514+ k_408 = 0
12515+ while k_408 < cs_408.length
12516+ collect_param_each_block_signals(cs_408[k_408], pname, val_types, key_types)
12517+ k_408 = k_408 + 1
12518+ end
12519+ end
12520+
12521+ def collect_each_block_concat_signals(nid, k_pname, v_pname, val_types, key_types)
12522+ if nid < 0
12523+ return
12524+ end
12525+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "+"
12526+ leaves_408 = []
12527+ collect_concat_chain_leaves(nid, leaves_408)
12528+ has_str_lit_408 = 0
12529+ has_k_408 = 0
12530+ has_v_408 = 0
12531+ li_408 = 0
12532+ while li_408 < leaves_408.length
12533+ lt_408 = @nd_type[leaves_408[li_408]]
12534+ if lt_408 == "StringNode" || lt_408 == "InterpolatedStringNode"
12535+ has_str_lit_408 = 1
12536+ end
12537+ if lt_408 == "LocalVariableReadNode"
12538+ ln_408 = @nd_name[leaves_408[li_408]]
12539+ if ln_408 == k_pname && k_pname != ""
12540+ has_k_408 = 1
12541+ end
12542+ if ln_408 == v_pname && v_pname != ""
12543+ has_v_408 = 1
12544+ end
12545+ end
12546+ li_408 = li_408 + 1
12547+ end
12548+ if has_str_lit_408 == 1
12549+ if has_k_408 == 1 && not_in("string", key_types) == 1
12550+ key_types.push("string")
12551+ end
12552+ if has_v_408 == 1 && not_in("string", val_types) == 1
12553+ val_types.push("string")
12554+ end
12555+ end
12556+ end
12557+ cs_408b = []
12558+ push_child_ids(nid, cs_408b)
12559+ k_408b = 0
12560+ while k_408b < cs_408b.length
12561+ collect_each_block_concat_signals(cs_408b[k_408b], k_pname, v_pname, val_types, key_types)
12562+ k_408b = k_408b + 1
12563+ end
12564+ end
12565+
12566+ # Flatten a left-associative `+` chain into its leaves. `out + k + "="
12567+ # + v` parses as `((out + k) + "=") + v`; this returns
12568+ # `[out, k, "=", v]` regardless of nesting depth so the caller can
12569+ # check membership without recursing through CallNode shells.
12570+ def collect_concat_chain_leaves(nid, leaves)
12571+ if nid < 0
12572+ return
12573+ end
12574+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "+"
12575+ r_chain = @nd_receiver[nid]
12576+ if r_chain >= 0
12577+ collect_concat_chain_leaves(r_chain, leaves)
12578+ end
12579+ args_id_chain = @nd_arguments[nid]
12580+ if args_id_chain >= 0
12581+ args_chain = get_args(args_id_chain)
12582+ ai_chain = 0
12583+ while ai_chain < args_chain.length
12584+ collect_concat_chain_leaves(args_chain[ai_chain], leaves)
12585+ ai_chain = ai_chain + 1
12586+ end
12587+ end
12588+ return
12589+ end
12590+ leaves.push(nid)
12591+ end
12592+
1247512593 def hash_key_part(t)
1247612594 if t == "str_int_hash" || t == "str_str_hash" || t == "str_poly_hash"
1247712595 return "str"