diff options
author | Yusuke Endoh <[email protected]> | 2019-09-05 17:03:38 +0900 |
---|---|---|
committer | Jeremy Evans <[email protected]> | 2019-09-05 17:47:12 -0700 |
commit | 0bfe3bf4d1a2cf6659a99c7466c733cf922ee0e0 (patch) | |
tree | 758d94cc9602d676d9a2535ea10dee757b187a07 | |
parent | 437ff408790d5426e0ee03a4b22171bf745471a7 (diff) |
Ignore an empty keyword splat for attr_reader/writer methods
-rw-r--r-- | test/ruby/test_keyword.rb | 46 | ||||
-rw-r--r-- | vm_insnhelper.c | 4 |
2 files changed, 48 insertions, 2 deletions
diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb index 5945a3b87e..04d3b247ae 100644 --- a/test/ruby/test_keyword.rb +++ b/test/ruby/test_keyword.rb @@ -759,6 +759,52 @@ class TestKeywordArguments < Test::Unit::TestCase assert_equal([1, h3], c.m(a: 1, **h2)) end + def test_attr_reader_kwsplat + kw = {} + h = {:a=>1} + h2 = {'a'=>1} + h3 = {'a'=>1, :a=>1} + + c = Object.new + class << c + attr_reader :m + end + assert_nil(c.m(**{})) + assert_nil(c.m(**kw)) + assert_raise(ArgumentError) { c.m(**h) } + assert_raise(ArgumentError) { c.m(a: 1) } + assert_raise(ArgumentError) { c.m(**h2) } + assert_raise(ArgumentError) { c.m(**h3) } + assert_raise(ArgumentError) { c.m(a: 1, **h2) } + end + + def test_attr_writer_kwsplat + kw = {} + h = {:a=>1} + h2 = {'a'=>1} + h3 = {'a'=>1, :a=>1} + + c = Object.new + class << c + attr_writer :m + end + assert_raise(ArgumentError) { c.send(:m=, **{}) } + assert_raise(ArgumentError) { c.send(:m=, **kw) } + assert_equal(h, c.send(:m=, **h)) + assert_equal(h, c.send(:m=, a: 1)) + assert_equal(h2, c.send(:m=, **h2)) + assert_equal(h3, c.send(:m=, **h3)) + assert_equal(h3, c.send(:m=, a: 1, **h2)) + + assert_equal(42, c.send(:m=, 42, **{})) + assert_equal(42, c.send(:m=, 42, **kw)) + assert_raise(ArgumentError) { c.send(:m=, 42, **h) } + assert_raise(ArgumentError) { c.send(:m=, 42, a: 1) } + assert_raise(ArgumentError) { c.send(:m=, 42, **h2) } + assert_raise(ArgumentError) { c.send(:m=, 42, **h3) } + assert_raise(ArgumentError) { c.send(:m=, 42, a: 1, **h2) } + end + def p1 Proc.new do |str: "foo", num: 424242| [str, num] diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 8435948994..baf99c4d66 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -2609,14 +2609,14 @@ vm_call_method_each_type(rb_execution_context_t *ec, rb_control_frame_t *cfp, st return vm_call_cfunc(ec, cfp, calling, ci, cc); case VM_METHOD_TYPE_ATTRSET: - CALLER_SETUP_ARG(cfp, calling, ci, 0); + CALLER_SETUP_ARG(cfp, calling, ci, 1); rb_check_arity(calling->argc, 1, 1); cc->aux.index = 0; CC_SET_FASTPATH(cc, vm_call_attrset, !((ci->flag & VM_CALL_ARGS_SPLAT) || (ci->flag & VM_CALL_KWARG))); return vm_call_attrset(ec, cfp, calling, ci, cc); case VM_METHOD_TYPE_IVAR: - CALLER_SETUP_ARG(cfp, calling, ci, 0); + CALLER_SETUP_ARG(cfp, calling, ci, 1); rb_check_arity(calling->argc, 0, 0); cc->aux.index = 0; CC_SET_FASTPATH(cc, vm_call_ivar, !(ci->flag & VM_CALL_ARGS_SPLAT)); |