ruby-changes:60414
From: Jeremy <ko1@a...>
Date: Sun, 15 Mar 2020 19:35:53 +0900 (JST)
Subject: [ruby-changes:60414] e70d52b0c3 (ruby_2_7): Make ruby2_keywords methods correctly handle **{} optimization
https://git.ruby-lang.org/ruby.git/commit/?id=e70d52b0c3 From e70d52b0c377f8b2ed04311710c0ca9f4ebabd90 Mon Sep 17 00:00:00 2001 From: Jeremy Evans <code@j...> Date: Mon, 24 Feb 2020 13:19:37 -0800 Subject: Make ruby2_keywords methods correctly handle **{} optimization Previously, this code: ruby2_keywords def foo(*a) a.last end foo(**{}) Returned an empty frozen hash. However, the final hash should not be frozen in this case, as it wouldn't be if foo accepted a keyword splat. Use a new unfrozen empty hash instead of reusing the frozen empty hash in this case. Fixes [Bug #16642] diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb index bbf3953..a045955 100644 --- a/test/ruby/test_keyword.rb +++ b/test/ruby/test_keyword.rb @@ -5051,4 +5051,18 @@ class TestKeywordArgumentsSymProcRefinements < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L5051 mock.new.foo end end + + def test_ruby2_keywords_hash_empty_kw_splat + def self.foo(*a) a.last end + singleton_class.send(:ruby2_keywords, :foo) + bug16642 = '[ruby-core:97203] [Bug #16642]' + + res = foo(**{}) + assert_equal({}, res, bug16642) + assert_equal(false, res.frozen?, bug16642) + + res = foo(*[], **{}) + assert_equal({}, res, bug16642) + assert_equal(false, res.frozen?, bug16642) + end end diff --git a/vm_args.c b/vm_args.c index 7bf61ce..1efb84a 100644 --- a/vm_args.c +++ b/vm_args.c @@ -821,6 +821,10 @@ setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * co https://github.com/ruby/ruby/blob/trunk/vm_args.c#L821 kw_flag &= ~VM_CALL_KW_SPLAT; } else { + if (RB_TYPE_P(rest_last, T_HASH) && rb_obj_frozen_p(rest_last)) { + rest_last = rb_hash_new(); + RARRAY_ASET(args->rest, len - 1, rest_last); + } flag_keyword_hash = rest_last; } } @@ -844,6 +848,10 @@ setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * co https://github.com/ruby/ruby/blob/trunk/vm_args.c#L848 kw_flag &= ~VM_CALL_KW_SPLAT; } else { + if (RB_TYPE_P(last_arg, T_HASH) && rb_obj_frozen_p(last_arg)) { + last_arg = rb_hash_new(); + args->argv[args->argc-1] = last_arg; + } flag_keyword_hash = last_arg; } } -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/