[前][次][番号順一覧][スレッド一覧]

ruby-changes:49953

From: nobu <ko1@a...>
Date: Sat, 27 Jan 2018 15:12:13 +0900 (JST)
Subject: [ruby-changes:49953] nobu:r62071 (trunk): defined? returns nil for toplevel constant lookup

nobu	2018-01-27 15:12:09 +0900 (Sat, 27 Jan 2018)

  New Revision: 62071

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62071

  Log:
    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@g...>

  Modified files:
    trunk/spec/ruby/language/defined_spec.rb
    trunk/test/ruby/test_defined.rb
    trunk/variable.c
Index: spec/ruby/language/defined_spec.rb
===================================================================
--- spec/ruby/language/defined_spec.rb	(revision 62070)
+++ spec/ruby/language/defined_spec.rb	(revision 62071)
@@ -752,8 +752,8 @@ describe "The defined? keyword for a sco https://github.com/ruby/ruby/blob/trunk/spec/ruby/language/defined_spec.rb#L752
     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
Index: test/ruby/test_defined.rb
===================================================================
--- test/ruby/test_defined.rb	(revision 62070)
+++ test/ruby/test_defined.rb	(revision 62071)
@@ -253,4 +253,8 @@ class TestDefined < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_defined.rb#L253
     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
Index: variable.c
===================================================================
--- variable.c	(revision 62070)
+++ variable.c	(revision 62071)
@@ -2513,6 +2513,11 @@ rb_const_defined_0(VALUE klass, ID id, i https://github.com/ruby/ruby/blob/trunk/variable.c#L2513
 	    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;

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]