From 7ac93e99a3db30ed41cbbe95df1d7be9a790b9cb Mon Sep 17 00:00:00 2001 From: Jemma Issroff Date: Wed, 13 Dec 2023 12:17:22 -0500 Subject: [PATCH] [PRISM] Account for multiple anonymous locals This commit adjusts the local table size to be consistent regardless of the number of anonymous locals. --- prism_compile.c | 14 ++++++++++++++ test/ruby/test_compile_prism.rb | 2 ++ 2 files changed, 16 insertions(+) diff --git a/prism_compile.c b/prism_compile.c index c629fdfc610a08..b86b4760b00ab3 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 f89aabfc0e5637..192094bafe1975 100644 --- a/test/ruby/test_compile_prism.rb +++ b/test/ruby/test_compile_prism.rb @@ -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 }")