Project

General

Profile

Actions

Bug #15780

closed

Module#const_defined?(String) executes autoloads

Added by fxn (Xavier Noria) almost 5 years ago. Updated over 4 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin18]
[ruby-core:92351]

Description

The documentation of Module#const_defined? says that autoloads are not executed:

[...] if the constant is not present but there is an autoload for it, true is returned directly without autoloading.

While this seems to work as documented for symbols, constant names passed as strings trigger the autoload:

$ cat x.rb
p X = 1

$ cat foo.rb
Object.autoload("X", "x")
p Object.const_defined?("X")

$ ruby -I. foo.rb
1
true

Related issues 1 (0 open1 closed)

Is duplicate of Ruby master - Bug #10741: const_defined? triggers autoload where it did not beforeClosedActions

Updated by Eregon (Benoit Daloze) almost 5 years ago

I can confirm the bug. Here is a single-line reproducer:

$ ruby -e 'autoload :X, "foo"; p Object.const_defined?("X")'
Traceback (most recent call last):
	3: from -e:1:in `<main>'
	2: from -e:1:in `const_defined?'
	1: from /home/eregon/.rubies/ruby-2.6.2/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/home/eregon/.rubies/ruby-2.6.2/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- foo (LoadError)

FWIW, const_defined?("A::B") needs to resolve A to know if it has a B constant, but the last component (B or X) should not trigger any autoload.

Updated by Eregon (Benoit Daloze) almost 5 years ago

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

All stable versions have this bug.

Actions #4

Updated by byroot (Jean Boussier) almost 5 years ago

  • Status changed from Open to Closed

Applied in changeset git|7d805e67f3275aef066d77aa9c32bef715c362ed.


Avoid triggering autoload in Module#const_defined?(String)

[Bug #15780]

Actions #5

Updated by nobu (Nobuyoshi Nakada) almost 5 years ago

  • Is duplicate of Bug #10741: const_defined? triggers autoload where it did not before added

Updated by nagachika (Tomoyuki Chikanaga) over 4 years ago

  • Backport changed from 2.4: REQUIRED, 2.5: REQUIRED, 2.6: REQUIRED to 2.4: REQUIRED, 2.5: REQUIRED, 2.6: DONE

ruby_2_6 r67831 merged revision(s) 7d805e67f3275aef066d77aa9c32bef715c362ed.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0