Skip to content

vm: Object.constants stops reporting the qualified index keys (1620 -> 317) - #946

Merged
tannevaled merged 1 commit into
mainfrom
constants-no-qualified-keys
Nov 15, 2026
Merged

tannevaled merged 1 commit into
mainfrom
constants-no-qualified-keys

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

vm: Object.constants stops reporting the qualified index keys (1620 -> 317)

#817 is titled "Object.constants is empty (MRI: 120)". Measured 2026-11-15
through cmd/oracle against MRI 4.0.7, it is the opposite, and the surplus has a
shape the title does not mention:

               MRI   before   after

total 120 1620 317
containing "::" 0 1303 0
core missing none none none

1303 of the 1620 were 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 was already right; Object carried flat aliases beside it

AASM.constants.include?(:Error) true
AASM.constants any name with "::" false
AASM.const_get(:Error) "AASM::Error"

So this was double registration, not a broken namespace.

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

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

Object.constants reported a key remove_const will not accept.

⛔ The fix is ONE site, not the 52 the count suggests

git grep -c 'vm.consts[qualified]' gives 52 files, 53 occurrences, so "change
the idiom" is the obvious move. Two readings stopped it, in opposite directions:

  1. json.go:58-61 says the flat key exists "so a re-raised library error's
    exceptionObject lookup finds the very same class".
  2. ⛔ That comment is STALE. exceptionObject -> errorClass (vm.go:252-266) walks
    the tree for a qualified name and never reads the flat key; its own comment
    says the walk is strict, segment by segment. The flat registration predates
    that walk -- the lines above errorClass describe exactly the fix that
    introduced it -- and the justification outlived its reason.

So the comment refuted the first hypothesis and the code refuted the comment.

⛔ But the keys ARE read, by the bindings rather than by exceptionObject:
vm.kafkaClass("Kafka::Producer") and three siblings in kafka alone, plus
actioncable_bind.go:204, io.go:2323 and embed.go:133. They have become an index
from qualified name to class. Removing the writes would break those, which is
why the enumeration is the right place: the table keeps the index, and
constants stops reporting it as constants.

One predicate in publicConstNames, which both Module#constants and the
no-argument Module.constants funnel through.

Proved by mutation

Removing the predicate: 1303 against the expected 0, on BOTH paths
(Object.constants and Module.constants), with every other assertion in the test
unchanged -- so the test isolates the filter, and it confirms that
Module.constants really is a second path into the same helper.

The test also asserts what a filter could break and a "zero qualified names"
check alone would not notice: 32 core constants still present, the tree still
reachable through its parent, and the bindings' own qualified lookups
(Kafka::Producer, JSON::ParserError, Acme::Client::Error::BadNonce) still
resolving.

⚠ What this does NOT close

317 against MRI's 120. The remainder is bound gem top-levels -- AASM, Acme,
ActionCable, AbstractController -- present before any require, where MRI
autoloads. That is a separate decision with a separate cost, and #817 should
stay open for it rather than be closed on the half that moved.

Refs #817, #941

Co-Authored-By: Claude Opus 5 noreply@anthropic.com

…> 317)

#817 is titled "Object.constants is empty (MRI: 120)". Measured 2026-11-15
through cmd/oracle against MRI 4.0.7, it is the opposite, and the surplus has a
shape the title does not mention:

                   MRI   before   after
  total             120    1620     317
  containing "::"     0    1303       0
  core missing     none    none     none

1303 of the 1620 were 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 was already right; Object carried flat aliases beside it

  AASM.constants.include?(:Error)                true
  AASM.constants any name with "::"              false
  AASM.const_get(:Error)                         "AASM::Error"

So this was double registration, not a broken namespace.

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

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

Object.constants reported a key remove_const will not accept.

## ⛔ The fix is ONE site, not the 52 the count suggests

`git grep -c 'vm.consts[qualified]'` gives 52 files, 53 occurrences, so "change
the idiom" is the obvious move. Two readings stopped it, in opposite directions:

1. json.go:58-61 says the flat key exists "so a re-raised library error's
   exceptionObject lookup finds the very same class".
2. ⛔ That comment is STALE. exceptionObject -> errorClass (vm.go:252-266) walks
   the tree for a qualified name and never reads the flat key; its own comment
   says the walk is strict, segment by segment. The flat registration predates
   that walk -- the lines above errorClass describe exactly the fix that
   introduced it -- and the justification outlived its reason.

So the comment refuted the first hypothesis and the code refuted the comment.

⛔ But the keys ARE read, by the bindings rather than by exceptionObject:
vm.kafkaClass("Kafka::Producer") and three siblings in kafka alone, plus
actioncable_bind.go:204, io.go:2323 and embed.go:133. They have become an index
from qualified name to class. Removing the writes would break those, which is
why the enumeration is the right place: the table keeps the index, and
`constants` stops reporting it as constants.

One predicate in publicConstNames, which both Module#constants and the
no-argument Module.constants funnel through.

## Proved by mutation

Removing the predicate: 1303 against the expected 0, on BOTH paths
(Object.constants and Module.constants), with every other assertion in the test
unchanged -- so the test isolates the filter, and it confirms that
Module.constants really is a second path into the same helper.

The test also asserts what a filter could break and a "zero qualified names"
check alone would not notice: 32 core constants still present, the tree still
reachable through its parent, and the bindings' own qualified lookups
(Kafka::Producer, JSON::ParserError, Acme::Client::Error::BadNonce) still
resolving.

## ⚠ What this does NOT close

317 against MRI's 120. The remainder is bound gem top-levels -- AASM, Acme,
ActionCable, AbstractController -- present before any `require`, where MRI
autoloads. That is a separate decision with a separate cost, and #817 should
stay open for it rather than be closed on the half that moved.

Refs #817, #941

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 2595a48 into main Nov 15, 2026
26 checks passed
@tannevaled
tannevaled deleted the constants-no-qualified-keys branch November 15, 2026 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant