Commit 6c2466b5

6c2466b5ec9bMerged

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

Description

fix(analyze): same-named attr_accessor on unrelated classes no longer widens reader to poly

When a method param's body called .payload and two unrelated user classes (Bag, Wire) each had attr_accessor :payload with different ivar types (Hash vs String), the param's body-driven type inference picked the FIRST class whose surface contained the called method. For handle_wire(w) where w.payload was called, this stamped obj_Bag onto handle_wire's w slot. The next pass unified the actual call site arg (obj_Wire from handle_wire(_w)) against obj_Bag and collapsed to poly. The poly-typed param then made w.payload resolve through the poly path, which the downstream first_byte_of(<poly>) couldn't lower for .bytes.

Two arms in infer_cls_meth_param_from_body had a "first class wins" rule. Tighten both: count matching classes and only commit a concrete obj_<C> ptype when exactly one class has the called surface; otherwise leave the param at its prior type and let call-site unify_call_types pin it.

Also tighten the cross-class fan-out in infer_method_return's int-recv arm (same shape — pre-fix returned the first class's attr_reader type, now requires every candidate's type to agree before returning anything).

Tests: default 457/0/0, promote 457/0/0, optcarrot checksum 59662.

Closes #684

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
6c2466b5ec9b
Parent
8a566c535e15

spinel_analyze.rbmodifiedView file

