diff options
-rw-r--r-- | ChangeLog | 15 | ||||
-rw-r--r-- | internal.h | 1 | ||||
-rw-r--r-- | object.c | 32 | ||||
-rw-r--r-- | proc.c | 18 | ||||
-rw-r--r-- | test/ruby/envutil.rb | 16 | ||||
-rw-r--r-- | test/ruby/test_module.rb | 10 | ||||
-rw-r--r-- | test/ruby/test_super.rb | 63 | ||||
-rw-r--r-- | version.h | 2 | ||||
-rw-r--r-- | vm_insnhelper.c | 5 |
9 files changed, 127 insertions, 35 deletions
@@ -1,3 +1,18 @@ +Wed May 28 00:57:06 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] + +Wed May 28 00:57:06 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. + Wed May 28 00:38:37 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 40916a59a2..9e48c13906 100644 --- a/internal.h +++ b/internal.h @@ -596,6 +596,7 @@ rb_float_new_inline(double d) /* object.c */ VALUE rb_obj_equal(VALUE obj1, VALUE obj2); +VALUE rb_class_search_ancestor(VALUE klass, VALUE super); struct RBasicRaw { VALUE flags; @@ -584,6 +584,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 @@ -644,15 +646,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_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 arg) 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; } @@ -1826,6 +1826,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; @@ -1834,7 +1835,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) @@ -1940,6 +1943,7 @@ umethod_bind(VALUE method, VALUE recv) { 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) 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/envutil.rb b/test/ruby/envutil.rb index c89309d845..fa130898d6 100644 --- a/test/ruby/envutil.rb +++ b/test/ruby/envutil.rb @@ -160,6 +160,22 @@ module EnvUtil 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' diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index f89071c10f..ca453fa5eb 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -1559,17 +1559,11 @@ class TestModule < Test::Unit::TestCase 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 diff --git a/test/ruby/test_super.rb b/test/ruby/test_super.rb index 82d6e19ec4..e42782191d 100644 --- a/test/ruby/test_super.rb +++ b/test/ruby/test_super.rb @@ -271,12 +271,12 @@ class TestSuper < Test::Unit::TestCase 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 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 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 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 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 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.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 diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 55c0515425..8c7071a91e 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -2002,6 +2002,7 @@ vm_search_super_method(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_inf } 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, rb_control_frame_t *reg_cfp, rb_call_inf 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)) { |