Commit 6fcaad4b
6fcaad4b160cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Array#include? with an incompatible-type argument
A typed (int/float/str) array can never contain an element of an incompatible type, so include? with a numeric arg on a string array (or vice versa) is statically false. Previously codegen passed the mistyped argument straight to sp_*Array_include and the C compile failed with an int/pointer conversion error. Both operands are still evaluated for side effects.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 6fcaad4b160c
- Parent
- b515baef27fa
src/codegen.cmodifiedView file
@@ -1852,6 +1852,20 @@
18521852
emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b); buf_puts(b, ")");18531853
return;18541854
}1855+
if (!strcmp(name, "include?") && argc == 1) {1856+
/* a typed array can never contain an element of an incompatible1857+
type (numeric vs string), so the answer is statically false;1858+
still evaluate both operands for any side effects. */1859+
int mismatch = 0;1860+
if (rt == TY_STR_ARRAY && a0 != TY_STRING && a0 != TY_UNKNOWN && a0 != TY_POLY) mismatch = 1;1861+
if ((rt == TY_INT_ARRAY || rt == TY_FLOAT_ARRAY) &&1862+
a0 != TY_INT && a0 != TY_FLOAT && a0 != TY_UNKNOWN && a0 != TY_POLY) mismatch = 1;1863+
if (mismatch) {1864+
buf_puts(b, "((void)("); emit_expr(c, recv, b);1865+
buf_puts(b, "), (void)("); emit_expr(c, argv[0], b); buf_puts(b, "), 0)");1866+
return;1867+
}1868+
}18551869
if ((!strcmp(name, "include?") || !strcmp(name, "index")) && argc == 1 && rt != TY_FLOAT_ARRAY) {18561870
const char *fn = !strcmp(name, "include?") ? "include" : "index";18571871
buf_printf(b, "sp_%sArray_%s(", k, fn);