1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Make single line pattern matching void expression

Instead of returning `nil`, raise a syntax error if its value is
used.  [Feature #16355]
This commit is contained in:
Nobuyoshi Nakada 2019-11-30 00:15:29 +09:00
parent 36da0b3da1
commit d1ef4fd08e
No known key found for this signature in database
GPG key ID: 4BC7D6DF58D8DF60
2 changed files with 13 additions and 2 deletions

11
parse.y
View file

@ -10740,6 +10740,17 @@ value_expr_check(struct parser_params *p, NODE *node)
case NODE_RETRY:
return void_node ? void_node : node;
case NODE_CASE3:
if (!node->nd_body || nd_type(node->nd_body) != NODE_IN) {
compile_error(p, "unexpected node");
return NULL;
}
if (node->nd_body->nd_body) {
return NULL;
}
/* single line pattern matching */
return void_node ? void_node : node;
case NODE_BLOCK:
while (node->nd_next) {
node = node->nd_next;

View file

@ -1261,12 +1261,12 @@ END
################################################################
def test_modifier_in
assert_nil (1 in a)
1 in a
assert_equal 1, a
assert_raise(NoMatchingPatternError) do
{a: 1} in {a: 0}
end
assert_valid_syntax "p(({} in {a:}), a:\n 1)"
assert_syntax_error("if {} in {a:}; end", /void value expression/)
assert_syntax_error(%q{
1 in a, b
}, /unexpected/, '[ruby-core:95098]')