зеркало из https://github.com/github/ruby.git
[Bug #11213] let defined?(super) call respond_to_missing?
This commit is contained in:
Родитель
4b899f9164
Коммит
fac2498e02
|
@ -99,7 +99,7 @@ MJIT_SYMBOL_EXPORT_END
|
|||
/* vm_method.c */
|
||||
struct rb_execution_context_struct;
|
||||
MJIT_SYMBOL_EXPORT_BEGIN
|
||||
int rb_ec_obj_respond_to(struct rb_execution_context_struct *ec, VALUE obj, ID id, int priv);
|
||||
int rb_ec_obj_respond_to(struct rb_execution_context_struct *ec, VALUE klass, VALUE obj, ID id, int priv);
|
||||
MJIT_SYMBOL_EXPORT_END
|
||||
|
||||
/* vm_dump.c */
|
||||
|
|
|
@ -302,6 +302,39 @@ class TestDefined < Test::Unit::TestCase
|
|||
assert_nil(defined?(TestDefined::Object))
|
||||
end
|
||||
|
||||
def test_super_with_method_missing
|
||||
c0 = EnvUtil.labeled_class("C0") do
|
||||
attr_reader :calls
|
||||
|
||||
def initialize
|
||||
@calls = []
|
||||
end
|
||||
|
||||
def method_missing(*args)
|
||||
@calls << [:method_missing, *args]
|
||||
end
|
||||
|
||||
def respond_to_missing?(*args)
|
||||
@calls << [:respond_to_missing?, *args]
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
c1 = EnvUtil.labeled_class("C1", c0) do
|
||||
def foo
|
||||
super
|
||||
defined?(super)
|
||||
end
|
||||
end
|
||||
|
||||
c = c1.new
|
||||
assert_not_nil(c.foo)
|
||||
assert_equal([
|
||||
[:method_missing, :foo],
|
||||
[:respond_to_missing?, :foo, true],
|
||||
], c.calls)
|
||||
end
|
||||
|
||||
class RefinedClass
|
||||
end
|
||||
|
||||
|
|
|
@ -3770,7 +3770,7 @@ vm_defined(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t op_
|
|||
}
|
||||
case DEFINED_FUNC:
|
||||
klass = CLASS_OF(v);
|
||||
if (rb_ec_obj_respond_to(ec, v, SYM2ID(obj), TRUE)) {
|
||||
if (rb_ec_obj_respond_to(ec, klass, v, SYM2ID(obj), TRUE)) {
|
||||
expr_type = DEFINED_METHOD;
|
||||
}
|
||||
break;
|
||||
|
@ -3811,7 +3811,7 @@ vm_defined(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t op_
|
|||
VALUE klass = vm_search_normal_superclass(me->defined_class);
|
||||
ID id = me->def->original_id;
|
||||
|
||||
if (rb_method_boundp(klass, id, 0)) {
|
||||
if (rb_ec_obj_respond_to(ec, klass, GET_SELF(), id, TRUE)) {
|
||||
expr_type = DEFINED_ZSUPER;
|
||||
}
|
||||
}
|
||||
|
|
12
vm_method.c
12
vm_method.c
|
@ -2322,9 +2322,8 @@ basic_obj_respond_to_missing(rb_execution_context_t *ec, VALUE klass, VALUE obj,
|
|||
}
|
||||
|
||||
static inline int
|
||||
basic_obj_respond_to(rb_execution_context_t *ec, VALUE obj, ID id, int pub)
|
||||
basic_obj_respond_to(rb_execution_context_t *ec, VALUE klass, VALUE obj, ID id, int pub)
|
||||
{
|
||||
VALUE klass = CLASS_OF(obj);
|
||||
VALUE ret;
|
||||
|
||||
switch (rb_method_boundp(klass, id, pub|BOUND_RESPONDS)) {
|
||||
|
@ -2393,15 +2392,14 @@ int
|
|||
rb_obj_respond_to(VALUE obj, ID id, int priv)
|
||||
{
|
||||
rb_execution_context_t *ec = GET_EC();
|
||||
return rb_ec_obj_respond_to(ec, obj, id, priv);
|
||||
return rb_ec_obj_respond_to(ec, CLASS_OF(obj), obj, id, priv);
|
||||
}
|
||||
|
||||
int
|
||||
rb_ec_obj_respond_to(rb_execution_context_t *ec, VALUE obj, ID id, int priv)
|
||||
rb_ec_obj_respond_to(rb_execution_context_t *ec, VALUE klass, VALUE obj, ID id, int priv)
|
||||
{
|
||||
VALUE klass = CLASS_OF(obj);
|
||||
int ret = vm_respond_to(ec, klass, obj, id, priv);
|
||||
if (ret == -1) ret = basic_obj_respond_to(ec, obj, id, !priv);
|
||||
if (ret == -1) ret = basic_obj_respond_to(ec, klass, obj, id, !priv);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -2446,7 +2444,7 @@ obj_respond_to(int argc, VALUE *argv, VALUE obj)
|
|||
if (ret == Qundef) ret = Qfalse;
|
||||
return ret;
|
||||
}
|
||||
if (basic_obj_respond_to(ec, obj, id, !RTEST(priv)))
|
||||
if (basic_obj_respond_to(ec, CLASS_OF(obj), obj, id, !RTEST(priv)))
|
||||
return Qtrue;
|
||||
return Qfalse;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче