diff options
author | Jeremy Evans <code@jeremyevans.net> | 2019-09-01 16:08:42 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2019-09-01 16:08:42 -0700 |
commit | 3fde9ef93781585a675b3e7184ad3a51072fb95f (patch) | |
tree | 05d58e88b03b1fa6850cdc418be819337af78784 | |
parent | 85dc89c90747d492e5ecbbbfc631a55088195f61 (diff) |
Fix keyword argument separation warning in method_missing
vm_call_method_missing was dropping VM_CALL_KW_SPLAT, so this just
makes it not drop it, to get the same behavior as calling the method
directly.
-rw-r--r-- | test/ruby/test_keyword.rb | 73 | ||||
-rw-r--r-- | vm_insnhelper.c | 3 |
2 files changed, 76 insertions, 0 deletions
diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb index 11fe44fffd..1803925bc6 100644 --- a/test/ruby/test_keyword.rb +++ b/test/ruby/test_keyword.rb @@ -443,6 +443,79 @@ class TestKeywordArguments < Test::Unit::TestCase assert_equal([1, h3], c.send(:m, **h3)) end + def test_send_kwsplat + kw = {} + h = {'a'=>1} + h2 = {'a'=>1} + h3 = {'a'=>1, :a=>1} + + c = Object.new + def c.method_missing(_, *args) + args + end + assert_equal([], c.send(:m, **{})) + assert_equal([], c.send(:m, **kw)) + assert_equal([h], c.send(:m, **h)) + assert_equal([h2], c.send(:m, **h2)) + assert_equal([h3], c.send(:m, **h3)) + + c.singleton_class.remove_method(:method_missing) + def c.method_missing(_); end + assert_nil(c.send(:m, **{})) + assert_nil(c.send(:m, **kw)) + assert_raise(ArgumentError) { c.send(:m, **h) } + assert_raise(ArgumentError) { c.send(:m, **h2) } + assert_raise(ArgumentError) { c.send(:m, **h3) } + + c.singleton_class.remove_method(:method_missing) + def c.method_missing(_, args) + args + end + assert_raise(ArgumentError) { c.send(:m, **{}) } + assert_raise(ArgumentError) { c.send(:m, **kw) } + assert_equal(h, c.send(:m, **h)) + assert_equal(h2, c.send(:m, **h2)) + assert_equal(h3, c.send(:m, **h3)) + + c.singleton_class.remove_method(:method_missing) + def c.method_missing(_, **args) + args + end + assert_equal(kw, c.send(:m, **{})) + assert_equal(kw, c.send(:m, **kw)) + assert_equal(h, c.send(:m, **h)) + assert_equal(h2, c.send(:m, **h2)) + assert_equal(h3, c.send(:m, **h3)) + + c.singleton_class.remove_method(:method_missing) + def c.method_missing(_, arg, **args) + [arg, args] + end + assert_raise(ArgumentError) { c.send(:m, **{}) } + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([kw, kw], c.send(:m, **kw)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([h, kw], c.send(:m, **h)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([h2, kw], c.send(:m, **h2)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([h3, kw], c.send(:m, **h3)) + end + + c.singleton_class.remove_method(:method_missing) + def c.method_missing(_, arg=1, **args) + [arg=1, args] + end + assert_equal([1, kw], c.send(:m, **{})) + assert_equal([1, kw], c.send(:m, **kw)) + assert_equal([1, h], c.send(:m, **h)) + assert_equal([1, h2], c.send(:m, **h2)) + assert_equal([1, h3], c.send(:m, **h3)) + end + def p1 Proc.new do |str: "foo", num: 424242| [str, num] diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 60d342daad..c65a9e260a 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -2408,6 +2408,9 @@ vm_call_method_missing(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, argc = calling->argc+1; ci_entry.flag = VM_CALL_FCALL | VM_CALL_OPT_SEND; + if (orig_ci->flag & VM_CALL_KW_SPLAT) { + ci_entry.flag |= VM_CALL_KW_SPLAT; + } ci_entry.mid = idMethodMissing; ci_entry.orig_argc = argc; ci = &ci_entry; |