ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: merch-redmine@jeremyevans.net
To: ruby-core@ruby-lang.org
Subject: [ruby-core:94912] [Ruby master Misc#16157] What is the correct and *portable* way to do generic delegation?
Date: Wed, 11 Sep 2019 23:20:18 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-81526.20190911232017.f57669863144d122@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16157.20190909141042@ruby-lang.org

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


Dan0042 (Daniel DeLorme) wrote:
> Of course this is all *extremely* unconventional usage and doesn't really deserve a fix. But I thought it was weird/interesting.

Very interesting actually.  However, while you think this method is written in C, it's actually not, at least not the version you are calling:

```
$ ruby --disable-gems -e 'warn({x: 1})'
$ ruby -e 'warn({x: 1})'
-e:1: warning: The last argument is used as the keyword parameter
/home/jeremy/tmp/ruby/lib/rubygems/core_ext/kernel_warn.rb:15: warning: for method defined here
Traceback (most recent call last):
        1: from -e:1:in `<main>'
/home/jeremy/tmp/ruby/lib/rubygems/core_ext/kernel_warn.rb:15:in `block in <module:Kernel>': unknown keyword: :x (ArgumentError)
```

Rubygems overwrites `Kernel#warn`! This is part of the implementation:

```ruby
    module_function define_method(:warn) {|*messages, uplevel: nil|
      unless uplevel
        return original_warn.call(*messages)
      end
      # filter callers
      original_warn.call(*messages, uplevel: uplevel)
    end
```

So the C version is loose with the keywords, in the sense that invalid keywords will be ignored. The Rubygems version is strict with the keywords, in that passing keywords other than :uplevel will result in an error.  The Rubygems version will then call the original version without keywords arguments.  Combined, this causes the very weird behavior you see.  Let's go through each example:

> ```
> >> warn({x:1})
> ArgumentError (unknown keyword: :x)
> ```

Happens when the Rubygems version is called, because :x is not a valid keyword.

> ```
> >> warn({x:1}, uplevel:0)
> (irb):4: warning: {:x=>1}
> ```

Rubygems version recognizes uplevel keyword and passes it to C version


> ```
> >> warn({x:1}, **{})
> ```

Passes no keywords to the Rubygems version, so Rubygems version passes `{x: 1}` as the sole argument to the C version, which is treated as keyword arguments and ignored.

> ```
> >> warn({x:1}, {})
> ```

This emits a keyword argument separation warning in the master branch, as the hash is treated as keywords to the Rubygems version.  The Rubygems version then passes `{x: 1}` as the sole argument to the C version, which is treated as keyword arguments and ignored.


> ```
> >> warn({x:1}, {}, {})
> {:x=>1}
> ```

Last hash passed to Rubygems version as keyword arguments with keyword argument separation warning emitted.  First two arguments passed to C version. Second argument treated as keyword argument by C version, and first argument treated as warning message.

> ```
> >> warn({x:1}, {}, {}, {})
> {:x=>1}
> {}
> ```

Same as previous, except there is an extra argument emitted as a warning.

> ```
> >> warn({x:1}, {y:2}, {})
> ArgumentError (unknown keyword: :y)
> ```

Last hash passed to Rubygems version as keyword arguments with keyword argument separation warning emitted.  First two arguments passed to C version. Second argument treated as keyword argument by C version, and ArgumentError raised as C version doesn't support the :y keyword.

> ```
> >> warn({x:1}, {y:2}, {}, {})
> {:x=>1}
> {:y=>2}
> ```

Same as the `warn({x:1}, {}, {}, {})` case other than the elements of the 2nd argument.

I sent a pull request to Rubygems to fix this behavior (https://github.com/rubygems/rubygems/pull/2911).  Personally, I think it would be better for them to drop the override of `Kernel#warn`.

----------------------------------------
Misc #16157: What is the correct and *portable* way to do generic delegation?
https://bugs.ruby-lang.org/issues/16157#change-81526

* Author: Dan0042 (Daniel DeLorme)
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
With the keyword argument changes in 2.7 we must now specify keyword arguments explicitly when doing generic delegation. But this change is not compatible with 2.6, where it adds an empty hash to the argument list of methods that do not need/accept keyword arguments.

To illustrate the problem:

```ruby
class ProxyWithoutKW < BasicObject
  def initialize(target)
    @target = target
  end
  def method_missing(*a, &b)
    @target.send(*a, &b)
  end
end

class ProxyWithKW < BasicObject
  def initialize(target)
    @target = target
  end
  def method_missing(*a, **o, &b)
    @target.send(*a, **o, &b)
  end
end

class Test
  def args(*a)   a  end
  def arg(a)     a  end
  def opts(**o)  o  end
end
                                          # 2.6        2.7              3.0
ProxyWithoutKW.new(Test.new).args(42)     # [42]       [42]             [42]        ok
ProxyWithoutKW.new(Test.new).arg(42)      # 42         42               42          ok
ProxyWithoutKW.new(Test.new).opts(k: 42)  # {:k=>42}   {:k=>42} +warn   [{:k=>42}]  incompatible with >= 2.7
ProxyWithKW.new(Test.new).args(42)        # [42, {}]   [42]             [42]        incompatible with <= 2.6
ProxyWithKW.new(Test.new).arg(42)         # error      42               42          incompatible with <= 2.6
ProxyWithKW.new(Test.new).opts(k: 42)     # {:k=>42}   {:k=>42} +warn   {:k=>42}    must ignore warning? cannot use pass_positional_hash in 2.6
```

I don't know how to solve this, so I'm asking for the **official** correct way to write portable delegation code. And by **portable** I mean code that can be used in gems that target ruby 2.6 and above.




-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2019-09-11 23:20 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-16157.20190909141042@ruby-lang.org>
2019-09-09 14:10 ` [ruby-core:94860] [Ruby master Misc#16157] What is the correct and *portable* way to do generic delegation? daniel
2019-09-09 14:22 ` [ruby-core:94861] " merch-redmine
2019-09-09 17:09 ` [ruby-core:94865] " daniel
2019-09-09 19:36 ` [ruby-core:94872] " merch-redmine
2019-09-10 14:41 ` [ruby-core:94888] " daniel
2019-09-10 16:37 ` [ruby-core:94889] " merch-redmine
2019-09-10 19:14 ` [ruby-core:94894] " daniel
2019-09-10 20:09 ` [ruby-core:94895] " merch-redmine
2019-09-11 15:36 ` [ruby-core:94904] " daniel
2019-09-11 21:25 ` [ruby-core:94909] " merch-redmine
2019-09-11 23:20 ` merch-redmine [this message]
2019-09-12  2:05 ` [ruby-core:94916] " daniel
2019-09-12 12:53 ` [ruby-core:94922] " daniel
2019-09-12 15:40 ` [ruby-core:94923] " merch-redmine
2019-09-12 19:57 ` [ruby-core:94924] " daniel
2019-09-13 14:55 ` [ruby-core:94927] " daniel
2019-09-13 15:15 ` [ruby-core:94928] " merch-redmine
2019-09-13 17:00 ` [ruby-core:94931] " daniel
2019-09-13 17:54 ` [ruby-core:94933] " merch-redmine
2019-10-14 18:10 ` [ruby-core:95317] " merch-redmine
2019-10-15  5:37 ` [ruby-core:95326] " eregontp
2019-10-15  7:23 ` [ruby-core:95328] " eregontp
2019-10-15 15:16 ` [ruby-core:95337] " merch-redmine
2019-10-15 15:49 ` [ruby-core:95339] " eregontp
2019-10-15 16:28 ` [ruby-core:95341] " merch-redmine

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-81526.20190911232017.f57669863144d122@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).