Commit aca207ae

aca207aebb49Merged

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

Description

fix(analyze): propagate hash-each block-arg types into nested cmeth widening (#424)

When a method's hash-typed param is iterated via h.each |k, v| and the block body calls a sibling cmeth with k or v as an arg (Json.escape(k) etc.), the called cmeth's param type stayed at the int default. The in-pipeline scan_new_calls runs before block scope is pushed, so its infer_type(k) at the call-site widening step sees the empty var-type table and falls back to int -- the cmeth then C-compiles its param as mrb_int and the caller passes const char *, breaking the build with passing 'const char *' to parameter of type 'mrb_int'.

Per the earlier #424 comment, the obvious "push block-iteration scope inside scan_new_calls" approach perturbs #207's symbolize_keys convergence (where v's int fallback during early passes is load-bearing for the static is_a?(Symbol) fold to apply). This fix instead runs a targeted pre-scan:

widen_cmeths_via_hash_each_blocks: For every method (top-level, imeth, cmeth) and every hash-typed param p in that method: - Walk the body looking for <p>.each |k, v| { ... } - For each call site <Class>.<cmeth>(args) inside the block body, for each arg that is a LocalVariableReadNode named k or v, widen the called cmeth's param at that index via unify_call_types using the hash-derived k/v type (key from hash_key_type_from_variant; value from hash_leaf_type).

The pass runs inside the iterative inference loop after narrow_param_hash_types_from_body_writes so a later iteration that pins p to a more specific hash variant feeds the more- specific k/v downstream too. scan_new_calls and the wider inference loop stay untouched -- #207's convergence path is unaffected.

Scope:

  • Constant-receiver cmeth calls (<Class>.<m>(args)). The canonical #424 repro. Other shapes (bare method calls, instance recv) stay handled by the existing iterative passes; broadening the pre-scan to cover those would need more care to avoid perturbing other tests' convergence.
  • 2-arity each |k, v| only. 1-arity (each |pair|) and other iterator shapes aren't covered.

Optcarrot: checksum 59662 unchanged, fps 651.2 (within noise). 381 tests pass including #207's existing repro.

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

Details

Commit
aca207aebb49
Parent
1368c2896f92

spinel_analyze.rbmodifiedView file

@@ -14853,6 +14853,13 @@
1485314853 infer_param_array_type_from_body
1485414854 narrow_param_types_from_body_method_calls
1485514855 narrow_param_hash_types_from_body_writes
14856+ # Issue #424: propagate hash-each block-arg types into
14857+ # nested cmeth/method-call param widening. Runs inside
14858+ # the iterative loop so a later iteration of
14859+ # narrow_param_hash_types_from_body_writes (which may
14860+ # pin the enclosing param to a more specific hash variant)
14861+ # gets its k/v types fed downstream too.
14862+ widen_cmeths_via_hash_each_blocks
1485614863 detect_poly_params
1485714864 cur_sig = inference_signature
1485814865 if cur_sig == prev_sig
@@ -20255,6 +20262,231 @@
2025520262 end
2025620263 end
2025720264
20265+ # Issue #424: hash-each block-arg widening for nested cmeth /
20266+ # method calls. The in-pipeline scan_new_calls runs without
20267+ # the iterator's k/v scope pushed, so a call site like
20268+ # `Json.escape(k)` inside `h.each |k, v| { ... }` sees k as
20269+ # untyped and the cmeth's param ends up at the int default.
20270+ # The block-scope push approach (matz comment, option 2/3)
20271+ # perturbed #207's symbolize_keys convergence; this pass
20272+ # stays surgical -- for every hash-typed param p of a method,
20273+ # find any `lv_p.each |k, v|` blocks in the body and walk the
20274+ # block body for `<recv>.<m>(args)` where args reference k or
20275+ # v. Widen the called method's param types from the hash's
20276+ # key/value variant only at those specific sites, leaving
20277+ # scan_new_calls and the wider iterative loop untouched.
20278+ def widen_cmeths_via_hash_each_blocks
20279+ # Top-level methods.
20280+ mi = 0
20281+ while mi < @meth_names.length
20282+ bid_h = @meth_body_ids[mi]
20283+ if bid_h >= 0
20284+ pnames = @meth_param_names[mi].split(",")
20285+ ptypes = @meth_param_types[mi].split(",")
20286+ each_widen_walk(bid_h, pnames, ptypes)
20287+ end
20288+ mi = mi + 1
20289+ end
20290+ # Class instance methods.
20291+ ci = 0
20292+ while ci < @cls_names.length
20293+ all_params = @cls_meth_params[ci].split("|")
20294+ all_ptypes = @cls_meth_ptypes[ci].split("|")
20295+ bodies = @cls_meth_bodies[ci].split(";")
20296+ mj = 0
20297+ while mj < all_params.length
20298+ bid_c = -1
20299+ if mj < bodies.length
20300+ bid_c = bodies[mj].to_i
20301+ end
20302+ if bid_c >= 0
20303+ cm_pnames = all_params[mj].split(",")
20304+ cm_ptypes = "".split(",")
20305+ if mj < all_ptypes.length
20306+ cm_ptypes = all_ptypes[mj].split(",")
20307+ end
20308+ each_widen_walk(bid_c, cm_pnames, cm_ptypes)
20309+ end
20310+ mj = mj + 1
20311+ end
20312+ ci = ci + 1
20313+ end
20314+ # Class methods (cmeths).
20315+ ci = 0
20316+ while ci < @cls_names.length
20317+ all_params = @cls_cmeth_params[ci].split("|")
20318+ all_ptypes = @cls_cmeth_ptypes[ci].split("|")
20319+ bodies = @cls_cmeth_bodies[ci].split(";")
20320+ mj = 0
20321+ while mj < all_params.length
20322+ bid_c = -1
20323+ if mj < bodies.length
20324+ bid_c = bodies[mj].to_i
20325+ end
20326+ if bid_c >= 0
20327+ cm_pnames = all_params[mj].split(",")
20328+ cm_ptypes = "".split(",")
20329+ if mj < all_ptypes.length
20330+ cm_ptypes = all_ptypes[mj].split(",")
20331+ end
20332+ each_widen_walk(bid_c, cm_pnames, cm_ptypes)
20333+ end
20334+ mj = mj + 1
20335+ end
20336+ ci = ci + 1
20337+ end
20338+ end
20339+
20340+ # Recurse through `nid` looking for `<local>.each |k, v| { body }`
20341+ # where `<local>` matches one of the enclosing method's hash-typed
20342+ # params. When found, walk the block body for nested call sites
20343+ # whose args reference k or v and widen the called method's
20344+ # param types accordingly.
20345+ def each_widen_walk(nid, pnames, ptypes)
20346+ if nid < 0
20347+ return
20348+ end
20349+ t = @nd_type[nid]
20350+ if t == "CallNode" && @nd_name[nid] == "each" && @nd_block[nid] >= 0
20351+ recv = @nd_receiver[nid]
20352+ if recv >= 0 && @nd_type[recv] == "LocalVariableReadNode"
20353+ local_name = @nd_name[recv]
20354+ # Locate the param's type in the enclosing method.
20355+ local_t = ""
20356+ pi = 0
20357+ while pi < pnames.length
20358+ if pnames[pi] == local_name
20359+ if pi < ptypes.length
20360+ local_t = ptypes[pi]
20361+ end
20362+ pi = pnames.length
20363+ else
20364+ pi = pi + 1
20365+ end
20366+ end
20367+ if is_hash_type(local_t) == 1
20368+ k_name_424 = get_block_param(nid, 0)
20369+ v_name_424 = get_block_param(nid, 1)
20370+ if k_name_424 != "" && v_name_424 != ""
20371+ k_t_424 = hash_key_type_from_variant(local_t)
20372+ v_t_424 = hash_leaf_type(local_t)
20373+ blk = @nd_block[nid]
20374+ block_body = @nd_body[blk]
20375+ widen_callsites_referencing_kv(block_body, k_name_424, k_t_424, v_name_424, v_t_424)
20376+ end
20377+ end
20378+ end
20379+ end
20380+ cs = []
20381+ push_child_ids(nid, cs)
20382+ k = 0
20383+ while k < cs.length
20384+ each_widen_walk(cs[k], pnames, ptypes)
20385+ k = k + 1
20386+ end
20387+ end
20388+
20389+ # Walk the block body. For each CallNode whose args reference
20390+ # `k_name` / `v_name`, widen the called method's param types
20391+ # from the hash-derived k/v types. Covers <Class>.<cmeth>(...)
20392+ # constant-recv shape (the canonical #424 repro). Other shapes
20393+ # (bare method, instance recv) stay handled by the existing
20394+ # iterative loop.
20395+ def widen_callsites_referencing_kv(nid, k_name, k_t, v_name, v_t)
20396+ if nid < 0
20397+ return
20398+ end
20399+ if @nd_type[nid] == "CallNode"
20400+ mname_w = @nd_name[nid]
20401+ recv_w = @nd_receiver[nid]
20402+ args_id_w = @nd_arguments[nid]
20403+ if recv_w >= 0 && @nd_type[recv_w] == "ConstantReadNode" && args_id_w >= 0
20404+ cls_name_w = @nd_name[recv_w]
20405+ ci_w = find_class_idx(cls_name_w)
20406+ if ci_w >= 0
20407+ cm_names_w = @cls_cmeth_names[ci_w].split(";")
20408+ cmpall_w = @cls_cmeth_ptypes[ci_w].split("|")
20409+ cmi_w = 0
20410+ while cmi_w < cm_names_w.length
20411+ if cm_names_w[cmi_w] == mname_w && cmi_w < cmpall_w.length
20412+ cmpt_w = cmpall_w[cmi_w].split(",")
20413+ arg_ids_w = get_args(args_id_w)
20414+ ai_w = 0
20415+ changed_w = 0
20416+ while ai_w < arg_ids_w.length && ai_w < cmpt_w.length
20417+ arg_t_w = arg_type_in_each_block(arg_ids_w[ai_w], k_name, k_t, v_name, v_t)
20418+ if arg_t_w != ""
20419+ new_t_w = unify_call_types(cmpt_w[ai_w], arg_t_w, arg_ids_w[ai_w])
20420+ if new_t_w != cmpt_w[ai_w]
20421+ cmpt_w[ai_w] = new_t_w
20422+ changed_w = 1
20423+ end
20424+ end
20425+ ai_w = ai_w + 1
20426+ end
20427+ if changed_w == 1
20428+ cmpall_w[cmi_w] = cmpt_w.join(",")
20429+ @cls_cmeth_ptypes[ci_w] = cmpall_w.join("|")
20430+ @cls_cmeth_ptypes_version = @cls_cmeth_ptypes_version + 1
20431+ end
20432+ end
20433+ cmi_w = cmi_w + 1
20434+ end
20435+ end
20436+ end
20437+ end
20438+ cs2 = []
20439+ push_child_ids(nid, cs2)
20440+ k = 0
20441+ while k < cs2.length
20442+ widen_callsites_referencing_kv(cs2[k], k_name, k_t, v_name, v_t)
20443+ k = k + 1
20444+ end
20445+ end
20446+
20447+ # Returns the type to use for `arg` in the context of an each
20448+ # block where k/v have hash-derived types. Mirrors infer_type
20449+ # for the LocalVariableReadNode case but pins k/v to their
20450+ # hash-derived types instead of consulting the (empty) var-type
20451+ # table.
20452+ def arg_type_in_each_block(arg, k_name, k_t, v_name, v_t)
20453+ if arg < 0
20454+ return ""
20455+ end
20456+ if @nd_type[arg] == "LocalVariableReadNode"
20457+ n = @nd_name[arg]
20458+ if n == k_name
20459+ return k_t
20460+ end
20461+ if n == v_name
20462+ return v_t
20463+ end
20464+ end
20465+ ""
20466+ end
20467+
20468+ # Hash key type from a variant name. "str_int_hash" -> "string",
20469+ # "sym_int_hash" -> "symbol", etc. Mirrors hash_leaf_type's
20470+ # value-side counterpart.
20471+ def hash_key_type_from_variant(t)
20472+ if is_nullable_type(t) == 1
20473+ t = base_type(t)
20474+ end
20475+ if t == "str_int_hash" || t == "str_str_hash" || t == "str_poly_hash"
20476+ return "string"
20477+ end
20478+ if t == "sym_int_hash" || t == "sym_str_hash" || t == "sym_poly_hash"
20479+ return "symbol"
20480+ end
20481+ if t == "int_str_hash"
20482+ return "int"
20483+ end
20484+ if t == "poly_poly_hash"
20485+ return "poly"
20486+ end
20487+ ""
20488+ end
20489+
2025820490 # Walk the method body looking for `lv_<pname>[k] = v` writes
2025920491 # (CallNode `[]=` with recv = LocalVariableReadNode named pname).
2026020492 # Returns a more-specific hash type than `cur` based on the