1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Revert "range.c: prohibit (1..nil)"

This reverts commit a44c010764a16ae09aaed49d76eec055ca0057c8.
Refs #14845.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63649 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2018-06-13 11:00:28 +00:00
parent 4f4ed3e9ea
commit 606d6b3470
4 changed files with 41 additions and 35 deletions

View file

@ -7188,10 +7188,10 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
case NODE_DOT2: case NODE_DOT2:
case NODE_DOT3:{ case NODE_DOT3:{
int excl = type == NODE_DOT3; int excl = type == NODE_DOT3;
VALUE flag = INT2FIX(excl);
const NODE *b = node->nd_beg; const NODE *b = node->nd_beg;
const NODE *e = node->nd_end; const NODE *e = node->nd_end;
VALUE flag = INT2FIX(excl | (e ? 0 : 2)); if (number_literal_p(b) && number_literal_p(e)) {
if (number_literal_p(b) && e && number_literal_p(e)) {
if (!popped) { if (!popped) {
VALUE val = rb_range_new(b->nd_lit, e->nd_lit, excl); VALUE val = rb_range_new(b->nd_lit, e->nd_lit, excl);
iseq_add_mark_object_compile_time(iseq, val); iseq_add_mark_object_compile_time(iseq, val);

12
parse.y
View file

@ -1916,16 +1916,24 @@ arg : lhs '=' arg_rhs
| arg tDOT2 | arg tDOT2
{ {
/*%%%*/ /*%%%*/
YYLTYPE loc;
loc.beg_pos = @2.end_pos;
loc.end_pos = @2.end_pos;
value_expr($1); value_expr($1);
$$ = NEW_DOT2($1, 0, &@$); $$ = NEW_DOT2($1, new_nil(&loc), &@$);
/*% %*/ /*% %*/
/*% ripper: dot2!($1, Qnil) %*/ /*% ripper: dot2!($1, Qnil) %*/
} }
| arg tDOT3 | arg tDOT3
{ {
/*%%%*/ /*%%%*/
YYLTYPE loc;
loc.beg_pos = @2.end_pos;
loc.end_pos = @2.end_pos;
value_expr($1); value_expr($1);
$$ = NEW_DOT3($1, 0, &@$); $$ = NEW_DOT3($1, new_nil(&loc), &@$);
/*% %*/ /*% %*/
/*% ripper: dot3!($1, Qnil) %*/ /*% ripper: dot3!($1, Qnil) %*/
} }

14
range.c
View file

@ -37,7 +37,7 @@ static VALUE r_cover_p(VALUE, VALUE, VALUE, VALUE);
static void static void
range_init(VALUE range, VALUE beg, VALUE end, VALUE exclude_end) range_init(VALUE range, VALUE beg, VALUE end, VALUE exclude_end)
{ {
if ((!FIXNUM_P(beg) || !FIXNUM_P(end)) && end != Qundef) { if ((!FIXNUM_P(beg) || !FIXNUM_P(end)) && !NIL_P(end)) {
VALUE v; VALUE v;
v = rb_funcall(beg, id_cmp, 1, end); v = rb_funcall(beg, id_cmp, 1, end);
@ -47,16 +47,15 @@ range_init(VALUE range, VALUE beg, VALUE end, VALUE exclude_end)
RANGE_SET_EXCL(range, exclude_end); RANGE_SET_EXCL(range, exclude_end);
RANGE_SET_BEG(range, beg); RANGE_SET_BEG(range, beg);
RANGE_SET_END(range, end == Qundef ? Qnil : end); RANGE_SET_END(range, end);
} }
VALUE VALUE
rb_range_new(VALUE beg, VALUE end, int flag) rb_range_new(VALUE beg, VALUE end, int exclude_end)
{ {
VALUE range = rb_obj_alloc(rb_cRange); VALUE range = rb_obj_alloc(rb_cRange);
if (flag & 2) end = Qundef; range_init(range, beg, end, RBOOL(exclude_end));
range_init(range, beg, end, RBOOL(flag & 1));
return range; return range;
} }
@ -86,7 +85,6 @@ range_initialize(int argc, VALUE *argv, VALUE range)
rb_scan_args(argc, argv, "21", &beg, &end, &flags); rb_scan_args(argc, argv, "21", &beg, &end, &flags);
range_modify(range); range_modify(range);
if (NIL_P(end)) end = Qundef;
range_init(range, beg, end, RBOOL(RTEST(flags))); range_init(range, beg, end, RBOOL(RTEST(flags)));
return Qnil; return Qnil;
} }
@ -1341,7 +1339,7 @@ range_dumper(VALUE range)
rb_ivar_set(v, id_excl, RANGE_EXCL(range)); rb_ivar_set(v, id_excl, RANGE_EXCL(range));
rb_ivar_set(v, id_beg, RANGE_BEG(range)); rb_ivar_set(v, id_beg, RANGE_BEG(range));
if (!NIL_P(RANGE_END(range))) rb_ivar_set(v, id_end, RANGE_END(range)); rb_ivar_set(v, id_end, RANGE_END(range));
return v; return v;
} }
@ -1356,7 +1354,7 @@ range_loader(VALUE range, VALUE obj)
range_modify(range); range_modify(range);
beg = rb_ivar_get(obj, id_beg); beg = rb_ivar_get(obj, id_beg);
end = rb_ivar_lookup(obj, id_end, Qundef); end = rb_ivar_get(obj, id_end);
excl = rb_ivar_get(obj, id_excl); excl = rb_ivar_get(obj, id_excl);
if (!NIL_P(excl)) { if (!NIL_P(excl)) {
range_init(range, beg, end, RBOOL(RTEST(excl))); range_init(range, beg, end, RBOOL(RTEST(excl)));

View file

@ -13,8 +13,8 @@ class TestRange < Test::Unit::TestCase
assert_raise(ArgumentError) { (1.."3") } assert_raise(ArgumentError) { (1.."3") }
assert_equal((0..), Range.new(0, nil, false)) assert_equal((0..nil), Range.new(0, nil, false))
assert_equal((0...), Range.new(0, nil, true)) assert_equal((0...nil), Range.new(0, nil, true))
obj = Object.new obj = Object.new
def obj.<=>(other) def obj.<=>(other)
@ -161,15 +161,15 @@ class TestRange < Test::Unit::TestCase
assert_not_equal(r, (1..2)) assert_not_equal(r, (1..2))
assert_not_equal(r, (0..2)) assert_not_equal(r, (0..2))
assert_not_equal(r, (0...1)) assert_not_equal(r, (0...1))
assert_not_equal(r, (0..)) assert_not_equal(r, (0..nil))
subclass = Class.new(Range) subclass = Class.new(Range)
assert_equal(r, subclass.new(0,1)) assert_equal(r, subclass.new(0,1))
r = (0..) r = (0..nil)
assert_equal(r, r) assert_equal(r, r)
assert_equal(r, (0..)) assert_equal(r, (0..nil))
assert_not_equal(r, 0) assert_not_equal(r, 0)
assert_not_equal(r, (0...)) assert_not_equal(r, (0...nil))
subclass = Class.new(Range) subclass = Class.new(Range)
assert_equal(r, subclass.new(0,nil)) assert_equal(r, subclass.new(0,nil))
end end
@ -185,11 +185,11 @@ class TestRange < Test::Unit::TestCase
subclass = Class.new(Range) subclass = Class.new(Range)
assert_operator(r, :eql?, subclass.new(0,1)) assert_operator(r, :eql?, subclass.new(0,1))
r = (0..) r = (0..nil)
assert_operator(r, :eql?, r) assert_operator(r, :eql?, r)
assert_operator(r, :eql?, 0..) assert_operator(r, :eql?, 0..nil)
assert_not_operator(r, :eql?, 0) assert_not_operator(r, :eql?, 0)
assert_not_operator(r, :eql?, 0...) assert_not_operator(r, :eql?, 0...nil)
subclass = Class.new(Range) subclass = Class.new(Range)
assert_operator(r, :eql?, subclass.new(0,nil)) assert_operator(r, :eql?, subclass.new(0,nil))
end end
@ -198,8 +198,8 @@ class TestRange < Test::Unit::TestCase
assert_kind_of(Integer, (0..1).hash) assert_kind_of(Integer, (0..1).hash)
assert_equal((0..1).hash, (0..1).hash) assert_equal((0..1).hash, (0..1).hash)
assert_not_equal((0..1).hash, (0...1).hash) assert_not_equal((0..1).hash, (0...1).hash)
assert_equal((0..).hash, (0..).hash) assert_equal((0..nil).hash, (0..nil).hash)
assert_not_equal((0..).hash, (0...).hash) assert_not_equal((0..nil).hash, (0...nil).hash)
end end
def test_step def test_step
@ -380,9 +380,9 @@ class TestRange < Test::Unit::TestCase
assert_equal(0, (0..1).begin) assert_equal(0, (0..1).begin)
assert_equal(1, (0..1).end) assert_equal(1, (0..1).end)
assert_equal(1, (0...1).end) assert_equal(1, (0...1).end)
assert_equal(0, (0..).begin) assert_equal(0, (0..nil).begin)
assert_equal(nil, (0..).end) assert_equal(nil, (0..nil).end)
assert_equal(nil, (0...).end) assert_equal(nil, (0...nil).end)
end end
def test_first_last def test_first_last
@ -402,17 +402,17 @@ class TestRange < Test::Unit::TestCase
assert_equal("c", ("a"..."c").last) assert_equal("c", ("a"..."c").last)
assert_equal(0, (2...0).last) assert_equal(0, (2...0).last)
assert_equal([0, 1, 2], (0..).first(3)) assert_equal([0, 1, 2], (0..nil).first(3))
assert_equal(0, (0..).first) assert_equal(0, (0..nil).first)
assert_equal("a", ("a"..).first) assert_equal("a", ("a"..nil).first)
# XXX: How should (0...).last(3) behave? # XXX: How should (0...).last(3) behave?
end end
def test_to_s def test_to_s
assert_equal("0..1", (0..1).to_s) assert_equal("0..1", (0..1).to_s)
assert_equal("0...1", (0...1).to_s) assert_equal("0...1", (0...1).to_s)
assert_equal("0..", (0..).to_s) assert_equal("0..", (0..nil).to_s)
assert_equal("0...", (0...).to_s) assert_equal("0...", (0...nil).to_s)
bug11767 = '[ruby-core:71811] [Bug #11767]' bug11767 = '[ruby-core:71811] [Bug #11767]'
assert_predicate(("0".taint.."1").to_s, :tainted?, bug11767) assert_predicate(("0".taint.."1").to_s, :tainted?, bug11767)
@ -423,8 +423,8 @@ class TestRange < Test::Unit::TestCase
def test_inspect def test_inspect
assert_equal("0..1", (0..1).inspect) assert_equal("0..1", (0..1).inspect)
assert_equal("0...1", (0...1).inspect) assert_equal("0...1", (0...1).inspect)
assert_equal("0..", (0..).inspect) assert_equal("0..", (0..nil).inspect)
assert_equal("0...", (0...).inspect) assert_equal("0...", (0...nil).inspect)
bug11767 = '[ruby-core:71811] [Bug #11767]' bug11767 = '[ruby-core:71811] [Bug #11767]'
assert_predicate(("0".taint.."1").inspect, :tainted?, bug11767) assert_predicate(("0".taint.."1").inspect, :tainted?, bug11767)
@ -435,8 +435,8 @@ class TestRange < Test::Unit::TestCase
def test_eqq def test_eqq
assert_operator(0..10, :===, 5) assert_operator(0..10, :===, 5)
assert_not_operator(0..10, :===, 11) assert_not_operator(0..10, :===, 11)
assert_operator(5.., :===, 11) assert_operator(5..nil, :===, 11)
assert_not_operator(5.., :===, 0) assert_not_operator(5..nil, :===, 0)
end end
def test_eqq_time def test_eqq_time