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

ruby-changes:57720

From: Jeremy <ko1@a...>
Date: Thu, 12 Sep 2019 03:21:41 +0900 (JST)
Subject: [ruby-changes:57720] ed96c9f270 (master): Emit missing keyword argument separation warnings for define_method

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

From ed96c9f270829c7c07852f0aadcd88a58a13875b Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Wed, 11 Sep 2019 11:17:35 -0700
Subject: Emit missing keyword argument separation warnings for define_method

Previously, the warning functions skipped warning in these cases.
This removes the skipping, and uses a less descriptive warning
instead.

This affected both last argument to keyword warnings and keyword
split warnings.

diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb
index 5ddcfd8..506db34 100644
--- a/test/ruby/test_keyword.rb
+++ b/test/ruby/test_keyword.rb
@@ -1238,6 +1238,28 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L1238
     assert_equal([1, h2], c.m(**h2))
     assert_equal([1, h3], c.m(**h3))
     assert_equal([1, h3], c.m(a: 1, **h2))
+
+    c = Object.new
+    class << c
+      define_method(:m) {|*args, **opt| [args, opt] }
+    end
+    assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+      assert_equal([[], h], c.m(h))
+    end
+    assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+      assert_equal([[h], h], c.m(h, h))
+    end
+
+    c = Object.new
+    class << c
+      define_method(:m) {|arg=nil, a: nil| [arg, a] }
+    end
+    assert_warn(/The last argument is split into positional and keyword parameters.*for method/m) do
+      assert_equal([h2, 1], c.m(h3))
+    end
+    assert_warn(/The last argument is split into positional and keyword parameters.*for method/m) do
+      assert_equal([h2, 1], c.m(**h3))
+    end
   end
 
   def test_attr_reader_kwsplat
diff --git a/vm_args.c b/vm_args.c
index e471095..0f4f95e 100644
--- a/vm_args.c
+++ b/vm_args.c
@@ -597,8 +597,14 @@ rb_warn_keyword_to_last_hash(struct rb_calling_info *calling, const struct rb_ca https://github.com/ruby/ruby/blob/trunk/vm_args.c#L597
     }
     else {
         rb_warn("The keyword argument is passed as the last hash parameter");
-        rb_compile_warn(RSTRING_PTR(RARRAY_AREF(loc, 0)), FIX2INT(RARRAY_AREF(loc, 1)),
-                        "for `%"PRIsVALUE"' defined here", name);
+        if (name) {
+            rb_compile_warn(RSTRING_PTR(RARRAY_AREF(loc, 0)), FIX2INT(RARRAY_AREF(loc, 1)),
+                            "for `%"PRIsVALUE"' defined here", name);
+        }
+        else {
+            rb_compile_warn(RSTRING_PTR(RARRAY_AREF(loc, 0)), FIX2INT(RARRAY_AREF(loc, 1)),
+                            "for method defined here");
+        }
     }
 }
 
@@ -606,7 +612,6 @@ static inline void https://github.com/ruby/ruby/blob/trunk/vm_args.c#L612
 rb_warn_split_last_hash_to_keyword(struct rb_calling_info *calling, const struct rb_call_info *ci, const rb_iseq_t * const iseq)
 {
     VALUE name, loc;
-    if (calling->recv == Qundef) return;
     name = rb_id2str(ci->mid);
     loc = rb_iseq_location(iseq);
     if (NIL_P(loc)) {
@@ -615,8 +620,14 @@ rb_warn_split_last_hash_to_keyword(struct rb_calling_info *calling, const struct https://github.com/ruby/ruby/blob/trunk/vm_args.c#L620
     }
     else {
         rb_warn("The last argument is split into positional and keyword parameters");
-        rb_compile_warn(RSTRING_PTR(RARRAY_AREF(loc, 0)), FIX2INT(RARRAY_AREF(loc, 1)),
-                        "for `%"PRIsVALUE"' defined here", name);
+        if (calling->recv != Qundef) {
+            rb_compile_warn(RSTRING_PTR(RARRAY_AREF(loc, 0)), FIX2INT(RARRAY_AREF(loc, 1)),
+                            "for `%"PRIsVALUE"' defined here", name);
+        }
+        else {
+            rb_compile_warn(RSTRING_PTR(RARRAY_AREF(loc, 0)), FIX2INT(RARRAY_AREF(loc, 1)),
+                            "for method defined here");
+        }
     }
 }
 
@@ -624,7 +635,6 @@ static inline void https://github.com/ruby/ruby/blob/trunk/vm_args.c#L635
 rb_warn_last_hash_to_keyword(struct rb_calling_info *calling, const struct rb_call_info *ci, const rb_iseq_t * const iseq)
 {
     VALUE name, loc;
-    if (calling->recv == Qundef) return;
     name = rb_id2str(ci->mid);
     loc = rb_iseq_location(iseq);
     if (NIL_P(loc)) {
@@ -633,8 +643,14 @@ rb_warn_last_hash_to_keyword(struct rb_calling_info *calling, const struct rb_ca https://github.com/ruby/ruby/blob/trunk/vm_args.c#L643
     }
     else {
         rb_warn("The last argument is used as the keyword parameter");
-        rb_compile_warn(RSTRING_PTR(RARRAY_AREF(loc, 0)), FIX2INT(RARRAY_AREF(loc, 1)),
-                        "for `%"PRIsVALUE"' defined here", name);
+        if (calling->recv != Qundef) {
+            rb_compile_warn(RSTRING_PTR(RARRAY_AREF(loc, 0)), FIX2INT(RARRAY_AREF(loc, 1)),
+                            "for `%"PRIsVALUE"' defined here", name);
+        }
+        else {
+            rb_compile_warn(RSTRING_PTR(RARRAY_AREF(loc, 0)), FIX2INT(RARRAY_AREF(loc, 1)),
+                            "for method defined here");
+        }
     }
 }
 
-- 
cgit v0.10.2


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

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