Project

General

Profile

Actions

Bug #16332

closed

`defined?` constant access on `nil` is inconsistent with normal constant access

Added by dylants (Dylan Thacker-Smith) over 4 years ago. Updated over 4 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.0dev (2019-11-07T14:33:45Z master d62abc47c8) [x86_64-darwin18]
[ruby-core:95746]

Description

The fix for https://bugs.ruby-lang.org/issues/11718 to disallow constant access through nil (e.g. nil::CONSTANT) didn't make a corresponding change for defined?.

This inconsistency can be seen with the example nil::Object, which will raise TypeError, even though defined?(nil::Object) returns "constant". I believe that defined?(nil::Object) should behave the same way as checking if a constant is defined on any other non-namespace object (e.g. defined?(1::Object)), which currently returns nil.

I've opened pull request https://github.com/ruby/ruby/pull/2657 with a fix for this issue.

Actions #1

Updated by dylants (Dylan Thacker-Smith) over 4 years ago

  • Status changed from Open to Closed

Applied in changeset git|ac112f2b5dc7e16ccde8f048be80946187a033b0.


Avoid top-level search for nested constant reference from nil in defined?

Fixes [Bug #16332]

Constant access was changed to no longer allow top-level constant access
through nil, but defined? wasn't changed at the same time to stay
consistent.

Use a separate defined type to distinguish between a constant
referenced from the current lexical scope and one referenced from
another namespace.

Actions

Also available in: Atom PDF

Like0
Like0