Commit 6790c5ee
6790c5ee7e77Merged
Yasuhiro Matsumoto <[email protected]> · 5 months ago · 5 files · +4 added · ~1 modified
Description
codegen: fail closed on find pattern with non-array scrutinee; add tests
Address review: a find pattern against a statically non-array (or unknown) scrutinee fell through to emit_pm_cond, which has no find-pattern case, so has_cond stayed 0 and the arm matched unconditionally. Emit a constant-false condition instead so it never matches. Add tests for find patterns (non-match + head/required/tail binding) and case/in in value position.
Co-Authored-By: Claude [email protected]
Details
- Commit
- 6790c5ee7e77
- Parent
- 5a0a023baf3e
src/codegen_stmt.cmodifiedView file
@@ -1148,7 +1148,14 @@
11481148
position temp (-1 if none). The arm matches when that position >= 0. */11491149
int find_pat = -1, find_pos = -1;11501150
const char *find_k = NULL;1151-
if (!strcmp(pty, "FindPatternNode") && ty_is_array(pt)) {1151+
if (!strcmp(pty, "FindPatternNode") && !ty_is_array(pt)) {1152+
/* A find pattern only matches arrays. With a non-array (or statically1153+
unknown) scrutinee, fail closed -- never match -- rather than fall1154+
through to emit_pm_cond (which has no find-pattern case and would1155+
leave has_cond=0, i.e. match unconditionally). */1156+
buf_puts(&cond_buf, "0");1157+
has_cond = 1;1158+
} else if (!strcmp(pty, "FindPatternNode")) {11521159
find_pat = pat;11531160
find_k = (pt == TY_POLY_ARRAY) ? "Poly" : array_kind(pt);11541161
if (!find_k) find_k = "Int";test/case_in_value_position.rbaddedView file
Loading diff…
test/case_in_value_position.rb.expectedaddedView file
Loading diff…
test/find_pattern_match.rbaddedView file
Loading diff…
test/find_pattern_match.rb.expectedaddedView file
Loading diff…