Commit 6192c45a

6192c45a2820Merged

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

Description

fix(codegen): empty {} arg widens to body-driven hash variant (#397)

Fixes #397.

def store(receiver); receiver["k"] = "v"; end; store({}) failed C-compile: {} defaulted to str_int_hash, the param widened from the caller to that, and the body's String store emitted sp_StrIntHash_set(..., (const char *)) -- type mismatch into the int slot.

Three coordinated changes:

  1. narrow_param_hash_types_from_body_writes — new iterative-loop pass that scans method bodies for lv_<param>[k] = v writes, unifies the observed key/value types via compose_hash_type, and widens the param's hash variant when the observation is more specific than the current default.

  2. empty_hash_coerce in compile_call_args_with_defaults — when a positional arg is an empty {} literal AND the receiving param is a hash variant, emit the matching sp_<HashType>_new() instead of falling through to compile_hash_literal's default sp_StrIntHash_new().

  3. detect_poly_params carve-out — empty {} literal is compatible with any concrete hash variant, mirroring the existing empty [] carve-out. Without it, the body-widened ct and the call-site at (still str_int_hash) disagree and the param falls back to poly.

The body-driven inference covers str_str / str_poly / sym_int / sym_str / sym_poly / int_str / poly_poly variants symmetric to the existing str_int default. compose_hash_type also flips the right @needs_<hash>_hash flag so the runtime helpers / typedefs get emitted for the widened variant.

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

Details

Commit
6192c45a2820
Parent
621daeea4caf

spinel_analyze.rbmodifiedView file

@@ -13740,6 +13740,19 @@
1374013740 next
1374113741 end
1374213742 end
13743+ # Issue #397: an empty `{}` literal at the call site is
13744+ # compatible with any concrete hash variant the body
13745+ # widened the param to. Without this, the body-widened
13746+ # `ct` and the call-site `at` (still `str_int_hash` from
13747+ # the empty-hash default) disagree and detect_poly_params
13748+ # folds the param back to poly, and the call site ends
13749+ # up boxing the hash through poly dispatch.
13750+ if is_empty_hash_literal(arg_ids[k]) == 1
13751+ if is_hash_type(ct) == 1
13752+ k = k + 1
13753+ next
13754+ end
13755+ end
1374313756 if ct != at
1374413757 if ct != "poly"
1374513758 # Only mark as poly if both types are meaningful
@@ -14390,6 +14403,7 @@
1439014403 infer_ivar_types_from_writers
1439114404 infer_param_array_type_from_body
1439214405 narrow_param_types_from_body_method_calls
14406+ narrow_param_hash_types_from_body_writes
1439314407 detect_poly_params
1439414408 cur_sig = inference_signature
1439514409 if cur_sig == prev_sig
@@ -19490,6 +19504,226 @@
1949019504 # refinement would compute. Codegen reads these from IR to declare
1949119505 # locals without re-running scan_locals or any of its multi-pass
1949219506 # refinement / bigint / lambda-upgrade dependencies.
19507+ # the caller's empty `{}` literal to match the widened param type.
19508+ def narrow_param_hash_types_from_body_writes
19509+ # Top-level methods.
19510+ mi = 0
19511+ while mi < @meth_names.length
19512+ bid_h = @meth_body_ids[mi]
19513+ if bid_h >= 0
19514+ pnames = @meth_param_names[mi].split(",")
19515+ ptypes = @meth_param_types[mi].split(",")
19516+ changed = 0
19517+ pk = 0
19518+ while pk < pnames.length
19519+ if pk < ptypes.length && is_hash_type(ptypes[pk]) == 1
19520+ new_t = infer_param_hash_from_writes(bid_h, pnames[pk], ptypes[pk])
19521+ if new_t != "" && new_t != ptypes[pk]
19522+ ptypes[pk] = new_t
19523+ changed = 1
19524+ end
19525+ end
19526+ pk = pk + 1
19527+ end
19528+ if changed == 1
19529+ @meth_param_types[mi] = ptypes.join(",")
19530+ end
19531+ end
19532+ mi = mi + 1
19533+ end
19534+ # Class instance methods.
19535+ ci = 0
19536+ while ci < @cls_names.length
19537+ all_params = @cls_meth_params[ci].split("|")
19538+ all_ptypes = @cls_meth_ptypes[ci].split("|")
19539+ bodies = @cls_meth_bodies[ci].split(";")
19540+ cls_changed = 0
19541+ mj = 0
19542+ while mj < all_params.length
19543+ bid_c = -1
19544+ if mj < bodies.length
19545+ bid_c = bodies[mj].to_i
19546+ end
19547+ if bid_c >= 0
19548+ cm_pnames = all_params[mj].split(",")
19549+ cm_ptypes = "".split(",")
19550+ if mj < all_ptypes.length
19551+ cm_ptypes = all_ptypes[mj].split(",")
19552+ end
19553+ m_changed = 0
19554+ pk2 = 0
19555+ while pk2 < cm_pnames.length
19556+ if pk2 < cm_ptypes.length && is_hash_type(cm_ptypes[pk2]) == 1
19557+ new_t = infer_param_hash_from_writes(bid_c, cm_pnames[pk2], cm_ptypes[pk2])
19558+ if new_t != "" && new_t != cm_ptypes[pk2]
19559+ cm_ptypes[pk2] = new_t
19560+ m_changed = 1
19561+ end
19562+ end
19563+ pk2 = pk2 + 1
19564+ end
19565+ if m_changed == 1
19566+ all_ptypes[mj] = cm_ptypes.join(",")
19567+ cls_changed = 1
19568+ end
19569+ end
19570+ mj = mj + 1
19571+ end
19572+ if cls_changed == 1
19573+ @cls_meth_ptypes[ci] = all_ptypes.join("|")
19574+ end
19575+ ci = ci + 1
19576+ end
19577+ end
19578+
19579+ # Walk the method body looking for `lv_<pname>[k] = v` writes
19580+ # (CallNode `[]=` with recv = LocalVariableReadNode named pname).
19581+ # Returns a more-specific hash type than `cur` based on the
19582+ # observed key + value types, or "" if there's no widening needed
19583+ # (or if the observed types are inconsistent).
19584+ def infer_param_hash_from_writes(nid, pname, cur)
19585+ if nid < 0
19586+ return ""
19587+ end
19588+ # Track all observed value types via a single string accumulator.
19589+ val_types = "".split(",")
19590+ key_types = "".split(",")
19591+ collect_param_hash_writes(nid, pname, val_types, key_types)
19592+ if val_types.length == 0
19593+ return ""
19594+ end
19595+ # Decide hash variant from the union of observed types. If any
19596+ # observed value type doesn't fit `cur`'s value slot, widen.
19597+ cur_kt = hash_key_part(cur)
19598+ cur_vt = hash_value_part(cur)
19599+ new_vt = unify_hash_value_types(val_types)
19600+ new_kt = unify_hash_key_types(key_types, cur_kt)
19601+ return compose_hash_type(new_kt, new_vt)
19602+ end
19603+
19604+ def collect_param_hash_writes(nid, pname, val_types, key_types)
19605+ if nid < 0
19606+ return
19607+ end
19608+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "[]="
19609+ r = @nd_receiver[nid]
19610+ if r >= 0 && @nd_type[r] == "LocalVariableReadNode" && @nd_name[r] == pname
19611+ args_id_h = @nd_arguments[nid]
19612+ if args_id_h >= 0
19613+ args = get_args(args_id_h)
19614+ if args.length >= 2
19615+ kt = infer_type(args[0])
19616+ vt = infer_type(args[args.length - 1])
19617+ if not_in(kt, key_types) == 1
19618+ key_types.push(kt)
19619+ end
19620+ if not_in(vt, val_types) == 1
19621+ val_types.push(vt)
19622+ end
19623+ end
19624+ end
19625+ end
19626+ end
19627+ cs = []
19628+ push_child_ids(nid, cs)
19629+ k = 0
19630+ while k < cs.length
19631+ collect_param_hash_writes(cs[k], pname, val_types, key_types)
19632+ k = k + 1
19633+ end
19634+ end
19635+
19636+ def hash_key_part(t)
19637+ if t == "str_int_hash" || t == "str_str_hash" || t == "str_poly_hash"
19638+ return "str"
19639+ end
19640+ if t == "sym_int_hash" || t == "sym_str_hash" || t == "sym_poly_hash"
19641+ return "sym"
19642+ end
19643+ if t == "int_str_hash"
19644+ return "int"
19645+ end
19646+ if t == "poly_poly_hash"
19647+ return "poly"
19648+ end
19649+ "str"
19650+ end
19651+
19652+ def hash_value_part(t)
19653+ if t == "str_int_hash" || t == "sym_int_hash"
19654+ return "int"
19655+ end
19656+ if t == "str_str_hash" || t == "sym_str_hash" || t == "int_str_hash"
19657+ return "str"
19658+ end
19659+ if t == "str_poly_hash" || t == "sym_poly_hash" || t == "poly_poly_hash"
19660+ return "poly"
19661+ end
19662+ "int"
19663+ end
19664+
19665+ def unify_hash_value_types(vts)
19666+ if vts.length == 0
19667+ return "int"
19668+ end
19669+ if vts.length == 1
19670+ v = vts[0]
19671+ return "int" if v == "int" || v == "bool" || v == "nil"
19672+ return "str" if v == "string"
19673+ return "poly"
19674+ end
19675+ # Multiple distinct types -> poly.
19676+ "poly"
19677+ end
19678+
19679+ def unify_hash_key_types(kts, cur_kt)
19680+ if kts.length == 0
19681+ return cur_kt
19682+ end
19683+ if kts.length == 1
19684+ k = kts[0]
19685+ return "str" if k == "string"
19686+ return "sym" if k == "symbol"
19687+ return cur_kt
19688+ end
19689+ "poly"
19690+ end
19691+
19692+ # Issue #397 helper: when positional arg `nid` is an empty `{}` and
19693+
19694+ def compose_hash_type(kt, vt)
19695+ if kt == "str" && vt == "int"
19696+ @needs_str_int_hash = 1
19697+ return "str_int_hash"
19698+ end
19699+ if kt == "str" && vt == "str"
19700+ @needs_str_str_hash = 1
19701+ return "str_str_hash"
19702+ end
19703+ if kt == "str" && vt == "poly"
19704+ @needs_str_poly_hash = 1
19705+ return "str_poly_hash"
19706+ end
19707+ if kt == "sym" && vt == "int"
19708+ @needs_sym_int_hash = 1
19709+ return "sym_int_hash"
19710+ end
19711+ if kt == "sym" && vt == "str"
19712+ @needs_sym_str_hash = 1
19713+ return "sym_str_hash"
19714+ end
19715+ if kt == "sym" && vt == "poly"
19716+ @needs_sym_poly_hash = 1
19717+ return "sym_poly_hash"
19718+ end
19719+ if kt == "int" && vt == "str"
19720+ @needs_int_str_hash = 1
19721+ return "int_str_hash"
19722+ end
19723+ @needs_poly_poly_hash = 1
19724+ return "poly_poly_hash"
19725+ end
19726+
1949319727 def precompute_all_scope_decls
1949419728 # ---- Top-level main ----
1949519729 push_scope