1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Skip refined method when exporting methods with changed visibility

Previously, attempting to change the visibility of a method in a
singleton class for a class/module that is prepended to and refined
would raise a NoMethodError.

Fixes [Bug #17519]
This commit is contained in:
Jeremy Evans 2021-02-18 15:22:37 -08:00
parent 756e8a2cf3
commit 58660e9434
Notes: git 2021-03-17 04:10:31 +09:00
2 changed files with 34 additions and 3 deletions

View file

@ -2294,6 +2294,29 @@ class TestModule < Test::Unit::TestCase
assert_equal(0, 1 / 2)
end
def test_visibility_after_refine_and_visibility_change
m = Module.new
c = Class.new do
def x; :x end
end
c.prepend(m)
Module.new do
refine c do
def x; :y end
end
end
o1 = c.new
o2 = c.new
assert_equal(:x, o1.public_send(:x))
assert_equal(:x, o2.public_send(:x))
o1.singleton_class.send(:private, :x)
o2.singleton_class.send(:public, :x)
assert_raise(NoMethodError) { o1.public_send(:x) }
assert_equal(:x, o2.public_send(:x))
end
def test_prepend_visibility
bug8005 = '[ruby-core:53106] [Bug #8005]'
c = Class.new do

View file

@ -968,7 +968,7 @@ rb_method_entry_at(VALUE klass, ID id)
}
static inline rb_method_entry_t*
search_method(VALUE klass, ID id, VALUE *defined_class_ptr)
search_method0(VALUE klass, ID id, VALUE *defined_class_ptr, bool skip_refined)
{
rb_method_entry_t *me = NULL;
@ -977,7 +977,9 @@ search_method(VALUE klass, ID id, VALUE *defined_class_ptr)
for (; klass; klass = RCLASS_SUPER(klass)) {
RB_DEBUG_COUNTER_INC(mc_search_super);
if ((me = lookup_method_table(klass, id)) != 0) {
break;
if (!skip_refined || me->def->type != VM_METHOD_TYPE_REFINED) {
break;
}
}
}
@ -989,6 +991,12 @@ search_method(VALUE klass, ID id, VALUE *defined_class_ptr)
return me;
}
static inline rb_method_entry_t*
search_method(VALUE klass, ID id, VALUE *defined_class_ptr)
{
return search_method0(klass, id, defined_class_ptr, false);
}
static rb_method_entry_t *
search_method_protect(VALUE klass, ID id, VALUE *defined_class_ptr)
{
@ -1376,7 +1384,7 @@ rb_export_method(VALUE klass, ID name, rb_method_visibility_t visi)
VALUE defined_class;
VALUE origin_class = RCLASS_ORIGIN(klass);
me = search_method(origin_class, name, &defined_class);
me = search_method0(origin_class, name, &defined_class, true);
if (!me && RB_TYPE_P(klass, T_MODULE)) {
me = search_method(rb_cObject, name, &defined_class);