Issue #16154 has been updated by jeremyevans0 (Jeremy Evans).


As some background for the dev meeting, I tried a couple of different approaches before this that didn't work.

One approach was to flag methods and have all positional hashes be treated as positional hashes (Ruby 3 behavior).  That didn't work as it changed the behavior for the case where `bar` accepts keyword arguments.

Another approach was to flag methods and ignore positional hash to keyword warnings for the method.  But in that case, this is no warning for the case where `bar` accepts keyword arguments, even though that behavior will change in Ruby 3.

I chose to use `Module#pass_positional_hash` as the way to turn on the flag, as that seemed easiest.  I thought about using a per-method magic comment, but the issue with that approach is it probably doesn't work well for methods defined with `define_method`.

Instead of using a VM frame flag in the next frame when doing positional hash to keyword argument conversion, we could set an object flag on the keyword argument hash, and check the flag later.  That will probably be more challenging to implement, as I think it will require compiler modifications, because when you use `**kw`, the object passed to the callee is not `kw`, but a copy of `kw`.  It also raises issues if pass `kw` to another method as a positional argument, and that method splats it.

I've updated the pull request to document that `pass_positional_hash` should only be used with methods that do not modify keyword arguments or append positional arguments before delegation.  Doing so can result in behavior changes in Ruby 3 without any warning in 2.7.  See https://github.com/jeremyevans/ruby/commit/97eeab3d90fe2f560996618c391b68aed4783610#diff-5dbe613f725860801fa4c2c812b1d181.

----------------------------------------
Bug #16154: lib/delegate.rb issues keyword argument warnings
https://bugs.ruby-lang.org/issues/16154#change-81505

* Author: jeremyevans0 (Jeremy Evans)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 
* Backport: 2.5: DONTNEED, 2.6: DONTNEED
----------------------------------------
In the current master branch, `lib/delegate.rb` (e.g. `Delegator`/`SimpleDelegator`/`DelegateClass`) does not delegate keyword arguments, leading to keyword argument warnings when using it.  It is simple to fix this by changing it to delegate keyword arguments, and I have a commit that does this (https://github.com/ruby/ruby/pull/2439/commits/30a70491b11d5235877a13d1a1d3fe0157960a64).

However, one issue with this approach, that you can notice at the bottom of the commit, is that it warns in a case where the behavior will not change between 2.6 and 3.0:

```ruby
foo = Object.new
def foo.bar(*args)
  args
end
d = SimpleDelegator.new(foo)
d.bar({a: 1})
# warns in 2.7, even though 2.6, 2.7, and 3.0 will return [{a: 1}]
```

The reason it warns even though the behavior will not change is that there is a behavior change, but the behavior change is inside the delegator.  When you call `d.bar`, the positional hash is converted to a keyword argument, which generates a warning.  When `d.bar` calls `foo.bar`, the keyword splat is converted back to a positional hash.  Because of this hash->keyword->hash conversion, the net result is behavior does not change outside the delegator, and therefore it would be great if we can somehow flag this particular case as not warning, while still warning in the case where `foo.bar` would accept keyword arguments (as in that case, behavior will change).

I came up with an implementation that makes delegate not emit the warning in the case where behavior does not change, but still emit the warning in the case where behavior will change (https://github.com/ruby/ruby/pull/2439/commits/6b7bd4f89143d692932dc89fadc341980816ee14).  This works by flagging a method that accepts keyword arguments.  If the flag is set for the method, and the method is passed a positional hash that is converted to a keyword argument, no warning is emitted, and instead a VM frame flag is set in the next frame so that keyword splats inside that method are converted to positional arguments.  The method flag is enabled by calling a `Module#pass_positional_hash` method, and this method would only exist in 2.7, since it is only for skipping this 2.7-specific warning.

I'm not sure this implementation approach is best, and would be open to any approach that allows delegate to not emit a warning for the code above, as long as it doesn't change the default keyword behavior in 2.7.

Attached is a patch that implements this, and there is also a pull request that has passed CI for it (https://github.com/ruby/ruby/pull/2439).

---Files--------------------------------
delegate-keyword-argument-separation.patch (19.1 KB)


-- 
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>