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

ruby-changes:57740

From: Jeremy <ko1@a...>
Date: Sat, 14 Sep 2019 02:55:44 +0900 (JST)
Subject: [ruby-changes:57740] 24b1b33975 (master): Correctly handle keywords for Method#call for cfuncs, send, and attr_*

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

From 24b1b339757ecab4539a2cb00a545bfcf885d3ef Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Fri, 13 Sep 2019 09:31:13 -0700
Subject: Correctly handle keywords for Method#call for cfuncs, send, and
 attr_*

This sets the correct VM frame flags when using Method#call to
call funcs, and handles empty keyword hashes for cfuncs,
attr_reader, and attr_writer. It also fixes calls to send through
Method#call.  It adds tests for all of those, as well as tests for
using Method#call to call define_method, lambda, and sym_procs
(which didn't require code changes).

diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb
index 506db34..5a85b85 100644
--- a/test/ruby/test_keyword.rb
+++ b/test/ruby/test_keyword.rb
@@ -561,6 +561,80 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L561
     assert_equal([1, h3], f[a: 1, **h2])
   end
 
+  def test_lambda_method_kwsplat_call
+    kw = {}
+    h = {:a=>1}
+    h2 = {'a'=>1}
+    h3 = {'a'=>1, :a=>1}
+
+    f = -> { true }
+    f = f.method(:call)
+    assert_equal(true, f[**{}])
+    assert_equal(true, f[**kw])
+    assert_raise(ArgumentError) { f[**h] }
+    assert_raise(ArgumentError) { f[a: 1] }
+    assert_raise(ArgumentError) { f[**h2] }
+    assert_raise(ArgumentError) { f[**h3] }
+
+    f = ->(a) { a }
+    f = f.method(:call)
+    assert_warn(/The keyword argument is passed as the last hash parameter/m) do
+      assert_equal(kw, f[**{}])
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter/m) do
+      assert_equal(kw, f[**kw])
+    end
+    assert_equal(h, f[**h])
+    assert_equal(h, f[a: 1])
+    assert_equal(h2, f[**h2])
+    assert_equal(h3, f[**h3])
+    assert_equal(h3, f[a: 1, **h2])
+
+    f = ->(**x) { x }
+    f = f.method(:call)
+    assert_equal(kw, f[**{}])
+    assert_equal(kw, f[**kw])
+    assert_equal(h, f[**h])
+    assert_equal(h, f[a: 1])
+    assert_equal(h2, f[**h2])
+    assert_equal(h3, f[**h3])
+    assert_equal(h3, f[a: 1, **h2])
+
+    f = ->(a, **x) { [a,x] }
+    f = f.method(:call)
+    assert_warn(/The keyword argument is passed as the last hash parameter/) do
+      assert_equal([{}, {}], f[**{}])
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter/) do
+      assert_equal([{}, {}], f[**kw])
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter/) do
+      assert_equal([h, {}], f[**h])
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter/) do
+      assert_equal([h, {}], f[a: 1])
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter/) do
+      assert_equal([h2, {}], f[**h2])
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter/) do
+      assert_equal([h3, {}], f[**h3])
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter/) do
+      assert_equal([h3, {}], f[a: 1, **h2])
+    end
+
+    f = ->(a=1, **x) { [a, x] }
+    f = f.method(:call)
+    assert_equal([1, kw], f[**{}])
+    assert_equal([1, kw], f[**kw])
+    assert_equal([1, h], f[**h])
+    assert_equal([1, h], f[a: 1])
+    assert_equal([1, h2], f[**h2])
+    assert_equal([1, h3], f[**h3])
+    assert_equal([1, h3], f[a: 1, **h2])
+  end
+
   def test_Class_new_kwsplat_call
     kw = {}
     h = {:a=>1}
@@ -669,6 +743,111 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L743
     assert_equal([1, h3], c[a: 1, **h2].args)
   end
 
