summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJemma Issroff <jemmaissroff@gmail.com>2023-12-13 12:17:22 -0500
committerJemma Issroff <jemmaissroff@gmail.com>2023-12-14 13:45:41 -0500
commit7ac93e99a3db30ed41cbbe95df1d7be9a790b9cb (patch)
tree7d6bfa92e54390fdcfa381e6aaaabaa8f3167fd0
parent01f21d57297f1e6820a6a374ccf9c432c6178038 (diff)
[PRISM] Account for multiple anonymous locals
This commit adjusts the local table size to be consistent regardless of the number of anonymous locals.
-rw-r--r--prism_compile.c14
-rw-r--r--test/ruby/test_compile_prism.rb2
2 files changed, 16 insertions, 0 deletions
diff --git a/prism_compile.c b/prism_compile.c
index c629fdfc61..b86b4760b0 100644
--- a/prism_compile.c
+++ b/prism_compile.c
@@ -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
@@ -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);
}
}
diff --git a/test/ruby/test_compile_prism.rb b/test/ruby/test_compile_prism.rb
index f89aabfc0e..192094bafe 100644
--- a/test/ruby/test_compile_prism.rb
+++ b/test/ruby/test_compile_prism.rb
@@ -1128,6 +1128,8 @@ module Prism
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 }")