Skip to content

Commit

Permalink
[PRISM] Support repeated required parameter names.
Browse files Browse the repository at this point in the history
Fixes: ruby/prism#2062

This patch only fixes positional parameters, we still need to fix the
other cases spelled out in test/prism/fixtures/repeat_parameters.txt
  • Loading branch information
tenderlove committed Jan 11, 2024
1 parent 72be786 commit f2149dc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
17 changes: 5 additions & 12 deletions prism_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5582,7 +5582,6 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
}

if (requireds_list) {
int number_of_anonymous_locals = 0;
for (size_t i = 0; i < requireds_list->size; i++) {
// For each MultiTargetNode, we're going to have one
// additional anonymous local not represented in the locals table
Expand All @@ -5592,19 +5591,11 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
table_size++;
}
else if (PM_NODE_TYPE_P(required, PM_REQUIRED_PARAMETER_NODE)) {
if (pm_constant_id_lookup(scope_node, ((pm_required_parameter_node_t *)required)->name) == rb_intern("_")) {
number_of_anonymous_locals++;
if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
table_size++;
}
}
}

// For each anonymous local we also want to increase the size
// of the locals table. Prism's locals table accounts for all
// anonymous locals as 1, so we need to increase the table size
// by the number of anonymous locals - 1
if (number_of_anonymous_locals > 1) {
table_size += (number_of_anonymous_locals - 1);
}
}

if (posts_list) {
Expand Down Expand Up @@ -5686,7 +5677,9 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
case PM_REQUIRED_PARAMETER_NODE: {
pm_required_parameter_node_t * param = (pm_required_parameter_node_t *)required;

pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
if (!PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
}
break;
}
default: {
Expand Down
4 changes: 4 additions & 0 deletions test/ruby/test_compile_prism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,10 @@ def self.prism_test_def_node(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1,
CODE
end

def test_repeated_method_params
assert_prism_eval("def self.foo(_a, _a); _a; end; foo(1, 2)")
end

def test_method_parameters
assert_prism_eval(<<-CODE)
def self.prism_test_method_parameters(a, b=1, *c, d:, e: 2, **f, &g)
Expand Down

0 comments on commit f2149dc

Please sign in to comment.