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

ruby-changes:60782

From: Takashi <ko1@a...>
Date: Wed, 15 Apr 2020 15:50:15 +0900 (JST)
Subject: [ruby-changes:60782] 79f3403be0 (master): Invalidate fastpath when calling attr_reader by super

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

From 79f3403be0cdbec814be29308c0583599ca5824f Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@g...>
Date: Tue, 14 Apr 2020 23:49:28 -0700
Subject: Invalidate fastpath when calling attr_reader by super

The same bug as 8355a99883 existed in attr_reader too.

diff --git a/test/ruby/test_super.rb b/test/ruby/test_super.rb
index 7cbe851..78c8ee5 100644
--- a/test/ruby/test_super.rb
+++ b/test/ruby/test_super.rb
@@ -604,7 +604,7 @@ class TestSuper < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_super.rb#L604
     assert_equal :boo2, subklass.new.boo
   end
 
-  def test_super_attr_writer # Bug #16785
+  def test_super_attr_writer # [Bug #16785]
     writer_class = Class.new do
       attr_writer :test
     end
@@ -633,4 +633,31 @@ class TestSuper < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_super.rb#L633
     assert_equal 3, superwriter.instance_variable_get(:@test)
     assert_equal 4, inherited.instance_variable_get(:@test)
   end
+
+  def test_super_attr_reader
+    writer_class = Class.new do
+      attr_reader :test
+    end
+    superwriter_class = Class.new(writer_class) do
+      def initialize
+        @test = 1 # index: 1
+      end
+
+      def test
+        super
+      end
+    end
+    inherited_class = Class.new(superwriter_class) do
+      def initialize
+        @a = nil
+        @test = 2 # index: 2
+      end
+    end
+
+    superwriter = superwriter_class.new
+    assert_equal 1, superwriter.test # set ic->index of superwriter_class#test to 1
+
+    inherited = inherited_class.new
+    assert_equal 2, inherited.test # it may read index=1 while it should be index=2
+  end
 end
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
index c4a8dfa..70c957c 100644
--- a/vm_insnhelper.c
+++ b/vm_insnhelper.c
@@ -3235,9 +3235,9 @@ vm_search_super_method(const rb_control_frame_t *reg_cfp, struct rb_call_data *c https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L3235
             switch (cached_cme->def->type) {
               // vm_call_refined (search_refined_method) assumes cc->call is vm_call_super_method on invokesuper
               case VM_METHOD_TYPE_REFINED:
-              // cc->klass is superclass of a class of receiver. Checking cc->klass is not enough to invalidate IVC for the receiver class.
+              // cc->klass is superclass of receiver class. Checking cc->klass is not enough to invalidate IVC for the receiver class.
               case VM_METHOD_TYPE_ATTRSET:
-              // TODO: case VM_METHOD_TYPE_IVAR:
+              case VM_METHOD_TYPE_IVAR:
                 vm_cc_call_set(cd->cc, vm_call_super_method); // invalidate fastpath
                 break;
               default:
-- 
cgit v0.10.2


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

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