From 5bcae57c6f0c5270763737c058f091fa86d1ce8a Mon Sep 17 00:00:00 2001 From: ko1 Date: Tue, 2 Jun 2015 19:36:43 +0000 Subject: * 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50737 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ test/ruby/test_defined.rb | 25 +++++++++++++++++++++++++ vm_insnhelper.c | 11 ++++------- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index f98bbdd935..fd22c4ddae 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Wed Jun 3 04:34:39 2015 Koichi Sasada + + * 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 * 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; -- cgit v1.2.3