+  def test_Class_new_method_kwsplat_call
+    kw = {}
+    h = {:a=>1}
+    h2 = {'a'=>1}
+    h3 = {'a'=>1, :a=>1}
+
+    sc = Class.new do
+      attr_reader :args
+    end
+
+    c = Class.new(sc) do
+      def initialize(*args)
+        @args = args
+      end
+    end.method(:new)
+    assert_equal([], c[**{}].args)
+    assert_equal([], c[**kw].args)
+    assert_equal([h], c[**h].args)
+    assert_equal([h], c[a: 1].args)
+    assert_equal([h2], c[**h2].args)
+    assert_equal([h3], c[**h3].args)
+    assert_equal([h3], c[a: 1, **h2].args)
+
+    c = Class.new(sc) do
+      def initialize; end
+    end.method(:new)
+    assert_nil(c[**{}].args)
+    assert_nil(c[**kw].args)
+    assert_raise(ArgumentError) { c[**h] }
+    assert_raise(ArgumentError) { c[a: 1] }
+    assert_raise(ArgumentError) { c[**h2] }
+    assert_raise(ArgumentError) { c[**h3] }
+    assert_raise(ArgumentError) { c[a: 1, **h2] }
+
+    c = Class.new(sc) do
+      def initialize(args)
+        @args = args
+      end
+    end.method(:new)
+    assert_warn(/The keyword argument is passed as the last hash parameter.* for `initialize'/m) do
+      assert_equal(kw, c[**{}].args)
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter.* for `initialize'/m) do
+      assert_equal(kw, c[**kw].args)
+    end
+    assert_equal(h, c[**h].args)
+    assert_equal(h, c[a: 1].args)
+    assert_equal(h2, c[**h2].args)
+    assert_equal(h3, c[**h3].args)
+    assert_equal(h3, c[a: 1, **h2].args)
+
+    c = Class.new(sc) do
+      def initialize(**args)
+        @args = args
+      end
+    end.method(:new)
+    assert_equal(kw, c[**{}].args)
+    assert_equal(kw, c[**kw].args)
+    assert_equal(h, c[**h].args)
+    assert_equal(h, c[a: 1].args)
+    assert_equal(h2, c[**h2].args)
+    assert_equal(h3, c[**h3].args)
+    assert_equal(h3, c[a: 1, **h2].args)
+
+    c = Class.new(sc) do
+      def initialize(arg, **args)
+        @args = [arg, args]
+      end
+    end.method(:new)
+    assert_warn(/The keyword argument is passed as the last hash parameter.* for `initialize'/m) do
+      assert_equal([kw, kw], c[**{}].args)
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter.* for `initialize'/m) do
+      assert_equal([kw, kw], c[**kw].args)
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter.* for `initialize'/m) do
+      assert_equal([h, kw], c[**h].args)
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter.* for `initialize'/m) do
+      assert_equal([h, kw], c[a: 1].args)
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter.* for `initialize'/m) do
+      assert_equal([h2, kw], c[**h2].args)
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter.* for `initialize'/m) do
+      assert_equal([h3, kw], c[**h3].args)
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter.* for `initialize'/m) do
+      assert_equal([h3, kw], c[a: 1, **h2].args)
+    end
+
+    c = Class.new(sc) do
+      def initialize(arg=1, **args)
+        @args = [arg=1, args]
+      end
+    end.method(:new)
+    assert_equal([1, kw], c[**{}].args)
+    assert_equal([1, kw], c[**kw].args)
+    assert_equal([1, h], c[**h].args)
+    assert_equal([1, h], c[a: 1].args)
+    assert_equal([1, h2], c[**h2].args)
+    assert_equal([1, h3], c[**h3].args)
+    assert_equal([1, h3], c[a: 1, **h2].args)
+  end
+
   def test_Method_call_kwsplat_call
     kw = {}
     h = {:a=>1}
@@ -954,6 +1133,106 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L1133
     assert_equal([1, h3], c.send(:m, a: 1, **h2))
   end
 
+  def test_send_method_kwsplat
+    kw = {}
+    h = {:a=>1}
+    h2 = {'a'=>1}
+    h3 = {'a'=>1, :a=>1}
+
+    c = Object.new
+    def c.m(*args)
+      args
+    end
+    m = c.method(:send)
+    assert_equal([], m.call(:m, **{}))
+    assert_equal([], m.call(:m, **kw))
+    assert_equal([h], m.call(:m, **h))
+    assert_equal([h], m.call(:m, a: 1))
+    assert_equal([h2], m.call(:m, **h2))
+    assert_equal([h3], m.call(:m, **h3))
+    assert_equal([h3], m.call(:m, a: 1, **h2))
+
+    c.singleton_class.remove_method(:m)
+    def c.m; end
+    m = c.method(:send)
+    assert_nil(m.call(:m, **{}))
+    assert_nil(m.call(:m, **kw))
+    assert_raise(ArgumentError) { m.call(:m, **h) }
+    assert_raise(ArgumentError) { m.call(:m, a: 1) }
+    assert_raise(ArgumentError) { m.call(:m, **h2) }
+    assert_raise(ArgumentError) { m.call(:m, **h3) }
+    assert_raise(ArgumentError) { m.call(:m, a: 1, **h2) }
+
+    c.singleton_class.remove_method(:m)
+    def c.m(args)
+      args
+    end
+    m = c.method(:send)
+    assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
+      assert_equal(kw, m.call(:m, **{}))
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
+      assert_equal(kw, m.call(:m, **kw))
+    end
+    assert_equal(h, m.call(:m, **h))
+    assert_equal(h, m.call(:m, a: 1))
+    assert_equal(h2, m.call(:m, **h2))
+    assert_equal(h3, m.call(:m, **h3))
+    assert_equal(h3, m.call(:m, a: 1, **h2))
+
+    c.singleton_class.remove_method(:m)
+    def c.m(**args)
+      args
+    end
+    m = c.method(:send)
+    assert_equal(kw, m.call(:m, **{}))
+    assert_equal(kw, m.call(:m, **kw))
+    assert_equal(h, m.call(:m, **h))
+    assert_equal(h, m.call(:m, a: 1))
+    assert_equal(h2, m.call(:m, **h2))
+    assert_equal(h3, m.call(:m, a: 1, **h2))
+
+    c.singleton_class.remove_method(:m)
+    def c.m(arg, **args)
+      [arg, args]
+    end
+    m = c.method(:send)
+    assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
+      m.call(:m, **{})
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
+      m.call(:m, **kw)
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
+      assert_equal([h, kw], m.call(:m, **h))
+    end
+    assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
+      assert_equal([h, kw], m.call(:m, a: 1))
+    end
+    assert_warn(/The keyword argument is passed as the las (... truncated)

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

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