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

ruby-changes:34076

From: usa <ko1@a...>
Date: Tue, 27 May 2014 13:54:44 +0900 (JST)
Subject: [ruby-changes:34076] usa:r46157 (ruby_2_0_0): merge revision(s) 45564, 45565, 45584, 45585: [Backport #9721]

usa	2014-05-27 13:54:29 +0900 (Tue, 27 May 2014)

  New Revision: 46157

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

  Log:
    merge revision(s) 45564,45565,45584,45585: [Backport #9721]
    
    * proc.c (rb_method_call_with_block, umethod_bind): call with
      IClass including the module for a module instance method.
      [ruby-core:61936] [Bug #9721]
    
    * vm_insnhelper.c (vm_search_super_method): allow bound
      UnboundMethod case.
    
    * proc.c (umethod_bind): use the ancestor iclass instead of new
      iclass to get rid of infinite recursion, if the defined module
      is already included.  [ruby-core:62014] [Bug #9721]

  Modified directories:
    branches/ruby_2_0_0/
  Modified files:
    branches/ruby_2_0_0/ChangeLog
    branches/ruby_2_0_0/internal.h
    branches/ruby_2_0_0/object.c
    branches/ruby_2_0_0/proc.c
    branches/ruby_2_0_0/test/ruby/test_super.rb
    branches/ruby_2_0_0/version.h
    branches/ruby_2_0_0/vm_insnhelper.c
Index: ruby_2_0_0/ChangeLog
===================================================================
--- ruby_2_0_0/ChangeLog	(revision 46156)
+++ ruby_2_0_0/ChangeLog	(revision 46157)
@@ -1,3 +1,18 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/ChangeLog#L1
+Tue May 27 13:42:36 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* proc.c (umethod_bind): use the ancestor iclass instead of new
+	  iclass to get rid of infinite recursion, if the defined module
+	  is already included.  [ruby-core:62014] [Bug #9721]
+
+Tue May 27 13:42:36 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* proc.c (rb_method_call_with_block, umethod_bind): call with
+	  IClass including the module for a module instance method.
+	  [ruby-core:61936] [Bug #9721]
+
+	* vm_insnhelper.c (vm_search_super_method): allow bound
+	  UnboundMethod case.
+
 Tue May 27 11:51:00 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* array.c (ary_reject): may be turned into a shared array during
Index: ruby_2_0_0/object.c
===================================================================
--- ruby_2_0_0/object.c	(revision 46156)
+++ ruby_2_0_0/object.c	(revision 46157)
@@ -524,6 +524,8 @@ class_or_module_required(VALUE c) https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/object.c#L524
     return c;
 }
 
+static VALUE class_search_ancestor(VALUE cl, VALUE c);
+
 /*
  *  call-seq:
  *     obj.instance_of?(class)    -> true or false
@@ -584,15 +586,27 @@ rb_obj_is_kind_of(VALUE obj, VALUE c) https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/object.c#L586
     VALUE cl = CLASS_OF(obj);
 
     c = class_or_module_required(c);
-    c = RCLASS_ORIGIN(c);
+    return class_search_ancestor(cl, RCLASS_ORIGIN(c)) ? Qtrue : Qfalse;
+}
+
+static VALUE
+class_search_ancestor(VALUE cl, VALUE c)
+{
     while (cl) {
 	if (cl == c || RCLASS_M_TBL(cl) == RCLASS_M_TBL(c))
-	    return Qtrue;
+	    return cl;
 	cl = RCLASS_SUPER(cl);
     }
-    return Qfalse;
+    return 0;
 }
 
+VALUE
+rb_class_search_ancestor(VALUE cl, VALUE c)
+{
+    cl = class_or_module_required(cl);
+    c = class_or_module_required(c);
+    return class_search_ancestor(cl, RCLASS_ORIGIN(c));
+}
 
 /*
  *  call-seq:
@@ -1486,16 +1500,12 @@ rb_class_inherited_p(VALUE mod, VALUE ar https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/object.c#L1500
 	rb_raise(rb_eTypeError, "compared with non class/module");
     }
     arg = RCLASS_ORIGIN(arg);
-    while (mod) {
-	if (RCLASS_M_TBL(mod) == RCLASS_M_TBL(arg))
-	    return Qtrue;
-	mod = RCLASS_SUPER(mod);
+    if (class_search_ancestor(mod, arg)) {
+	return Qtrue;
     }
     /* not mod < arg; check if mod > arg */
-    while (arg) {
-	if (RCLASS_M_TBL(arg) == RCLASS_M_TBL(start))
-	    return Qfalse;
-	arg = RCLASS_SUPER(arg);
+    if (class_search_ancestor(arg, start)) {
+	return Qfalse;
     }
     return Qnil;
 }
Index: ruby_2_0_0/proc.c
===================================================================
--- ruby_2_0_0/proc.c	(revision 46156)
+++ ruby_2_0_0/proc.c	(revision 46157)
@@ -1550,6 +1550,7 @@ rb_method_call_with_block(int argc, VALU https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/proc.c#L1550
     if ((state = EXEC_TAG()) == 0) {
 	rb_thread_t *th = GET_THREAD();
 	rb_block_t *block = 0;
+	VALUE defined_class;
 
 	if (!NIL_P(pass_procval)) {
 	    rb_proc_t *pass_proc;
@@ -1558,7 +1559,9 @@ rb_method_call_with_block(int argc, VALU https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/proc.c#L1559
 	}
 
 	th->passed_block = block;
-	result = rb_vm_call(th, data->recv, data->id,  argc, argv, data->me, data->defined_class);
+	defined_class = data->defined_class;
+	if (BUILTIN_TYPE(defined_class) == T_MODULE) defined_class = data->rclass;
+	result = rb_vm_call(th, data->recv, data->id, argc, argv, data->me, defined_class);
     }
     POP_TAG();
     if (safe >= 0)
@@ -1663,18 +1666,21 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/proc.c#L1666
 umethod_bind(VALUE method, VALUE recv)
 {
     struct METHOD *data, *bound;
+    VALUE methclass;
+    VALUE rclass;
 
     TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
 
-    if (!RB_TYPE_P(data->rclass, T_MODULE) &&
-	data->rclass != CLASS_OF(recv) && !rb_obj_is_kind_of(recv, data->rclass)) {
-	if (FL_TEST(data->rclass, FL_SINGLETON)) {
+    methclass = data->rclass;
+    if (!RB_TYPE_P(methclass, T_MODULE) &&
+	methclass != CLASS_OF(recv) && !rb_obj_is_kind_of(recv, methclass)) {
+	if (FL_TEST(methclass, FL_SINGLETON)) {
 	    rb_raise(rb_eTypeError,
 		     "singleton method called for a different object");
 	}
 	else {
 	    rb_raise(rb_eTypeError, "bind argument must be an instance of %s",
-		     rb_class2name(data->rclass));
+		     rb_class2name(methclass));
 	}
     }
 
@@ -1683,8 +1689,18 @@ umethod_bind(VALUE method, VALUE recv) https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/proc.c#L1689
     bound->me = ALLOC(rb_method_entry_t);
     *bound->me = *data->me;
     if (bound->me->def) bound->me->def->alias_count++;
+    rclass = CLASS_OF(recv);
+    if (BUILTIN_TYPE(bound->defined_class) == T_MODULE) {
+	VALUE ic = rb_class_search_ancestor(rclass, bound->defined_class);
+	if (ic) {
+	    rclass = ic;
+	}
+	else {
+	    rclass = rb_include_class_new(methclass, rclass);
+	}
+    }
     bound->recv = recv;
-    bound->rclass = CLASS_OF(recv);
+    bound->rclass = rclass;
     data->ume = ALLOC(struct unlinked_method_entry_list_entry);
 
     return method;
Index: ruby_2_0_0/internal.h
===================================================================
--- ruby_2_0_0/internal.h	(revision 46156)
+++ ruby_2_0_0/internal.h	(revision 46157)
@@ -183,6 +183,7 @@ VALUE rb_int_pred(VALUE num); https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/internal.h#L183
 
 /* object.c */
 VALUE rb_obj_equal(VALUE obj1, VALUE obj2);
+VALUE rb_class_search_ancestor(VALUE klass, VALUE super);
 
 /* parse.y */
 VALUE rb_parser_get_yydebug(VALUE);
Index: ruby_2_0_0/version.h
===================================================================
--- ruby_2_0_0/version.h	(revision 46156)
+++ ruby_2_0_0/version.h	(revision 46157)
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/version.h#L1
 #define RUBY_VERSION "2.0.0"
 #define RUBY_RELEASE_DATE "2014-05-27"
-#define RUBY_PATCHLEVEL 485
+#define RUBY_PATCHLEVEL 486
 
 #define RUBY_RELEASE_YEAR 2014
 #define RUBY_RELEASE_MONTH 5
Index: ruby_2_0_0/vm_insnhelper.c
===================================================================
--- ruby_2_0_0/vm_insnhelper.c	(revision 46156)
+++ ruby_2_0_0/vm_insnhelper.c	(revision 46157)
@@ -2021,15 +2021,17 @@ vm_search_super_method(rb_thread_t *th, https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/vm_insnhelper.c#L2021
 	current_defined_class = RCLASS_REFINED_CLASS(current_defined_class);
     }
 
-    if (!FL_TEST(current_defined_class, RMODULE_INCLUDED_INTO_REFINEMENT) &&
+    if (BUILTIN_TYPE(current_defined_class) != T_MODULE &&
+	BUILTIN_TYPE(current_defined_class) != T_ICLASS && /* bound UnboundMethod */
+	!FL_TEST(current_defined_class, RMODULE_INCLUDED_INTO_REFINEMENT) &&
 	!rb_obj_is_kind_of(ci->recv, current_defined_class)) {
 	VALUE m = RB_TYPE_P(current_defined_class, T_ICLASS) ?
 	    RBASIC(current_defined_class)->klass : current_defined_class;
 
 	rb_raise(rb_eTypeError,
 		 "self has wrong type to call super in this context: "
-		 "%s (expected %s)",
-		 rb_obj_classname(ci->recv), rb_class2name(m));
+		 "%"PRIsVALUE" (expected %"PRIsVALUE")",
+		 rb_obj_class(ci->recv), m);
     }
 
     switch (vm_search_superclass(GET_CFP(), iseq, sigval, ci)) {
Index: ruby_2_0_0/test/ruby/test_super.rb
===================================================================
--- ruby_2_0_0/test/ruby/test_super.rb	(revision 46156)
+++ ruby_2_0_0/test/ruby/test_super.rb	(revision 46157)
@@ -454,4 +454,45 @@ class TestSuper < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/ruby/test_super.rb#L454
       m.call
     end
   end
+
+  def test_super_in_module_unbound_method
+    bug9721 = '[ruby-core:61936] [Bug #9721]'
+
+    a = Module.new do
+      def foo(result)
+        result << "A"
+      end
+    end
+
+    b = Module.new do
+      def foo(result)
+        result << "B"
+        super
+      end
+    end
+
+    um = b.instance_method(:foo)
+
+    m = um.bind(Object.new.extend(a))
+    result = []
+    assert_nothing_raised(NoMethodError, bug9721) do
+      m.call(result)
+    end
+    assert_equal(%w[B A], result, bug9721)
+
+    bug9740 = '[ruby-core:62017] [Bug #9740]'
+
+    b.module_eval do
+      define_method(:foo) do |result|
+        um.bind(self).call(result)
+      end
+    end
+
+    result.clear
+    o = Object.new.extend(a).extend(b)
+    assert_nothing_raised(NoMethodError, SystemStackError, bug9740) do
+      o.foo(result)
+    end
+    assert_equal(%w[B A], result, bug9721)
+  end
 end

Property changes on: ruby_2_0_0
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r45564-45565,45584-45585


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

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