ruby-changes:49246
From: nagachika <ko1@a...>
Date: Wed, 20 Dec 2017 23:54:04 +0900 (JST)
Subject: [ruby-changes:49246] nagachika:r61361 (ruby_2_4): merge revision(s) 57649, 57651: [Backport #13222]
nagachika 2017-12-20 23:53:56 +0900 (Wed, 20 Dec 2017) New Revision: 61361 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=61361 Log: merge revision(s) 57649,57651: [Backport #13222] array.c: finish_exact_sum * array.c (finish_exact_sum): extract duplicate code from rb_ary_sum. array.c: check if numeric * array.c (finish_exact_sum): add 0 and the initial value to check if the latter is numeric. [ruby-core:79572] [Bug #13222] Modified directories: branches/ruby_2_4/ Modified files: branches/ruby_2_4/array.c branches/ruby_2_4/test/ruby/test_array.rb branches/ruby_2_4/version.h Index: ruby_2_4/array.c =================================================================== --- ruby_2_4/array.c (revision 61360) +++ ruby_2_4/array.c (revision 61361) @@ -5691,6 +5691,26 @@ rb_ary_dig(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/ruby_2_4/array.c#L5691 return rb_obj_dig(argc, argv, self, Qnil); } +static inline VALUE +finish_exact_sum(long n, VALUE r, VALUE v, int z) +{ + if (n != 0) + v = rb_fix_plus(LONG2FIX(n), v); + if (r != Qundef) { + /* r can be an Integer when mathn is loaded */ + if (FIXNUM_P(r)) + v = rb_fix_plus(r, v); + else if (RB_TYPE_P(r, T_BIGNUM)) + v = rb_big_plus(r, v); + else + v = rb_rational_plus(r, v); + } + else if (!n && z) { + v = rb_fix_plus(LONG2FIX(0), v); + } + return v; +} + /* * call-seq: * ary.sum(init=0) -> number @@ -5772,31 +5792,11 @@ rb_ary_sum(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/ruby_2_4/array.c#L5792 else goto not_exact; } - if (n != 0) - v = rb_fix_plus(LONG2FIX(n), v); - if (r != Qundef) { - /* r can be an Integer when mathn is loaded */ - if (FIXNUM_P(r)) - v = rb_fix_plus(r, v); - else if (RB_TYPE_P(r, T_BIGNUM)) - v = rb_big_plus(r, v); - else - v = rb_rational_plus(r, v); - } + v = finish_exact_sum(n, r, v, argc!=0); return v; not_exact: - if (n != 0) - v = rb_fix_plus(LONG2FIX(n), v); - if (r != Qundef) { - /* r can be an Integer when mathn is loaded */ - if (FIXNUM_P(r)) - v = rb_fix_plus(r, v); - else if (RB_TYPE_P(r, T_BIGNUM)) - v = rb_big_plus(r, v); - else - v = rb_rational_plus(r, v); - } + v = finish_exact_sum(n, r, v, i!=0); if (RB_FLOAT_TYPE_P(e)) { /* Index: ruby_2_4/test/ruby/test_array.rb =================================================================== --- ruby_2_4/test/ruby/test_array.rb (revision 61360) +++ ruby_2_4/test/ruby/test_array.rb (revision 61361) @@ -2839,6 +2839,9 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_4/test/ruby/test_array.rb#L2839 assert_equal("abc", ["a", "b", "c"].sum("")) assert_equal([1, [2], 3], [[1], [[2]], [3]].sum([])) + assert_raise(TypeError) {[0].sum("")} + assert_raise(TypeError) {[1].sum("")} + assert_separately(%w[-rmathn], <<-EOS, ignore_stderr: true) assert_equal(6, [1r, 2, 3r].sum) EOS Index: ruby_2_4/version.h =================================================================== --- ruby_2_4/version.h (revision 61360) +++ ruby_2_4/version.h (revision 61361) @@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_4/version.h#L1 #define RUBY_VERSION "2.4.4" #define RUBY_RELEASE_DATE "2017-12-20" -#define RUBY_PATCHLEVEL 208 +#define RUBY_PATCHLEVEL 209 #define RUBY_RELEASE_YEAR 2017 #define RUBY_RELEASE_MONTH 12 Index: ruby_2_4 =================================================================== --- ruby_2_4 (revision 61360) +++ ruby_2_4 (revision 61361) Property changes on: ruby_2_4 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /trunk:r57649,57651 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/