Commit d054d4cc

d054d4cc3e7eMerged

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

Description

fix(codegen): #302 don't pin param to user class on primitive-shared method

infer_meth_param_from_body (and the cls-method twin) commits a param's type to a user class when the param's body uses methods that collectively appear on that class's surface. The called_methods_only_on_container_builtins gate is supposed to filter out call shapes whose only evidence is methods every built-in container also has — but the list missed methods shared with the String / Hash / Integer scalars.

The Rails-shaped reproducer def find_bracket(s); s.index("["); end with class StringIndexer; def index(...); end defined elsewhere routed through to obj_StringIndexer even when called as find_bracket("hello[world]") — the call-site String type then unified with obj_StringIndexer and the dispatch arm cast the string's bytes pointer through (sp_StringIndexer *).

Extend the whitelist with the canonical String / Hash / Integer shared methods (index / gsub / start_with? / keys / succ / times / etc.) so a def show / def index / def create on a user class — the universal Rails resource controller pattern — no longer poisons primitive-typed callers.

Fixes #302

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

Details

Commit
d054d4cc3e7e
Parent
1a2c223cb598

spinel_codegen.rbmodifiedView file

@@ -10085,7 +10085,33 @@
1008510085 m != "+" && m != "-" && m != "*" && m != "/" && m != "%" &&
1008610086 m != "&" && m != "|" && m != "^" && m != "~" &&
1008710087 m != "<" && m != ">" && m != "<=" && m != ">=" && m != "<=>" &&
10088- m != "===" && m != "!"
10088+ m != "===" && m != "!" &&
10089+ # Issue #302: methods shared across String / Hash / Integer
10090+ # primitive types that the body-side inference must NOT use as
10091+ # evidence of a user-class receiver. The Rails pattern of
10092+ # `def index` / `def show` / `def create` on every controller
10093+ # collides with `String#index` etc., and the canonical caller
10094+ # (`s.index("[")` on a string) was being routed to whichever
10095+ # user-class `index` the inference picked first.
10096+ m != "index" && m != "rindex" && m != "match" && m != "match?" &&
10097+ m != "scan" && m != "sub" && m != "gsub" && m != "tr" &&
10098+ m != "split" && m != "chars" && m != "bytes" && m != "lines" &&
10099+ m != "chomp" && m != "chop" && m != "strip" && m != "lstrip" && m != "rstrip" &&
10100+ m != "upcase" && m != "downcase" && m != "capitalize" && m != "swapcase" &&
10101+ m != "start_with?" && m != "end_with?" &&
10102+ m != "to_i" && m != "to_f" && m != "to_sym" && m != "to_str" &&
10103+ m != "chr" && m != "ord" && m != "bytesize" && m != "=~" &&
10104+ m != "ljust" && m != "rjust" && m != "center" && m != "replace" && m != "clear" &&
10105+ # Hash-shared
10106+ m != "keys" && m != "values" && m != "each_pair" && m != "each_key" && m != "each_value" &&
10107+ m != "has_key?" && m != "has_value?" && m != "key?" && m != "value?" &&
10108+ m != "merge" && m != "merge!" && m != "invert" &&
10109+ m != "transform_keys" && m != "transform_values" && m != "delete" &&
10110+ # Integer-shared (`succ` is also Range/String etc.)
10111+ m != "succ" && m != "next" && m != "pred" && m != "digits" && m != "bit_length" &&
10112+ m != "times" && m != "upto" && m != "downto" && m != "step" &&
10113+ m != "abs" && m != "divmod" && m != "gcd" && m != "lcm" &&
10114+ m != "even?" && m != "odd?" && m != "zero?" && m != "positive?" && m != "negative?"
1008910115 return 0
1009010116 end
1009110117 k = k + 1