1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

defined? returns nil for toplevel constant lookup

* variable.c (rb_const_defined_0): toplevel constant lookup has
  been removed, should return nil too.
  [ruby-core:85142] [Bug #14407] [Fix GH-1800]

From: Gonzalo <grzuy0@gmail.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62071 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-01-27 06:12:09 +00:00
parent 17d8433d27
commit 9473f86fb0
3 changed files with 11 additions and 2 deletions

View file

@ -752,8 +752,8 @@ describe "The defined? keyword for a scoped constant" do
defined?(DefinedSpecs::String).should be_nil
end
it "returns 'constant' when a constant is defined on top-level but not on the class" do
defined?(DefinedSpecs::Basic::String).should == "constant"
it "returns nil when a constant is defined on top-level but not on the class" do
defined?(DefinedSpecs::Basic::String).should be_nil
end
it "returns 'constant' if the scoped-scoped constant is defined" do

View file

@ -253,4 +253,8 @@ class TestDefined < Test::Unit::TestCase
assert_equal(nil, obj.func_defined_non_existing_func, bug_11212)
assert_equal(true, obj.called, bug_11212)
end
def test_top_level_constant_not_defined
assert_nil(defined?(TestDefined::Object))
end
end

View file

@ -2513,6 +2513,11 @@ rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse, int visibility)
if (ce->value == Qundef && !check_autoload_required(tmp, id, 0) &&
!rb_autoloading_value(tmp, id, 0))
return (int)Qfalse;
if (exclude && tmp == rb_cObject && klass != rb_cObject) {
return (int)Qfalse;
}
return (int)Qtrue;
}
if (!recurse) break;