Skip to content

Object.constants is empty (MRI: 120) and Module.constants returns 1615: the top-level constant table is invisible to enumeration #817

Open

Description

@tannevaled

Measured

MRI 4.0.7 and rbgo on a4097c8, same host. Two defects, in opposite directions.

expression rbgo MRI 4.0.7
Object.constants.size 0 120
self.class.constants.size 0 120
Module.constants.size 1615 120
String.constants.size 0 0
Float.constants.size 12 12
module M; X=1; Y=2; end; M.constants [:X, :Y] [:X, :Y]
class C; Z=1; end; C.constants [:Z] [:Z]
Object.const_defined?(:String) true true
Object.const_get(:Integer) Integer Integer

So lookup works and enumeration does not, and only for the top level:

  • Object.constants answers nothing, though const_get and
    const_defined? find those same constants happily. The top-level table is
    reachable by name and invisible to enumeration.
  • Module.constants answers 1615, which is an order of magnitude more than
    the 120 MRI reports. In MRI that call returns the constants accessible in the
    current scope — the same 120 as Object.constants. 1615 looks like every
    constant of every class, so the two calls are wrong in opposite ways: one
    looks in the wrong place and finds none, the other looks everywhere.
  • A module or class the program defines enumerates correctly, which is why this
    has not surfaced: the test that would catch it is about Object, and
    Object is the one that fails.

How it was found, which says something about who hits it

I was writing a census — "which core classes inherit Kernel#to_s?" — as

Object.constants.sort.each { |n| c = Object.const_get(n); … }

It reported 0 classes examined, and I read that as an answer for about a
minute. The same script against MRI reported 82. A loop over Object.constants
does not fail on rbgo: it runs zero times and finishes successfully.

That is the shape worth flagging. Enumerating top-level constants is an ordinary
Ruby idiom — autoloaders, plugin discovery, DI containers,
Object.constants.grep(/Serializer\z/) — and every one of them gets an empty
result and no error. Code that looks up a constant works, so a program can
pass every direct test and silently see an empty world through the enumerating
path.

Not in scope here

RubyVM and ThreadGroup are absent as constants, and Enumerator#to_s is
defined where MRI inherits Kernel's. Noticed in the same census; separate and
much smaller.

🤖 Generated with Claude Code

Activity

  1. tannevaled commented on Nov 15, 2026

    @tannevaled
    ContributorAuthor

    Measured: the title is wrong twice over, and the fix is one site rather than fifty-two

    Measured 2026-11-15 through cmd/oracle against MRI 4.0.7, rbgo built from
    main:

    MRI rbgo
    Object.constants.size 120 1620
    of those, names containing :: 0 1303
    core constants missing none none

    So it is not "empty" — it is 13.5× too large, and 1303 of the 1620 are
    QUALIFIED names
    : AASM::Error, ActionCable::Channel::Base,
    Acme::Client::Error::BadNonce. MRI has none, because a nested constant belongs
    to its enclosing module.

    The tree is correct and independent

    Object.constants.include?(:"AASM::Error")   true    <- the flat key
    defined?(AASM)                              "constant"
    AASM.constants.include?(:Error)             true    <- the tree IS built
    AASM.constants any name with "::"           false   <- and it is clean
    AASM.const_get(:Error)                      "AASM::Error"
    

    So this is double registration, not a broken namespace: the tree is right
    and Object additionally carries flattened aliases.

    ⭐ And one of those aliases is a name rbgo's own API refuses:

    Object.send(:remove_const, :"AASM::Error")
      => NameError: wrong constant name AASM::Error
    

    Object.constants reports a key that remove_const will not accept as a
    constant name.

    ⛔ Where I was about to go wrong, twice, and what stopped each

    git grep -c 'vm.consts\[qualified\]' gives 52 files, 53 occurrences — so
    the obvious reading is "a hand-written idiom repeated 52 times; change the
    idiom". Two readings stopped that.

    1. The comment above one of those lines says the keys are load-bearing.
    json.go:58-61:

    Each class is registered both as a nested constant of JSON (so Ruby
    JSON::ParserError resolves it) and under its qualified name in the
    top-level table (so a re-raised library error's exceptionObject lookup finds
    the very same class)
    , exactly as Errno:: classes are.

    2. That comment is STALE. exceptionObject → errorClass
    (vm.go:252-266) does not read the flat key: for a qualified name it walks
    the tree
    , and its own comment says the walk is strict, "each segment must be a
    class/module in the PREVIOUS segment's own table". The flat registration was
    needed before that walk existed — the lines just above it describe exactly that
    fix — and the justification outlived its reason while all 52 writes stayed.

    So the comment refuted my hypothesis and the code refuted the comment. Reading
    only one of the two gives the wrong answer either way.

    ⛔ But the keys ARE read — by the bindings, not by exceptionObject

    internal/vm/kafka.go:88     vm.kafkaClass("Kafka::Producer")
    internal/vm/kafka.go:102    vm.kafkaClass("Kafka::Consumer")
    internal/vm/kafka.go:214    vm.kafkaClass("Kafka::Batch")
    internal/vm/kafka_bind.go:87 vm.kafkaClass("Kafka::Message")
    

    and kafkaClass(name) = vm.consts[name].(*RClass). Four sites in one binding;
    actioncable_bind.go:204, io.go:2323 and embed.go:133 read the table the
    same way. Removing the writes would break those, which is why "change the
    idiom in 52 files" is the wrong fix even though the idiom is what produces the
    1303.

    ⇒ The fix is in the ENUMERATION

    Module#constants must not report a key containing ::. The table keeps them
    as what they have become — an internal index from qualified name to class, which
    the bindings use — and the Ruby-visible list matches MRI, which has zero such
    names by construction.

    One site, no binding touched, and the parity target is exact rather than
    approximate: 0 qualified names in Object.constants.

    ⚠ Two things that still need deciding and are NOT settled by this measurement:

    • The remaining surplus. 1620 − 1303 = 317 unqualified names against
      MRI's 120. The difference is bound gem top-levels (AASM, Acme,
      ActionCable, AbstractController…) present before any require. Whether
      rbgo should register those lazily is a separate question from this one, with a
      separate cost.
    • Whether the flat index should eventually become a real field rather than
      extra keys in the constant table. That would let constants need no filter at
      all, and it is the version a reader would expect; it is also a wider change
      than the parity defect requires.

    Implemented in #946 — one predicate in publicConstNames, proved by mutation
    (removing it gives 1303 against 0 on both paths, every other assertion in the
    test unchanged). ⚠ That pull request closes the 1303, not this issue: the
    317-against-120 remainder above is still open and is why #817 should stay open.

    Refs #946, #941

    🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions