Commit f21643c8

f21643c8b300Merged

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

Description

fix(codegen): require all method calls on a param to fit the inferred class

Closes #35.

infer_cls_meth_param_from_body locked in the first class whose surface matched a SINGLE reader on a parameter, ignoring all other method calls. With

def move_to(other)
  other.block = Block.new(other.x)
end

and Block defined before Cell, other was inferred as Block (because Block has #x), and the generated C tried to assign to other->block on a struct that has no such field. Swapping the class definitions made the bug invisible: Cell got picked first and happened to satisfy both accesses.

Add collect_param_methods to gather every CallNode whose receiver is the named param, and class_has_method/class_has_all_methods to test the candidate class's readers, writers (with = suffix), and instance methods (walking parents for inheritance). The inference loop now picks the first class that contains every called method, not the first with any single match. The same fix is applied to the top-level method-param inference branch directly below.

Bootstrap fixed-point clean; 95 tests + 55 benches green.

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

Details

Commit
f21643c8b300
Parent
cc9752a1353b

spinel_codegen.rbmodifiedView file

@@ -5100,49 +5100,28 @@
51005100 while pk < pnames.length
51015101 if pk < ptypes.length
51025102 if ptypes[pk] == "int"
5103- # Check against ALL classes' readers and zero-arg methods
5104- ci2 = 0
5105- found_class = 0
5106- while ci2 < @cls_names.length
5107- if found_class == 0
5108- readers = @cls_attr_readers[ci2].split(";")
5109- if readers.length > 0
5110- if param_calls_reader(bid, pnames[pk], readers) == 1
5111- found_class = 1
5112- end
5103+ # Pick the class whose surface (readers + writers +
5104+ # methods, walked through parents) contains every
5105+ # method actually called on this param. The old
5106+ # algorithm matched on a single reader and ignored
5107+ # later accesses, picking a class that didn't satisfy
5108+ # them — issue #35.
5109+ called = "".split(",")
5110+ collect_param_methods(bid, pnames[pk], called)
5111+ if called.length > 0
5112+ ci2 = 0
5113+ best = -1
5114+ while ci2 < @cls_names.length
5115+ if best < 0 && class_has_all_methods(ci2, called) == 1
5116+ best = ci2
51135117 end
5114- # Also check zero-arg class methods as readers
5115- if found_class == 0
5116- ci2_mnames = @cls_meth_names[ci2].split(";")
5117- ci2_mparams = @cls_meth_params[ci2].split("|")
5118- zero_arg_meths = "".split(",")
5119- mi2 = 0
5120- while mi2 < ci2_mnames.length
5121- mn2 = ci2_mnames[mi2]
5122- if mn2 != "initialize"
5123- mp2 = ""
5124- if mi2 < ci2_mparams.length
5125- mp2 = ci2_mparams[mi2]
5126- end
5127- if mp2 == ""
5128- zero_arg_meths.push(mn2)
5129- end
5130- end
5131- mi2 = mi2 + 1
5132- end
5133- if zero_arg_meths.length > 0
5134- if param_calls_reader(bid, pnames[pk], zero_arg_meths) == 1
5135- found_class = 1
5136- end
5137- end
5138- end
5139- if found_class == 1
5140- ptypes[pk] = "obj_" + @cls_names[ci2]
5141- all_ptypes[j] = ptypes.join(",")
5142- @cls_meth_ptypes[oci] = all_ptypes.join("|")
5143- end
5118+ ci2 = ci2 + 1
5119+ end
5120+ if best >= 0
5121+ ptypes[pk] = "obj_" + @cls_names[best]
5122+ all_ptypes[j] = ptypes.join(",")
5123+ @cls_meth_ptypes[oci] = all_ptypes.join("|")
51445124 end
5145- ci2 = ci2 + 1
51465125 end
51475126 end
51485127 end
@@ -5154,7 +5133,8 @@
51545133 end
51555134 oci = oci + 1
51565135 end
5157- # Also infer top-level method param types from body usage
5136+ # Also infer top-level method param types from body usage. Same
5137+ # all-methods-must-match rule as the cls_meth_param branch above.
51585138 mi = 0
51595139 while mi < @meth_names.length
51605140 bid = @meth_body_ids[mi]
@@ -5165,46 +5145,21 @@
51655145 while pk < pnames.length
51665146 if pk < ptypes.length
51675147 if ptypes[pk] == "int"
5168- ci2 = 0
5169- found_class = 0
5170- while ci2 < @cls_names.length
5171- if found_class == 0
5172- readers = @cls_attr_readers[ci2].split(";")
5173- if readers.length > 0
5174- if param_calls_reader(bid, pnames[pk], readers) == 1
5175- found_class = 1
5176- end
5177- end
5178- if found_class == 0
5179- ci2_mnames = @cls_meth_names[ci2].split(";")
5180- ci2_mparams = @cls_meth_params[ci2].split("|")
5181- zero_arg_meths = "".split(",")
5182- mi2 = 0
5183- while mi2 < ci2_mnames.length
5184- mn2 = ci2_mnames[mi2]
5185- if mn2 != "initialize"
5186- mp2 = ""
5187- if mi2 < ci2_mparams.length
5188- mp2 = ci2_mparams[mi2]
5189- end
5190- if mp2 == ""
5191- zero_arg_meths.push(mn2)
5192- end
5193- end
5194- mi2 = mi2 + 1
5195- end
5196- if zero_arg_meths.length > 0
5197- if param_calls_reader(bid, pnames[pk], zero_arg_meths) == 1
5198- found_class = 1
5199- end
5200- end
5201- end
5202- if found_class == 1
5203- ptypes[pk] = "obj_" + @cls_names[ci2]
5204- @meth_param_types[mi] = ptypes.join(",")
5148+ called = "".split(",")
5149+ collect_param_methods(bid, pnames[pk], called)
5150+ if called.length > 0
5151+ ci2 = 0
5152+ best = -1
5153+ while ci2 < @cls_names.length
5154+ if best < 0 && class_has_all_methods(ci2, called) == 1
5155+ best = ci2
52055156 end
5157+ ci2 = ci2 + 1
52065158 end
5207- ci2 = ci2 + 1
5159+ if best >= 0
5160+ ptypes[pk] = "obj_" + @cls_names[best]
5161+ @meth_param_types[mi] = ptypes.join(",")
5162+ end
52085163 end
52095164 end
52105165 end
@@ -5286,6 +5241,98 @@
52865241 0
52875242 end
52885243
5244+ # Collect every method name called on `pname` anywhere under nid.
5245+ # Used by parameter type inference to find the class that satisfies
5246+ # ALL accesses (vs param_calls_reader's any-match shortcut, which
5247+ # picked the first class with a single matching reader and ignored
5248+ # later accesses, leading to issue #35).
5249+ def collect_param_methods(nid, pname, acc)
5250+ if nid < 0
5251+ return
5252+ end
5253+ if @nd_type[nid] == "CallNode"
5254+ recv = @nd_receiver[nid]
5255+ if recv >= 0
5256+ if @nd_type[recv] == "LocalVariableReadNode"
5257+ if @nd_name[recv] == pname
5258+ mname = @nd_name[nid]
5259+ if not_in(mname, acc) == 1
5260+ acc.push(mname)
5261+ end
5262+ end
5263+ end
5264+ end
5265+ end
5266+ if @nd_body[nid] >= 0
5267+ collect_param_methods(@nd_body[nid], pname, acc)
5268+ end
5269+ stmts = parse_id_list(@nd_stmts[nid])
5270+ k = 0
5271+ while k < stmts.length
5272+ collect_param_methods(stmts[k], pname, acc)
5273+ k = k + 1
5274+ end
5275+ if @nd_expression[nid] >= 0
5276+ collect_param_methods(@nd_expression[nid], pname, acc)
5277+ end
5278+ if @nd_left[nid] >= 0
5279+ collect_param_methods(@nd_left[nid], pname, acc)
5280+ end
5281+ if @nd_right[nid] >= 0
5282+ collect_param_methods(@nd_right[nid], pname, acc)
5283+ end
5284+ if @nd_arguments[nid] >= 0
5285+ collect_param_methods(@nd_arguments[nid], pname, acc)
5286+ end
5287+ args = parse_id_list(@nd_args[nid])
5288+ k = 0
5289+ while k < args.length
5290+ collect_param_methods(args[k], pname, acc)
5291+ k = k + 1
5292+ end
5293+ if @nd_receiver[nid] >= 0
5294+ collect_param_methods(@nd_receiver[nid], pname, acc)
5295+ end
5296+ end
5297+
5298+ # Does class `ci` provide `mname` as a reader, writer, or method?
5299+ # Walks parent classes for inherited members.
5300+ def class_has_method(ci, mname)
5301+ readers = @cls_attr_readers[ci].split(";")
5302+ if not_in(mname, readers) == 0
5303+ return 1
5304+ end
5305+ if mname.length > 1 && mname[mname.length - 1] == "="
5306+ bname = mname[0, mname.length - 1]
5307+ writers = @cls_attr_writers[ci].split(";")
5308+ if not_in(bname, writers) == 0
5309+ return 1
5310+ end
5311+ end
5312+ mnames = @cls_meth_names[ci].split(";")
5313+ if not_in(mname, mnames) == 0
5314+ return 1
5315+ end
5316+ if @cls_parents[ci] != ""
5317+ pi = find_class_idx(@cls_parents[ci])
5318+ if pi >= 0
5319+ return class_has_method(pi, mname)
5320+ end
5321+ end
5322+ return 0
5323+ end
5324+
5325+ def class_has_all_methods(ci, called)
5326+ k = 0
5327+ while k < called.length
5328+ if class_has_method(ci, called[k]) == 0
5329+ return 0
5330+ end
5331+ k = k + 1
5332+ end
5333+ return 1
5334+ end
5335+
52895336 def infer_ivar_types_from_writers
52905337 # Set up main scope for type inference
52915338 push_scope