[前][次][番号順一覧][スレッド一覧]

ruby-changes:65568

From: nagachika <ko1@a...>
Date: Sat, 20 Mar 2021 16:30:36 +0900 (JST)
Subject: [ruby-changes:65568] 6e962f02b2 (ruby_2_7): merge revision(s) 58660e943488778563b9e41005a601e9660ce21f: [Backport #17519]

https://git.ruby-lang.org/ruby.git/commit/?id=6e962f02b2

From 6e962f02b266c3a6c47e50cf2e9ab7b1db25e515 Mon Sep 17 00:00:00 2001
From: nagachika <nagachika@r...>
Date: Sat, 20 Mar 2021 16:14:18 +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                |  2 +-
 vm_method.c              | 16 +++++++++++++---
 3 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb
index 69f03ae..5cfda01 100644
--- a/test/ruby/test_module.rb
+++ b/test/ruby/test_module.rb
@@ -1924,6 +1924,29 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L1924
     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 bf6b906..ce6610c 100644
--- a/version.h
+++ b/version.h
@@ -2,7 +2,7 @@ https://github.com/ruby/ruby/blob/trunk/version.h#L2
 # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
 #define RUBY_VERSION_TEENY 3
 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
-#define RUBY_PATCHLEVEL 175
+#define RUBY_PATCHLEVEL 176
 
 #define RUBY_RELEASE_YEAR 2021
 #define RUBY_RELEASE_MONTH 3
diff --git a/vm_method.c b/vm_method.c
index efe7160..3c08784 100644
--- a/vm_method.c
+++ b/vm_method.c
@@ -738,13 +738,17 @@ rb_get_alloc_func(VALUE klass) https://github.com/ruby/ruby/blob/trunk/vm_method.c#L738
 }
 
 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;
 
     for (; klass; klass = RCLASS_SUPER(klass)) {
 	RB_DEBUG_COUNTER_INC(mc_search_super);
-	if ((me = lookup_method_table(klass, id)) != 0) break;
+	if ((me = lookup_method_table(klass, id)) != 0) {
+	    if (!skip_refined || me->def->type != VM_METHOD_TYPE_REFINED) {
+		break;
+	    }
+	}
     }
 
     if (defined_class_ptr)
@@ -813,6 +817,12 @@ verify_method_cache(VALUE klass, ID id, VALUE defined_class, rb_method_entry_t * https://github.com/ruby/ruby/blob/trunk/vm_method.c#L817
     }
 }
 
+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 *
 method_entry_get(VALUE klass, ID id, VALUE *defined_class_ptr)
 {
@@ -1068,7 +1078,7 @@ rb_export_method(VALUE klass, ID name, rb_method_visibility_t visi) https://github.com/ruby/ruby/blob/trunk/vm_method.c#L1078
     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.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]