aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-11-22 07:53:07 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-11-22 07:53:07 +0000
commit633fef6dec9f078d7c02b737a76f3aad0ba5ccc8 (patch)
tree066fd47de5067080401333949c4871111a12ad9b
parent8a15e0800615a849e93ed3192290bc8315b6c200 (diff)
downloadruby-633fef6dec9f078d7c02b737a76f3aad0ba5ccc8.tar.gz
Enable refinements to public_send.
[Feature #15326] [Fix GH-2019] From: manga_osyo <manga.osyo@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65919 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--NEWS4
-rw-r--r--spec/ruby/core/module/refine_spec.rb18
-rw-r--r--test/ruby/test_refinement.rb33
-rw-r--r--vm_eval.c12
4 files changed, 64 insertions, 3 deletions
diff --git a/NEWS b/NEWS
index 7487b26541..24bd484cc2 100644
--- a/NEWS
+++ b/NEWS
@@ -16,7 +16,9 @@ sufficient information, see the ChangeLog file or Redmine
* <code>$SAFE</code> is a process global state and we can set 0 again. [Feature #14250]
-* refinements take place at block passing. [Feature #14223]
+* refinements takes place at block passing. [Feature #14223]
+
+* refinements takes place at Kernel#public_send. [Feature #15326]
* +else+ without +rescue+ now causes a syntax error. [EXPERIMENTAL]
diff --git a/spec/ruby/core/module/refine_spec.rb b/spec/ruby/core/module/refine_spec.rb
index 287aa601a9..97645e5466 100644
--- a/spec/ruby/core/module/refine_spec.rb
+++ b/spec/ruby/core/module/refine_spec.rb
@@ -425,6 +425,24 @@ describe "Module#refine" do
end
end
+ ruby_version_is "2.6" do
+ it "is honored by Kernel#public_send" do
+ refinement = Module.new do
+ refine ModuleSpecs::ClassWithFoo do
+ def foo; "foo from refinement"; end
+ end
+ end
+
+ result = nil
+ Module.new do
+ using refinement
+ result = ModuleSpecs::ClassWithFoo.new.public_send :foo
+ end
+
+ result.should == "foo from refinement"
+ end
+ end
+
ruby_version_is "" ... "2.5" do
it "is not honored by string interpolation" do
refinement = Module.new do
diff --git a/test/ruby/test_refinement.rb b/test/ruby/test_refinement.rb
index 050842724a..11eea720bd 100644
--- a/test/ruby/test_refinement.rb
+++ b/test/ruby/test_refinement.rb
@@ -19,6 +19,10 @@ class TestRefinement < Test::Unit::TestCase
return "Foo#a"
end
+ def b
+ return "Foo#b"
+ end
+
def call_x
return x
end
@@ -41,6 +45,10 @@ class TestRefinement < Test::Unit::TestCase
def a
return "FooExt#a"
end
+
+ private def b
+ return "FooExt#b"
+ end
end
end
@@ -94,6 +102,18 @@ class TestRefinement < Test::Unit::TestCase
return foo.send(:z)
end
+ def self.send_b_on(foo)
+ return foo.send(:b)
+ end
+
+ def self.public_send_z_on(foo)
+ return foo.public_send(:z)
+ end
+
+ def self.public_send_b_on(foo)
+ return foo.public_send(:b)
+ end
+
def self.method_z(foo)
return foo.method(:z)
end
@@ -179,11 +199,20 @@ class TestRefinement < Test::Unit::TestCase
foo = Foo.new
assert_raise(NoMethodError) { foo.send(:z) }
assert_equal("FooExt#z", FooExtClient.send_z_on(foo))
+ assert_equal("FooExt#b", FooExtClient.send_b_on(foo))
assert_raise(NoMethodError) { foo.send(:z) }
assert_equal(true, RespondTo::Sub.new.respond_to?(:foo))
end
+ def test_public_send_should_use_refinements
+ foo = Foo.new
+ assert_raise(NoMethodError) { foo.public_send(:z) }
+ assert_equal("FooExt#z", FooExtClient.public_send_z_on(foo))
+ assert_equal("Foo#b", foo.public_send(:b))
+ assert_raise(NoMethodError) { FooExtClient.public_send_b_on(foo) }
+ end
+
def test_method_should_not_use_refinements
foo = Foo.new
assert_raise(NameError) { foo.method(:z) }
@@ -907,6 +936,10 @@ class TestRefinement < Test::Unit::TestCase
return foo.send(:z)
end
+ def self.public_send_z_on(foo)
+ return foo.public_send(:z)
+ end
+
def self.method_z(foo)
return foo.method(:z)
end
diff --git a/vm_eval.c b/vm_eval.c
index c0d7062947..47191a0aab 100644
--- a/vm_eval.c
+++ b/vm_eval.c
@@ -290,8 +290,16 @@ rb_call0(rb_execution_context_t *ec,
VALUE recv, ID mid, int argc, const VALUE *argv,
call_type scope, VALUE self)
{
- const rb_callable_method_entry_t *me = rb_search_method_entry(recv, mid);
- enum method_missing_reason call_status = rb_method_call_status(ec, me, scope, self);
+ const rb_callable_method_entry_t *me;
+ enum method_missing_reason call_status;
+
+ if (scope == CALL_PUBLIC) {
+ me = rb_callable_method_entry_with_refinements(CLASS_OF(recv), mid, NULL);
+ }
+ else {
+ me = rb_search_method_entry(recv, mid);
+ }
+ call_status = rb_method_call_status(ec, me, scope, self);
if (call_status != MISSING_NONE) {
return method_missing(recv, mid, argc, argv, call_status);