Project

General

Profile

Bug #15875

const_defined? behavior inconsistency in 2.6.x series

Added by matsuda (Akira Matsuda) 3 months ago. Updated 3 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
2.6.1, 2.6.2, 2.6.3
[ruby-core:92832]

Description

Module#const_defined? returns true for unreachable nested fully qualified module name.
Here are the results of const_defined? for each ruby version on my machine.

% ruby -e "module A; end; B = 1; p Object.const_defined?('A::B')"

[1.9.3-p551, 2.0.0-p648]
-e:1:in `const_defined?': wrong constant name A::B (NameError)
    from -e:1:in `<main>'

[2.1.10, 2.2.10, 2.3.8, 2.4.6, 2.5.5, 2.6.0]
false

[2.6.1, 2.6.2, 2.6.3]
true

[2.7.0-dev (2019-05-25 trunk 559dca509d)]
false

For (2.6.1...2.6.3), it fails to reference the constant even though const_defined? returns true. So IMO this should be const_defined?'s bug.

% ruby -e "module A; end; B = 1; p Object.const_defined?('A::B') && A::B"
-e:1:in `<main>': uninitialized constant A::B (NameError)

Related issues

Is duplicate of Ruby master - Bug #15758: Object.const_defined?(name) falsely returns true on classes that are not definedClosedActions

History

Updated by matsuda (Akira Matsuda) 3 months ago

Side Note #1

const_get() could get this constant for all released versions, but has been intentionally changed in 2.7. Correct?

% ruby -e "module A; end; B = 1; p Object.const_get('A::B')"

[1.9.3...2.6.3]
1

[2.7.0-dev (2019-05-25 trunk 559dca509d)]
Traceback (most recent call last):
    1: from -e:1:in `<main>'
-e:1:in `const_get': uninitialized constant A::B (NameError)

Side Note #2

Current stable jruby seems to be following the 2.6 behavior.

% ruby -e "module A; end; B = 1; p Object.const_defined?('A::B')"

[jruby-9.2.7.0]
true
#3

Updated by nobu (Nobuyoshi Nakada) 3 months ago

  • Is duplicate of Bug #15758: Object.const_defined?(name) falsely returns true on classes that are not defined added
#4

Updated by nagachika (Tomoyuki Chikanaga) 3 months ago

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

Also available in: Atom PDF