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

@@ -7029,16 +7029,43 @@
70297029 diverged = 0
70307030 ci = 0
70317031 while ci < @cls_names.length
7032- if cls_find_method_direct(ci, mname) >= 0
7033- mr = cls_method_return(ci, mname)
7034- if mr != "int" && mr != ""
7035- if unique_rt == ""
7036- unique_rt = mr
7037- elsif unique_rt != mr
7038- diverged = 1
7039- ci = @cls_names.length
7032+ # Collect `mname`'s candidate return type on this class from any
7033+ # source the fan-out below also consults: a zero-arg method, an
7034+ # attr_reader, or an arg method. Unlike the old guard, attr_readers
7035+ # count AND `int` is not pre-filtered -- a Struct member (an int
7036+ # attr_reader) named the same as another class's non-int method is
7037+ # a genuine divergence (#1043). With int candidates suppressed the
7038+ # guard saw only the non-int side, let the fan-out commit it, and
7039+ # mis-typed an unresolved recv's int use.
7040+ cand_g = ""
7041+ gmnames = @cls_meth_names[ci].split(";", -1)
7042+ gmparams = @cls_meth_params[ci].split("|", -1)
7043+ gi = 0
7044+ while gi < gmnames.length
7045+ if gmnames[gi] == mname
7046+ gp = ""
7047+ if gi < gmparams.length
7048+ gp = gmparams[gi]
7049+ end
7050+ if gp == ""
7051+ cand_g = cls_method_return(ci, mname)
70407052 end
70417053 end
7054+ gi = gi + 1
7055+ end
7056+ if cand_g == "" && cls_has_attr_reader(ci, mname) == 1
7057+ cand_g = cls_ivar_type(ci, "@" + mname)
7058+ end
7059+ if cand_g == "" && cls_find_method_direct(ci, mname) >= 0
7060+ cand_g = cls_method_return(ci, mname)
7061+ end
7062+ if cand_g != ""
7063+ if unique_rt == ""
7064+ unique_rt = cand_g
7065+ elsif unique_rt != cand_g
7066+ diverged = 1
7067+ ci = @cls_names.length
7068+ end
70427069 end
70437070 ci = ci + 1
70447071 end