Commit 002de75c

002de75cb713Merged

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

Description

fix(codegen): #305 don't pick user class on int-recv fallback for primitive-shared method

Follow-up to #302: the body-side param-type inference was fixed there, but compile_int_class_fallback_expr at emit time still walked every user class for an int-typed receiver and picked the first match by method name. When a param flowed in as mrb_int (e.g. unpinned upstream), raw_key.index("[") would route to ArticlesController#index, producing a C-level arity mismatch or a silent miscompile.

Refuse the int-to-class cast when mname is a primitive-shared method (String/Array/Hash/Integer) AND any user class defines it. The call falls through to the unresolved-call placeholder — same contract the fallback already uses for genuinely unresolvable calls, vastly better than picking a random user class.

Fixes #305

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

Details

Commit
002de75cb713
Parent
5d3bcbbab6b0

spinel_codegen.rbmodifiedView file

@@ -10387,6 +10387,80 @@
1038710387 # on — even when the actual call site passes an IntArray. The body
1038810388 # signal is too weak to commit; leave the param at "int" so call-
1038910389 # site type unification (scan_new_calls) decides.
10390+ # Issue #305: is `mname` a method that's also defined on a primitive
10391+ # type (String / Array / Hash / Integer) AND on at least one user
10392+ # class? When both are true, an int-typed receiver shouldn't pick
10393+ # the user class on the auto-cast fallback path — that's almost
10394+ # certainly the wrong dispatch (a param that flowed in as mrb_int
10395+ # because no upstream call site pinned it, with the user's actual
10396+ # intent being the primitive method).
10397+ def primitive_method_shared_with_user_class(mname)
10398+ return 0 unless is_primitive_shared_method(mname) == 1
10399+ # Cheap guard: the user must define a class with this method.
10400+ ci2 = 0
10401+ while ci2 < @cls_names.length
10402+ mns = @cls_meth_names[ci2].split(";")
10403+ jj = 0
10404+ while jj < mns.length
10405+ if mns[jj] == mname
10406+ return 1
10407+ end
10408+ jj = jj + 1
10409+ end
10410+ ci2 = ci2 + 1
10411+ end
10412+ 0
10413+ end
10414+
10415+ # Whitelist of methods defined on built-in primitive types that
10416+ # commonly collide with user-class method names. Mirrors (and
10417+ # delegates the surface to) the predicate
10418+ # `called_methods_only_on_container_builtins` introduced for
10419+ # issue #302 — same intent, applied at a different decision site.
10420+ def is_primitive_shared_method(m)
10421+ if m == "length" || m == "size" || m == "[]" || m == "[]=" ||
10422+ m == "<<" || m == "push" || m == "pop" ||
10423+ m == "shift" || m == "unshift" ||
10424+ m == "first" || m == "last" ||
10425+ m == "each" || m == "each_with_index" || m == "each_index" ||
10426+ m == "map" || m == "map!" || m == "collect" ||
10427+ m == "select" || m == "reject" || m == "filter" ||
10428+ m == "count" || m == "include?" ||
10429+ m == "empty?" || m == "any?" || m == "all?" || m == "none?" ||
10430+ m == "sort" || m == "sort!" || m == "reverse" || m == "reverse!" ||
10431+ m == "join" || m == "to_a" || m == "to_s" || m == "inspect" ||
10432+ m == "find" || m == "find_index" ||
10433+ m == "sum" || m == "min" || m == "max" || m == "minmax" ||
10434+ m == "concat" || m == "flatten" || m == "uniq" || m == "compact" ||
10435+ m == "slice" || m == "fetch" || m == "dig" ||
10436+ m == "freeze" || m == "frozen?" || m == "dup" || m == "clone" ||
10437+ m == "hash" || m == "class" || m == "tap" ||
10438+ m == "==" || m == "!=" || m == "eql?" || m == "equal?" ||
10439+ m == "nil?" || m == "is_a?" || m == "kind_of?" || m == "respond_to?" ||
10440+ # String / Hash / Integer-shared (kept in sync with the
10441+ # extension to called_methods_only_on_container_builtins).
10442+ m == "index" || m == "rindex" || m == "match" || m == "match?" ||
10443+ m == "scan" || m == "sub" || m == "gsub" || m == "tr" ||
10444+ m == "split" || m == "chars" || m == "bytes" || m == "lines" ||
10445+ m == "chomp" || m == "chop" || m == "strip" || m == "lstrip" || m == "rstrip" ||
10446+ m == "upcase" || m == "downcase" || m == "capitalize" || m == "swapcase" ||
10447+ m == "start_with?" || m == "end_with?" ||
10448+ m == "to_i" || m == "to_f" || m == "to_sym" || m == "to_str" ||
10449+ m == "chr" || m == "ord" || m == "bytesize" || m == "=~" ||
10450+ m == "ljust" || m == "rjust" || m == "center" || m == "replace" || m == "clear" ||
10451+ m == "keys" || m == "values" || m == "each_pair" || m == "each_key" || m == "each_value" ||
10452+ m == "has_key?" || m == "has_value?" || m == "key?" || m == "value?" ||
10453+ m == "merge" || m == "merge!" || m == "invert" ||
10454+ m == "transform_keys" || m == "transform_values" || m == "delete" ||
10455+ m == "succ" || m == "next" || m == "pred" || m == "digits" || m == "bit_length" ||
10456+ m == "times" || m == "upto" || m == "downto" || m == "step" ||
10457+ m == "abs" || m == "divmod" || m == "gcd" || m == "lcm" ||
10458+ m == "even?" || m == "odd?" || m == "zero?" || m == "positive?" || m == "negative?"
10459+ return 1
10460+ end
10461+ 0
10462+ end
10463+
1039010464 def called_methods_only_on_container_builtins(called)
1039110465 k = 0
1039210466 while k < called.length
@@ -23848,7 +23922,20 @@
2384823922
2384923923 def compile_int_class_fallback_expr(nid, mname, rc, recv_type)
2385023924 # Fallback: if receiver is int (e.g. from IntArray get) but method belongs to a class,
23851- # cast the int to the appropriate class pointer and dispatch
23925+ # cast the int to the appropriate class pointer and dispatch.
23926+ #
23927+ # Issue #305: but NOT when `mname` is a method that's also defined
23928+ # on a primitive type (String, Array, Hash, Integer, etc.). The
23929+ # int-to-class cast here would silently pick the first user class
23930+ # that happens to define the same method name — exactly the
23931+ # `String#index` vs `ArticlesController#index` collision the issue
23932+ # reports. A param typed `mrb_int` because it wasn't pinned upstream
23933+ # is NOT the IntArray-of-pointers shape this fallback was written
23934+ # for; refuse to pick a user class and let compile_call_expr's
23935+ # unresolved-call diagnostic fire instead.
23936+ if recv_type == "int" && primitive_method_shared_with_user_class(mname) == 1
23937+ return ""
23938+ end
2385223939 if recv_type == "int"
2385323940 ci2 = 0
2385423941 while ci2 < @cls_names.length