Project

General

Profile

Actions

Bug #15758

closed

Object.const_defined?(name) falsely returns true on classes that are not defined

Bug #15758: Object.const_defined?(name) falsely returns true on classes that are not defined

Added by dux (Dino Reic) over 6 years ago. Updated almost 6 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
2.6.2, 2.7.0-dev
[ruby-core:92202]

Description

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

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) bug.rb dux (Dino Reic), 04/08/2019 11:27 AM

Related issues 1 (0 open1 closed)

Has duplicate Ruby - Bug #15875: const_defined? behavior inconsistency in 2.6.x seriesClosedActions

Updated by dux (Dino Reic) over 6 years ago Actions #1

  • Description updated (diff)

Updated by dux (Dino Reic) over 6 years ago Actions #2

  • ruby -v changed from 2.6.2 to 2.6.2, 2.7.0-dev

Updated by dux (Dino Reic) over 6 years ago Actions #3

  • Description updated (diff)

Updated by nobu (Nobuyoshi Nakada) over 6 years ago Actions #4

  • Status changed from Open to Closed

Applied in changeset trunk|r67472.


object.c: fix searching nested const paths

  • object.c (rb_mod_const_get, rb_mod_const_defined): nested const
    paths should not search from toplevel constants.
    [ruby-core:92202] [Bug #15758]

Updated by nobu (Nobuyoshi Nakada) over 6 years ago Actions #5

  • Has duplicate Bug #15875: const_defined? behavior inconsistency in 2.6.x series added

Updated by nobu (Nobuyoshi Nakada) over 6 years ago Actions #6

  • Backport changed from 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN to 2.4: DONTNEED, 2.5: DONTNEED, 2.6: REQUIRED

Updated by nagachika (Tomoyuki Chikanaga) almost 6 years ago Actions #7 [ruby-core:96158]

  • 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.

Actions

Also available in: PDF Atom