mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
codegen.c: raise NoMatchingPatternError in case/in without else
case/in without else clause now raises NoMatchingPatternError when no pattern matches, matching CRuby behavior. Fixes #6741. Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -4455,10 +4455,9 @@ codegen_case_match(codegen_scope *s, node *varnode, int val)
|
||||
current_in = current_in->cdr;
|
||||
}
|
||||
|
||||
/* No pattern matched - generate nil or error */
|
||||
if (val) {
|
||||
genop_1(s, OP_LOADNIL, cursp());
|
||||
}
|
||||
/* No pattern matched - raise NoMatchingPatternError */
|
||||
genop_1(s, OP_LOADFALSE, cursp());
|
||||
genop_1(s, OP_MATCHERR, cursp());
|
||||
|
||||
/* Dispatch all end jumps */
|
||||
if (case_end_jumps != JMPLINK_START) {
|
||||
|
||||
+6
-5
@@ -1246,12 +1246,13 @@ assert('pattern matching - NoMatchingPatternError') do
|
||||
assert_true e.message.is_a?(String)
|
||||
end
|
||||
|
||||
# case/in without else returns nil (no match)
|
||||
result = case 5
|
||||
in 1 then :one
|
||||
in 2 then :two
|
||||
# case/in without else raises NoMatchingPatternError
|
||||
assert_raise(NoMatchingPatternError) do
|
||||
case 5
|
||||
in 1 then :one
|
||||
in 2 then :two
|
||||
end
|
||||
end
|
||||
assert_nil result
|
||||
end
|
||||
|
||||
assert('pattern matching - complex patterns') do
|
||||
|
||||
Reference in New Issue
Block a user