From 2ba598726360226b5a00d7c72a62479c6eb0e87a Mon Sep 17 00:00:00 2001 From: ydah Date: Wed, 27 Nov 2024 19:18:58 +0900 Subject: [PATCH] [ruby/prism] Reject invalid dot method call after match predicate or after match required Partially fixes: https://github.com/ruby/prism/issues/3171 https://github.com/ruby/prism/commit/5c33fa5a1a --- prism/prism.c | 26 +++++++++++++++++++ ...edicate_after_and_with_dot_method_call.txt | 3 +++ ...redicate_after_or_with_dot_method_call.txt | 3 +++ ...cate_after_rescue_with_dot_method_call.txt | 3 +++ ...equired_after_and_with_dot_method_call.txt | 3 +++ ...required_after_or_with_dot_method_call.txt | 3 +++ ...ired_after_rescue_with_dot_method_call.txt | 3 +++ 7 files changed, 44 insertions(+) create mode 100644 test/prism/errors/match_predicate_after_and_with_dot_method_call.txt create mode 100644 test/prism/errors/match_predicate_after_or_with_dot_method_call.txt create mode 100644 test/prism/errors/match_predicate_after_rescue_with_dot_method_call.txt create mode 100644 test/prism/errors/match_required_after_and_with_dot_method_call.txt create mode 100644 test/prism/errors/match_required_after_or_with_dot_method_call.txt create mode 100644 test/prism/errors/match_required_after_rescue_with_dot_method_call.txt diff --git a/prism/prism.c b/prism/prism.c index 567c337054..4cf453dbdf 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -21443,6 +21443,32 @@ parse_expression_infix(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t return (pm_node_t *) pm_call_node_shorthand_create(parser, node, &operator, &arguments); } + switch (PM_NODE_TYPE(node)) { + case PM_RESCUE_MODIFIER_NODE: { + pm_rescue_modifier_node_t *cast = (pm_rescue_modifier_node_t *) node; + if (PM_NODE_TYPE_P(cast->rescue_expression, PM_MATCH_PREDICATE_NODE) || PM_NODE_TYPE_P(cast->rescue_expression, PM_MATCH_REQUIRED_NODE)) { + PM_PARSER_ERR_TOKEN_FORMAT(parser, operator, PM_ERR_EXPECT_EOL_AFTER_STATEMENT, pm_token_type_human(operator.type)); + } + break; + } + case PM_AND_NODE: { + pm_and_node_t *cast = (pm_and_node_t *) node; + if (PM_NODE_TYPE_P(cast->right, PM_MATCH_PREDICATE_NODE) || PM_NODE_TYPE_P(cast->right, PM_MATCH_REQUIRED_NODE)) { + PM_PARSER_ERR_TOKEN_FORMAT(parser, operator, PM_ERR_EXPECT_EOL_AFTER_STATEMENT, pm_token_type_human(operator.type)); + } + break; + } + case PM_OR_NODE: { + pm_or_node_t *cast = (pm_or_node_t *) node; + if (PM_NODE_TYPE_P(cast->right, PM_MATCH_PREDICATE_NODE) || PM_NODE_TYPE_P(cast->right, PM_MATCH_REQUIRED_NODE)) { + PM_PARSER_ERR_TOKEN_FORMAT(parser, operator, PM_ERR_EXPECT_EOL_AFTER_STATEMENT, pm_token_type_human(operator.type)); + } + break; + } + default: + break; + } + pm_token_t message; switch (parser->current.type) { diff --git a/test/prism/errors/match_predicate_after_and_with_dot_method_call.txt b/test/prism/errors/match_predicate_after_and_with_dot_method_call.txt new file mode 100644 index 0000000000..32b77d127c --- /dev/null +++ b/test/prism/errors/match_predicate_after_and_with_dot_method_call.txt @@ -0,0 +1,3 @@ +1 and 2 in 3.inspect + ^ unexpected '.', expecting end-of-input + diff --git a/test/prism/errors/match_predicate_after_or_with_dot_method_call.txt b/test/prism/errors/match_predicate_after_or_with_dot_method_call.txt new file mode 100644 index 0000000000..0a940166dc --- /dev/null +++ b/test/prism/errors/match_predicate_after_or_with_dot_method_call.txt @@ -0,0 +1,3 @@ +'a' or 1 in 1.upcase + ^ unexpected '.', expecting end-of-input + diff --git a/test/prism/errors/match_predicate_after_rescue_with_dot_method_call.txt b/test/prism/errors/match_predicate_after_rescue_with_dot_method_call.txt new file mode 100644 index 0000000000..fead8aaf23 --- /dev/null +++ b/test/prism/errors/match_predicate_after_rescue_with_dot_method_call.txt @@ -0,0 +1,3 @@ +'a' rescue 2 in 3.upcase + ^ unexpected '.', expecting end-of-input + diff --git a/test/prism/errors/match_required_after_and_with_dot_method_call.txt b/test/prism/errors/match_required_after_and_with_dot_method_call.txt new file mode 100644 index 0000000000..0ecf86bae1 --- /dev/null +++ b/test/prism/errors/match_required_after_and_with_dot_method_call.txt @@ -0,0 +1,3 @@ +1 and 2 => 3.inspect + ^ unexpected '.', expecting end-of-input + diff --git a/test/prism/errors/match_required_after_or_with_dot_method_call.txt b/test/prism/errors/match_required_after_or_with_dot_method_call.txt new file mode 100644 index 0000000000..479413250d --- /dev/null +++ b/test/prism/errors/match_required_after_or_with_dot_method_call.txt @@ -0,0 +1,3 @@ +1 or 2 => 3.inspect + ^ unexpected '.', expecting end-of-input + diff --git a/test/prism/errors/match_required_after_rescue_with_dot_method_call.txt b/test/prism/errors/match_required_after_rescue_with_dot_method_call.txt new file mode 100644 index 0000000000..d72d72ce60 --- /dev/null +++ b/test/prism/errors/match_required_after_rescue_with_dot_method_call.txt @@ -0,0 +1,3 @@ +1 rescue 2 => 3.inspect + ^ unexpected '.', expecting end-of-input +