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

ruby-changes:34109

From: nagachika <ko1@a...>
Date: Wed, 28 May 2014 01:03:30 +0900 (JST)
Subject: [ruby-changes:34109] nagachika:r46190 (ruby_2_1): merge revision(s) r45179, r45564, r45565, r45584, r45585: [Backport #9721]

nagachika	2014-05-28 01:03:12 +0900 (Wed, 28 May 2014)

  New Revision: 46190

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

  Log:
    merge revision(s) r45179,r45564,r45565,r45584,r45585: [Backport #9721]
    
    envutil.rb: move labeled_module and labeled_class
    
    * test/ruby/envutil.rb (labeled_module, labeled_class): move from
      test/ruby/test_module.rb.
    * 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_1/
  Modified files:
    branches/ruby_2_1/ChangeLog
    branches/ruby_2_1/internal.h
    branches/ruby_2_1/object.c
    branches/ruby_2_1/proc.c
    branches/ruby_2_1/test/ruby/envutil.rb
    branches/ruby_2_1/test/ruby/test_module.rb
    branches/ruby_2_1/test/ruby/test_super.rb
    branches/ruby_2_1/version.h
    branches/ruby_2_1/vm_insnhelper.c
Index: ruby_2_1/ChangeLog
===================================================================
--- ruby_2_1/ChangeLog	(revision 46189)
+++ ruby_2_1/ChangeLog	(revision 46190)
@@ -1,3 +1,18 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_1/ChangeLog#L1
+Wed May 28 00:57:06 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]
+
+Wed May 28 00:57:06 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.
+
 Wed May 28 00:38:37 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* array.c (ary_reject): may be turned into a shared array during
Index: ruby_2_1/object.c
===================================================================
--- ruby_2_1/object.c	(revision 46189)
+++ ruby_2_1/object.c	(revision 46190)
@@ -584,6 +584,8 @@ class_or_module_required(VALUE c) https://github.com/ruby/ruby/blob/trunk/ruby_2_1/object.c#L584
     return c;
 }
 
+static VALUE class_search_ancestor(VALUE cl, VALUE c);
+
 /*
  *  call-seq:
  *     obj.instance_of?(class)    -> true or false
@@ -644,15 +646,27 @@ rb_obj_is_kind_of(VALUE obj, VALUE c) https://github.com/ruby/ruby/blob/trunk/ruby_2_1/object.c#L646
     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_WRAPPER(cl) == RCLASS_M_TBL_WRAPPER(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:
@@ -1548,16 +1562,12 @@ rb_class_inherited_p(VALUE mod, VALUE ar https://github.com/ruby/ruby/blob/trunk/ruby_2_1/object.c#L1562
 	rb_raise(rb_eTypeError, "compared with non class/module");
     }
     arg = RCLASS_ORIGIN(arg);
-    while (mod) {
-	if (RCLASS_M_TBL_WRAPPER(mod) == RCLASS_M_TBL_WRAPPER(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_WRAPPER(arg) == RCLASS_M_TBL_WRAPPER(start))
-	    return Qfalse;
-	arg = RCLASS_SUPER(arg);
+    if (class_search_ancestor(arg, start)) {
+	return Qfalse;
     }
     return Qnil;
 }
Index: ruby_2_1/proc.c
===================================================================
--- ruby_2_1/proc.c	(revision 46189)
+++ ruby_2_1/proc.c	(revision 46190)
@@ -1826,6 +1826,7 @@ rb_method_call_with_block(int argc, VALU https://github.com/ruby/ruby/blob/trunk/ruby_2_1/proc.c#L1826
     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;
@@ -1834,7 +1835,9 @@ rb_method_call_with_block(int argc, VALU https://github.com/ruby/ruby/blob/trunk/ruby_2_1/proc.c#L1835
 	}
 
 	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)
@@ -1940,6 +1943,7 @@ umethod_bind(VALUE method, VALUE recv) https://github.com/ruby/ruby/blob/trunk/ruby_2_1/proc.c#L1943
 {
     struct METHOD *data, *bound;
     VALUE methclass;
+    VALUE rclass;
 
     TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
 
@@ -1961,8 +1965,18 @@ umethod_bind(VALUE method, VALUE recv) https://github.com/ruby/ruby/blob/trunk/ruby_2_1/proc.c#L1965
     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_1/internal.h
===================================================================
--- ruby_2_1/internal.h	(revision 46189)
+++ ruby_2_1/internal.h	(revision 46190)
@@ -596,6 +596,7 @@ rb_float_new_inline(double d) https://github.com/ruby/ruby/blob/trunk/ruby_2_1/internal.h#L596
 
 /* object.c */
 VALUE rb_obj_equal(VALUE obj1, VALUE obj2);
