From 7fa16fd9624ca38f2d82c121a9af5f22c87e2697 Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 28 Jan 2019 13:32:32 +0000 Subject: [PATCH] Fix `Module#const_defined?` on inherited constants [Fix GH-2061] From: manga_osyo git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66938 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- object.c | 1 - test/ruby/test_module.rb | 11 +++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/object.c b/object.c index 9029dad99c..d37c923fa6 100644 --- a/object.c +++ b/object.c @@ -2688,7 +2688,6 @@ rb_mod_const_defined(int argc, VALUE *argv, VALUE mod) return Qfalse; mod = rb_const_get_at(mod, id); } - recur = Qfalse; if (p < pend && !RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) { rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module", diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index d5c66be1ec..3bd438ef6e 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -338,6 +338,17 @@ class TestModule < Test::Unit::TestCase assert_raise(NameError) {self.class.const_defined?(const)} end + def test_nested_defined_inheritance + assert_send([Object, :const_defined?, [self.class.name, 'User', 'MIXIN'].join('::')]) + assert_send([self.class, :const_defined?, 'User::MIXIN']) + assert_send([Object, :const_defined?, 'File::SEEK_SET']) + + # const_defined? with `false` + assert_not_send([Object, :const_defined?, [self.class.name, 'User', 'MIXIN'].join('::'), false]) + assert_not_send([self.class, :const_defined?, 'User::MIXIN', false]) + assert_not_send([Object, :const_defined?, 'File::SEEK_SET', false]) + end + def test_nested_defined_bad_class assert_raise(TypeError) do self.class.const_defined?('User::USER::Foo')