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

ruby-changes:26668

From: nobu <ko1@a...>
Date: Mon, 7 Jan 2013 12:09:41 +0900 (JST)
Subject: [ruby-changes:26668] nobu:r38719 (trunk): vm_insnhelper.c: keyrest should not overwrite rest arg

nobu	2013-01-07 12:09:28 +0900 (Mon, 07 Jan 2013)

  New Revision: 38719

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

  Log:
    vm_insnhelper.c: keyrest should not overwrite rest arg
    
    * vm_insnhelper.c (vm_callee_setup_arg_complex, vm_yield_setup_block_args):
      set keyrest hash after making rest array, so that the last element
      will not be overwritten.  [ruby-core:51278] [Bug #7665]

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_keyword.rb
    trunk/vm_insnhelper.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38718)
+++ ChangeLog	(revision 38719)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Jan  7 12:09:24 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* vm_insnhelper.c (vm_callee_setup_arg_complex, vm_yield_setup_block_args):
+	  set keyrest hash after making rest array, so that the last element
+	  will not be overwritten.  [ruby-core:51278] [Bug #7665]
+
 Mon Jan  7 09:37:24 2013  Koichi Sasada  <ko1@a...>
 
 	* NEWS: add a NEWS entry about RubyVM.
Index: vm_insnhelper.c
===================================================================
--- vm_insnhelper.c	(revision 38718)
+++ vm_insnhelper.c	(revision 38719)
@@ -1065,13 +1065,13 @@ vm_caller_setup_args(const rb_thread_t * https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1065
 }
 
 static inline int
-vm_callee_setup_keyword_arg(const rb_iseq_t *iseq, int argc, VALUE *orig_argv)
+vm_callee_setup_keyword_arg(const rb_iseq_t *iseq, int argc, VALUE *orig_argv, VALUE *kwd)
 {
-    VALUE keyword_hash = Qnil;
+    VALUE keyword_hash;
     int i, j;
 
-    if (argc > 0) keyword_hash = rb_check_hash_type(orig_argv[argc-1]);
-    if (!NIL_P(keyword_hash)) {
+    if (argc > 0 &&
+	!NIL_P(keyword_hash = rb_check_hash_type(orig_argv[argc-1]))) {
 	argc--;
 	keyword_hash = rb_hash_dup(keyword_hash);
 	if (iseq->arg_keyword_check) {
@@ -1087,7 +1087,7 @@ vm_callee_setup_keyword_arg(const rb_ise https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1087
 	keyword_hash = rb_hash_new();
     }
 
-    orig_argv[iseq->arg_keyword] = keyword_hash;
+    *kwd = keyword_hash;
 
     return argc;
 }
@@ -1102,13 +1102,14 @@ vm_callee_setup_arg_complex(rb_thread_t https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1102
     const int orig_argc = ci->argc;
     int argc = orig_argc;
     VALUE *argv = orig_argv;
+    VALUE keyword_hash = Qnil;
     rb_num_t opt_pc = 0;
 
     th->mark_stack_len = argc + iseq->arg_size;
 
     /* keyword argument */
     if (iseq->arg_keyword != -1) {
-	argc = vm_callee_setup_keyword_arg(iseq, argc, orig_argv);
+	argc = vm_callee_setup_keyword_arg(iseq, argc, orig_argv, &keyword_hash);
     }
 
     /* mandatory */
@@ -1154,6 +1155,11 @@ vm_callee_setup_arg_complex(rb_thread_t https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1155
 	argc = 0;
     }
 
+    /* keyword argument */
+    if (iseq->arg_keyword != -1) {
+	orig_argv[iseq->arg_keyword] = keyword_hash;
+    }
+
     /* block arguments */
     if (iseq->arg_block != -1) {
 	VALUE blockval = Qnil;
@@ -2088,13 +2094,14 @@ vm_yield_setup_block_args(rb_thread_t *t https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L2094
     int argc = orig_argc;
     const int m = iseq->argc;
     VALUE ary, arg0;
+    VALUE keyword_hash = Qnil;
     int opt_pc = 0;
 
     th->mark_stack_len = argc;
 
     /* keyword argument */
     if (iseq->arg_keyword != -1) {
-	argc = vm_callee_setup_keyword_arg(iseq, argc, argv);
+	argc = vm_callee_setup_keyword_arg(iseq, argc, argv, &keyword_hash);
     }
 
     /*
@@ -2155,6 +2162,11 @@ vm_yield_setup_block_args(rb_thread_t *t https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L2162
 	th->mark_stack_len = iseq->arg_size;
     }
 
+    /* keyword argument */
+    if (iseq->arg_keyword != -1) {
+	argv[iseq->arg_keyword] = keyword_hash;
+    }
+
     /* {|&b|} */
     if (iseq->arg_block != -1) {
 	VALUE procval = Qnil;
Index: test/ruby/test_keyword.rb
===================================================================
--- test/ruby/test_keyword.rb	(revision 38718)
+++ test/ruby/test_keyword.rb	(revision 38719)
@@ -255,4 +255,15 @@ class TestKeywordArguments < Test::Unit: https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L255
     assert_equal(["foo", 111111], m1(num: 111111, &blk))
     assert_equal(["bar", 111111], m1(str: "bar", num: 111111, &blk))
   end
+
+  def rest_keyrest(*args, **opt)
+    return *args, opt
+  end
+
+  def test_rest_keyrest
+    bug7665 = '[ruby-core:51278]'
+    expect = [*%w[foo bar], {zzz: 42}]
+    assert_equal(expect, rest_keyrest(*expect), bug7665)
+    assert_equal(expect, proc {|*args, **opt| next *args, opt}.call(*expect), bug7665)
+  end
 end

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

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