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-Status: No, score=-4.0 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=ham 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 60E611F5AE for ; Fri, 17 Jul 2020 17:48:57 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 6792D1209AD; Sat, 18 Jul 2020 02:48:21 +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 9204B120975 for ; Sat, 18 Jul 2020 02:48:18 +0900 (JST) Received: by filterdrecv-p3iad2-5b55dcd864-blwjn with SMTP id filterdrecv-p3iad2-5b55dcd864-blwjn-19-5F11E479-3D 2020-07-17 17:48:41.467852681 +0000 UTC m=+1816765.511679414 Received: from herokuapp.com (unknown) by ismtpd0001p1iad2.sendgrid.net (SG) with ESMTP id N0U0c6PaTYyk4slOSbgy5g for ; Fri, 17 Jul 2020 17:48:41.437 +0000 (UTC) Date: Fri, 17 Jul 2020 17:48:41 +0000 (UTC) From: merch-redmine@jeremyevans.net Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 74996 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Bug X-Redmine-Issue-Id: 17034 X-Redmine-Issue-Author: citizen428 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?BI+AyMZjznX5iZ4Mz2wqPOj0OvjByWhKG9Y67TY?= =?us-ascii?Q?E94tHq5U5Z=2FWIrJGLKhyIKfq4Y=2FUloOs2aAqgTC?= =?us-ascii?Q?tUMTYO3eGk4Lbvvz3jv73swQj6bO03=2F3GKFv2S0?= =?us-ascii?Q?q82hoTNaYZJ8QxLya6ixykqAiIZ5DwvOGw3t5we?= =?us-ascii?Q?1Qn0iojleXjrynUW4=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 99207 Subject: [ruby-core:99207] [Ruby master Bug#17034] Unexpected behavior in #max for beginless range 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 #17034 has been updated by jeremyevans0 (Jeremy Evans). citizen428 (Michael Kohl) wrote: > I think this is not ideal. Possible solutions: > > 1. Return `e` (`RANGE_END(range)` for beginless ranges or > 2. return a `RangeError` with a message like "cannot get the maximum of beginless range" (similar to `.min`). > > > Happy to provide a patch if people want this changed and can agree on what the new behavior should be. I think a `RangeError` is more correct. Without having a beginning, you cannot know the increment value, and therefore cannot know the maximum value. People that want the end of the range should use `Range#end`, not `Range#max`. However, I can see where `Range#max` returning the end for an inclusive beginless range could potentially be more useful. ---------------------------------------- Bug #17034: Unexpected behavior in #max for beginless range https://bugs.ruby-lang.org/issues/17034#change-86585 * Author: citizen428 (Michael Kohl) * Status: Open * Priority: Normal * ruby -v: 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19] * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN ---------------------------------------- When calling `max` on a beginless range, a non-intuitive error gets raised: ``` ruby r = ..9 r.max # ArgumentError: comparison of NilClass with 9 failed ``` There's a check for `NIL_P(RANGE_BEG(range))` but it's inside another check which is false for the example case above: ``` ruby if (rb_block_given_p() || (EXCL(range) && !nm) || argc) { if (NIL_P(RANGE_BEG(range))) { rb_raise(rb_eRangeError, "cannot get the maximum of beginless range with custom comparison method"); } return rb_call_super(argc, argv); } ``` The first part of the condition is false since there is no block, and even though I'm not sure what `EXCL` does the second part of the condition will be false due to `!nm` (`nm` will be true because of `FIXNUM_P(e)`). So I think the error gets raised here: ``` ruby int c = OPTIMIZED_CMP(b, e, cmp_opt); ``` I think this is not ideal. Possible solutions: 1. Return `e` (`RANGE_END(range)` for beginless ranges or 2. return a `RangeError` with a message like "cannot get the maximum of beginless range" (similar to `.min`). Happy to provide a patch if people want this changed and can agree on what the new behavior should be. ---Files-------------------------------- range-max-beginless.patch (1.26 KB) -- https://bugs.ruby-lang.org/