From 708fa7740479823e0758623d43afee7b13beba21 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Mon, 22 Jan 2024 13:52:41 -0500 Subject: [PATCH] [PRISM] Fix keyword arguments in IndexOrWriteNode Fixes ruby/prism#2236. --- prism_compile.c | 4 ++-- test/ruby/test_compile_prism.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/prism_compile.c b/prism_compile.c index e5ba24115d..3fd9efbf0d 100644 --- a/prism_compile.c +++ b/prism_compile.c @@ -1298,9 +1298,9 @@ pm_compile_index_and_or_write_node(bool and_node, pm_node_t *receiver, pm_node_t ADD_INSNL(ret, &dummy_line_node, jump, lfin); ADD_LABEL(ret, label); if (!popped) { - ADD_INSN1(ret, &dummy_line_node, setn, FIXNUM_INC(argc, 2 + block_offset)); + ADD_INSN1(ret, &dummy_line_node, setn, FIXNUM_INC(argc, 2 + block_offset + (keywords ? keywords->keyword_len : 0))); } - ADD_INSN1(ret, &dummy_line_node, adjuststack, FIXNUM_INC(argc, 2 + block_offset)); + ADD_INSN1(ret, &dummy_line_node, adjuststack, FIXNUM_INC(argc, 2 + block_offset + (keywords ? keywords->keyword_len : 0))); ADD_LABEL(ret, lfin); return; diff --git a/test/ruby/test_compile_prism.rb b/test/ruby/test_compile_prism.rb index fd5787e467..f54097ab66 100644 --- a/test/ruby/test_compile_prism.rb +++ b/test/ruby/test_compile_prism.rb @@ -425,6 +425,15 @@ module Prism hash CODE + # Test with keyword arguments + assert_prism_eval(<<~RUBY) + h = Object.new + def h.[](**b) = 0 + def h.[]=(*a, **b); end + + h[foo: 1] ||= 2 + RUBY + # Test with keyword splat assert_prism_eval(<<~RUBY) h = Object.new