merge revision(s) 58660e9434: [Backport #17519]

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]
	---
	 test/ruby/test_module.rb | 23 +++++++++++++++++++++++
	 vm_method.c              | 14 +++++++++++---
	 2 files changed, 34 insertions(+), 3 deletions(-)
This commit is contained in:
NARUSE, Yui 2021-04-02 12:26:56 +09:00
parent d3779ab3b8
commit d1cec0bca5
3 changed files with 37 additions and 6 deletions

View file

@ -960,7 +960,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;
@ -969,7 +969,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;
}
}
}
@ -981,6 +983,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)
{
@ -1368,7 +1376,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);