@@ -5893,6 +5893,15 @@
58935893 end
58945894 end
58955895 if rt == "int" && recv_is_unresolved_local == 0
5896+ # Cross-class fan-out: when an int (unresolved) recv calls
5897+ # `mname`, multiple classes may define the same accessor /
5898+ # method. Pre-fix #684 returned the FIRST match, which picked
5899+ # an unrelated class's attr_reader type and widened the call's
5900+ # static type to a wrong shape (Bag#payload=Hash being returned
5901+ # for what is actually Wire#payload=String). Collect candidate
5902+ # return types across all classes; only commit when they agree.
5903+ cand_rt_cross = ""
5904+ cand_div_cross = 0
58965905 ci = 0
58975906 while ci < @cls_names.length
58985907 # Check zero-arg methods (getters)
@@ -5906,10 +5915,13 @@
59065915 mp2 = ci2_mparams[mi2]
59075916 end
59085917 if mp2 == ""
5909- # Found zero-arg method match
5910- mr = cls_method_return(ci, mname)
5911- if mr != "int"
5912- return mr
5918+ mr_cross = cls_method_return(ci, mname)
5919+ if mr_cross != "int" && mr_cross != ""
5920+ if cand_rt_cross == ""
5921+ cand_rt_cross = mr_cross
5922+ elsif cand_rt_cross != mr_cross
5923+ cand_div_cross = 1
5924+ end
59135925 end
59145926 end
59155927 end
@@ -5918,20 +5930,31 @@
59185930 # Check attr_readers (walks parent chain — issue #508).
59195931 if cls_has_attr_reader(ci, mname) == 1
59205932 ivt = cls_ivar_type(ci, "@" + mname)
5921- if ivt != "int"
5922- return ivt
5933+ if ivt != "int" && ivt != ""
5934+ if cand_rt_cross == ""
5935+ cand_rt_cross = ivt
5936+ elsif cand_rt_cross != ivt
5937+ cand_div_cross = 1
5938+ end
59235939 end
59245940 end
59255941 # Check methods with args
59265942 midx = cls_find_method_direct(ci, mname)
59275943 if midx >= 0
5928- mr = cls_method_return(ci, mname)
5929- if mr != "int"
5930- return mr
5944+ mr_arg_cross = cls_method_return(ci, mname)
5945+ if mr_arg_cross != "int" && mr_arg_cross != ""
5946+ if cand_rt_cross == ""
5947+ cand_rt_cross = mr_arg_cross
5948+ elsif cand_rt_cross != mr_arg_cross
5949+ cand_div_cross = 1
5950+ end
59315951 end
59325952 end
59335953 ci = ci + 1
59345954 end
5955+ if cand_div_cross == 0 && cand_rt_cross != ""
5956+ return cand_rt_cross
5957+ end
59355958 end
59365959 if is_obj_type(rt) == 1
59375960 bt_rt = base_type(rt)
@@ -12876,15 +12899,22 @@
1287612899 ptypes[pk] = default_obj_t
1287712900 cls_meth_ptypes_put(oci, j, ptypes)
1287812901 else
12902+ # See the top-level-method twin below: commit only when
12903+ # exactly one class has the called surface. Multi-match
12904+ # ambiguity (same-named attr_accessor on unrelated
12905+ # classes) used to silently pick the first hit, mis-typing
12906+ # the param and cascading to poly downstream. Issue #684.
1287912907 ci2 = 0
12908+ match_count_cm = 0
1288012909 best = -1
1288112910 while ci2 < @cls_names.length
12882- if best < 0 && class_has_all_methods(ci2, called) == 1
12911+ if class_has_all_methods(ci2, called) == 1
12912+ match_count_cm = match_count_cm + 1
1288312913 best = ci2
1288412914 end
1288512915 ci2 = ci2 + 1
1288612916 end
12887- if best >= 0
12917+ if best >= 0 && match_count_cm == 1
1288812918 ptypes[pk] = "obj_" + @cls_names[best]
1288912919 cls_meth_ptypes_put(oci, j, ptypes)
1289012920 end
@@ -12940,15 +12970,23 @@
1294012970 ptypes[pk] = default_obj_t
1294112971 @meth_param_types[mi] = ptypes.join(",")
1294212972 else
12973+ # Collect EVERY user class that has all the methods called on
12974+ # this param; commit only when the set has a single member.
12975+ # The pre-fix "first class wins" rule picked an arbitrary
12976+ # class when multiple shared the same attr name, mis-typing
12977+ # the param and polluting downstream call-site widening
12978+ # (#684).
1294312979 ci2 = 0
12980+ match_count_param = 0
1294412981 best = -1
1294512982 while ci2 < @cls_names.length
12946- if best < 0 && class_has_all_methods(ci2, called) == 1
12983+ if class_has_all_methods(ci2, called) == 1
12984+ match_count_param = match_count_param + 1
1294712985 best = ci2
1294812986 end
1294912987 ci2 = ci2 + 1
1295012988 end
12951- if best >= 0
12989+ if best >= 0 && match_count_param == 1
1295212990 ptypes[pk] = "obj_" + @cls_names[best]
1295312991 @meth_param_types[mi] = ptypes.join(",")
1295412992 end
@@ -16991,15 +17029,45 @@
1699117029 at_p = infer_type(arg_ids_p[0])
1699217030 if at_p != "int" && at_p != "nil"
1699317031 iname_p = "@" + bname
17032+ # Narrow the fan-out: when the receiver has a closed
17033+ # observed-class set, restrict the writer-widening to
17034+ # those classes only. Without this guard, every class
17035+ # that happens to define the same attr_writer (e.g.
17036+ # unrelated `Bag#payload=` and `Wire#payload=`) gets
17037+ # its ivar widened to the call-site arg's type, then
17038+ # `widen_uninit_attr_ivars_to_poly` cascades that to
17039+ # poly across the program. Issue #684.
17040+ obs_ids_pw = observed_class_ids_for_recv(@nd_receiver[nid], @current_class_idx)
17041+ obs_filter_pw = (obs_ids_pw != "")
17042+ obs_list_pw = "".split(",")
17043+ if obs_filter_pw
17044+ obs_list_pw = obs_ids_pw.split(",")
17045+ end
1699417046 ci_p = 0
1699517047 while ci_p < @cls_names.length
16996- writers_p = @cls_attr_writers[ci_p].split(";")
16997- wk_p = 0
16998- while wk_p < writers_p.length
16999- if writers_p[wk_p] == bname
17000- update_ivar_type(ci_p, iname_p, at_p)
17048+ skip_pw = 0
17049+ if obs_filter_pw
17050+ in_set_pw = 0
17051+ ol_pw = 0
17052+ while ol_pw < obs_list_pw.length
17053+ if obs_list_pw[ol_pw].to_i == ci_p
17054+ in_set_pw = 1
17055+ end
17056+ ol_pw = ol_pw + 1
17057+ end
17058+ if in_set_pw == 0
17059+ skip_pw = 1
17060+ end
17061+ end
17062+ if skip_pw == 0
17063+ writers_p = @cls_attr_writers[ci_p].split(";")
17064+ wk_p = 0
17065+ while wk_p < writers_p.length
17066+ if writers_p[wk_p] == bname
17067+ update_ivar_type(ci_p, iname_p, at_p)
17068+ end
17069+ wk_p = wk_p + 1
1700117070 end
17002- wk_p = wk_p + 1
1700317071 end
1700417072 ci_p = ci_p + 1
1700517073 end