[前][次][番号順一覧][スレッド一覧]

ruby-changes:33643

From: nobu <ko1@a...>
Date: Sat, 26 Apr 2014 10:55:38 +0900 (JST)
Subject: [ruby-changes:33643] nobu:r45724 (trunk): compile.c: non-destructive keyword splat

nobu	2014-04-26 10:55:34 +0900 (Sat, 26 Apr 2014)

  New Revision: 45724

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45724

  Log:
    compile.c: non-destructive keyword splat
    
    * compile.c (compile_array_): make copy a first hash not to modify
      the argument itself.  keyword splat should be non-destructive.
      [ruby-core:62161] [Bug #9776]

  Modified files:
    trunk/ChangeLog
    trunk/compile.c
    trunk/test/ruby/test_keyword.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 45723)
+++ ChangeLog	(revision 45724)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Apr 26 10:55:33 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* compile.c (compile_array_): make copy a first hash not to modify
+	  the argument itself.  keyword splat should be non-destructive.
+	  [ruby-core:62161] [Bug #9776]
+
 Sat Apr 26 08:05:36 2014  Tanaka Akira  <akr@f...>
 
 	* test/ruby/test_process.rb (test_rlimit_nofile): Don't limit
Index: compile.c
===================================================================
--- compile.c	(revision 45723)
+++ compile.c	(revision 45724)
@@ -2479,6 +2479,7 @@ compile_array_(rb_iseq_t *iseq, LINK_ANC https://github.com/ruby/ruby/blob/trunk/compile.c#L2479
 			    if (i > 0 || !first) ADD_INSN(ret, line, swap);
 			    COMPILE(ret, "keyword splat", kw);
 			    ADD_SEND(ret, line, ID2SYM(id_core_hash_merge_kwd), nhash);
+			    if (nhash == INT2FIX(1)) ADD_SEND(ret, line, ID2SYM(rb_intern("dup")), INT2FIX(0));
 			}
 			first = 0;
 			break;
Index: test/ruby/test_keyword.rb
===================================================================
--- test/ruby/test_keyword.rb	(revision 45723)
+++ test/ruby/test_keyword.rb	(revision 45724)
@@ -439,6 +439,19 @@ class TestKeywordArguments < Test::Unit: https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L439
     assert_equal([1, 2, 1, [], {:f=>5}, 2, {}], a.new.foo(1, 2, f:5), bug8993)
   end
 
+  def test_splat_keyword_nondestructive
+    bug9776 = '[ruby-core:62161] [Bug #9776]'
+
+    h = {a: 1}
+    assert_equal({a:1, b:2}, {**h, b:2})
+    assert_equal({a:1}, h, bug9776)
+
+    pr = proc {|**opt| next opt}
+    assert_equal({a: 1}, pr.call(**h))
+    assert_equal({a: 1, b: 2}, pr.call(**h, b: 2))
+    assert_equal({a: 1}, h, bug9776)
+  end
+
   def test_gced_object_in_stack
     bug8964 = '[ruby-dev:47729] [Bug #8964]'
     assert_normal_exit %q{

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]