diff options
author | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-05-27 04:54:29 +0000 |
---|---|---|
committer | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-05-27 04:54:29 +0000 |
commit | 309ac6447d67fe87d956d01d2491542d6f56818f (patch) | |
tree | 7ecd832f534904e4c03c2d3030474926aa1f2e96 | |
parent | 05ebc662fb6e5fc95046714c6c638ffc0016023e (diff) |
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]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@46157 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 15 | ||||
-rw-r--r-- | internal.h | 1 | ||||
-rw-r--r-- | object.c | 32 | ||||
-rw-r--r-- | proc.c | 28 | ||||
-rw-r--r-- | test/ruby/test_super.rb | 41 | ||||
-rw-r--r-- | version.h | 2 | ||||
-rw-r--r-- | vm_insnhelper.c | 8 |
7 files changed, 106 insertions, 21 deletions
@@ -1,3 +1,18 @@ +Tue May 27 13:42:36 2014 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * 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@ruby-lang.org> + + * 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@ruby-lang.org> * array.c (ary_reject): may be turned into a shared array during diff --git a/internal.h b/internal.h index 82890b04da..aded941637 100644 --- a/internal.h +++ b/internal.h @@ -183,6 +183,7 @@ VALUE rb_int_pred(VALUE num); /* 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); @@ -524,6 +524,8 @@ class_or_module_required(VALUE c) 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) 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 arg) 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; } @@ -1550,6 +1550,7 @@ rb_method_call_with_block(int argc, VALUE *argv, VALUE method, VALUE pass_procva 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, VALUE *argv, VALUE method, VALUE pass_procva } 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 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) 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; diff --git a/test/ruby/test_super.rb b/test/ruby/test_super.rb index 87cb3791ca..44fdf850fc 100644 --- a/test/ruby/test_super.rb +++ b/test/ruby/test_super.rb @@ -454,4 +454,45 @@ class TestSuper < Test::Unit::TestCase 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 @@ -1,6 +1,6 @@ #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 diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 253515c63f..b639dd6f96 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -2021,15 +2021,17 @@ vm_search_super_method(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_inf 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)) { |