Project

General

Profile

Actions

Bug #15758

closed

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

Added by dux (Dino Reic) almost 5 years ago. Updated over 4 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 master - Bug #15875: const_defined? behavior inconsistency in 2.6.x seriesClosedActions
Actions #1

Updated by dux (Dino Reic) almost 5 years ago

  • Description updated (diff)
Actions #2

Updated by dux (Dino Reic) almost 5 years ago

  • ruby -v changed from 2.6.2 to 2.6.2, 2.7.0-dev
Actions #3

Updated by dux (Dino Reic) almost 5 years ago

  • Description updated (diff)
Actions #4

Updated by nobu (Nobuyoshi Nakada) almost 5 years ago

  • 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]
Actions #5

Updated by nobu (Nobuyoshi Nakada) almost 5 years ago

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

Updated by nobu (Nobuyoshi Nakada) almost 5 years ago

  • 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) over 4 years ago

  • 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: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0