+VALUE rb_class_search_ancestor(VALUE klass, VALUE super);
 
 struct RBasicRaw {
     VALUE flags;
Index: ruby_2_1/version.h
===================================================================
--- ruby_2_1/version.h	(revision 46189)
+++ ruby_2_1/version.h	(revision 46190)
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_1/version.h#L1
 #define RUBY_VERSION "2.1.2"
 #define RUBY_RELEASE_DATE "2014-05-28"
-#define RUBY_PATCHLEVEL 110
+#define RUBY_PATCHLEVEL 111
 
 #define RUBY_RELEASE_YEAR 2014
 #define RUBY_RELEASE_MONTH 5
Index: ruby_2_1/vm_insnhelper.c
===================================================================
--- ruby_2_1/vm_insnhelper.c	(revision 46189)
+++ ruby_2_1/vm_insnhelper.c	(revision 46190)
@@ -2002,6 +2002,7 @@ vm_search_super_method(rb_thread_t *th, https://github.com/ruby/ruby/blob/trunk/ruby_2_1/vm_insnhelper.c#L2002
     }
 
     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) ?
@@ -2009,8 +2010,8 @@ vm_search_super_method(rb_thread_t *th, https://github.com/ruby/ruby/blob/trunk/ruby_2_1/vm_insnhelper.c#L2010
 
 	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_1/test/ruby/test_module.rb
===================================================================
--- ruby_2_1/test/ruby/test_module.rb	(revision 46189)
+++ ruby_2_1/test/ruby/test_module.rb	(revision 46190)
@@ -1559,17 +1559,11 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_1/test/ruby/test_module.rb#L1559
   end
 
   def labeled_module(name, &block)
-    Module.new do
-      singleton_class.class_eval {define_method(:to_s) {name}; alias inspect to_s}
-      class_eval(&block) if block
-    end
+    EnvUtil.labeled_module(name, &block)
   end
 
   def labeled_class(name, superclass = Object, &block)
-    Class.new(superclass) do
-      singleton_class.class_eval {define_method(:to_s) {name}; alias inspect to_s}
-      class_eval(&block) if block
-    end
+    EnvUtil.labeled_class(name, superclass, &block)
   end
 
   def test_prepend_instance_methods_false
Index: ruby_2_1/test/ruby/test_super.rb
===================================================================
--- ruby_2_1/test/ruby/test_super.rb	(revision 46189)
+++ ruby_2_1/test/ruby/test_super.rb	(revision 46190)
@@ -271,12 +271,12 @@ class TestSuper < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_1/test/ruby/test_super.rb#L271
   end
 
   def test_super_in_instance_eval
-    super_class = Class.new {
+    super_class = EnvUtil.labeled_class("Super\u{30af 30e9 30b9}") {
       def foo
         return [:super, self]
       end
     }
-    sub_class = Class.new(super_class) {
+    sub_class = EnvUtil.labeled_class("Sub\u{30af 30e9 30b9}", super_class) {
       def foo
         x = Object.new
         x.instance_eval do
@@ -285,18 +285,18 @@ class TestSuper < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_1/test/ruby/test_super.rb#L285
       end
     }
     obj = sub_class.new
-    assert_raise(TypeError) do
+    assert_raise_with_message(TypeError, /Sub\u{30af 30e9 30b9}/) do
       obj.foo
     end
   end
 
   def test_super_in_instance_eval_with_define_method
-    super_class = Class.new {
+    super_class = EnvUtil.labeled_class("Super\u{30af 30e9 30b9}") {
       def foo
         return [:super, self]
       end
     }
-    sub_class = Class.new(super_class) {
+    sub_class = EnvUtil.labeled_class("Sub\u{30af 30e9 30b9}", super_class) {
       define_method(:foo) do
         x = Object.new
         x.instance_eval do
@@ -305,18 +305,18 @@ class TestSuper < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_1/test/ruby/test_super.rb#L305
       end
     }
     obj = sub_class.new
-    assert_raise(TypeError) do
+    assert_raise_with_message(TypeError, /Sub\u{30af 30e9 30b9}/) do
       obj.foo
     end
   end
 
   def test_super_in_orphan_block
-    super_class = Class.new {
+    super_class = EnvUtil.labeled_class("Super\u{30af 30e9 30b9}") {
       def foo
         return [:super, self]
       end
     }
-    sub_class = Class.new(super_class) {
+    sub_class = EnvUtil.labeled_class("Sub\u{30af 30e9 30b9}", super_class) {
       def foo
         x = Object.new
         lambda { super() }
@@ -327,12 +327,12 @@ class TestSuper < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_1/test/ruby/test_super.rb#L327
   end
 
   def test_super_in_orphan_block_with_instance_eval
-    super_class = Class.new {
+    super_class = EnvUtil.labeled_class("Super\u{30af 30e9 30b9}") {
       def foo
         return [:super, self]
       end
     }
-    sub_class = Class.new(super_class) {
+    sub_class = EnvUtil.labeled_class("Sub\u{30af 30e9 30b9}", super_class) {
       def foo
         x = Object.new
         x.instance_eval do
@@ -341,7 +341,7 @@ class TestSuper < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_1/test/ruby/test_super.rb#L341
       end
     }
     obj = sub_class.new
-    assert_raise(TypeError) do
+    assert_raise_with_message(TypeError, /Sub\u{30af 30e9 30b9}/) do
       obj.foo.call
     end
   end
@@ -453,4 +453,45 @@ class TestSuper < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_1/test/ruby/test_super.rb#L453
       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
Index: ruby_2_1/test/ruby/envutil.rb
===================================================================
--- ruby_2_1/test/ruby/envutil.rb	(revision 46189)
+++ ruby_2_1/test/ruby/envutil.rb	(revision 46190)
@@ -160,6 +160,22 @@ module EnvUtil https://github.com/ruby/ruby/blob/trunk/ruby_2_1/test/ruby/envutil.rb#L160
   end
   module_function :with_default_internal
 
+  def labeled_module(name, &block)
+    Module.new do
+      singleton_class.class_eval {define_method(:to_s) {name}; alias inspect to_s}
+      class_eval(&block) if block
+    end
+  end
+  module_function :labeled_module
+
+  def labeled_class(name, superclass = Object, &block)
+    Class.new(superclass) do
+      singleton_class.class_eval {define_method(:to_s) {name}; alias inspect to_s}
+      class_eval(&block) if block
+    end
+  end
+  module_function :labeled_class
+
   if /darwin/ =~ RUBY_PLATFORM
     DIAGNOSTIC_REPORTS_PATH = File.expand_path("~/Library/Logs/DiagnosticReports")
     DIAGNOSTIC_REPORTS_TIMEFORMAT = '%Y-%m-%d-%H%M%S'

Property changes on: ruby_2_1
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r45179,45564-45565,45584-45585


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

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