From f53dfab95c30e222f67e610234f63d3e9189234d Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Fri, 19 Nov 2021 09:38:22 -0800 Subject: Add support for anonymous rest and keyword rest argument forwarding This allows for the following syntax: ```ruby def foo(*) bar(*) end def baz(**) quux(**) end ``` This is a natural addition after the introduction of anonymous block forwarding. Anonymous rest and keyword rest arguments were already supported in method parameters, this just allows them to be used as arguments to other methods. The same advantages of anonymous block forwarding apply to rest and keyword rest argument forwarding. This has some minor changes to #parameters output. Now, instead of `[:rest], [:keyrest]`, you get `[:rest, :*], [:keyrest, :**]`. These were already used for `...` forwarding, so I think it makes it more consistent to include them in other cases. If we want to use `[:rest], [:keyrest]` in both cases, that is also possible. I don't think the previous behavior of `[:rest], [:keyrest]` in the non-... case and `[:rest, :*], [:keyrest, :**]` in the ... case makes sense, but if we did want that behavior, we'll have to make more substantial changes, such as using a different ID in the ... forwarding case. Implements [Feature #18351] --- spec/ruby/core/method/parameters_spec.rb | 15 ++++++++++++--- spec/ruby/core/proc/parameters_spec.rb | 12 ++++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) (limited to 'spec/ruby/core') diff --git a/spec/ruby/core/method/parameters_spec.rb b/spec/ruby/core/method/parameters_spec.rb index 3fdaf9ce6f..67b42afafa 100644 --- a/spec/ruby/core/method/parameters_spec.rb +++ b/spec/ruby/core/method/parameters_spec.rb @@ -222,9 +222,18 @@ describe "Method#parameters" do m.method(:handled_via_method_missing).parameters.should == [[:rest]] end - it "adds nameless rest arg for \"star\" argument" do - m = MethodSpecs::Methods.new - m.method(:one_unnamed_splat).parameters.should == [[:rest]] + ruby_version_is '3.1' do + it "adds * rest arg for \"star\" argument" do + m = MethodSpecs::Methods.new + m.method(:one_unnamed_splat).parameters.should == [[:rest, :*]] + end + end + + ruby_version_is ''...'3.1' do + it "adds nameless rest arg for \"star\" argument" do + m = MethodSpecs::Methods.new + m.method(:one_unnamed_splat).parameters.should == [[:rest]] + end end it "returns the args and block for a splat and block argument" do diff --git a/spec/ruby/core/proc/parameters_spec.rb b/spec/ruby/core/proc/parameters_spec.rb index 5fb5cf418d..60620722f8 100644 --- a/spec/ruby/core/proc/parameters_spec.rb +++ b/spec/ruby/core/proc/parameters_spec.rb @@ -80,8 +80,16 @@ describe "Proc#parameters" do -> x {}.parameters.should == [[:req, :x]] end - it "adds nameless rest arg for \"star\" argument" do - -> x, * {}.parameters.should == [[:req, :x], [:rest]] + ruby_version_is '3.1' do + it "adds * rest arg for \"star\" argument" do + -> x, * {}.parameters.should == [[:req, :x], [:rest, :*]] + end + end + + ruby_version_is ''...'3.1' do + it "adds nameless rest arg for \"star\" argument" do + -> x, * {}.parameters.should == [[:req, :x], [:rest]] + end end it "does not add locals as block options with a block and splat" do -- cgit v1.2.3