From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-1.4 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_BL_SPAMCOP_NET,RCVD_IN_DNSWL_MED,RCVD_IN_SBL_CSS,SPF_HELO_NONE, SPF_PASS shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id DD26B1F463 for ; Wed, 11 Sep 2019 23:20:29 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id A67B8120A63; Thu, 12 Sep 2019 08:20:20 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id B0835120A3F for ; Thu, 12 Sep 2019 08:20:17 +0900 (JST) Received: by filter0114p3las1.sendgrid.net with SMTP id filter0114p3las1-5178-5D798131-61 2019-09-11 23:20:18.017285467 +0000 UTC m=+107237.022470836 Received: from herokuapp.com (unknown [54.242.45.138]) by ismtpd0068p1mdw1.sendgrid.net (SG) with ESMTP id glnFykO_QqSJJFn-xSRcwg for ; Wed, 11 Sep 2019 23:20:17.892 +0000 (UTC) Date: Wed, 11 Sep 2019 23:20:18 +0000 (UTC) From: merch-redmine@jeremyevans.net Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70466 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16157 X-Redmine-Issue-Author: Dan0042 X-Redmine-Sender: jeremyevans0 X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?RVE3t853K5scBhbmJHUzZTFFeVC=2FZSUmHZ0Dc+26wcEi2CTgsF1oz0wTSSxGGN?= =?us-ascii?Q?BIXUvA2nh52XvTtskSu2aVPvNzRYgfXge2Wl4kl?= =?us-ascii?Q?w99ZvWPtJeJSWuFprJ21gex5AMdbNTBAIblDGzN?= =?us-ascii?Q?+pCddgjY=2FP=2FDe8cr7EbjUqIWZXC3VMflJT05wLR?= =?us-ascii?Q?HEMHe98MQTyq6MTIMlm9tREizhZWeQXpgmg=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 94912 Subject: [ruby-core:94912] [Ruby master Misc#16157] What is the correct and *portable* way to do generic delegation? X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" 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 `
' /home/jeremy/tmp/ruby/lib/rubygems/core_ext/kernel_warn.rb:15:in `block in ': 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/