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

ruby-changes:32096

From: nobu <ko1@a...>
Date: Fri, 13 Dec 2013 22:29:33 +0900 (JST)
Subject: [ruby-changes:32096] nobu:r44175 (trunk): proc.c, vm_method.c: fix super and alias

nobu	2013-12-13 22:29:23 +0900 (Fri, 13 Dec 2013)

  New Revision: 44175

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44175

  Log:
    proc.c, vm_method.c: fix super and alias
    
    * proc.c (method_owner): return the class where alias is defined, not
      the class original method is defined.
    * vm_method.c (rb_method_entry_make, rb_alias): store the originally
      defined class in me.  [Bug #7993] [Bug #7842] [Bug #9236]
    * vm_method.c (rb_method_entry_get_without_cache): cache included
      module but not iclass.

  Modified files:
    trunk/ChangeLog
    trunk/proc.c
    trunk/test/ruby/test_method.rb
    trunk/test/ruby/test_module.rb
    trunk/vm_method.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 44174)
+++ ChangeLog	(revision 44175)
@@ -1,3 +1,14 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Dec 13 22:29:21 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* proc.c (method_owner): return the class where alias is defined, not
+	  the class original method is defined.
+
+	* vm_method.c (rb_method_entry_make, rb_alias): store the originally
+	  defined class in me.  [Bug #7993] [Bug #7842] [Bug #9236]
+
+	* vm_method.c (rb_method_entry_get_without_cache): cache included
+	  module but not iclass.
+
 Fri Dec 13 16:27:17 2013  Aman Gupta <ruby@t...>
 
 	* gc.c (gc_info_decode): Use :major_by=>:nofree as fallback reason
Index: proc.c
===================================================================
--- proc.c	(revision 44174)
+++ proc.c	(revision 44175)
@@ -1395,7 +1395,7 @@ method_owner(VALUE obj) https://github.com/ruby/ruby/blob/trunk/proc.c#L1395
     struct METHOD *data;
 
     TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
-    return data->me->klass;
+    return data->rclass;
 }
 
 void
