summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog19
-rw-r--r--proc.c10
-rw-r--r--test/ruby/test_method.rb15
-rw-r--r--test/ruby/test_module.rb51
-rw-r--r--version.h2
-rw-r--r--vm_method.c34
6 files changed, 117 insertions, 14 deletions
diff --git a/ChangeLog b/ChangeLog
index f8d979b95b..6ffc34d979 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,22 @@
+Sun Dec 22 22:56:06 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * proc.c (mnew_from_me): achieve the original defined_class from
+ prepended iclass, to fix inherited owner.
+
+ * proc.c (method_owner): return the defined class, but not the
+ class which the method object is created from.
+
+Sun Dec 22 22:56:06 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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.
+
Sun Dec 22 00:07:09 2013 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
* insns.def: Fix optimization bug of Float#/ [Bug #9238]
diff --git a/proc.c b/proc.c
index 41908d5b94..38b5157f16 100644
--- a/proc.c
+++ b/proc.c
@@ -991,6 +991,10 @@ mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
goto again;
}
+ if (RB_TYPE_P(defined_class, T_ICLASS)) {
+ defined_class = RBASIC(defined_class)->klass;
+ }
+
klass = defined_class;
while (rclass != klass &&
@@ -998,10 +1002,6 @@ mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
rclass = RCLASS_SUPER(rclass);
}
- if (RB_TYPE_P(klass, T_ICLASS)) {
- klass = RBASIC(klass)->klass;
- }
-
gen_method:
method = TypedData_Make_Struct(mclass, struct METHOD, &method_data_type, data);
@@ -1174,7 +1174,7 @@ method_owner(VALUE obj)
struct METHOD *data;
TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
- return data->me->klass;
+ return data->defined_class;
}
void
diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb
index 6d0f6acf35..1514c26f5c 100644
--- a/test/ruby/test_method.rb
+++ b/test/ruby/test_method.rb
@@ -180,6 +180,15 @@ class TestMethod < Test::Unit::TestCase
assert_equal(Array.instance_method(:map).hash, Array.instance_method(:collect).hash)
end
+ def test_owner
+ c = Class.new do
+ def foo; end
+ end
+ assert_equal(c, c.instance_method(:foo).owner)
+ c2 = Class.new(c)
+ assert_equal(c, c2.instance_method(:foo).owner)
+ end
+
def test_receiver_name_owner
o = Object.new
def o.foo; end
@@ -534,17 +543,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(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_gced_bmethod
diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb
index 915a1b9feb..aa2d82841f 100644
--- a/test/ruby/test_module.rb
+++ b/test/ruby/test_module.rb
@@ -1555,6 +1555,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/version.h b/version.h
index 0174bbfaa1..e40b211523 100644
--- a/version.h
+++ b/version.h
@@ -1,6 +1,6 @@
#define RUBY_VERSION "2.0.0"
#define RUBY_RELEASE_DATE "2013-12-22"
-#define RUBY_PATCHLEVEL 370
+#define RUBY_PATCHLEVEL 371
#define RUBY_RELEASE_YEAR 2013
#define RUBY_RELEASE_MONTH 12
diff --git a/vm_method.c b/vm_method.c
index 2801107c16..6a6485e09c 100644
--- a/vm_method.c
+++ b/vm_method.c
@@ -222,7 +222,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
@@ -320,7 +321,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) def->alias_count++;
@@ -404,7 +405,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;
@@ -460,15 +461,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
@@ -524,6 +533,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 = cache + EXPR1(klass, id);
@@ -1182,6 +1194,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;
@@ -1195,7 +1208,7 @@ rb_alias(VALUE klass, ID name, ID def)
}
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)) ||
@@ -1210,9 +1223,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(defined_class)->klass;
+ 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);
}
/*