ruby-changes:62390
From: Nobuyoshi <ko1@a...>
Date: Fri, 24 Jul 2020 21:19:49 +0900 (JST)
Subject: [ruby-changes:62390] 3ead2770a1 (master): Respect visibility in non-array Enumerable#inject [Bug #13592]
https://git.ruby-lang.org/ruby.git/commit/?id=3ead2770a1 From 3ead2770a1fd7452a9b875a8be7b93335f41abda Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Fri, 24 Jul 2020 21:08:50 +0900 Subject: Respect visibility in non-array Enumerable#inject [Bug #13592] diff --git a/enum.c b/enum.c index 8769680..69c1641 100644 --- a/enum.c +++ b/enum.c @@ -734,7 +734,7 @@ inject_op_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, p)) https://github.com/ruby/ruby/blob/trunk/enum.c#L734 } else if (SYMBOL_P(name = memo->u3.value)) { const ID mid = SYM2ID(name); - MEMO_V1_SET(memo, rb_funcallv(memo->v1, mid, 1, &i)); + MEMO_V1_SET(memo, rb_funcallv_public(memo->v1, mid, 1, &i)); } else { VALUE args[2]; diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index 809db31..8d30b34 100644 --- a/test/ruby/test_enum.rb +++ b/test/ruby/test_enum.rb @@ -239,6 +239,62 @@ class TestEnumerable < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L239 assert_equal(2.0+3.0i, [2.0, 3.0i].inject(:+)) end + def test_inject_op_redefined + assert_separately([], "#{<<~"end;"}\n""end") + k = Class.new do + include Enumerable + def each + yield 1 + yield 2 + yield 3 + end + end + all_assertions_foreach("", *%i[+ * / - %]) do |op| + bug = '[ruby-dev:49510] [Bug#12178] should respect redefinition' + begin + Integer.class_eval do + alias_method :orig, op + define_method(op) do |x| + 0 + end + end + assert_equal(0, k.new.inject(op), bug) + ensure + Integer.class_eval do + undef_method op + alias_method op, :orig + end + end + end; + end + + def test_inject_op_private + assert_separately([], "#{<<~"end;"}\n""end") + k = Class.new do + include Enumerable + def each + yield 1 + yield 2 + yield 3 + end + end + all_assertions_foreach("", *%i[+ * / - %]) do |op| + bug = '[ruby-core:81349] [Bug #13592] should respect visibility' + assert_raise_with_message(NoMethodError, /private method/, bug) do + begin + Integer.class_eval do + private op + end + k.new.inject(op) + ensure + Integer.class_eval do + public op + end + end + end + end; + end + def test_inject_array_op_redefined assert_separately([], "#{<<~"end;"}\n""end") all_assertions_foreach("", *%i[+ * / - %]) do |op| -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/