Index: vm_method.c
===================================================================
--- vm_method.c	(revision 44174)
+++ vm_method.c	(revision 44175)
@@ -227,7 +227,8 @@ rb_add_refined_method_entry(VALUE refine https://github.com/ruby/ruby/blob/trunk/vm_method.c#L227
 
 static rb_method_entry_t *
 rb_method_entry_make(VALUE klass, ID mid, rb_method_type_t type,
-		     rb_method_definition_t *def, rb_method_flag_t noex)
+		     rb_method_definition_t *def, rb_method_flag_t noex,
+		     VALUE defined_class)
 {
     rb_method_entry_t *me;
 #if NOEX_NOREDEF
@@ -321,7 +322,7 @@ rb_method_entry_make(VALUE klass, ID mid https://github.com/ruby/ruby/blob/trunk/vm_method.c#L322
     me->flag = NOEX_WITH_SAFE(noex);
     me->mark = 0;
     me->called_id = mid;
-    me->klass = klass;
+    me->klass = defined_class;
     me->def = def;
 
     if (def) {
@@ -422,7 +423,7 @@ rb_add_method(VALUE klass, ID mid, rb_me https://github.com/ruby/ruby/blob/trunk/vm_method.c#L423
     rb_thread_t *th;
     rb_control_frame_t *cfp;
     int line;
-    rb_method_entry_t *me = rb_method_entry_make(klass, mid, type, 0, noex);
+    rb_method_entry_t *me = rb_method_entry_make(klass, mid, type, 0, noex, klass);
     rb_method_definition_t *def = ALLOC(rb_method_definition_t);
     if (me->def && me->def->type == VM_METHOD_TYPE_REFINED) {
 	me->def->body.orig_me->def = def;
@@ -481,15 +482,23 @@ rb_add_method(VALUE klass, ID mid, rb_me https://github.com/ruby/ruby/blob/trunk/vm_method.c#L482
     return me;
 }
 
-rb_method_entry_t *
-rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me, rb_method_flag_t noex)
+static rb_method_entry_t *
+method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me,
+		 rb_method_flag_t noex, VALUE defined_class)
 {
     rb_method_type_t type = me->def ? me->def->type : VM_METHOD_TYPE_UNDEF;
-    rb_method_entry_t *newme = rb_method_entry_make(klass, mid, type, me->def, noex);
+    rb_method_entry_t *newme = rb_method_entry_make(klass, mid, type, me->def, noex,
+						    defined_class);
     method_added(klass, mid);
     return newme;
 }
 
+rb_method_entry_t *
+rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me, rb_method_flag_t noex)
+{
+    return method_entry_set(klass, mid, me, noex, klass);
+}
+
 #define UNDEF_ALLOC_FUNC ((rb_alloc_func_t)-1)
 
 void
@@ -551,6 +560,9 @@ rb_method_entry_get_without_cache(VALUE https://github.com/ruby/ruby/blob/trunk/vm_method.c#L560
     VALUE defined_class;
     rb_method_entry_t *me = search_method(klass, id, &defined_class);
 
+    if (me && RB_TYPE_P(me->klass, T_ICLASS))
+	defined_class = me->klass;
+
     if (ruby_running) {
 	struct cache_entry *ent;
 	ent = GLOBAL_METHOD_CACHE(klass, id);
@@ -1218,6 +1230,7 @@ void https://github.com/ruby/ruby/blob/trunk/vm_method.c#L1230
 rb_alias(VALUE klass, ID name, ID def)
 {
     VALUE target_klass = klass;
+    VALUE defined_class;
     rb_method_entry_t *orig_me;
     rb_method_flag_t flag = NOEX_UNDEF;
 
@@ -1228,7 +1241,7 @@ rb_alias(VALUE klass, ID name, ID def) https://github.com/ruby/ruby/blob/trunk/vm_method.c#L1241
     rb_frozen_class_p(klass);
 
   again:
-    orig_me = search_method(klass, def, 0);
+    orig_me = search_method(klass, def, &defined_class);
 
     if (UNDEFINED_METHOD_ENTRY_P(orig_me)) {
 	if ((!RB_TYPE_P(klass, T_MODULE)) ||
@@ -1243,9 +1256,14 @@ rb_alias(VALUE klass, ID name, ID def) https://github.com/ruby/ruby/blob/trunk/vm_method.c#L1256
 	flag = orig_me->flag;
 	goto again;
     }
+    if (RB_TYPE_P(defined_class, T_ICLASS)) {
+	VALUE real_class = RBASIC_CLASS(defined_class);
+	if (real_class && RCLASS_ORIGIN(real_class) == defined_class)
+	    defined_class = real_class;
+    }
 
     if (flag == NOEX_UNDEF) flag = orig_me->flag;
-    rb_method_entry_set(target_klass, name, orig_me, flag);
+    method_entry_set(target_klass, name, orig_me, flag, defined_class);
 }
 
 /*
Index: test/ruby/test_module.rb
===================================================================
--- test/ruby/test_module.rb	(revision 44174)
+++ test/ruby/test_module.rb	(revision 44175)
@@ -1587,6 +1587,57 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L1587
     assert_include(im, mixin, bug8025)
   end
 
+  def test_prepend_super_in_alias
+    bug7842 = '[Bug #7842]'
+
+    p = labeled_module("P") do
+      def m; "P"+super; end
+    end
+    a = labeled_class("A") do
+      def m; "A"; end
+    end
+    b = labeled_class("B", a) do
+      def m; "B"+super; end
+      alias m2 m
+      prepend p
+      alias m3 m
+    end
+    assert_equal("BA", b.new.m2, bug7842)
+    assert_equal("PBA", b.new.m3, bug7842)
+  end
+
+  def test_include_super_in_alias
+    bug9236 = '[Bug #9236]'
+
+    fun = labeled_module("Fun") do
+      def hello
+        orig_hello
+      end
+    end
+
+    m1 = labeled_module("M1") do
+      def hello
+        'hello!'
+      end
+    end
+
+    m2 = labeled_module("M2") do
+      def hello
+        super
+      end
+    end
+
+    foo = labeled_class("Foo") do
+      include m1
+      include m2
+
+      alias orig_hello hello
+      include fun
+    end
+
+    assert_equal('hello!', foo.new.hello, bug9236)
+  end
+
   def test_class_variables
     m = Module.new
     m.class_variable_set(:@@foo, 1)
Index: test/ruby/test_method.rb
===================================================================
--- test/ruby/test_method.rb	(revision 44174)
+++ test/ruby/test_method.rb	(revision 44175)
@@ -365,7 +365,7 @@ class TestMethod < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_method.rb#L365
     c3 = Class.new(c)
     c3.class_eval { alias bar foo }
     m3 = c3.new.method(:bar)
-    assert_equal("#<Method: #{ c3.inspect }#bar(foo)>", m3.inspect, bug7806)
+    assert_equal("#<Method: #{c3.inspect}(#{c.inspect})#bar(foo)>", m3.inspect, bug7806)
   end
 
   def test_callee_top_level
@@ -571,17 +571,23 @@ class TestMethod < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_method.rb#L571
 
   def test_alias_owner
     bug7613 = '[ruby-core:51105]'
+    bug7993 = '[Bug #7993]'
     c = Class.new {
       def foo
       end
+      prepend Module.new
+      attr_reader :zot
     }
     x = c.new
     class << x
       alias bar foo
     end
+    assert_equal(c, c.instance_method(:foo).owner)
     assert_equal(c, x.method(:foo).owner)
     assert_equal(x.singleton_class, x.method(:bar).owner)
     assert_not_equal(x.method(:foo), x.method(:bar), bug7613)
+    assert_equal(c, x.method(:zot).owner, bug7993)
+    assert_equal(c, c.instance_method(:zot).owner, bug7993)
   end
 
   def test_included

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

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