mruby-compiler: raise error for pin operator with undefined variable

CRuby raises SyntaxError for `^a` in pattern matching when `a` is
not a local variable. Previously mruby silently generated an
unconditional fail jump, which also led to bytecode corruption
when combined with alternation patterns.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-02-05 22:38:42 +09:00
parent eea9e30979
commit b287c12e48
+5 -3
View File
@@ -4567,9 +4567,11 @@ codegen_pattern(codegen_scope *s, node *pattern, int target, uint32_t *fail_pos,
*fail_pos = tmp;
}
else {
/* Variable not found - this is an error case, but for robustness fail the match */
tmp = genjmp(s, OP_JMP, *fail_pos);
*fail_pos = tmp;
/* Variable not found - raise compile error like CRuby */
const char *name = mrb_sym_name_len(s->mrb, pat_pin->name, NULL);
char buf[256];
snprintf(buf, sizeof(buf), "%.200s: no such local variable", name);
codegen_error(s, buf);
}
}
break;