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.1 required=3.0 tests=AWL,BAYES_00, 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 7523E1F5AE for ; Sat, 18 Jul 2020 16:12:39 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 56C70120925; Sun, 19 Jul 2020 01:12:05 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id 9C941120924 for ; Sun, 19 Jul 2020 01:12:02 +0900 (JST) Received: by filterdrecv-p3mdw1-75c584b9c6-q9wps with SMTP id filterdrecv-p3mdw1-75c584b9c6-q9wps-21-5F131F6D-12 2020-07-18 16:12:29.173006647 +0000 UTC m=+1897372.069402985 Received: from herokuapp.com (unknown) by ismtpd0004p1iad1.sendgrid.net (SG) with ESMTP id YVTMY3iFR3GbRcpR2z1GIA for ; Sat, 18 Jul 2020 16:12:29.085 +0000 (UTC) Date: Sat, 18 Jul 2020 16:12:29 +0000 (UTC) From: nobu@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 75003 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Bug X-Redmine-Issue-Id: 17034 X-Redmine-Issue-Author: citizen428 X-Redmine-Sender: nobu 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?q8Dly+pU2+3ektTtZVXgZtbJPXwqo7p86jCsvYTW4BzkWa3jHHB1ybVjTWQgNN?= =?us-ascii?Q?8c=2FskhHPZ=2FNiHCKsI78yPiTRZimxbI3HwlmjeJr?= =?us-ascii?Q?d2FJkuvVGnY+avxO5b+dC24z77LckFQmrZ0wx3V?= =?us-ascii?Q?CSVkdsQN5NomfZ4cWsQqPf62EKXTy1KENTVgJMT?= =?us-ascii?Q?g6phOmQQSlUCCk7gWLLqdJJ9OSep5YRppWghWyn?= =?us-ascii?Q?CjNmUxYTdpN6njixQ=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 99214 Subject: [ruby-core:99214] [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 nobu (Nobuyoshi Nakada). Backport changed from 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN to 2.5: DONTNEED, 2.6: DONTNEED, 2.7: REQUIRED I agree that `RangeError` would be better, but it seems a separate issue as `TypeError` is used already in other cases. ---------------------------------------- Bug #17034: Unexpected behavior in #max for beginless range https://bugs.ruby-lang.org/issues/17034#change-86594 * Author: citizen428 (Michael Kohl) * Status: Open * Priority: Normal * ruby -v: 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19] * Backport: 2.5: DONTNEED, 2.6: DONTNEED, 2.7: REQUIRED ---------------------------------------- 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/