From d1cec0bca588266b9af1d55e592016c45ee68fbb Mon Sep 17 00:00:00 2001 From: "NARUSE, Yui" Date: Fri, 2 Apr 2021 12:26:56 +0900 Subject: merge revision(s) 58660e943488778563b9e41005a601e9660ce21f: [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(-) --- test/ruby/test_module.rb | 23 +++++++++++++++++++++++ version.h | 6 +++--- vm_method.c | 14 +++++++++++--- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index 43f0c51753..254dc51b9b 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -2237,6 +2237,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 diff --git a/version.h b/version.h index 4edcb6ddbe..5f07f2c35e 100644 --- a/version.h +++ b/version.h @@ -12,11 +12,11 @@ # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR #define RUBY_VERSION_TEENY 0 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR -#define RUBY_PATCHLEVEL 57 +#define RUBY_PATCHLEVEL 58 #define RUBY_RELEASE_YEAR 2021 -#define RUBY_RELEASE_MONTH 3 -#define RUBY_RELEASE_DAY 24 +#define RUBY_RELEASE_MONTH 4 +#define RUBY_RELEASE_DAY 2 #include "ruby/version.h" diff --git a/vm_method.c b/vm_method.c index 721caeb431..e72ef1a7a5 100644 --- a/vm_method.c +++ b/vm_method.c @@ -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); -- cgit v1.2.3