ruby-changes:57588
From: Yusuke <ko1@a...>
Date: Fri, 6 Sep 2019 10:45:11 +0900 (JST)
Subject: [ruby-changes:57588] 0bfe3bf4d1 (master): Ignore an empty keyword splat for attr_reader/writer methods
https://git.ruby-lang.org/ruby.git/commit/?id=0bfe3bf4d1 From 0bfe3bf4d1a2cf6659a99c7466c733cf922ee0e0 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh <mame@r...> Date: Thu, 5 Sep 2019 17:03:38 +0900 Subject: Ignore an empty keyword splat for attr_reader/writer methods diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb index 5945a3b..04d3b24 100644 --- a/test/ruby/test_keyword.rb +++ b/test/ruby/test_keyword.rb @@ -759,6 +759,52 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L759 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 8435948..baf99c4 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 https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L2609 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)); -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/