summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog15
-rw-r--r--internal.h1
-rw-r--r--object.c32
-rw-r--r--proc.c28
-rw-r--r--test/ruby/test_super.rb41
-rw-r--r--version.h2
-rw-r--r--vm_insnhelper.c8
7 files changed, 106 insertions, 21 deletions
diff --git a/ChangeLog b/ChangeLog
index 02e54f4878..0001656cf0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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);
diff --git a/object.c b/object.c
index 9ded75f9d3..386df592ae 100644
--- a/object.c
+++ b/object.c
@@ -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;
}
diff --git a/proc.c b/proc.c
index 2f6760315c..5442233747 100644
--- a/proc.c
+++ b/proc.c
@@ -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
diff --git a/version.h b/version.h
index 6f9c467855..889cd95289 100644
--- a/version.h
+++ b/version.h
@@ -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)) {