Skip to content

Commit cb303ff

Browse files
committed
[Bug #21669] Fix void value expression check for pattern-matching
If the all `in` and `else` branches are void, the `case` is also void.
1 parent baaf0cc commit cb303ff

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

parse.y

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13877,15 +13877,25 @@ value_expr_check(struct parser_params *p, NODE *node)
1387713877
break;
1387813878

1387913879
case NODE_CASE3:
13880-
if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13881-
compile_error(p, "unexpected node");
13882-
return NULL;
13883-
}
13884-
if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13885-
return NULL;
13880+
{
13881+
NODE *in = RNODE_CASE3(node)->nd_body;
13882+
if (!in || !nd_type_p(in, NODE_IN)) {
13883+
compile_error(p, "unexpected node");
13884+
return NULL;
13885+
}
13886+
if (!RNODE_IN(in)->nd_body) {
13887+
/* single line pattern matching with "=>" operator */
13888+
goto found;
13889+
}
13890+
do {
13891+
vn = value_expr_check(p, RNODE_IN(in)->nd_body);
13892+
if (!vn) return NULL;
13893+
if (!void_node) void_node = vn;
13894+
in = RNODE_IN(in)->nd_next;
13895+
} while (in && nd_type_p(in, NODE_IN));
13896+
node = in; /* else */
1388613897
}
13887-
/* single line pattern matching with "=>" operator */
13888-
goto found;
13898+
break;
1388913899

1389013900
case NODE_BLOCK:
1389113901
while (RNODE_BLOCK(node)->nd_next) {

test/ruby/test_syntax.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,6 +2132,18 @@ def test_value_expr_in_case2
21322132
};
21332133
end
21342134

2135+
def test_value_expr_in_case3
2136+
assert_syntax_error("#{<<~"{#"}\n#{<<~'};'}", /void value expression/, nil, "#{BUG_21669} 1.3")
2137+
{#
2138+
x =
2139+
case a
2140+
in 1; return
2141+
in 2; return
2142+
else return
2143+
end
2144+
};
2145+
end
2146+
21352147
def test_tautological_condition
21362148
assert_valid_syntax("def f() return if false and invalid; nil end")
21372149
assert_valid_syntax("def f() return unless true or invalid; nil end")

0 commit comments

Comments
 (0)