[ruby-core:104491] [Ruby master Bug#18011] `Method#parameters` is incorrect for forwarded arguments
From:
josh.cheek@...
Date:
2021-07-04 08:48:26 UTC
List:
ruby-core #104491
Issue #18011 has been updated by josh.cheek (Josh Cheek).
jeremyevans0 (Jeremy Evans) wrote in #note-4:
> Note that from a caller's perspective, `[[:rest, :*]]` and `[[:rest, :*], [:keyrest, :**]]` have identical behavior. Both accept an arbitrary number of positional argument and arbitrary keywords.
Do you have thoughts on `super`? I can' t say you'll necessarily think differently in that case, but that was the context it initially arose in, and I switched it over to method wrapping because it made the example shorter. In the case of `super`, it is less clear to me that these can be considered equivalent, because I haven't touched the arguments, so I'm still thinking about them in a black-box sort of way, I'm definitely thinking about it from the caller's perspective (in my brain, personally, not as a general statement), so it's especially confusing when one works and one fails.
```ruby
RUBY_VERSION # => "3.0.1"
# Identical parent methods
def m1(a:) = 'success'
def m2(a:) = 'success'
# Child methods with identical bodies
def self.m1(*r, &b) = super
def self.m2(...) = super
# And allegedly identical parameters
method(:m1).parameters # => [[:rest, :r], [:block, :b]]
method(:m2).parameters # => [[:rest, :*], [:block, :&]]
# Yet one works and one fails
m1 a: 1 rescue $! # => #<ArgumentError: wrong number of arguments (given 1, expected 0; required keyword: a)>
m2 a: 1 rescue $! # => "success"
```
----------------------------------------
Bug #18011: `Method#parameters` is incorrect for forwarded arguments
https://bugs.ruby-lang.org/issues/18011#change-92758
* Author: josh.cheek (Josh Cheek)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [arm64-darwin20]
* Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
When asking a method about its parameters, forwarded arguments say they are a rest and a block (`wrapper1` in the example below).
However, when we use that signature, it raises an ArgumentError (`wrapper2` in the example below).
When I add a keyrest to the signature, it works as expected (`wrapper3` in the example below).
So I think forwarded arguments should include a keyrest in the output of `Method#parameters`
```ruby
def wrapped(ord, kw:) = [ord, {kw: kw}]
methods = [
def wrapper1(...) = wrapped(...),
def wrapper2(*r, &b) = wrapped(*r, &b),
def wrapper3(*r, **k, &b) = wrapped(*r, **k, &b),
]
methods.each do |name|
puts File.read(__FILE__)[/#{name}\(.*?\)/]
puts " params: #{method(name).parameters.inspect}"
puts " result: #{(method(name).call 123, kw: 456 rescue $!).inspect}"
puts
end
```
Output:
```
wrapper1(...)
params: [[:rest, :*], [:block, :&]]
result: [123, {:kw=>456}]
wrapper2(*r, &b)
params: [[:rest, :r], [:block, :b]]
result: #<ArgumentError: wrong number of arguments (given 2, expected 1; required keyword: kw)>
wrapper3(*r, **k, &b)
params: [[:rest, :r], [:keyrest, :k], [:block, :b]]
result: [123, {:kw=>456}]
```
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>