Skip to content

Commit

Permalink
[Bug #19977] Fix (nil..nil) === x not to raise TypeError
Browse files Browse the repository at this point in the history
  • Loading branch information
kyanagi authored and nobu committed Dec 22, 2023
1 parent f263e44 commit e863909
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 52 deletions.
51 changes: 0 additions & 51 deletions range.c
Expand Up @@ -1844,7 +1844,6 @@ range_inspect(VALUE range)
}

static VALUE range_include_internal(VALUE range, VALUE val);
static VALUE range_string_cover_internal(VALUE range, VALUE val);
VALUE rb_str_include_range_p(VALUE beg, VALUE end, VALUE val, VALUE exclusive);

/*
Expand Down Expand Up @@ -1889,8 +1888,6 @@ VALUE rb_str_include_range_p(VALUE beg, VALUE end, VALUE val, VALUE exclusive);
static VALUE
range_eqq(VALUE range, VALUE val)
{
VALUE ret = range_string_cover_internal(range, val);
if (!UNDEF_P(ret)) return ret;
return r_cover_p(range, RANGE_BEG(range), RANGE_END(range), val);
}

Expand Down Expand Up @@ -1939,12 +1936,6 @@ range_integer_edge_p(VALUE beg, VALUE end)
!NIL_P(rb_check_to_integer(end, "to_int")));
}

static inline bool
range_string_edge_p(VALUE beg, VALUE end)
{
return RB_TYPE_P(beg, T_STRING) || RB_TYPE_P(end, T_STRING);
}

static inline bool
range_string_range_p(VALUE beg, VALUE end)
{
Expand All @@ -1965,48 +1956,6 @@ range_include_fallback(VALUE beg, VALUE end, VALUE val)
return Qundef;
}

static VALUE
range_string_cover_internal(VALUE range, VALUE val)
{
VALUE beg = RANGE_BEG(range);
VALUE end = RANGE_END(range);
int nv = FIXNUM_P(beg) || FIXNUM_P(end) ||
linear_object_p(beg) || linear_object_p(end);

if (nv || range_integer_edge_p(beg, end)) {
return r_cover_p(range, beg, end, val);
}
else if (range_string_edge_p(beg, end)) {
if (range_string_range_p(beg, end)) {
return r_cover_p(range, beg, end, val);
}
if (NIL_P(beg)) {
unbounded_begin:;
VALUE r = rb_funcall(val, id_cmp, 1, end);
if (NIL_P(r)) return Qfalse;
if (RANGE_EXCL(range)) {
return RBOOL(rb_cmpint(r, val, end) < 0);
}
return RBOOL(rb_cmpint(r, val, end) <= 0);
}
else if (NIL_P(end)) {
unbounded_end:;
VALUE r = rb_funcall(beg, id_cmp, 1, val);
if (NIL_P(r)) return Qfalse;
return RBOOL(rb_cmpint(r, beg, val) <= 0);
}
}

if (!NIL_P(beg) && NIL_P(end)) {
goto unbounded_end;
}
if (NIL_P(beg) && !NIL_P(end)) {
goto unbounded_begin;
}

return range_include_fallback(beg, end, val);
}

static VALUE
range_include_internal(VALUE range, VALUE val)
{
Expand Down
4 changes: 3 additions & 1 deletion test/ruby/test_range.rb
Expand Up @@ -726,14 +726,16 @@ def test_eqq
assert_not_operator(5..nil, :===, 0)
assert_operator(nil..10, :===, 0)
assert_operator(nil..nil, :===, 0)
assert_operator(nil..nil, :===, Object.new)
assert_not_operator(0..10, :===, 0..10)
end

def test_eqq_string
assert_operator('A'..'Z', :===, 'ANA')
assert_not_operator('A'..'Z', :===, 'ana')
assert_operator('A'.., :===, 'ANA')
assert_operator(..'Z', :===, 'ANA')
assert_raise(TypeError) {(nil..nil) === 'ANA'}
assert_operator(nil..nil, :===, 'ANA')
end

def test_eqq_time
Expand Down

0 comments on commit e863909

Please sign in to comment.