Commit d7618c3f

d7618c3f5e89Merged

Ori Pekelman <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): don't commit param to obj_ on container-only body usage

infer_cls_meth_param_from_body picks a user class for a param whose only signal is "what methods does the body call on it". When the called set is e.g. just .length, every built-in container type satisfies it just as well — but the pre-typing committed to the first user class found, locking the param to e.g. obj_<Bag>. Then the real call-site unify (scan_new_calls) saw obj_<Bag> vs int_array and collapsed the param to poly, which propagated through the call chain (e.g. backward → cross_entropy_grad), turning token_ids[i+1] into a poly dispatch and breaking the C compile.

Gate the user-class commit on the called set containing at least one method that is not a built-in-container method. Container methods (length / size / [] / push / each / first / last / map / sort / etc.) on their own are too weak a signal — leave the param at the "int" default so call-site unification decides.

Add regression issue_266_def_length_inference.rb: defining def length on an unrelated user class (Bag) used to flip a Net class's backward/cross_entropy_grad chain to poly when the call site passed an IntArray returned from a top-level def make_ids. With the fix, the program compiles and prints 0.0 (matches CRuby).

Refs #266

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

Details

Commit
d7618c3f5e89
Parent
e94b60bc2cc6

spinel_codegen.rbmodifiedView file

@@ -8983,7 +8983,7 @@
89838983 # them — issue #35.
89848984 called = "".split(",")
89858985 collect_param_methods(bid, pnames[pk], called)
8986- if called.length > 0
8986+ if called.length > 0 && called_methods_only_on_container_builtins(called) == 0
89878987 ci2 = 0
89888988 best = -1
89898989 while ci2 < @cls_names.length
@@ -9022,7 +9022,7 @@
90229022 if ptypes[pk] == "int"
90239023 called = "".split(",")
90249024 collect_param_methods(bid, pnames[pk], called)
9025- if called.length > 0
9025+ if called.length > 0 && called_methods_only_on_container_builtins(called) == 0
90269026 ci2 = 0
90279027 best = -1
90289028 while ci2 < @cls_names.length
@@ -9454,6 +9454,44 @@
94549454 return 1
94559455 end
94569456
9457+ # Issue #266: when picking a user class for a parameter from "what
9458+ # methods does the body call on it", reject sets that consist only
9459+ # of methods also available on built-in container types
9460+ # (int_array / float_array / str_array / sym_array / ptr_array).
9461+ # Otherwise a user class that happens to define `def length` (with
9462+ # nothing else) gets picked for any param the body calls `.length`
9463+ # on — even when the actual call site passes an IntArray. The body
9464+ # signal is too weak to commit; leave the param at "int" so call-
9465+ # site type unification (scan_new_calls) decides.
9466+ def called_methods_only_on_container_builtins(called)
9467+ k = 0
9468+ while k < called.length
9469+ m = called[k]
9470+ if m != "length" && m != "size" && m != "[]" && m != "[]=" &&
9471+ m != "<<" && m != "push" && m != "pop" &&
9472+ m != "shift" && m != "unshift" &&
9473+ m != "first" && m != "last" &&
9474+ m != "each" && m != "each_with_index" && m != "each_index" &&
9475+ m != "map" && m != "map!" && m != "collect" &&
9476+ m != "select" && m != "reject" && m != "filter" &&
9477+ m != "count" && m != "include?" &&
9478+ m != "empty?" && m != "any?" && m != "all?" && m != "none?" &&
9479+ m != "sort" && m != "sort!" && m != "reverse" && m != "reverse!" &&
9480+ m != "join" && m != "to_a" && m != "to_s" && m != "inspect" &&
9481+ m != "find" && m != "find_index" &&
9482+ m != "sum" && m != "min" && m != "max" && m != "minmax" &&
9483+ m != "concat" && m != "flatten" && m != "uniq" && m != "compact" &&
9484+ m != "slice" && m != "fetch" && m != "dig" &&
9485+ m != "freeze" && m != "frozen?" && m != "dup" && m != "clone" &&
9486+ m != "hash" && m != "==" && m != "!=" && m != "eql?" && m != "equal?" &&
9487+ m != "nil?" && m != "is_a?" && m != "kind_of?" && m != "respond_to?"
9488+ return 0
9489+ end
9490+ k = k + 1
9491+ end
9492+ return 1
9493+ end
9494+
94579495 def infer_ivar_types_from_writers
94589496 # Set up main scope for type inference
94599497 push_scope