Commit 06697296

06697296bb38Merged

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

Description

fix(analyze): LV back-propagation from callee arg slot

Phase 3. Body-derived widening of a callee's param (via narrow_param_hash_types_from_body_writes — task d) widens the callee's slot to a poly variant, but the caller's local variable that holds the value passed in didn't widen. The call site then passes sp_StrIntHash * into a sp_StrPolyHash

  • slot.

Real-blog shape (CgiIo.parse_request / parse_form_into):

def self.parse_request(env, stdin)
  params = {}                       # str_int_hash by default
  parse_form_into(query, params)    # callee's `into` widened
                                     # to str_poly_hash
  ...
end

Fix: refine_method_body_locals grows a Pass 5 that, after the existing 4-pass scan/refine sequence, walks each hash-typed LV and back-propagates the callee's matched slot type if it has widened to a poly variant of the same key family. Mirrors the param-level back-prop (infer_param_type_from_callee_slot hash arm landed in d77ca62) but at the LV scope, so the widening flows: callee body → callee param → caller LV.

Integrating into refine_method_body_locals (rather than a standalone pass) means the widening sticks through subsequent return-type recomputation — refine_method_body_locals is called by infer_all_returns + precompute_all_scope_decls, and the in-place mutation of ltypes affects both.

The pass derives @current_lexical_scope from @current_method_name when running for module-class-method synthetics (Form_cls_run → "Form"), so the sibling-cmeth callee lookup <Scope>_cls_<callee> resolves.

