Skip to content

Commit

Permalink
[PRISM] Account for multiple anonymous locals
Browse files Browse the repository at this point in the history
This commit adjusts the local table size to be consistent regardless
of the number of anonymous locals.
  • Loading branch information
jemmaissroff committed Dec 14, 2023
1 parent 01f21d5 commit 7ac93e9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
14 changes: 14 additions & 0 deletions prism_compile.c
Expand Up @@ -4489,6 +4489,7 @@ 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 @@ -4497,6 +4498,19 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
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++;
}
}
}

// 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);
}
}

Expand Down
2 changes: 2 additions & 0 deletions test/ruby/test_compile_prism.rb
Expand Up @@ -1128,6 +1128,8 @@ def test_BlockNode
assert_prism_eval("[].tap { _1 }")

assert_prism_eval("[].each { |a,| }")
assert_prism_eval("[[1, 2, 3]].map { |_, _, a| a }")
assert_prism_eval("[[1, 2, 3]].map { |_, a| a }")

assert_prism_eval("[[]].map { |a| a }")
assert_prism_eval("[[]].map { |a| a }")
Expand Down

0 comments on commit 7ac93e9

Please sign in to comment.