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
# them — issue #35. called = "".split(",") collect_param_methods(bid, pnames[pk], called) if called.length > 0 if called.length > 0 && called_methods_only_on_container_builtins(called) == 0 ci2 = 0 best = -1 while ci2 < @cls_names.length if ptypes[pk] == "int" called = "".split(",") collect_param_methods(bid, pnames[pk], called) if called.length > 0 if called.length > 0 && called_methods_only_on_container_builtins(called) == 0 ci2 = 0 best = -1 while ci2 < @cls_names.length return 1 end # Issue #266: when picking a user class for a parameter from "what # methods does the body call on it", reject sets that consist only # of methods also available on built-in container types # (int_array / float_array / str_array / sym_array / ptr_array). # Otherwise a user class that happens to define `def length` (with # nothing else) gets picked for any param the body calls `.length` # on — even when the actual call site passes an IntArray. The body # signal is too weak to commit; leave the param at "int" so call- # site type unification (scan_new_calls) decides. def called_methods_only_on_container_builtins(called) k = 0 while k < called.length m = called[k] if m != "length" && m != "size" && m != "[]" && m != "[]=" && m != "<<" && m != "push" && m != "pop" && m != "shift" && m != "unshift" && m != "first" && m != "last" && m != "each" && m != "each_with_index" && m != "each_index" && m != "map" && m != "map!" && m != "collect" && m != "select" && m != "reject" && m != "filter" && m != "count" && m != "include?" && m != "empty?" && m != "any?" && m != "all?" && m != "none?" && m != "sort" && m != "sort!" && m != "reverse" && m != "reverse!" && m != "join" && m != "to_a" && m != "to_s" && m != "inspect" && m != "find" && m != "find_index" && m != "sum" && m != "min" && m != "max" && m != "minmax" && m != "concat" && m != "flatten" && m != "uniq" && m != "compact" && m != "slice" && m != "fetch" && m != "dig" && m != "freeze" && m != "frozen?" && m != "dup" && m != "clone" && m != "hash" && m != "==" && m != "!=" && m != "eql?" && m != "equal?" && m != "nil?" && m != "is_a?" && m != "kind_of?" && m != "respond_to?" return 0 end k = k + 1 end return 1 end def infer_ivar_types_from_writers # Set up main scope for type inference push_scopetest/issue_266_def_length_inference.rbaddedView file
Loading diff…
test/issue_266_def_length_inference.rb.expectedaddedView file
Loading diff…