From b71956d12b061613f8ef9dfe3f8a75e56dfa799d Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 13 Dec 2013 13:29:23 +0000 Subject: 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44175 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 11 +++++++++++ proc.c | 2 +- test/ruby/test_method.rb | 8 +++++++- test/ruby/test_module.rb | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ vm_method.c | 34 ++++++++++++++++++++++++-------- 5 files changed, 96 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 42488ac315..3fb504666f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Fri Dec 13 22:29:21 2013 Nobuyoshi Nakada + + * 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 * gc.c (gc_info_decode): Use :major_by=>:nofree as fallback reason diff --git a/proc.c b/proc.c index bc8d35450b..f72b019684 100644 --- a/proc.c +++ b/proc.c @@ -1395,7 +1395,7 @@ method_owner(VALUE obj) struct METHOD *data; TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data); - return data->me->klass; + return data->rclass; } void diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb index 273f3f83d1..a81d6d3812 100644 --- a/test/ruby/test_method.rb +++ b/test/ruby/test_method.rb @@ -365,7 +365,7 @@ class TestMethod < Test::Unit::TestCase c3 = Class.new(c) c3.class_eval { alias bar foo } m3 = c3.new.method(:bar) - assert_equal("#", m3.inspect, bug7806) + assert_equal("#", m3.inspect, bug7806) end def test_callee_top_level @@ -571,17 +571,23 @@ class TestMethod < Test::Unit::TestCase 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 diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index edcf271a76..269fb15045 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -1587,6 +1587,57 @@ class TestModule < Test::Unit::TestCase 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) diff --git a/vm_method.c b/vm_method.c index a95c37e30e..af336db616 100644 --- a/vm_method.c +++ b/vm_method.c @@ -227,7 +227,8 @@ rb_add_refined_method_entry(VALUE refined_class, ID mid) 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, rb_method_type_t type, 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_method_type_t type, void *opts, rb_method_ 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_method_type_t type, void *opts, rb_method_ 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 klass, ID id, 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 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) 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) 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); } /* -- cgit v1.2.3