From aeaa4117670459d62bce3b14de2b18643d5b8089 Mon Sep 17 00:00:00 2001 From: TSUYUSATO Kitsune Date: Tue, 14 Nov 2023 17:05:16 +0900 Subject: [PATCH] Check value expressions on creating a node --- src/prism.c | 25 +++++++++++ test/prism/errors_test.rb | 92 +++++++++++++++++++++++++++++++++++---- 2 files changed, 109 insertions(+), 8 deletions(-) diff --git a/src/prism.c b/src/prism.c index 0778373a73..c53e1cab65 100644 --- a/src/prism.c +++ b/src/prism.c @@ -990,6 +990,8 @@ pm_alternation_pattern_node_create(pm_parser_t *parser, pm_node_t *left, pm_node */ static pm_and_node_t * pm_and_node_create(pm_parser_t *parser, pm_node_t *left, const pm_token_t *operator, pm_node_t *right) { + pm_assert_value_expression(parser, left); + pm_and_node_t *node = PM_ALLOC_NODE(parser, pm_and_node_t); *node = (pm_and_node_t) { @@ -1584,6 +1586,8 @@ pm_call_node_create(pm_parser_t *parser) { */ static pm_call_node_t * pm_call_node_aref_create(pm_parser_t *parser, pm_node_t *receiver, pm_arguments_t *arguments) { + pm_assert_value_expression(parser, receiver); + pm_call_node_t *node = pm_call_node_create(parser); node->base.location.start = receiver->location.start; @@ -1611,6 +1615,9 @@ pm_call_node_aref_create(pm_parser_t *parser, pm_node_t *receiver, pm_arguments_ */ static pm_call_node_t * pm_call_node_binary_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t *operator, pm_node_t *argument) { + pm_assert_value_expression(parser, receiver); + pm_assert_value_expression(parser, argument); + pm_call_node_t *node = pm_call_node_create(parser); node->base.location.start = MIN(receiver->location.start, argument->location.start); @@ -1632,6 +1639,8 @@ pm_call_node_binary_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t */ static pm_call_node_t * pm_call_node_call_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t *operator, pm_token_t *message, pm_arguments_t *arguments) { + pm_assert_value_expression(parser, receiver); + pm_call_node_t *node = pm_call_node_create(parser); node->base.location.start = receiver->location.start; @@ -1695,6 +1704,8 @@ pm_call_node_fcall_create(pm_parser_t *parser, pm_token_t *message, pm_arguments */ static pm_call_node_t * pm_call_node_not_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t *message, pm_arguments_t *arguments) { + pm_assert_value_expression(parser, receiver); + pm_call_node_t *node = pm_call_node_create(parser); node->base.location.start = message->start; @@ -1719,6 +1730,8 @@ pm_call_node_not_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t *me */ static pm_call_node_t * pm_call_node_shorthand_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t *operator, pm_arguments_t *arguments) { + pm_assert_value_expression(parser, receiver); + pm_call_node_t *node = pm_call_node_create(parser); node->base.location.start = receiver->location.start; @@ -1748,6 +1761,8 @@ pm_call_node_shorthand_create(pm_parser_t *parser, pm_node_t *receiver, pm_token */ static pm_call_node_t * pm_call_node_unary_create(pm_parser_t *parser, pm_token_t *operator, pm_node_t *receiver, const char *name) { + pm_assert_value_expression(parser, receiver); + pm_call_node_t *node = pm_call_node_create(parser); node->base.location.start = operator->start; @@ -3243,6 +3258,7 @@ pm_if_node_modifier_create(pm_parser_t *parser, pm_node_t *statement, const pm_t */ static pm_if_node_t * pm_if_node_ternary_create(pm_parser_t *parser, pm_node_t *predicate, pm_node_t *true_expression, const pm_token_t *colon, pm_node_t *false_expression) { + pm_assert_value_expression(parser, predicate); pm_conditional_predicate(predicate); pm_statements_node_t *if_statements = pm_statements_node_create(parser); @@ -4000,6 +4016,8 @@ pm_local_variable_target_node_create(pm_parser_t *parser, const pm_token_t *name */ static pm_match_predicate_node_t * pm_match_predicate_node_create(pm_parser_t *parser, pm_node_t *value, pm_node_t *pattern, const pm_token_t *operator) { + pm_assert_value_expression(parser, value); + pm_match_predicate_node_t *node = PM_ALLOC_NODE(parser, pm_match_predicate_node_t); *node = (pm_match_predicate_node_t) { @@ -4023,6 +4041,8 @@ pm_match_predicate_node_create(pm_parser_t *parser, pm_node_t *value, pm_node_t */ static pm_match_required_node_t * pm_match_required_node_create(pm_parser_t *parser, pm_node_t *value, pm_node_t *pattern, const pm_token_t *operator) { + pm_assert_value_expression(parser, value); + pm_match_required_node_t *node = PM_ALLOC_NODE(parser, pm_match_required_node_t); *node = (pm_match_required_node_t) { @@ -4296,6 +4316,8 @@ pm_optional_parameter_node_create(pm_parser_t *parser, const pm_token_t *name, c */ static pm_or_node_t * pm_or_node_create(pm_parser_t *parser, pm_node_t *left, const pm_token_t *operator, pm_node_t *right) { + pm_assert_value_expression(parser, left); + pm_or_node_t *node = PM_ALLOC_NODE(parser, pm_or_node_t); *node = (pm_or_node_t) { @@ -4566,6 +4588,9 @@ pm_pre_execution_node_create(pm_parser_t *parser, const pm_token_t *keyword, con */ static pm_range_node_t * pm_range_node_create(pm_parser_t *parser, pm_node_t *left, const pm_token_t *operator, pm_node_t *right) { + pm_assert_value_expression(parser, left); + pm_assert_value_expression(parser, right); + pm_range_node_t *node = PM_ALLOC_NODE(parser, pm_range_node_t); pm_node_flags_t flags = 0; diff --git a/test/prism/errors_test.rb b/test/prism/errors_test.rb index 170df4d3ad..da1aa9ed25 100644 --- a/test/prism/errors_test.rb +++ b/test/prism/errors_test.rb @@ -1517,6 +1517,8 @@ def test_void_value_expression_in_modifier 1 unless (return) 1 while (return) 1 until (return) + (return) => a + (return) in a RUBY message = 'Unexpected void value expression' assert_errors expression(source), source, [ @@ -1524,6 +1526,66 @@ def test_void_value_expression_in_modifier [message, 24..30], [message, 41..47], [message, 58..64], + [message, 67..73], + [message, 81..87] + ], compare_ripper: false # Ripper does not check 'void value expression'. + end + + def test_void_value_expression_in_expression + source = <<~RUBY + (return) ? 1 : 1 + (return)..1 + 1..(return) + (return)...1 + 1...(return) + (..(return)) + (...(return)) + ((return)..) + ((return)...) + RUBY + message = 'Unexpected void value expression' + assert_errors expression(source), source, [ + [message, 1..7], + [message, 18..24], + [message, 33..39], + [message, 42..48], + [message, 59..65], + [message, 71..77], + [message, 85..91], + [message, 96..102], + [message, 109..115] + ], compare_ripper: false # Ripper does not check 'void value expression'. + end + + def test_void_value_expression_in_hash + source = <<~RUBY + { return => 1 } + { 1 => return } + { a: return } + { **return } + RUBY + message = 'Unexpected void value expression' + assert_errors expression(source), source, [ + [message, 2..8], + [message, 23..29], + [message, 37..43], + [message, 50..56], + ], compare_ripper: false # Ripper does not check 'void value expression'. + end + + def test_void_value_expression_in_call + source = <<~RUBY + (return).foo + (return).(1) + (return)[1] + (return)[1] = 2 + RUBY + message = 'Unexpected void value expression' + assert_errors expression(source), source, [ + [message, 1..7], + [message, 14..20], + [message, 27..33], + [message, 39..45], ], compare_ripper: false # Ripper does not check 'void value expression'. end @@ -1551,19 +1613,33 @@ def test_void_value_expression_in_arguments ], compare_ripper: false # Ripper does not check 'void value expression'. end - def test_void_value_expression_in_hash + def test_void_value_expression_in_unary_call source = <<~RUBY - { return => 1 } - { 1 => return } - { a: return } - { **return } + +(return) + not return RUBY message = 'Unexpected void value expression' assert_errors expression(source), source, [ [message, 2..8], - [message, 23..29], - [message, 37..43], - [message, 50..56], + [message, 14..20], + ], compare_ripper: false # Ripper does not check 'void value expression'. + end + + def test_void_value_expression_in_binary_call + source = <<~RUBY + 1 + (return) + (return) + 1 + 1 and (return) + (return) and 1 + 1 or (return) + (return) or 1 + RUBY + message = 'Unexpected void value expression' + assert_errors expression(source), source, [ + [message, 5..11], + [message, 14..20], + [message, 42..48], + [message, 71..77], ], compare_ripper: false # Ripper does not check 'void value expression'. end