[ruby-core:96158] [Ruby master Bug#15758] Object.const_defined?(name) falsely returns true on classes that are not defined

[email protected]
Newsgroups gmane.comp.lang.ruby.core
Message-ID <redmine.journal-83036.20191209120644.bdc13942b935f48c@ruby-lang.org>
Issue #15758 has been updated by nagachika (Tomoyuki Chikanaga).

Backport changed from 2.4: DONTNEED, 2.5: DONTNEED, 2.6: REQUIRED to 2.4: DONTNEED, 2.5: DONTNEED, 2.6: DONE

ruby_2_6 r67830 merged revision(s) e1b592b508c72a56ae012869d97fe1580ff87246,d10451f3fd51f577e704db770de48d05044eb45c.

----------------------------------------
Bug #15758: Object.const_defined?(name) falsely returns true on classes that are not defined
https://bugs.ruby-lang.org/issues/15758#change-83036

* Author: dux (Dino Reic)
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 2.6.2, 2.7.0-dev
* Backport: 2.4: DONTNEED, 2.5: DONTNEED, 2.6: DONE
----------------------------------------
in short https://i.imgur.com/qvIIBuu.png

Object.const_defined?('Baz::Bar') -> true

yet Baz::Bar -> class not found!

test to run, all ok on ruby 2.6.0, bug in 2.6.2

~~~ruby
class Foo
end

class Bar
end

class Baz
  class Foo
  end
end

###

def report klass
  name     = "Object.const_defined?('%s')" % klass
  exists   = Object.const_defined?(klass)
  instance = eval klass rescue nil
  check    = (exists && instance) || (!exists && !instance)? 'ok' : 'ERROR!'

  puts [
    name.ljust(35),
    exists.to_s.ljust(5),
    (instance ? instance.to_s : 'nil').ljust(8),
    check
  ].join(' -> ')
end

report 'Foo'
report 'Bar'
report 'Baz'
report 'Naat'
report 'Baz::Foo'
report 'Baz::Bar'
report 'Baz::Naat'
puts   '---'
report 'Baz::Foo::Bar::Baz::Foo' # true in 2.6.2 :)
~~~

running produces output

~~~
Object.const_defined?('Foo')        -> true  -> Foo      -> ok
Object.const_defined?('Bar')        -> true  -> Bar      -> ok
Object.const_defined?('Baz')        -> true  -> Baz      -> ok
Object.const_defined?('Naat')       -> false -> nil      -> ok
Object.const_defined?('Baz::Foo')   -> true  -> Baz::Foo -> ok
Object.const_defined?('Baz::Bar')   -> true  -> nil      -> ERROR!
Object.const_defined?('Baz::Naat')  -> false -> nil      -> ok
---
Object.const_defined?('Baz::Foo::Bar::Baz::Foo') -> true  -> nil      -> ERROR!
~~~


---Files--------------------------------
bug.rb (581 Bytes)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.