From b53d10a5b2bd87238e85cf53a3ad6b8533a9f868 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sat, 9 Aug 2014 09:44:18 +0900 Subject: [PATCH] vm.c: allow to splat non-symbol keys * vm.c (core_hash_merge_kwd): allow non-symbol keys to splat a hash. those pairs would be passed as a normal hash argument and should be checked by called method. [ruby-core:64251] [Feature #10118] --- test/ruby/test_syntax.rb | 4 +++- vm.c | 10 +--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index 1e988dd..960eb1d 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -113,7 +113,9 @@ class TestSyntax < Test::Unit::TestCase h = {k3: 31} assert_raise(ArgumentError) {o.kw(**h)} h = {"k1"=>11, k2: 12} - assert_raise(TypeError) {o.kw(**h)} + assert_raise(ArgumentError) {o.kw(**h)} + assert_equal({"k1"=>11, :"k2"=>12, :"k3"=>31}, {k3: 31, **h}) + assert_equal({"k1"=>11, :"k2"=>12}, {**h}) end def test_keyword_self_reference diff --git a/vm.c b/vm.c index f9a2c3d..c980079 100644 --- a/vm.c +++ b/vm.c @@ -2372,20 +2372,12 @@ kwmerge_ii(st_data_t *key, st_data_t *value, st_data_t arg, int existing) static int kwmerge_i(VALUE key, VALUE value, VALUE hash) { - if (!SYMBOL_P(key)) Check_Type(key, T_SYMBOL); if (st_update(RHASH_TBL_RAW(hash), key, kwmerge_ii, (st_data_t)value) == 0) { /* !existing */ RB_OBJ_WRITTEN(hash, Qundef, value); } return ST_CONTINUE; } -static int -kwcheck_i(VALUE key, VALUE value, VALUE hash) -{ - if (!SYMBOL_P(key)) Check_Type(key, T_SYMBOL); - return ST_CONTINUE; -} - static VALUE m_core_hash_merge_kwd(int argc, VALUE *argv, VALUE recv) { @@ -2403,7 +2395,7 @@ core_hash_merge_kwd(int argc, VALUE *argv) kw = argv[argc-1]; kw = rb_convert_type(kw, T_HASH, "Hash", "to_hash"); if (argc < 2) hash = kw; - rb_hash_foreach(kw, argc < 2 ? kwcheck_i : kwmerge_i, hash); + else rb_hash_foreach(kw, kwmerge_i, hash); return hash; } -- 2.0.4