[前][次][番号順一覧][スレッド一覧]

ruby-changes:54534

From: mrkn <ko1@a...>
Date: Tue, 8 Jan 2019 13:37:46 +0900 (JST)
Subject: [ruby-changes:54534] mrkn:r66749 (trunk): range.c (range_last): disable optimization when each is redefined

mrkn	2019-01-08 13:37:40 +0900 (Tue, 08 Jan 2019)

  New Revision: 66749

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66749

  Log:
    range.c (range_last): disable optimization when each is redefined
    
    Do not use the optimized version of Range#last when Range#each is
    redefined.

  Modified files:
    trunk/range.c
    trunk/test/ruby/test_range.rb
Index: range.c
===================================================================
--- range.c	(revision 66748)
+++ range.c	(revision 66749)
@@ -1088,7 +1088,8 @@ range_last(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/range.c#L1088
 
     b = RANGE_BEG(range);
     e = RANGE_END(range);
-    if (RB_INTEGER_TYPE_P(b) && RB_INTEGER_TYPE_P(e)) {
+    if (RB_INTEGER_TYPE_P(b) && RB_INTEGER_TYPE_P(e) &&
+        RB_LIKELY(rb_method_basic_definition_p(rb_cRange, idEach))) {
         return rb_int_range_last(argc, argv, range);
     }
     return rb_ary_last(argc, argv, rb_Array(range));
Index: test/ruby/test_range.rb
===================================================================
--- test/ruby/test_range.rb	(revision 66748)
+++ test/ruby/test_range.rb	(revision 66749)
@@ -450,6 +450,18 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L450
     assert_raise(ArgumentError) { (0..10).last(-1) }
   end
 
+  def test_last_with_redefine_each
+    assert_in_out_err([], <<-'end;', ['true'], [])
+      class Range
+        remove_method :each
+        def each(&b)
+          [1, 2, 3, 4, 5].each(&b)
+        end
+      end
+      puts [3, 4, 5] == (1..10).last(3)
+    end;
+  end
+
   def test_to_s
     assert_equal("0..1", (0..1).to_s)
     assert_equal("0...1", (0...1).to_s)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]