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
end def compile_array_method_expr(nid, mname, rc, recv_type) # Skip non-array types if recv_type == "str_int_hash" || recv_type == "str_str_hash" # Skip non-array types. Without this guard a user class with a # method whose name happens to overlap an Array method (e.g. # `def sample`, `def first`) would be dispatched as that Array # method, with `array_c_prefix` falling back to `IntArray` and # the receiver pointer used as if it were an `sp_IntArray *`. # `is_array_type` deliberately omits *_ptr_array (see its # docstring), so include it explicitly here — Array#length, #[], # #each, … all work on a ptr_array via `sp_PtrArray_*`. if is_array_type(recv_type) == 0 && is_ptr_array_type(recv_type) == 0 return "" end # Array#inspect and Array#to_s (CRuby aliases them for arrays, sotest/array_method_name_clash.rbaddedView file
Loading diff…