ruby-changes:51442
From: mame <ko1@a...>
Date: Wed, 13 Jun 2018 20:00:35 +0900 (JST)
Subject: [ruby-changes:51442] mame:r63649 (trunk): Revert "range.c: prohibit `(1..nil)`"
mame 2018-06-13 20:00:28 +0900 (Wed, 13 Jun 2018) New Revision: 63649 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63649 Log: Revert "range.c: prohibit `(1..nil)`" This reverts commit a44c010764a16ae09aaed49d76eec055ca0057c8. Refs #14845. Modified files: trunk/compile.c trunk/parse.y trunk/range.c trunk/test/ruby/test_range.rb Index: compile.c =================================================================== --- compile.c (revision 63648) +++ compile.c (revision 63649) @@ -7188,10 +7188,10 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK https://github.com/ruby/ruby/blob/trunk/compile.c#L7188 case NODE_DOT2: case NODE_DOT3:{ int excl = type == NODE_DOT3; + VALUE flag = INT2FIX(excl); const NODE *b = node->nd_beg; const NODE *e = node->nd_end; - VALUE flag = INT2FIX(excl | (e ? 0 : 2)); - if (number_literal_p(b) && e && number_literal_p(e)) { + if (number_literal_p(b) && number_literal_p(e)) { if (!popped) { VALUE val = rb_range_new(b->nd_lit, e->nd_lit, excl); iseq_add_mark_object_compile_time(iseq, val); Index: range.c =================================================================== --- range.c (revision 63648) +++ range.c (revision 63649) @@ -37,7 +37,7 @@ static VALUE r_cover_p(VALUE, VALUE, VAL https://github.com/ruby/ruby/blob/trunk/range.c#L37 static void 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; v = rb_funcall(beg, id_cmp, 1, end); @@ -47,16 +47,15 @@ range_init(VALUE range, VALUE beg, VALUE https://github.com/ruby/ruby/blob/trunk/range.c#L47 RANGE_SET_EXCL(range, exclude_end); RANGE_SET_BEG(range, beg); - RANGE_SET_END(range, end == Qundef ? Qnil : end); + RANGE_SET_END(range, end); } 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); - if (flag & 2) end = Qundef; - range_init(range, beg, end, RBOOL(flag & 1)); + range_init(range, beg, end, RBOOL(exclude_end)); return range; } @@ -86,7 +85,6 @@ range_initialize(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/range.c#L85 rb_scan_args(argc, argv, "21", &beg, &end, &flags); range_modify(range); - if (NIL_P(end)) end = Qundef; range_init(range, beg, end, RBOOL(RTEST(flags))); return Qnil; } @@ -1341,7 +1339,7 @@ range_dumper(VALUE range) https://github.com/ruby/ruby/blob/trunk/range.c#L1339 rb_ivar_set(v, id_excl, RANGE_EXCL(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; } @@ -1356,7 +1354,7 @@ range_loader(VALUE range, VALUE obj) https://github.com/ruby/ruby/blob/trunk/range.c#L1354 range_modify(range); 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); if (!NIL_P(excl)) { range_init(range, beg, end, RBOOL(RTEST(excl))); Index: parse.y =================================================================== --- parse.y (revision 63648) +++ parse.y (revision 63649) @@ -1916,16 +1916,24 @@ arg : lhs '=' arg_rhs https://github.com/ruby/ruby/blob/trunk/parse.y#L1916 | arg tDOT2 { /*%%%*/ + YYLTYPE loc; + loc.beg_pos = @2.end_pos; + loc.end_pos = @2.end_pos; + value_expr($1); - $$ = NEW_DOT2($1, 0, &@$); + $$ = NEW_DOT2($1, new_nil(&loc), &@$); /*% %*/ /*% ripper: dot2!($1, Qnil) %*/ } | arg tDOT3 { /*%%%*/ + YYLTYPE loc; + loc.beg_pos = @2.end_pos; + loc.end_pos = @2.end_pos; + value_expr($1); - $$ = NEW_DOT3($1, 0, &@$); + $$ = NEW_DOT3($1, new_nil(&loc), &@$); /*% %*/ /*% ripper: dot3!($1, Qnil) %*/ } Index: test/ruby/test_range.rb =================================================================== --- test/ruby/test_range.rb (revision 63648) +++ test/ruby/test_range.rb (revision 63649) @@ -13,8 +13,8 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L13 assert_raise(ArgumentError) { (1.."3") } - assert_equal((0..), Range.new(0, nil, false)) - assert_equal((0...), Range.new(0, nil, true)) + assert_equal((0..nil), Range.new(0, nil, false)) + assert_equal((0...nil), Range.new(0, nil, true)) obj = Object.new def obj.<=>(other) @@ -161,15 +161,15 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L161 assert_not_equal(r, (1..2)) assert_not_equal(r, (0..2)) assert_not_equal(r, (0...1)) - assert_not_equal(r, (0..)) + assert_not_equal(r, (0..nil)) subclass = Class.new(Range) assert_equal(r, subclass.new(0,1)) - r = (0..) + r = (0..nil) 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...nil)) subclass = Class.new(Range) assert_equal(r, subclass.new(0,nil)) end @@ -185,11 +185,11 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L185 subclass = Class.new(Range) assert_operator(r, :eql?, subclass.new(0,1)) - r = (0..) + r = (0..nil) 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...nil) subclass = Class.new(Range) assert_operator(r, :eql?, subclass.new(0,nil)) end @@ -198,8 +198,8 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L198 assert_kind_of(Integer, (0..1).hash) assert_equal((0..1).hash, (0..1).hash) assert_not_equal((0..1).hash, (0...1).hash) - assert_equal((0..).hash, (0..).hash) - assert_not_equal((0..).hash, (0...).hash) + assert_equal((0..nil).hash, (0..nil).hash) + assert_not_equal((0..nil).hash, (0...nil).hash) end def test_step @@ -380,9 +380,9 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L380 assert_equal(0, (0..1).begin) assert_equal(1, (0..1).end) assert_equal(1, (0...1).end) - assert_equal(0, (0..).begin) - assert_equal(nil, (0..).end) - assert_equal(nil, (0...).end) + assert_equal(0, (0..nil).begin) + assert_equal(nil, (0..nil).end) + assert_equal(nil, (0...nil).end) end def test_first_last @@ -402,17 +402,17 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L402 assert_equal("c", ("a"..."c").last) assert_equal(0, (2...0).last) - assert_equal([0, 1, 2], (0..).first(3)) - assert_equal(0, (0..).first) - assert_equal("a", ("a"..).first) + assert_equal([0, 1, 2], (0..nil).first(3)) + assert_equal(0, (0..nil).first) + assert_equal("a", ("a"..nil).first) # XXX: How should (0...).last(3) behave? end def test_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...).to_s) + assert_equal("0..", (0..nil).to_s) + assert_equal("0...", (0...nil).to_s) bug11767 = '[ruby-core:71811] [Bug #11767]' assert_predicate(("0".taint.."1").to_s, :tainted?, bug11767) @@ -423,8 +423,8 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L423 def test_inspect assert_equal("0..1", (0..1).inspect) assert_equal("0...1", (0...1).inspect) - assert_equal("0..", (0..).inspect) - assert_equal("0...", (0...).inspect) + assert_equal("0..", (0..nil).inspect) + assert_equal("0...", (0...nil).inspect) bug11767 = '[ruby-core:71811] [Bug #11767]' assert_predicate(("0".taint.."1").inspect, :tainted?, bug11767) @@ -435,8 +435,8 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L435 def test_eqq assert_operator(0..10, :===, 5) assert_not_operator(0..10, :===, 11) - assert_operator(5.., :===, 11) - assert_not_operator(5.., :===, 0) + assert_operator(5..nil, :===, 11) + assert_not_operator(5..nil, :===, 0) end def test_eqq_time -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/