Skip to content

Commit

Permalink
[ruby/prism] Check "void value expression" for array literals
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust authored and matzbot committed Dec 4, 2023
1 parent 85bc80a commit 2a65d83
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions prism/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -13666,7 +13666,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power) {
pm_parser_err_token(parser, &operator, PM_ERR_ARGUMENT_NO_FORWARDING_STAR);
}
} else {
expression = parse_expression(parser, PM_BINDING_POWER_DEFINED, PM_ERR_ARRAY_EXPRESSION_AFTER_STAR);
expression = parse_value_expression(parser, PM_BINDING_POWER_DEFINED, PM_ERR_ARRAY_EXPRESSION_AFTER_STAR);
}

element = (pm_node_t *) pm_splat_node_create(parser, &operator, expression);
Expand All @@ -13684,7 +13684,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power) {

parsed_bare_hash = true;
} else {
element = parse_expression(parser, PM_BINDING_POWER_DEFINED, PM_ERR_ARRAY_EXPRESSION);
element = parse_value_expression(parser, PM_BINDING_POWER_DEFINED, PM_ERR_ARRAY_EXPRESSION);

if (pm_symbol_node_label_p(element) || accept1(parser, PM_TOKEN_EQUAL_GREATER)) {
if (parsed_bare_hash) {
Expand All @@ -13700,7 +13700,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power) {
operator = not_provided(parser);
}

pm_node_t *value = parse_expression(parser, PM_BINDING_POWER_DEFINED, PM_ERR_HASH_VALUE);
pm_node_t *value = parse_value_expression(parser, PM_BINDING_POWER_DEFINED, PM_ERR_HASH_VALUE);
pm_node_t *assoc = (pm_node_t *) pm_assoc_node_create(parser, element, &operator, value);
pm_keyword_hash_node_elements_append(hash, assoc);

Expand Down
22 changes: 22 additions & 0 deletions test/prism/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,28 @@ def test_void_value_expression_in_expression
], compare_ripper: false # Ripper does not check 'void value expression'.
end

def test_void_value_expression_in_array
source = <<~RUBY
[return]
[1, return]
[ return => 1 ]
[ 1 => return ]
[ a: return ]
[ *return ]
[ **return ]
RUBY
message = 'unexpected void value expression'
assert_errors expression(source), source, [
[message, 1..7],
[message, 13..19],
[message, 23..29],
[message, 44..50],
[message, 58..64],
[message, 70..76],
[message, 83..89],
], compare_ripper: false # Ripper does not check 'void value expression'.
end

def test_void_value_expression_in_hash
source = <<~RUBY
{ return => 1 }
Expand Down

0 comments on commit 2a65d83

Please sign in to comment.