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

ruby-changes:58024

From: Jeremy <ko1@a...>
Date: Mon, 30 Sep 2019 02:34:31 +0900 (JST)
Subject: [ruby-changes:58024] 869e4f6e4c (master): Fix or suppress keyword argument separation warnings in util_spec

https://git.ruby-lang.org/ruby.git/commit/?id=869e4f6e4c

From 869e4f6e4c683bf8e76ae7db54a26b33fb925410 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Sun, 29 Sep 2019 09:15:43 -0700
Subject: Fix or suppress keyword argument separation warnings in util_spec

Some warnings are because the @o.rb_scan_args call doesn't
include keyword arguments, but the first argument is passed to
rb_scan_args may have a last hash treated as keywords.  Those
should be handled using rb_scan_args_kw on Ruby 2.7.

Other warnings are for the deprecated rb_scan_args behavior to
split option hashes or treat a nil argument as an option hash.
Those warnings should just be suppressed.

diff --git a/spec/ruby/optional/capi/ext/util_spec.c b/spec/ruby/optional/capi/ext/util_spec.c
index 50a5fcb..e579b5e 100644
--- a/spec/ruby/optional/capi/ext/util_spec.c
+++ b/spec/ruby/optional/capi/ext/util_spec.c
@@ -16,7 +16,14 @@ VALUE util_spec_rb_scan_args(VALUE self, VALUE argv, VALUE fmt, VALUE expected, https://github.com/ruby/ruby/blob/trunk/spec/ruby/optional/capi/ext/util_spec.c#L16
     args[i] = rb_ary_entry(argv, i);
   }
 
-  result = rb_scan_args(argc, args, RSTRING_PTR(fmt), &a1, &a2, &a3, &a4, &a5, &a6);
+  if (*RSTRING_PTR(fmt) == 'k') {
+#ifdef RB_SCAN_ARGS_KEYWORDS
+    result = rb_scan_args_kw(RB_SCAN_ARGS_KEYWORDS, argc, args, RSTRING_PTR(fmt)+1, &a1, &a2, &a3, &a4, &a5, &a6);
+#endif
+  }
+  else {
+    result = rb_scan_args(argc, args, RSTRING_PTR(fmt), &a1, &a2, &a3, &a4, &a5, &a6);
+  }
 
   switch(NUM2INT(expected)) {
   case 6:
diff --git a/spec/ruby/optional/capi/util_spec.rb b/spec/ruby/optional/capi/util_spec.rb
index ee48e2e..3556c8c 100644
--- a/spec/ruby/optional/capi/util_spec.rb
+++ b/spec/ruby/optional/capi/util_spec.rb
@@ -11,6 +11,7 @@ describe "C-API Util function" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/optional/capi/util_spec.rb#L11
     before :each do
       @prc = -> { 1 }
       @acc = []
+      @keyword_prefix = 'k' if RUBY_VERSION >= '2.7'
       ScratchPad.record @acc
     end
 
@@ -99,13 +100,13 @@ describe "C-API Util function" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/optional/capi/util_spec.rb#L100
 
     it "assigns Hash arguments" do
       h = {a: 1, b: 2}
-      @o.rb_scan_args([h], "0:", 1, @acc).should == 0
+      @o.rb_scan_args([h], "#{@keyword_prefix}0:", 1, @acc).should == 0
       ScratchPad.recorded.should == [h]
     end
 
     it "assigns required and Hash arguments" do
       h = {a: 1, b: 2}
-      @o.rb_scan_args([1, h], "1:", 2, @acc).should == 1
+      @o.rb_scan_args([1, h], "#{@keyword_prefix}1:", 2, @acc).should == 1
       ScratchPad.recorded.should == [1, h]
     end
 
@@ -115,7 +116,9 @@ describe "C-API Util function" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/optional/capi/util_spec.rb#L116
     end
 
     it "assigns required and Hash arguments with nil Hash" do
-      @o.rb_scan_args([1, nil], "1:", 2, @acc).should == 1
+      suppress_warning do
+        @o.rb_scan_args([1, nil], "1:", 2, @acc).should == 1
+      end
       ScratchPad.recorded.should == [1, nil]
     end
 
@@ -126,7 +129,7 @@ describe "C-API Util function" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/optional/capi/util_spec.rb#L129
 
     it "assigns required, optional, splat, post-splat, Hash and block arguments" do
       h = {a: 1, b: 2}
-      @o.rb_scan_args([1, 2, 3, 4, 5, h], "11*1:&", 6, @acc, &@prc).should == 5
+      @o.rb_scan_args([1, 2, 3, 4, 5, h], "#{@keyword_prefix}11*1:&", 6, @acc, &@prc).should == 5
       ScratchPad.recorded.should == [1, 2, [3, 4], 5, h, @prc]
     end
 
@@ -134,7 +137,9 @@ describe "C-API Util function" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/optional/capi/util_spec.rb#L137
     it "rejects non-keyword arguments" do
       h = {1 => 2, 3 => 4}
       -> {
-        @o.rb_scan_args([h], "0:", 1, @acc)
+        suppress_warning do
+          @o.rb_scan_args([h], "#{@keyword_prefix}0:", 1, @acc)
+        end
       }.should raise_error(ArgumentError)
       ScratchPad.recorded.should == []
     end
@@ -142,14 +147,18 @@ describe "C-API Util function" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/optional/capi/util_spec.rb#L147
     it "rejects required and non-keyword arguments" do
       h = {1 => 2, 3 => 4}
       -> {
-        @o.rb_scan_args([1, h], "1:", 2, @acc)
+        suppress_warning do
+          @o.rb_scan_args([1, h], "#{@keyword_prefix}1:", 2, @acc)
+        end
       }.should raise_error(ArgumentError)
       ScratchPad.recorded.should == []
     end
 
     it "considers the hash as a post argument when there is a splat" do
       h = {1 => 2, 3 => 4}
-      @o.rb_scan_args([1, 2, 3, 4, 5, h], "11*1:&", 6, @acc, &@prc).should == 6
+      suppress_warning do
+        @o.rb_scan_args([1, 2, 3, 4, 5, h], "#{@keyword_prefix}11*1:&", 6, @acc, &@prc).should == 6
+      end
       ScratchPad.recorded.should == [1, 2, [3, 4, 5], h, nil, @prc]
     end
   end
-- 
cgit v0.10.2


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

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