diff options
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | test/ruby/test_defined.rb | 25 | ||||
-rw-r--r-- | vm_insnhelper.c | 11 |
3 files changed, 37 insertions, 7 deletions
@@ -1,3 +1,11 @@ +Wed Jun 3 04:34:39 2015 Koichi Sasada <ko1@atdot.net> + + * vm_insnhelper.c (vm_defined): skip respond_to_missing? when + a method is available. + [Bug #11211] + + * test/ruby/test_defined.rb: add a test for this fix. + Wed Jun 3 04:14:13 2015 Koichi Sasada <ko1@atdot.net> * insns.def (defined), vm_insnhelper.c (vm_defined): diff --git a/test/ruby/test_defined.rb b/test/ruby/test_defined.rb index d5e84185cc..64757615ec 100644 --- a/test/ruby/test_defined.rb +++ b/test/ruby/test_defined.rb @@ -208,4 +208,29 @@ class TestDefined < Test::Unit::TestCase def test_super_toplevel assert_separately([], "assert_nil(defined?(super))") end + + class ExampleRespondToMissing + attr_reader :called + + def initialize + @called = false + end + + def respond_to_missing? *args + @called = true + false + end + + def existing_method + end + end + + def test_method_by_respond_to_missing + bug_11211 = '[Bug #11211]' + obj = ExampleRespondToMissing.new + assert_equal("method", defined?(obj.existing_method), bug_11211) + assert_equal(false, obj.called, bug_11211) + assert_equal(nil, defined?(obj.non_existing_method), bug_11211) + assert_equal(true, obj.called, bug_11211) + end end diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 4601f80dc9..619de32dce 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -2456,15 +2456,12 @@ vm_defined(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_num_t op_type, VALUE if (me) { const rb_method_definition_t *def = me->def; - if (!(def->flag & NOEX_PRIVATE)) { - if (!((def->flag & NOEX_PROTECTED) && - !rb_obj_is_kind_of(GET_SELF(), - rb_class_real(klass)))) { - expr_type = DEFINED_METHOD; - } + if (!(def->flag & NOEX_PRIVATE) && + !((def->flag & NOEX_PROTECTED) && !rb_obj_is_kind_of(GET_SELF(), rb_class_real(klass)))) { + expr_type = DEFINED_METHOD; } } - { + else { VALUE args[2]; VALUE r; |