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

ruby-changes:27817

From: nobu <ko1@a...>
Date: Fri, 22 Mar 2013 04:51:32 +0900 (JST)
Subject: [ruby-changes:27817] nobu:r39869 (trunk): vm_insnhelper.c: check required kwarg with resthash

nobu	2013-03-22 04:51:19 +0900 (Fri, 22 Mar 2013)

  New Revision: 39869

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

  Log:
    vm_insnhelper.c: check required kwarg with resthash
    
    * vm_insnhelper.c (vm_callee_setup_keyword_arg): should check required
      keyword arguments even if rest hash is defined.  [ruby-core:53608]
      [Bug #8139]

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 39868)
+++ ChangeLog	(revision 39869)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Mar 22 04:51:14 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* vm_insnhelper.c (vm_callee_setup_keyword_arg): should check required
+	  keyword arguments even if rest hash is defined.  [ruby-core:53608]
+	  [Bug #8139]
+
 Fri Mar 22 01:00:17 2013  Kazuhiro NISHIYAMA  <zn@m...>
 
 	* process.c (rb_execarg_addopt, run_exec_pgroup): use rb_pid_t
Index: vm_insnhelper.c
===================================================================
--- vm_insnhelper.c	(revision 39868)
+++ vm_insnhelper.c	(revision 39869)
@@ -1080,9 +1080,10 @@ vm_callee_setup_keyword_arg(const rb_ise https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1080
 	!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) {
+	i = 0;
+	if (iseq->arg_keyword_required) {
 	    VALUE missing = Qnil;
-	    for (i = 0; i < iseq->arg_keyword_required; i++) {
+	    for (; i < iseq->arg_keyword_required; i++) {
 		if (st_lookup(RHASH_TBL(keyword_hash), ID2SYM(iseq->arg_keyword_table[i]), 0))
 		    continue;
 		if (NIL_P(missing)) missing = rb_ary_tmp_new(1);
@@ -1091,6 +1092,8 @@ vm_callee_setup_keyword_arg(const rb_ise https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1092
 	    if (!NIL_P(missing)) {
 		keyword_error("missing", missing);
 	    }
+	}
+	if (iseq->arg_keyword_check) {
 	    for (j = i; i < iseq->arg_keywords; i++) {
 		if (st_lookup(RHASH_TBL(keyword_hash), ID2SYM(iseq->arg_keyword_table[i]), 0)) j++;
 	    }
@@ -1099,7 +1102,7 @@ vm_callee_setup_keyword_arg(const rb_ise https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1102
 	    }
 	}
     }
-    else if (iseq->arg_keyword_check && iseq->arg_keyword_required) {
+    else if (iseq->arg_keyword_required) {
 	VALUE missing = rb_ary_tmp_new(iseq->arg_keyword_required);
 	for (i = 0; i < iseq->arg_keyword_required; i++) {
 	    rb_ary_push(missing, ID2SYM(iseq->arg_keyword_table[i]));
Index: test/ruby/test_keyword.rb
===================================================================
--- test/ruby/test_keyword.rb	(revision 39868)
+++ test/ruby/test_keyword.rb	(revision 39869)
@@ -296,10 +296,17 @@ class TestKeywordArguments < Test::Unit: https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L296
     o = Object.new
     assert_nothing_raised(SyntaxError, feature7701) do
       eval("def o.foo(a:) a; end")
+      eval("def o.bar(a:,**b) [a, b]; end")
     end
     assert_raise(ArgumentError, feature7701) {o.foo}
     assert_equal(42, o.foo(a: 42), feature7701)
     assert_equal([[:keyreq, :a]], o.method(:foo).parameters, feature7701)
+
+    bug8139 = '[ruby-core:53608] [Bug #8139] required keyword argument with rest hash'
+    assert_equal([42, {}], o.bar(a: 42), feature7701)
+    assert_equal([[:keyreq, :a], [:keyrest, :b]], o.method(:bar).parameters, feature7701)
+    assert_raise(ArgumentError, bug8139) {o.bar(c: bug8139)}
+    assert_raise(ArgumentError, bug8139) {o.bar}
   end
 
   def test_block_required_keyword
@@ -310,5 +317,14 @@ class TestKeywordArguments < Test::Unit: https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L317
     assert_raise(ArgumentError, feature7701) {b.call}
     assert_equal(42, b.call(a: 42), feature7701)
     assert_equal([[:keyreq, :a]], b.parameters, feature7701)
+
+    bug8139 = '[ruby-core:53608] [Bug #8139] required keyword argument with rest hash'
+    b = assert_nothing_raised(SyntaxError, feature7701) do
+      break eval("proc {|a:, **b| [a, b]}")
+    end
+    assert_equal([42, {}], b.call(a: 42), feature7701)
+    assert_equal([[:keyreq, :a], [:keyrest, :b]], b.parameters, feature7701)
+    assert_raise(ArgumentError, bug8139) {b.call(c: bug8139)}
+    assert_raise(ArgumentError, bug8139) {b.call}
   end
 end

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

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