aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHaldun Bayhantopcu <haldun@github.com>2023-10-06 14:18:40 +0200
committergit <svn-admin@ruby-lang.org>2023-10-06 14:10:28 +0000
commit7eccc13c1f0be27063c979f94c34497712093bfd (patch)
treecced451660bbe5c8e72e362ab9bcd9865e73df80
parent7b8d472100332c77bb47a2e4834459c4a625d109 (diff)
downloadruby-7eccc13c1f0be27063c979f94c34497712093bfd.tar.gz
[ruby/prism] Emit error when assigning to a numbered parameter
https://github.com/ruby/prism/commit/66248ac2f6
-rw-r--r--prism/prism.c14
-rw-r--r--test/prism/errors_test.rb15
2 files changed, 24 insertions, 5 deletions
diff --git a/prism/prism.c b/prism/prism.c
index be227e2198..9b8ee23253 100644
--- a/prism/prism.c
+++ b/prism/prism.c
@@ -3444,11 +3444,20 @@ pm_local_variable_write_node_create(pm_parser_t *parser, pm_constant_id_t name,
return node;
}
+static inline bool
+token_is_numbered_parameter(const uint8_t *start, const uint8_t *end) {
+ return (end - start == 2) && (start[0] == '_') && (start[1] != '0') && (pm_char_is_decimal_digit(start[1]));
+}
+
// Allocate and initialize a new LocalVariableTargetNode node.
static pm_local_variable_target_node_t *
pm_local_variable_target_node_create(pm_parser_t *parser, const pm_token_t *name) {
pm_local_variable_target_node_t *node = PM_ALLOC_NODE(parser, pm_local_variable_target_node_t);
+ if (token_is_numbered_parameter(name->start, name->end)) {
+ pm_parser_err_token(parser, name, PM_ERR_PARAMETER_NUMBERED_RESERVED);
+ }
+
*node = (pm_local_variable_target_node_t) {
{
.type = PM_LOCAL_VARIABLE_TARGET_NODE,
@@ -4944,11 +4953,6 @@ pm_parser_local_add_owned(pm_parser_t *parser, const uint8_t *start, size_t leng
if (constant_id != 0) pm_parser_local_add(parser, constant_id);
}
-static inline bool
-token_is_numbered_parameter(const uint8_t *start, const uint8_t *end) {
- return (end - start == 2) && (start[0] == '_') && (start[1] != '0') && (pm_char_is_decimal_digit(start[1]));
-}
-
// Add a parameter name to the current scope and check whether the name of the
// parameter is unique or not.
static void
diff --git a/test/prism/errors_test.rb b/test/prism/errors_test.rb
index d383c66f41..8a88270b44 100644
--- a/test/prism/errors_test.rb
+++ b/test/prism/errors_test.rb
@@ -1389,6 +1389,21 @@ module Prism
], compare_ripper: compare_ripper
end
+ def test_assign_to_numbered_parameter
+ source = "
+ a in _1
+ a => _1
+ 1 => a, _1
+ 1 in a, _1
+ "
+ assert_errors expression(source), source, [
+ ["Token reserved for a numbered parameter", 14..16],
+ ["Token reserved for a numbered parameter", 30..32],
+ ["Token reserved for a numbered parameter", 49..51],
+ ["Token reserved for a numbered parameter", 68..70],
+ ]
+ end
+
private
def assert_errors(expected, source, errors, compare_ripper: RUBY_ENGINE == "ruby")