Commit e9b30d67

e9b30d6719d5Merged

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

Description

fix(analyze): back-propagate typed callee kwarg into untyped caller kwarg

Sam Ruby's #561. When method outer forwards inner(name: lvar) and inner's name: slot is typed (default value, body usage, etc.), outer's matching kwarg should pick up the callee's slot type rather than defaulting to int. Pre-fix the kwarg passthrough left outer's s: at int and the inner call site failed C compile with the canonical int -> const char * mismatch.

New pass infer_param_kwarg_passthrough mirrors the existing infer_param_type_from_callee_slot shape but:

  • collects kwarg-named passthroughs callee(kw: pname) rather than positional callee(pname) patterns
  • looks up the callee's slot by kwarg name (new callee_kwarg_slot_type helper) rather than positional index
  • accepts any concrete callee type (string / int_array / hash variant / etc.) instead of just ptr / obj_

Runs inside the iterative inference loop alongside infer_param_type_from_callee_slot so the fixpoint reaches the right answer after a few iterations even when the callee's own type lands later.

Closes #561

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

Details

Commit
e9b30d6719d5
Parent
0f605284523b

spinel_analyze.rbmodifiedView file

@@ -12523,6 +12523,174 @@
1252312523 # infer_param_type_from_callee_slot to look up evidence from a
1252412524 # callee body without committing to receiver-resolution
1252512525 # (`Db.column_int` vs sibling-call `column_int` lookups).
12526+ # Like callee_slot_type but matches by kwarg name rather
12527+ # than positional index. Returns the inferred type of the
12528+ # kwarg-named slot or "" when callee isn't found / has no
12529+ # matching kwarg. Used by infer_param_kwarg_passthrough to
12530+ # back-propagate a typed callee kwarg into an untyped caller
12531+ # kwarg. Issue #561.
12532+ def callee_kwarg_slot_type(callee_name, kwarg_name)
12533+ cmi = find_method_idx(callee_name)
12534+ if cmi >= 0
12535+ cpnames = @meth_param_names[cmi].split(",")
12536+ cptypes = @meth_param_types[cmi].split(",")
12537+ pi = 0
12538+ while pi < cpnames.length
12539+ if cpnames[pi] == kwarg_name && pi < cptypes.length
12540+ return cptypes[pi]
12541+ end
12542+ pi = pi + 1
12543+ end
12544+ end
12545+ if @current_lexical_scope != ""
12546+ synth = @current_lexical_scope + "_cls_" + callee_name
12547+ smi = find_method_idx(synth)
12548+ if smi >= 0
12549+ spnames = @meth_param_names[smi].split(",")
12550+ sptypes = @meth_param_types[smi].split(",")
12551+ pi = 0
12552+ while pi < spnames.length
12553+ if spnames[pi] == kwarg_name && pi < sptypes.length
12554+ return sptypes[pi]
12555+ end
12556+ pi = pi + 1
12557+ end
12558+ end
12559+ sci = find_class_idx(@current_lexical_scope)
12560+ if sci >= 0
12561+ sc_cmnames = @cls_cmeth_names[sci].split(";")
12562+ smj = 0
12563+ while smj < sc_cmnames.length
12564+ if sc_cmnames[smj] == callee_name
12565+ sc_pnames = cls_cmeth_pnames_get(sci, smj)
12566+ sc_ptypes = cls_cmeth_ptypes_get(sci, smj)
12567+ pi = 0
12568+ while pi < sc_pnames.length
12569+ if sc_pnames[pi] == kwarg_name && pi < sc_ptypes.length
12570+ return sc_ptypes[pi]
12571+ end
12572+ pi = pi + 1
12573+ end
12574+ end
12575+ smj = smj + 1
12576+ end
12577+ end
12578+ end
12579+ ""
12580+ end
12581+
12582+ # Walk `nid` collecting `callee(name: <lvar pname>)` kwarg
12583+ # passthroughs. `acc` receives `<callee_name>\t<kwarg_name>`
12584+ # records for each match.
12585+ def collect_param_kwarg_passthroughs(nid, pname, acc)
12586+ if nid < 0
12587+ return
12588+ end
12589+ if @nd_type[nid] == "DefNode"
12590+ return
12591+ end
12592+ if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode"
12593+ return
12594+ end
12595+ if @nd_type[nid] == "CallNode"
12596+ args_id = @nd_arguments[nid]
12597+ if args_id >= 0
12598+ aa = get_args(args_id)
12599+ ak = 0
12600+ while ak < aa.length
12601+ aid = aa[ak]
12602+ if @nd_type[aid] == "KeywordHashNode"
12603+ elems = parse_id_list(@nd_elements[aid])
12604+ ek = 0
12605+ while ek < elems.length
12606+ if @nd_type[elems[ek]] == "AssocNode"
12607+ kid = @nd_key[elems[ek]]
12608+ vid = @nd_expression[elems[ek]]
12609+ if kid >= 0 && vid >= 0 && @nd_type[kid] == "SymbolNode" && @nd_type[vid] == "LocalVariableReadNode" && @nd_name[vid] == pname
12610+ kname = @nd_content[kid]
12611+ if kname == ""
12612+ kname = @nd_name[kid]
12613+ end
12614+ acc.push(@nd_name[nid] + "\t" + kname)
12615+ end
12616+ end
12617+ ek = ek + 1
12618+ end
12619+ end
12620+ ak = ak + 1
12621+ end
12622+ end
12623+ end
12624+ cs = []
12625+ push_child_ids(nid, cs)
12626+ k = 0
12627+ while k < cs.length
12628+ collect_param_kwarg_passthroughs(cs[k], pname, acc)
12629+ k = k + 1
12630+ end
12631+ end
12632+
12633+ # Back-propagate kwarg passthrough types from typed callees
12634+ # to untyped callers. When a method's int/nil-defaulted param
12635+ # is forwarded via `callee(name: pname)` and the callee's
12636+ # `name:` slot has a concrete type, pin the caller's param to
12637+ # that type. Same shape as infer_param_type_from_callee_slot
12638+ # but keyed on kwarg name instead of positional index, and
12639+ # accepts any concrete type (not just ptr / obj). Issue #561.
12640+ def infer_param_kwarg_passthrough
12641+ mi = 0
12642+ while mi < @meth_names.length
12643+ bid = @meth_body_ids[mi]
12644+ if bid >= 0
12645+ pnames = @meth_param_names[mi].split(",")
12646+ ptypes = @meth_param_types[mi].split(",")
12647+ changed = 0
12648+ saved_scope = @current_lexical_scope
12649+ mname_ks = @meth_names[mi]
12650+ cls_marker_ks = mname_ks.index("_cls_")
12651+ if cls_marker_ks != nil
12652+ @current_lexical_scope = mname_ks[0, cls_marker_ks]
12653+ end
12654+ pk = 0
12655+ while pk < pnames.length
12656+ if pk < ptypes.length && (ptypes[pk] == "int" || ptypes[pk] == "nil")
12657+ obs = "".split(",")
12658+ collect_param_kwarg_passthroughs(bid, pnames[pk], obs)
12659+ agreed = ""
12660+ disagree = 0
12661+ kk = 0
12662+ while kk < obs.length
12663+ tab = obs[kk].index("\t")
12664+ if tab >= 0
12665+ callee_name = obs[kk][0, tab]
12666+ kw_name = obs[kk][tab + 1, obs[kk].length - tab - 1]
12667+ callee_t = callee_kwarg_slot_type(callee_name, kw_name)
12668+ if callee_t != "" && callee_t != "int" && callee_t != "nil"
12669+ if agreed == ""
12670+ agreed = callee_t
12671+ elsif agreed != callee_t
12672+ disagree = 1
12673+ end
12674+ end
12675+ end
12676+ kk = kk + 1
12677+ end
12678+ if agreed != "" && disagree == 0
12679+ ptypes[pk] = agreed
12680+ changed = 1
12681+ end
12682+ end
12683+ pk = pk + 1
12684+ end
12685+ if changed == 1
12686+ @meth_param_types[mi] = ptypes.join(",")
12687+ end
12688+ @current_lexical_scope = saved_scope
12689+ end
12690+ mi = mi + 1
12691+ end
12692+ end
12693+
1252612694 def callee_slot_type(callee_name, pos)
1252712695 cmi = find_method_idx(callee_name)
1252812696 if cmi >= 0
@@ -18372,6 +18540,10 @@
1837218540 widen_nil_default_params_used_as_hash
1837318541 widen_params_from_ivar_hash_aset
1837418542 infer_param_type_from_callee_slot
18543+ # Kwarg back-propagation: int/nil-defaulted kwarg params
18544+ # that pass through to a typed callee kwarg (same name)
18545+ # pick up the callee's slot type. Issue #561.
18546+ infer_param_kwarg_passthrough
1837518547 narrow_param_hash_types_from_body_writes
1837618548 # propagate hash-each block-arg types into
1837718549 # nested cmeth/method-call param widening. Runs inside