From 7da0e7987c72065671ec6875af2d1a46284b43c2 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 15 Nov 2026 14:11:56 +0100 Subject: [PATCH] vm: Object.constants stops reporting the qualified index keys (1620 -> 317) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #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 --- internal/vm/builtins.go | 27 ++++++++++ internal/vm/constants_qualified_test.go | 71 +++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 internal/vm/constants_qualified_test.go diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 5ea4aa37..6be1fcc5 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -10281,6 +10281,33 @@ func publicConstNames(chain []*RClass) []string { for i, c := range chain { earlier := chain[:i] keep := func(name string) bool { + // ⛔ A key containing "::" is not a constant NAME, it is an INDEX + // ENTRY. 52 binding files register a nested class twice — once in its + // parent's table under its simple name, and once in the top-level + // table under its qualified name — and the second form is how a + // binding looks its own classes up later + // (`vm.kafkaClass("Kafka::Producer")`, four sites in kafka alone; + // actioncable_bind.go, io.go and embed.go read the table the same way). + // + // Measured 2026-11-15 against MRI 4.0.7: Object.constants answered + // 1620 where MRI answers 120, and 1303 of those 1620 carried "::". + // MRI has NONE, because a nested constant belongs to its enclosing + // module (#817). + // + // ⛔ The fix is here and not at the 52 registration sites, which is the + // opposite of what the count suggests: those keys are READ. And + // json.go's comment justifies them by exceptionObject's lookup, which + // is stale — errorClass (vm.go) walks the tree for a qualified name + // and never reads the flat key. The comment refuted the first + // hypothesis and the code refuted the comment. + // + // Evidence that these are not constants in any usable sense: + // `Object.send(:remove_const, :"AASM::Error")` raises + // `NameError: wrong constant name` — this table reported a key that + // this interpreter's own constant API refuses. + if strings.Contains(name, "::") { + return false + } return !c.privateConsts[name] && !inConstTables(earlier, name) } if len(c.autoloads) == 0 { diff --git a/internal/vm/constants_qualified_test.go b/internal/vm/constants_qualified_test.go new file mode 100644 index 00000000..d0e80c99 --- /dev/null +++ b/internal/vm/constants_qualified_test.go @@ -0,0 +1,71 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +//go:build !rbgo_core + +package vm + +import "testing" + +// ⛔ #817: Object.constants answered 1620 where MRI 4.0.7 answers 120, and +// 1303 of those 1620 carried "::" — AASM::Error, ActionCable::Channel::Base, +// Acme::Client::Error::BadNonce. MRI has none, because a nested constant +// belongs to its enclosing module. +// +// The issue's title says "Object.constants is empty", which is the opposite of +// what it does; measured through cmd/oracle with reference, control and +// treatment: +// +// MRI before after +// total 120 1620 317 +// containing "::" 0 1303 0 +// core missing none none none +// +// ⛔ THE FIX IS IN THE ENUMERATION, NOT THE 52 REGISTRATION SITES, and that is +// the opposite of what counting suggests. `vm.consts[qualified] = c` appears in +// 52 files, but those keys are READ: vm.kafkaClass("Kafka::Producer") and three +// siblings in kafka alone, plus actioncable_bind.go, io.go and embed.go. They +// are an index from qualified name to class, not constants — so the table keeps +// them and `constants` stops reporting them. +// +// ⚠ What this does NOT close: 317 against MRI's 120. The remainder is bound gem +// top-levels (AASM, Acme, ActionCable…) present before any `require`, which is a +// separate decision with a separate cost. +func TestObjectConstantsReportsNoQualifiedNames(t *testing.T) { + got := runSrc(t, ` +all = Object.constants.map(&:to_s) +p all.count { |n| n.include?("::") } + +# ⛔ The core set must survive the filter. A guard that empties the list would +# also report zero qualified names. +core = %w[Object BasicObject Module Class Kernel Comparable Enumerable + String Symbol Integer Float Array Hash Range Proc Exception + StandardError RuntimeError Thread Mutex Queue File IO Dir + Encoding Regexp MatchData Struct Math Process GC ObjectSpace] +p core.reject { |n| all.include?(n) } +p all.size > 100 + +# The tree is untouched: a nested constant is still reachable through its +# parent, which is where MRI keeps it. +p AASM.constants.include?(:Error) +p AASM.constants.map(&:to_s).any? { |n| n.include?("::") } +p AASM::Error.to_s +p Object.const_get("AASM::Error").to_s + +# ⛔ And the bindings' own lookups, which are what made editing the 52 sites the +# wrong fix: these resolve through the qualified key the table still holds. +p Kafka::Producer.to_s +p JSON::ParserError.to_s +p Acme::Client::Error::BadNonce.to_s + +# Module.constants at the top level is Object.constants in MRI, so it must be +# filtered too -- it is a different code path into the same helper. +p Module.constants.map(&:to_s).count { |n| n.include?("::") } +`) + want := "0\n[]\ntrue\ntrue\nfalse\n\"AASM::Error\"\n\"AASM::Error\"\n" + + "\"Kafka::Producer\"\n\"JSON::ParserError\"\n\"Acme::Client::Error::BadNonce\"\n0" + if got != want { + t.Errorf("Object.constants qualified-name filter:\ngot %q\nwant %q", got, want) + } +}