Project

General

Profile

Actions

Bug #19977

closed

(nil..nil) === x can raise an exception, differing from Range#cover?

Added by kyanagi (Kouhei Yanagita) 6 months ago. Updated 4 months ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.3.0dev (2023-10-27T03:13:17Z master f9f0cfe785) [arm64-darwin22]
[ruby-core:115188]

Description

I investigated Range#=== and Range#cover?, and found that the only difference in behavior between them would be that
(nil..nil) === x could throw an exception.

% ~/tmp/ruby-master/bin/ruby -v
ruby 3.3.0dev (2023-10-27T03:13:17Z master f9f0cfe785) [arm64-darwin22]
% ~/tmp/ruby-master/bin/ruby -e 'p (nil..nil) === "a"'
-e:1:in `===': cannot determine inclusion in beginless/endless ranges (TypeError)

p (nil..nil) === "a"
                 ^^^
        from -e:1:in `<main>'
% ~/tmp/ruby-master/bin/ruby -e 'p (nil..nil).cover?("a")'
true

Is this difference intended?

According to NEWS, Range#=== uses cover? since Ruby 2.6 (For String, since Ruby 2.7).

Following this, (nil..nil) === x should not throw an exception in the same way as (nil..nil).cover?(x).

history:

(nil..nil) === "a" throws an exception since https://github.com/ruby/ruby/commit/04a92a6.
For "linear objects" (Integer, Float, Numeric, Time), it has beed fixed not to throw an exception on https://github.com/ruby/ruby/commit/fb17c83.

related issues:

  • [Bug #15449] Range#=== is not using cover in Ruby 2.6
  • [Bug #18580] Range#include? inconsistency for beginless String ranges
  • [Bug #19533] Behavior of ===/include? on a beginless/endless range (nil..nil) changed in ruby 3.2

Updated by kyanagi (Kouhei Yanagita) 6 months ago

Matz had taken the decision to make (nil..nil) === any_object #=> true in DevMeeting-2023-04-11.
Therefore, we can conclude that this behavior is a bug.

https://github.com/ruby/dev-meeting-log/blob/master/2023/DevMeeting-2023-04-11.md

I created a PR: https://github.com/ruby/ruby/pull/8797

Updated by knu (Akinori MUSHA) 6 months ago

Matz agreed to fix this.

Updated by kyanagi (Kouhei Yanagita) 5 months ago

Can this fix be included in 3.3.0?

Updated by knu (Akinori MUSHA) 4 months ago

I've approved the PR and I'll see if @nobu (Nobuyoshi Nakada) agrees. I definitely would love to see this merged to 3.3.

Actions #6

Updated by kyanagi (Kouhei Yanagita) 4 months ago

  • Status changed from Open to Closed

Applied in changeset git|e8639098ed78f8f8f7c263d8b6f2d68822945a78.


[Bug #19977] Fix (nil..nil) === x not to raise TypeError

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0