Commit b82d6794
b82d6794017fMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
codegen: typed Array#include? with wrong-type arg returns false
`[1,2,3].include?("hello")` used to be a C-compile error because the arg's char* didn't match the int parameter. Pre-check the arg's static type and short-circuit to FALSE for incompatible combinations (CRuby's behaviour for cross-type include?).
sym_array shares the int_array dispatch (sym ids stored as ints), so allow "symbol" args on that receiver.
Fixes #911
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- b82d6794017f
- Parent
- dd01169db6f9
spinel_codegen.rbmodifiedView file
@@ -21159,6 +21159,23 @@
2115921159
return "sp_IntArray_empty(" + rc + ")"2116021160
end2116121161
if mname == "include?"21162+
# Issue #911: cross-type include? returns false (CRuby). Sym_array21163+
# shares the int_array dispatch (sym ids stored as ints), so allow21164+
# "symbol" args too when receiver is sym_array.21165+
args_id_inc = @nd_arguments[nid]21166+
if args_id_inc >= 021167+
aa_inc = get_args(args_id_inc)21168+
if aa_inc.length > 021169+
at_inc = infer_type(aa_inc[0])21170+
ok_inc = at_inc == "int" || at_inc == "int?" || at_inc == "poly"21171+
if recv_type == "sym_array" && at_inc == "symbol"21172+
ok_inc = true21173+
end21174+
if !ok_inc21175+
return "FALSE"21176+
end21177+
end21178+
end2116221179
return "sp_IntArray_include(" + rc + ", " + compile_arg0_as_int(nid) + ")"2116321180
end2116421181
if mname == "index" || mname == "find_index"@@ -21757,6 +21774,17 @@
2175721774
return "sp_StrArray_empty(" + rc + ")"2175821775
end2175921776
if mname == "include?"21777+
# Issue #911.21778+
args_id_inc_s = @nd_arguments[nid]21779+
if args_id_inc_s >= 021780+
aa_inc_s = get_args(args_id_inc_s)21781+
if aa_inc_s.length > 021782+
at_inc_s = infer_type(aa_inc_s[0])21783+
if at_inc_s != "string" && at_inc_s != "string?" && at_inc_s != "mutable_str" && at_inc_s != "poly"21784+
return "FALSE"21785+
end21786+
end21787+
end2176021788
return "sp_StrArray_include(" + rc + ", " + compile_arg0(nid) + ")"2176121789
end2176221790
if mname == "index" || mname == "find_index"test/typed_array_include_cross_type.rbaddedView file
Loading diff…