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 end
2116121161 if mname == "include?"
21162+ # Issue #911: cross-type include? returns false (CRuby). Sym_array
21163+ # shares the int_array dispatch (sym ids stored as ints), so allow
21164+ # "symbol" args too when receiver is sym_array.
21165+ args_id_inc = @nd_arguments[nid]
21166+ if args_id_inc >= 0
21167+ aa_inc = get_args(args_id_inc)
21168+ if aa_inc.length > 0
21169+ 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 = true
21173+ end
21174+ if !ok_inc
21175+ return "FALSE"
21176+ end
21177+ end
21178+ end
2116221179 return "sp_IntArray_include(" + rc + ", " + compile_arg0_as_int(nid) + ")"
2116321180 end
2116421181 if mname == "index" || mname == "find_index"
@@ -21757,6 +21774,17 @@
2175721774 return "sp_StrArray_empty(" + rc + ")"
2175821775 end
2175921776 if mname == "include?"
21777+ # Issue #911.
21778+ args_id_inc_s = @nd_arguments[nid]
21779+ if args_id_inc_s >= 0
21780+ aa_inc_s = get_args(args_id_inc_s)
21781+ if aa_inc_s.length > 0
21782+ 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+ end
21786+ end
21787+ end
2176021788 return "sp_StrArray_include(" + rc + ", " + compile_arg0(nid) + ")"
2176121789 end
2176221790 if mname == "index" || mname == "find_index"