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
m != "+" && m != "-" && m != "*" && m != "/" && m != "%" && m != "&" && m != "|" && m != "^" && m != "~" && m != "<" && m != ">" && m != "<=" && m != ">=" && m != "<=>" && m != "===" && m != "!" m != "===" && m != "!" && # Issue #302: methods shared across String / Hash / Integer # primitive types that the body-side inference must NOT use as # evidence of a user-class receiver. The Rails pattern of # `def index` / `def show` / `def create` on every controller # collides with `String#index` etc., and the canonical caller # (`s.index("[")` on a string) was being routed to whichever # user-class `index` the inference picked first. m != "index" && m != "rindex" && m != "match" && m != "match?" && m != "scan" && m != "sub" && m != "gsub" && m != "tr" && m != "split" && m != "chars" && m != "bytes" && m != "lines" && m != "chomp" && m != "chop" && m != "strip" && m != "lstrip" && m != "rstrip" && m != "upcase" && m != "downcase" && m != "capitalize" && m != "swapcase" && m != "start_with?" && m != "end_with?" && m != "to_i" && m != "to_f" && m != "to_sym" && m != "to_str" && m != "chr" && m != "ord" && m != "bytesize" && m != "=~" && m != "ljust" && m != "rjust" && m != "center" && m != "replace" && m != "clear" && # Hash-shared m != "keys" && m != "values" && m != "each_pair" && m != "each_key" && m != "each_value" && m != "has_key?" && m != "has_value?" && m != "key?" && m != "value?" && m != "merge" && m != "merge!" && m != "invert" && m != "transform_keys" && m != "transform_values" && m != "delete" && # Integer-shared (`succ` is also Range/String etc.) m != "succ" && m != "next" && m != "pred" && m != "digits" && m != "bit_length" && m != "times" && m != "upto" && m != "downto" && m != "step" && m != "abs" && m != "divmod" && m != "gcd" && m != "lcm" && m != "even?" && m != "odd?" && m != "zero?" && m != "positive?" && m != "negative?" return 0 end k = k + 1test/primitive_method_shadow.rbaddedView file
Loading diff…
test/primitive_method_shadow.rb.expectedaddedView file
Loading diff…