mirror of
https://github.com/ruby/ruby.git
synced 2026-08-03 13:08:59 +08:00
[Bug #21669] Fix void value expression check for pattern-matching
If the all `in` and `else` branches are void, the `case` is also void.
This commit is contained in:
parent
baaf0ccb9c
commit
cb303ff4b8
Notes:
git
2026-02-04 05:01:21 +00:00
26
parse.y
26
parse.y
@ -13877,15 +13877,25 @@ value_expr_check(struct parser_params *p, NODE *node)
|
||||
break;
|
||||
|
||||
case NODE_CASE3:
|
||||
if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
|
||||
compile_error(p, "unexpected node");
|
||||
return NULL;
|
||||
{
|
||||
NODE *in = RNODE_CASE3(node)->nd_body;
|
||||
if (!in || !nd_type_p(in, NODE_IN)) {
|
||||
compile_error(p, "unexpected node");
|
||||
return NULL;
|
||||
}
|
||||
if (!RNODE_IN(in)->nd_body) {
|
||||
/* single line pattern matching with "=>" operator */
|
||||
goto found;
|
||||
}
|
||||
do {
|
||||
vn = value_expr_check(p, RNODE_IN(in)->nd_body);
|
||||
if (!vn) return NULL;
|
||||
if (!void_node) void_node = vn;
|
||||
in = RNODE_IN(in)->nd_next;
|
||||
} while (in && nd_type_p(in, NODE_IN));
|
||||
node = in; /* else */
|
||||
}
|
||||
if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
|
||||
return NULL;
|
||||
}
|
||||
/* single line pattern matching with "=>" operator */
|
||||
goto found;
|
||||
break;
|
||||
|
||||
case NODE_BLOCK:
|
||||
while (RNODE_BLOCK(node)->nd_next) {
|
||||
|
||||
@ -2132,6 +2132,18 @@ eom
|
||||
};
|
||||
end
|
||||
|
||||
def test_value_expr_in_case3
|
||||
assert_syntax_error("#{<<~"{#"}\n#{<<~'};'}", /void value expression/, nil, "#{BUG_21669} 1.3")
|
||||
{#
|
||||
x =
|
||||
case a
|
||||
in 1; return
|
||||
in 2; return
|
||||
else return
|
||||
end
|
||||
};
|
||||
end
|
||||
|
||||
def test_tautological_condition
|
||||
assert_valid_syntax("def f() return if false and invalid; nil end")
|
||||
assert_valid_syntax("def f() return unless true or invalid; nil end")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user