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

ruby-changes:64307

From: Koichi <ko1@a...>
Date: Sat, 19 Dec 2020 04:33:32 +0900 (JST)
Subject: [ruby-changes:64307] cee02d754d (master): fix refinements/prepend bug

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

From cee02d754d76563635c1db90d2ab6c01f8492470 Mon Sep 17 00:00:00 2001
From: Koichi Sasada <ko1@a...>
Date: Sat, 19 Dec 2020 04:30:25 +0900
Subject: fix refinements/prepend bug

replaced method entry should be invalidated.
[Bug #17386]

diff --git a/class.c b/class.c
index 45c9c6a..a766091 100644
--- a/class.c
+++ b/class.c
@@ -1110,7 +1110,9 @@ move_refined_method(ID key, VALUE value, void *data) https://github.com/ruby/ruby/blob/trunk/class.c#L1110
     struct rb_id_table *tbl = RCLASS_M_TBL(klass);
 
     if (me->def->type == VM_METHOD_TYPE_REFINED) {
-	if (me->def->body.refined.orig_me) {
+        rb_clear_method_cache(klass, me->called_id);
+
+        if (me->def->body.refined.orig_me) {
 	    const rb_method_entry_t *orig_me = me->def->body.refined.orig_me, *new_me;
 	    RB_OBJ_WRITE(me, &me->def->body.refined.orig_me, NULL);
 	    new_me = rb_method_entry_clone(me);
diff --git a/test/ruby/test_refinement.rb b/test/ruby/test_refinement.rb
index 61ad2d9..b27a176 100644
--- a/test/ruby/test_refinement.rb
+++ b/test/ruby/test_refinement.rb
@@ -2446,4 +2446,31 @@ class TestRefinement < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L2446
   def eval_using(mod, s)
     eval("using #{mod}; #{s}", Sandbox::BINDING)
   end
+
+  # [Bug #17386]
+  def test_prepended_with_method_cache
+    foo = Class.new do
+      def foo
+        :Foo
+      end
+    end
+
+    code = Module.new do
+      def foo
+        :Code
+      end
+    end
+
+    _ext = Module.new do
+      refine foo do
+        def foo; end
+      end
+    end
+
+    obj = foo.new
+
+    assert_equal :Foo, obj.foo
+    foo.prepend code
+    assert_equal :Code, obj.foo
+  end
 end
-- 
cgit v0.10.2


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

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