Commit 4099f6bc

4099f6bc25f0Merged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): skip Array method dispatch on non-array receivers

compile_array_method_expr only skipped two specific hash types and otherwise ran the Array path for any receiver. A user class with a method whose name overlaps an Array method (sample, first, etc.) was dispatched as the Array variant, with array_c_prefix falling back to IntArray, so the receiver pointer was used as if it were an sp_IntArray *:

class Mixer def sample; 42; end end puts Mixer.new.sample

Expected: 42.

Actual on master:

/tmp/_arr.c: In function 'main': /tmp/_arr.c:39:83: error: passing argument 1 of 'sp_IntArray_length' from incompatible pointer type [-Wincompatible-pointer-types] 39 | printf("%lld\n", (long long)sp_IntArray_get(lv_m, rand() % sp_IntArray_length(lv_m))); | ^~~~ | sp_Mixer * {aka struct sp_Mixer_s *}

Replace the narrow hash-only skip with a positive is_array_type check; on anything else the dispatcher returns "" and the call falls through to the user-class lookup, which finds Mixer#sample and emits sp_Mixer_sample(m) as expected.

Adds test/array_method_name_clash.rb.

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

Details

Commit
4099f6bc25f0
Parent
0c3c5d918fc5

spinel_codegen.rbmodifiedView file

@@ -14907,8 +14907,15 @@
1490714907 end
1490814908
1490914909 def compile_array_method_expr(nid, mname, rc, recv_type)
14910- # Skip non-array types
14911- if recv_type == "str_int_hash" || recv_type == "str_str_hash"
14910+ # Skip non-array types. Without this guard a user class with a
14911+ # method whose name happens to overlap an Array method (e.g.
14912+ # `def sample`, `def first`) would be dispatched as that Array
14913+ # method, with `array_c_prefix` falling back to `IntArray` and
14914+ # the receiver pointer used as if it were an `sp_IntArray *`.
14915+ # `is_array_type` deliberately omits *_ptr_array (see its
14916+ # docstring), so include it explicitly here — Array#length, #[],
14917+ # #each, … all work on a ptr_array via `sp_PtrArray_*`.
14918+ if is_array_type(recv_type) == 0 && is_ptr_array_type(recv_type) == 0
1491214919 return ""
1491314920 end
1491414921 # Array#inspect and Array#to_s (CRuby aliases them for arrays, so