Actions
Bug #15758
closedObject.const_defined?(name) falsely returns true on classes that are not defined
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
Updated by dux (Dino Reic) over 5 years ago
- ruby -v changed from 2.6.2 to 2.6.2, 2.7.0-dev
Updated by nobu (Nobuyoshi Nakada) over 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]
Updated by nobu (Nobuyoshi Nakada) over 5 years ago
- Has duplicate Bug #15875: const_defined? behavior inconsistency in 2.6.x series added
Updated by nobu (Nobuyoshi Nakada) over 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) almost 5 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
Like0
Like0Like0Like0Like0Like0Like0Like0