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

ruby-changes:63422

From: Kenta <ko1@a...>
Date: Fri, 23 Oct 2020 15:27:08 +0900 (JST)
Subject: [ruby-changes:63422] f754b42285 (master): numeric.c, range.c: prohibit zero step

https://git.ruby-lang.org/ruby.git/commit/?id=f754b42285

From f754b422855131111092c0c147d744775cc4793f Mon Sep 17 00:00:00 2001
From: Kenta Murata <mrkn@u...>
Date: Fri, 23 Oct 2020 15:26:51 +0900
Subject: numeric.c, range.c: prohibit zero step

* numeric.c: prohibit zero step in Numeric#step

* range.c: prohibit zero step in Range#step

* Fix ruby-spec

[Feature #15573]

diff --git a/numeric.c b/numeric.c
index fb9b28b..7495b11 100644
--- a/numeric.c
+++ b/numeric.c
@@ -2695,9 +2695,9 @@ num_step_check_fix_args(int argc, VALUE *to, VALUE *step, VALUE by, int fix_nil, https://github.com/ruby/ruby/blob/trunk/numeric.c#L2695
         if (argc > 1 && NIL_P(*step)) {
             rb_raise(rb_eTypeError, "step must be numeric");
         }
-        if (!allow_zero_step && rb_equal(*step, INT2FIX(0))) {
-            rb_raise(rb_eArgError, "step can't be 0");
-        }
+    }
+    if (!allow_zero_step && rb_equal(*step, INT2FIX(0))) {
+        rb_raise(rb_eArgError, "step can't be 0");
     }
     if (NIL_P(*step)) {
 	*step = INT2FIX(1);
@@ -2801,6 +2801,9 @@ num_step(int argc, VALUE *argv, VALUE from) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2801
         if (NIL_P(step)) {
             step = INT2FIX(1);
         }
+        else if (rb_equal(step, INT2FIX(0))) {
+            rb_raise(rb_eArgError, "step can't be 0");
+        }
         if ((NIL_P(to) || rb_obj_is_kind_of(to, rb_cNumeric)) &&
             rb_obj_is_kind_of(step, rb_cNumeric)) {
             return rb_arith_seq_new(from, ID2SYM(rb_frame_this_func()), argc, argv,
diff --git a/range.c b/range.c
index c59662d..15b3d57 100644
--- a/range.c
+++ b/range.c
@@ -415,6 +415,13 @@ range_step(int argc, VALUE *argv, VALUE range) https://github.com/ruby/ruby/blob/trunk/range.c#L415
     step = (!rb_check_arity(argc, 0, 1) ? INT2FIX(1) : argv[0]);
 
     if (!rb_block_given_p()) {
+        if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
+            step = rb_to_int(step);
+        }
+        if (rb_equal(step, INT2FIX(0))) {
+            rb_raise(rb_eArgError, "step can't be 0");
+        }
+
         const VALUE b_num_p = rb_obj_is_kind_of(b, rb_cNumeric);
         const VALUE e_num_p = rb_obj_is_kind_of(e, rb_cNumeric);
         if ((b_num_p && (NIL_P(e) || e_num_p)) || (NIL_P(b) && e_num_p)) {
diff --git a/spec/ruby/core/enumerator/arithmetic_sequence/step_spec.rb b/spec/ruby/core/enumerator/arithmetic_sequence/step_spec.rb
index 20a5cb6..8b00fd4 100644
--- a/spec/ruby/core/enumerator/arithmetic_sequence/step_spec.rb
+++ b/spec/ruby/core/enumerator/arithmetic_sequence/step_spec.rb
@@ -5,11 +5,9 @@ ruby_version_is "2.6" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/enumerator/arithmetic_sequence/step_spec.rb#L5
     it "returns the original value given to step method" do
       (1..10).step.step.should == 1
       (1..10).step(3).step.should == 3
-      (1..10).step(0).step.should == 0
 
       1.step(10).step.should == 1
       1.step(10, 3).step.should == 3
-      1.step(10, 0).step.should == 0
     end
   end
 end
diff --git a/spec/ruby/core/numeric/shared/step.rb b/spec/ruby/core/numeric/shared/step.rb
index fac79b3..6c47cde 100644
--- a/spec/ruby/core/numeric/shared/step.rb
+++ b/spec/ruby/core/numeric/shared/step.rb
@@ -261,8 +261,10 @@ describe :numeric_step, :shared => true do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/numeric/shared/step.rb#L261
       step_enum_class = Enumerator::ArithmeticSequence
     end
 
-    it "returns an #{step_enum_class} when step is 0" do
-      @step.call(1, 2, 0).should be_an_instance_of(step_enum_class)
+    ruby_version_is ""..."3.0" do
+      it "returns an #{step_enum_class} when step is 0" do
+        @step.call(1, 2, 0).should be_an_instance_of(step_enum_class)
+      end
     end
 
     it "returns an #{step_enum_class} when not passed a block and self > stop" do
diff --git a/spec/ruby/core/numeric/step_spec.rb b/spec/ruby/core/numeric/step_spec.rb
index e906786..2773000 100644
--- a/spec/ruby/core/numeric/step_spec.rb
+++ b/spec/ruby/core/numeric/step_spec.rb
@@ -26,12 +26,14 @@ describe "Numeric#step" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/numeric/step_spec.rb#L26
         step_enum_class = Enumerator::ArithmeticSequence
       end
 
-      it "returns an #{step_enum_class} when step is 0" do
-        1.step(5, 0).should be_an_instance_of(step_enum_class)
-      end
+      ruby_version_is ""..."3.0" do
+        it "returns an #{step_enum_class} when step is 0" do
+          1.step(5, 0).should be_an_instance_of(step_enum_class)
+        end
 
-      it "returns an #{step_enum_class} when step is 0.0" do
-        1.step(2, 0.0).should be_an_instance_of(step_enum_class)
+        it "returns an #{step_enum_class} when step is 0.0" do
+          1.step(2, 0.0).should be_an_instance_of(step_enum_class)
+        end
       end
 
       describe "returned #{step_enum_class}" do
@@ -48,7 +50,7 @@ describe "Numeric#step" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/numeric/step_spec.rb#L50
             end
           end
 
-          ruby_version_is "2.6" do
+          ruby_version_is "2.6"..."3.0" do
             it "is infinity when step is 0" do
               enum = 1.step(5, 0)
               enum.size.should == Float::INFINITY
@@ -85,18 +87,20 @@ describe "Numeric#step" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/numeric/step_spec.rb#L87
   end
 
   describe 'with keyword arguments' do
-    it "doesn't raise an error when step is 0" do
-      -> { 1.step(to: 5, by: 0) { break } }.should_not raise_error
-    end
+    ruby_version_is ""..."3.0" do
+      it "doesn't raise an error when step is 0" do
+        -> { 1.step(to: 5, by: 0) { break } }.should_not raise_error
+      end
 
-    it "doesn't raise an error when step is 0.0" do
-      -> { 1.step(to: 2, by: 0.0) { break } }.should_not raise_error
-    end
+      it "doesn't raise an error when step is 0.0" do
+        -> { 1.step(to: 2, by: 0.0) { break } }.should_not raise_error
+      end
 
-    it "should loop over self when step is 0 or 0.0" do
-      1.step(to: 2, by: 0.0).take(5).should eql [1.0, 1.0, 1.0, 1.0, 1.0]
-      1.step(to: 2, by: 0).take(5).should eql [1, 1, 1, 1, 1]
-      1.1.step(to: 2, by: 0).take(5).should eql [1.1, 1.1, 1.1, 1.1, 1.1]
+      it "should loop over self when step is 0 or 0.0" do
+        1.step(to: 2, by: 0.0).take(5).should eql [1.0, 1.0, 1.0, 1.0, 1.0]
+        1.step(to: 2, by: 0).take(5).should eql [1, 1, 1, 1, 1]
+        1.1.step(to: 2, by: 0).take(5).should eql [1.1, 1.1, 1.1, 1.1, 1.1]
+      end
     end
 
     describe "when no block is given" do
@@ -106,12 +110,14 @@ describe "Numeric#step" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/numeric/step_spec.rb#L110
             1.step(by: 42).size.should == infinity_value
           end
 
-          it "should return infinity_value when step is 0" do
-            1.step(to: 5, by: 0).size.should == infinity_value
-          end
+          ruby_version_is ""..."3.0" do
+            it "should return infinity_value when step is 0" do
+              1.step(to: 5, by: 0).size.should == infinity_value
+            end
 
-          it "should return infinity_value when step is 0.0" do
-            1.step(to: 2, by: 0.0).size.should == infinity_value
+            it "should return infinity_value when step is 0.0" do
+              1.step(to: 2, by: 0.0).size.should == infinity_value
+            end
           end
 
           it "should return infinity_value when ascending towards a limit of Float::INFINITY" do
@@ -146,12 +152,24 @@ describe "Numeric#step" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/numeric/step_spec.rb#L152
   end
 
   describe 'with mixed arguments' do
-    it "doesn't raise an error when step is 0" do
-      -> { 1.step(5, by: 0) { break } }.should_not raise_error
+    ruby_version_is ""..."3.0" do
+      it "doesn't raise an error when step is 0" do
+        -> { 1.step(5, by: 0) { break } }.should_not raise_error
+      end
+
+      it "doesn't raise an error when step is 0.0" do
+        -> { 1.step(2, by: 0.0) { break } }.should_not raise_error
+      end
     end
 
-    it "doesn't raise an error when step is 0.0" do
-      -> { 1.step(2, by: 0.0) { break } }.should_not raise_error
+    ruby_version_is "3.0" do
+      it " raises an ArgumentError when step is 0" do
+        -> { 1.step(5, by: 0) { break } }.should raise_error(ArgumentError)
+      end
+
+      it "raises an ArgumentError when step is 0.0" do
+        -> { 1.step(2, by: 0.0) { break } }.should raise_error(ArgumentError)
+      end
     end
 
     it "raises a ArgumentError when limit and to are defined" do
@@ -162,21 +180,25 @@ describe "Numeric#step" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/numeric/step_spec.rb#L180
       -> { 1.step(5, 1, by: 5) { break } }.should raise_error(ArgumentError)
     end
 
-    it "should loop over self when step is 0 or 0.0" do
-      1.step(2, by: 0.0).take(5).should eql [1.0, 1.0, 1.0, 1.0, 1.0]
-      1.step(2, by: 0).take(5).should eql [1, 1, 1, 1, 1]
-      1.1.step(2, by: 0).take(5).should eql [1.1, 1.1, 1.1, 1.1, 1.1]
+    ruby_version_is ""..."3.0" do
+      it "should loop over self when step is 0 or 0.0" do
+        1.step(2, by: 0.0).take(5).should eql [1.0, 1.0, 1.0, 1.0, 1.0]
+        1.step(2, by: 0).take(5).should eql [1, 1, 1, 1, 1]
+        1.1.step(2, by: 0).take(5).should eql [1.1, 1.1, 1.1, 1.1, 1.1]
+      end
     end
 
     describe "when no block is given" do
       describe "returned Enumerator" do
         describe "size" do
-          it "should return infinity_value when step is 0" do
-            1.step(5, by: 0).size.should == infinity_value
-          end
+          ruby_version_is ""..."3.0" do
+            it "should return infinity_value when step is 0" do
+              1.step(5, by: 0).size.should == infinity_value
+            end
 
-          it "should return infinity_value when step is 0.0" do
-            1.step(2, by: 0.0).siz (... truncated)

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

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