Commit dd5623bb

dd5623bbe116Merged

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

Description

fix(analyze): back-propagate poly-hash callee slot to caller param

Task d (heterogeneous str_*_hash widening): when a method forwards a hash param to a sibling whose matching slot has already widened to a poly variant (via body-write heterogeneity in narrow_param_hash_types_from_body_writes), back-propagate the wider type to the forwarding method's param too. Symmetric extension of the ptr-back-prop case (#453) to hash types.

Real-blog cluster:

def self.parse_form_into(input, into)
  input.split(\"&\").each { |pair| assign_form_pair(into, ...) }
end

def self.assign_form_pair(into, raw_key, val)
  ...
  into[raw_key] = val        # str
  into[outer] = {}           # hash      <- val_types diverge
  ...
end

`assign_form_pair.into` widens to str_poly_hash via the body's heterogeneous `[]=` writes. Pre-fix, `parse_form_into. into` stayed at str_int_hash and the bare call passed an sp_StrIntHash * into the str_poly_hash * slot, warning under -Wincompatible-pointer-types. With the back-prop, the outer param also widens; both arms of the call agree.

Wired into both the @meth_* walk (top-level and module-class synthetics) and the @cls_cmeth_* walk (real-class `def self.X`) that #453 already extended. Switched two .index nil- checks to .include? — spinel's String#index returns -1 for not-found rather than CRuby's nil, so the prior `!= nil` predicate always succeeded under self-hosted build.

Regression test (test/hash_param_widen_from_callee_slot.rb) pins the forwarding chain at the param level. Full callsite LV propagation (params = {} literal in a caller widening through the chain) is a separate LV-typing concern and not addressed here.

Refs #450

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

Details

Commit
dd5623bbe116
Parent
ee464d525a9b

spinel_analyze.rbmodifiedView file

@@ -11028,6 +11028,42 @@
1102811028 changed_cs = 1
1102911029 end
1103011030 end
11031+ # Hash-typed param widening: a body that forwards `pname` to
11032+ # a callee whose matching slot is a poly variant of the same
11033+ # hash family (str_int_hash forwarded to str_poly_hash, etc.)
11034+ # should widen the caller's param so both sides agree on the
11035+ # C struct. Mirrors the ptr-back-prop above for the hash case.
11036+ # parse_form_into(into) -> assign_form_pair(into, ...) where
11037+ # assign_form_pair's `into` widened to str_poly_hash via body
11038+ # heterogeneous `into[k] = val` + `into[k] = {}` writes.
11039+ if pk < ptypes.length && is_hash_type(ptypes[pk]) == 1 && ptypes[pk].include?("poly") == false
11040+ obs_h = "".split(",")
11041+ collect_param_callee_slots(bid, pnames[pk], obs_h)
11042+ cur_kt_h = hash_key_part(ptypes[pk])
11043+ agreed_h = ""
11044+ disagree_h = 0
11045+ kh = 0
11046+ while kh < obs_h.length
11047+ tab_h = obs_h[kh].index("\t")
11048+ if tab_h >= 0
11049+ cn_h = obs_h[kh][0, tab_h]
11050+ pos_h = obs_h[kh][tab_h + 1, obs_h[kh].length - tab_h - 1].to_i
11051+ ct_h = callee_slot_type(cn_h, pos_h)
11052+ if is_hash_type(ct_h) == 1 && ct_h.include?("poly") && hash_key_part(ct_h) == cur_kt_h
11053+ if agreed_h == ""
11054+ agreed_h = ct_h
11055+ elsif agreed_h != ct_h
11056+ disagree_h = 1
11057+ end
11058+ end
11059+ end
11060+ kh = kh + 1
11061+ end
11062+ if agreed_h != "" && disagree_h == 0
11063+ ptypes[pk] = agreed_h
11064+ changed_cs = 1
11065+ end
11066+ end
1103111067 pk = pk + 1
1103211068 end
1103311069 @current_lexical_scope = saved_scope_cs
@@ -11089,6 +11125,38 @@
1108911125 m_changed_cs = 1
1109011126 end
1109111127 end
11128+ # Hash-typed param widening: mirror the @meth_* branch above
11129+ # — when the body forwards `pname` to a callee whose matching
11130+ # slot is a poly variant of the same hash family, widen the
11131+ # caller's param accordingly.
11132+ if pk_cs < ptypes_cm_cs.length && is_hash_type(ptypes_cm_cs[pk_cs]) == 1 && ptypes_cm_cs[pk_cs].include?("poly") == false
11133+ obs_ch = "".split(",")
11134+ collect_param_callee_slots(bid_cm_cs, pnames_cm_cs[pk_cs], obs_ch)
11135+ cur_kt_ch = hash_key_part(ptypes_cm_cs[pk_cs])
11136+ agreed_ch = ""
11137+ disagree_ch = 0
11138+ kch = 0
11139+ while kch < obs_ch.length
11140+ tab_ch = obs_ch[kch].index("\t")
11141+ if tab_ch >= 0
11142+ cn_ch = obs_ch[kch][0, tab_ch]
11143+ pos_ch = obs_ch[kch][tab_ch + 1, obs_ch[kch].length - tab_ch - 1].to_i
11144+ ct_ch = callee_slot_type(cn_ch, pos_ch)
11145+ if is_hash_type(ct_ch) == 1 && ct_ch.include?("poly") && hash_key_part(ct_ch) == cur_kt_ch
11146+ if agreed_ch == ""
11147+ agreed_ch = ct_ch
11148+ elsif agreed_ch != ct_ch
11149+ disagree_ch = 1
11150+ end
11151+ end
11152+ end
11153+ kch = kch + 1
11154+ end
11155+ if agreed_ch != "" && disagree_ch == 0
11156+ ptypes_cm_cs[pk_cs] = agreed_ch
11157+ m_changed_cs = 1
11158+ end
11159+ end
1109211160 pk_cs = pk_cs + 1
1109311161 end
1109411162 @current_lexical_scope = saved_scope_cm_cs