Commit b9e10e60
b9e10e60c0f8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Don't let a Struct member name globally type-merge unrelated code
Struct.new(:a, :b) synthesizes int attr_readers whose names join the
whole-program cross-class type fan-out. The unresolved-receiver
fan-out in infer_recv_method_type commits a method's return type when
all same-named definitions across classes "agree", but its divergence
guard only looked at methods (via cls_find_method_direct) and filtered
out int. So a Struct member b (an int attr_reader) was invisible
to the guard, Weight#b's float_array won as the lone candidate, and
an unresolved ctx.b used as an Integer mis-typed the receiving
function's parameter as sp_FloatArray *.
Make the guard collect mname's candidate from the same sources the
fan-out consults (zero-arg method, attr_reader, arg method) and count
int as a real type. The int attr_reader now diverges against the
non-int method, so the fan-out is suppressed and the unresolved recv
stays int. Legitimate same-type fan-out (every class agrees) is
preserved — optcarrot/bootstrap, which rely on it, stay green.
Fixes #1043
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- b9e10e60c0f8
- Parent
- 5f84052665c6
spinel_analyze.rbmodifiedView file
diverged = 0 ci = 0 while ci < @cls_names.length if cls_find_method_direct(ci, mname) >= 0 mr = cls_method_return(ci, mname) if mr != "int" && mr != "" if unique_rt == "" unique_rt = mr elsif unique_rt != mr diverged = 1 ci = @cls_names.length # Collect `mname`'s candidate return type on this class from any # source the fan-out below also consults: a zero-arg method, an # attr_reader, or an arg method. Unlike the old guard, attr_readers # count AND `int` is not pre-filtered -- a Struct member (an int # attr_reader) named the same as another class's non-int method is # a genuine divergence (#1043). With int candidates suppressed the # guard saw only the non-int side, let the fan-out commit it, and # mis-typed an unresolved recv's int use. cand_g = "" gmnames = @cls_meth_names[ci].split(";", -1) gmparams = @cls_meth_params[ci].split("|", -1) gi = 0 while gi < gmnames.length if gmnames[gi] == mname gp = "" if gi < gmparams.length gp = gmparams[gi] end if gp == "" cand_g = cls_method_return(ci, mname) end end gi = gi + 1 end if cand_g == "" && cls_has_attr_reader(ci, mname) == 1 cand_g = cls_ivar_type(ci, "@" + mname) end if cand_g == "" && cls_find_method_direct(ci, mname) >= 0 cand_g = cls_method_return(ci, mname) end if cand_g != "" if unique_rt == "" unique_rt = cand_g elsif unique_rt != cand_g diverged = 1 ci = @cls_names.length end end ci = ci + 1 endtest/struct_member_name_no_global_merge.rbaddedView file
Loading diff…
test/struct_member_name_no_global_merge.rb.expectedaddedView file
Loading diff…