Archive (i450) warnings: 33 → 31 (2 parse_form_into arg 2 warnings cleared, plus the empty {} literal now lowers to sp_StrPolyHash_new() because the LV's expected type is right). Full test suite 419/0/0.

Refs #450

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

Details

Commit
06697296bb38
Parent
53bd7c7aa76a

spinel_analyze.rbmodifiedView file

@@ -11221,6 +11221,137 @@
1122111221 end
1122211222 end
1122311223
11224+ # LV counterpart of infer_param_type_from_callee_slot. Walks
11225+ # each method body's LV table and, for each hash-typed LV
11226+ # forwarded to a callee whose matching slot has widened to a
11227+ # poly variant of the same key family, widens the LV.
11228+ #
11229+ # Real-blog shape:
11230+ #
11231+ # def parse_request(env, stdin)
11232+ # params = {} # initial str_int_hash
11233+ # parse_form_into(query, params) # callee widened to str_poly_hash
11234+ # # via narrow_param_hash_types_from_body_writes
11235+ # ...
11236+ # end
11237+ #
11238+ # Pre-fix `params` stayed str_int_hash and the call passed
11239+ # sp_StrIntHash * into a sp_StrPolyHash * slot, warning. Plus
11240+ # 4 downstream `iv_X = lv_params` cascade warnings. With this
11241+ # pass, params widens up the chain.
11242+ def infer_lv_types_from_callee_arg_slots
11243+ # Top-level + module class methods (@meth_*).
11244+ mi_lv = 0
11245+ while mi_lv < @meth_names.length
11246+ bid_lv = @meth_body_ids[mi_lv]
11247+ if bid_lv >= 0
11248+ sn_lv = @nd_scope_names[bid_lv].split("|", -1)
11249+ st_lv = @nd_scope_types[bid_lv].split("|", -1)
11250+ changed_lv = 0
11251+ saved_scope_lv = @current_lexical_scope
11252+ mname_lv = @meth_names[mi_lv]
11253+ cls_marker_lv = mname_lv.index("_cls_")
11254+ if cls_marker_lv != nil && cls_marker_lv >= 0
11255+ @current_lexical_scope = mname_lv[0, cls_marker_lv]
11256+ end
11257+ lvi = 0
11258+ while lvi < sn_lv.length
11259+ if lvi < st_lv.length && is_hash_type(st_lv[lvi]) == 1 && st_lv[lvi].include?("poly") == false
11260+ obs_lv = "".split(",")
11261+ collect_param_callee_slots(bid_lv, sn_lv[lvi], obs_lv)
11262+ cur_kt_lv = hash_key_part(st_lv[lvi])
11263+ agreed_lv = ""
11264+ disagree_lv = 0
11265+ kk_lv = 0
11266+ while kk_lv < obs_lv.length
11267+ tab_lv = obs_lv[kk_lv].index("\t")
11268+ if tab_lv >= 0
11269+ cn_lv = obs_lv[kk_lv][0, tab_lv]
11270+ pos_lv = obs_lv[kk_lv][tab_lv + 1, obs_lv[kk_lv].length - tab_lv - 1].to_i
11271+ ct_lv = callee_slot_type(cn_lv, pos_lv)
11272+ if is_hash_type(ct_lv) == 1 && ct_lv.include?("poly") && hash_key_part(ct_lv) == cur_kt_lv
11273+ if agreed_lv == ""
11274+ agreed_lv = ct_lv
11275+ elsif agreed_lv != ct_lv
11276+ disagree_lv = 1
11277+ end
11278+ end
11279+ end
11280+ kk_lv = kk_lv + 1
11281+ end
11282+ if agreed_lv != "" && disagree_lv == 0
11283+ st_lv[lvi] = agreed_lv
11284+ changed_lv = 1
11285+ end
11286+ end
11287+ lvi = lvi + 1
11288+ end
11289+ @current_lexical_scope = saved_scope_lv
11290+ if changed_lv == 1
11291+ @nd_scope_types[bid_lv] = st_lv.join("|")
11292+ end
11293+ end
11294+ mi_lv = mi_lv + 1
11295+ end
11296+ # Class instance methods (@cls_meth_*) and real-class cmeths
11297+ # (@cls_cmeth_*) share the @nd_scope_names indirection; the
11298+ # per-(class, cmeth) tables from Task b also need updating.
11299+ # For brevity we update only the per-bid table here; the
11300+ # per-(ci, cmj) Task b tables are populated by a separate
11301+ # pass earlier in the inference loop and use these same scope
11302+ # entries, so the widening propagates on the next iteration.
11303+ ci_lv = 0
11304+ while ci_lv < @cls_names.length
11305+ bodies_lv = @cls_meth_bodies[ci_lv].split(";")
11306+ bj_lv = 0
11307+ while bj_lv < bodies_lv.length
11308+ bid_im = bodies_lv[bj_lv].to_i
11309+ if bid_im >= 0
11310+ sn_im = @nd_scope_names[bid_im].split("|", -1)
11311+ st_im = @nd_scope_types[bid_im].split("|", -1)
11312+ changed_im = 0
11313+ lvii = 0
11314+ while lvii < sn_im.length
11315+ if lvii < st_im.length && is_hash_type(st_im[lvii]) == 1 && st_im[lvii].include?("poly") == false
11316+ obs_im = "".split(",")
11317+ collect_param_callee_slots(bid_im, sn_im[lvii], obs_im)
11318+ cur_kt_im = hash_key_part(st_im[lvii])
11319+ agreed_im = ""
11320+ disagree_im = 0
11321+ kki = 0
11322+ while kki < obs_im.length
11323+ tab_im = obs_im[kki].index("\t")
11324+ if tab_im >= 0
11325+ cn_im = obs_im[kki][0, tab_im]
11326+ pos_im = obs_im[kki][tab_im + 1, obs_im[kki].length - tab_im - 1].to_i
11327+ ct_im = callee_slot_type(cn_im, pos_im)
11328+ if is_hash_type(ct_im) == 1 && ct_im.include?("poly") && hash_key_part(ct_im) == cur_kt_im
11329+ if agreed_im == ""
11330+ agreed_im = ct_im
11331+ elsif agreed_im != ct_im
11332+ disagree_im = 1
11333+ end
11334+ end
11335+ end
11336+ kki = kki + 1
11337+ end
11338+ if agreed_im != "" && disagree_im == 0
11339+ st_im[lvii] = agreed_im
11340+ changed_im = 1
11341+ end
11342+ end
11343+ lvii = lvii + 1
11344+ end
11345+ if changed_im == 1
11346+ @nd_scope_types[bid_im] = st_im.join("|")
11347+ end
11348+ end
11349+ bj_lv = bj_lv + 1
11350+ end
11351+ ci_lv = ci_lv + 1
11352+ end
11353+ end
11354+
1122411355 # Resolve `callee_name` (a bare-call mname or a `<Mod>_cls_<m>`
1122511356 # synthetic) to its param-types array and return the slot at
1122611357 # `pos`, or "" if unresolvable. Used by
@@ -21404,6 +21535,59 @@
2140421535 end
2140521536 j = j + 1
2140621537 end
21538+ # Pass 5: callee-arg-slot back-prop. For each hash-typed LV
21539+ # forwarded to a callee whose matching slot has widened to a
21540+ # poly variant (via narrow_param_hash_types_from_body_writes),
21541+ # widen the caller's LV. Same shape as infer_param_type_from_
21542+ # callee_slot but applied here at the LV layer so the refined
21543+ # type sticks through subsequent refine_method_body_locals
21544+ # invocations (return-type recompute, etc.). Surfaces in real-
21545+ # blog's parse_request -> parse_form_into chain where the
21546+ # caller's `params = {}` literal should widen alongside the
21547+ # callee's body-widened `into : str_poly_hash`.
21548+ saved_scope_p5 = @current_lexical_scope
21549+ # Derive the lexical scope from @current_method_name (set by
21550+ # the caller, typically precompute_all_scope_decls) so the
21551+ # sibling-cmeth synth lookup `<Scope>_cls_<callee>` resolves.
21552+ if @current_method_name != ""
21553+ cls_marker_p5 = @current_method_name.index("_cls_")
21554+ if cls_marker_p5 != nil && cls_marker_p5 >= 0
21555+ @current_lexical_scope = @current_method_name[0, cls_marker_p5]
21556+ end
21557+ end
21558+ j = 0
21559+ while j < lnames.length
21560+ if is_hash_type(ltypes[j]) == 1 && ltypes[j].include?("poly") == false
21561+ obs_p5 = "".split(",")
21562+ collect_param_callee_slots(bid, lnames[j], obs_p5)
21563+ cur_kt_p5 = hash_key_part(ltypes[j])
21564+ agreed_p5 = ""
21565+ disagree_p5 = 0
21566+ kk_p5 = 0
21567+ while kk_p5 < obs_p5.length
21568+ tab_p5 = obs_p5[kk_p5].index("\t")
21569+ if tab_p5 >= 0
21570+ cn_p5 = obs_p5[kk_p5][0, tab_p5]
21571+ pos_p5 = obs_p5[kk_p5][tab_p5 + 1, obs_p5[kk_p5].length - tab_p5 - 1].to_i
21572+ ct_p5 = callee_slot_type(cn_p5, pos_p5)
21573+ if is_hash_type(ct_p5) == 1 && ct_p5.include?("poly") && hash_key_part(ct_p5) == cur_kt_p5
21574+ if agreed_p5 == ""
21575+ agreed_p5 = ct_p5
21576+ elsif agreed_p5 != ct_p5
21577+ disagree_p5 = 1
21578+ end
21579+ end
21580+ end
21581+ kk_p5 = kk_p5 + 1
21582+ end
21583+ if agreed_p5 != "" && disagree_p5 == 0
21584+ ltypes[j] = agreed_p5
21585+ set_var_type(lnames[j], agreed_p5)
21586+ end
21587+ end
21588+ j = j + 1
21589+ end
21590+ @current_lexical_scope = saved_scope_p5
2140721591 end
2140821592
2140921593 # Constant-initializer locals — block params introduced inside