From 6b4fa5d79367325bb7f3656925a5df9cda8d21c5 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 12 Oct 2026 10:34:16 +0200 Subject: [PATCH] internal/vm: Kernel#to_s prints the address for a plain object, and Thread::* carry their qualified names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two issues that meet on one line of output, so they are fixed together: doing either alone trades one divergence for another. #756 -- Kernel#to_s is MRI's rb_any_to_s, which ALWAYS prints the address. `Object.new.to_s` is "#", not "#". rbgo printed the short form, so #to_s and #inspect disagreed with each other INSIDE rbgo while #inspect agreed with MRI. #759 (kind A) -- Thread::Mutex, Thread::Queue, Thread::SizedQueue and Thread::ConditionVariable answered their BARE name. In MRI the class has one name, the qualified one, and the top-level `Mutex` is a second constant pointing at the same class: `Mutex.name` is "Thread::Mutex" however it was reached, and `Mutex.equal?(Thread::Mutex)` is true in both engines. They are coupled because RMutex.ToS hardcoded "#". Once Kernel#to_s builds its string from classOf(self).name, the bare name becomes visible as "#" -- a correct address with a wrong name. The first version of this commit did exactly that. ⚠ THE SCOPE IS NARROWER THAN MRI'S, deliberately. The fix applies to a plain *RObject only. Thirteen core classes reach Kernel#to_s here and MRI gives all thirteen an address, but most are Go types whose own ToS() IS their #to_s -- rbgo implements many objects' to_s as the Go method rather than as a registered Ruby one, and they arrive at Kernel#to_s relying on the delegation. A version that applied the identity form to everything reaching Kernel#to_s broke 78 TESTS, and they were right to break: Capistrano::Server#to_s "db1" the hostname Capistrano::Task#to_s "solo" the task name Net::SMTP::Response#to_s "250 2.0.0 queued" OpenIDConnect::ProviderMetadata "#<… issuer=…>" the top-level self "main" irb's prompt I had transposed a rule that is true of MRI -- "reaching rb_any_to_s means the receiver has no useful representation" -- into an engine whose dispatch works differently. The count is in the code comment so the next person to widen this knows what they will meet. Three tests needed updating, each checked against MRI 4.0.7 rather than assumed: two pinned "#" / "#" for a PLAIN object, which MRI contradicts, and wave59's wrapper block already normalised addresses to 0xA, so it was built for this. That block is now byte-identical to MRI on 13 of its 14 lines; the fourteenth, Enumerator#to_s answering MRI's *inspect* form, is a separate divergence named in the test rather than folded into the expectation. The full suite is green: 0 failures. Closes #756. Closes the kind-A half of issue 759. Co-Authored-By: Claude Opus 5 --- internal/vm/builtin_subclass_test.go | 7 +- internal/vm/builtins.go | 46 +++++++++++- internal/vm/conditionvariable.go | 3 +- internal/vm/kernel_to_s_test.go | 108 +++++++++++++++++++++++++++ internal/vm/phase1_test.go | 4 +- internal/vm/queue.go | 5 +- internal/vm/thread.go | 9 ++- internal/vm/wave59_puts_to_s_test.go | 12 ++- 8 files changed, 185 insertions(+), 9 deletions(-) create mode 100644 internal/vm/kernel_to_s_test.go diff --git a/internal/vm/builtin_subclass_test.go b/internal/vm/builtin_subclass_test.go index e720e35b..8a847c28 100644 --- a/internal/vm/builtin_subclass_test.go +++ b/internal/vm/builtin_subclass_test.go @@ -41,8 +41,11 @@ h = H.new; h[:x] = 9; p [h.fetch_or(:x), h.fetch_or(:y), h.keys, h]`, "[9, :none // Object identity / equal? keep operating on the wrapper, not the value. {`class S < String; end; a = S.new("x"); p [a.equal?(a), a.equal?(S.new("x"))]`, "[true, false]\n"}, - // A plain object (no wrapped value) still renders as #. - {`class Plain; end; p Plain.new.to_s`, "\"#\"\n"}, + // A plain object (no wrapped value) renders as MRI's rb_any_to_s does, + // WITH the address (#756). The address is normalised because it is not + // fixed; the shape is MRI 4.0.7's, measured. This read `"#"`, + // which was green only while rbgo omitted it. + {`class Plain; end; p Plain.new.to_s.gsub(/0x[0-9a-f]+/, "0xA")`, "\"#\"\n"}, // As a Hash key, a String-subclass instance hashes/compares as its content. {`class S < String; end; h = {}; h[S.new("a")] = 1; p [h["a"], h[S.new("a")], h.keys.first.class]`, "[1, 1, S]\n"}, {`class S < String; end; p({"k" => 1}[S.new("k")])`, "1\n"}, diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index a3483f32..d379b2e7 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -1052,7 +1052,51 @@ func (vm *VM) bootstrap() { vm.cObject.define("class", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return vm.classOf(self) }) - vm.cObject.define("to_s", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + // Kernel#to_s is MRI's rb_any_to_s, and rb_any_to_s ALWAYS prints the + // address: `Object.new.to_s` is "#", not "#". rbgo + // printed the short form, so #to_s and #inspect disagreed with each other + // inside rbgo while #inspect agreed with MRI (#756). + // + // The fix is here rather than in (*RObject).ToS for two reasons. The Go + // method has no VM and so cannot reach refID at all; and it has 707 callers + // in this package, where Kernel#to_s has exactly the reach MRI gives + // rb_any_to_s. That reach was measured rather than assumed: of 24 core types + // probed for `method(:to_s).owner`, exactly two answer Kernel -- Object and + // Thread::Mutex -- and MRI answers Kernel for the same two and no others. + // Everything else, including a subclass of String or Array, defines or + // inherits its own. + // + // It covers `puts obj` and "#{obj}" as well, because both dispatch #to_s: + // a user-defined `to_s` wins for all three in rbgo exactly as in MRI, so + // fixing the method fixes every path instead of creating a new disagreement + // between them. + vm.cObject.define("to_s", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + // A PLAIN Ruby object gets MRI's rb_any_to_s, which always prints the + // address: `Object.new.to_s` is "#", not "#". rbgo + // printed the short form, so #to_s and #inspect disagreed with each + // other inside rbgo while #inspect agreed with MRI (#756). + // + // ⚠ ONLY a plain *RObject, and that narrowness is the whole design. + // Reaching Kernel#to_s does NOT mean "this object has no meaningful + // to_s" here, the way it does in MRI: rbgo implements many objects' + // #to_s as the GO method ToS() rather than by registering a Ruby #to_s, + // so those types arrive here relying on the delegation below. + // + // A version of this that applied the identity form to everything that + // reached Kernel#to_s broke 78 tests, and they were right to break: + // Capistrano::Server#to_s must be its hostname ("db1"), + // Capistrano::Task#to_s its name ("solo"), Net::SMTP::Response#to_s its + // status line, OpenIDConnect::ProviderMetadata#to_s its summary -- and + // the top-level self must be "main", which is what irb's prompt is built + // from. Each of those is a Go ToS() doing real work. + // + // A built-in value subclass (`class MyStr < String`) carries its value + // in o.builtin and renders as that value, so it is excluded too -- it + // does not in fact arrive here, since it inherits String#to_s in both + // engines, but the guard states the rule rather than relying on that. + if o, ok := self.(*RObject); ok && object.IsNil(o.builtin) { + return object.NewString(vm.objectIdentityRepr(self)) + } return object.NewString(self.ToS()) }) // nil, true, false and Range each own their #to_s in MRI, and reported diff --git a/internal/vm/conditionvariable.go b/internal/vm/conditionvariable.go index 53399d72..8bc1850d 100644 --- a/internal/vm/conditionvariable.go +++ b/internal/vm/conditionvariable.go @@ -34,7 +34,8 @@ func (c *RConditionVariable) removeWaiter(t *RThread) { } func (vm *VM) registerConditionVariable() { - cCV := newClass("ConditionVariable", vm.cObject) + // Qualified name, bare constant: see registerMutex for why (#759 / #756). + cCV := newClass("Thread::ConditionVariable", vm.cObject) vm.consts["ConditionVariable"] = cCV vm.consts["Thread"].(*RClass).consts["ConditionVariable"] = cCV diff --git a/internal/vm/kernel_to_s_test.go b/internal/vm/kernel_to_s_test.go new file mode 100644 index 00000000..3f71761c --- /dev/null +++ b/internal/vm/kernel_to_s_test.go @@ -0,0 +1,108 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm_test + +import "testing" + +// TestKernelToSPrintsTheAddress (#756). +// +// MRI's rb_any_to_s ALWAYS prints the object's address, and Kernel#to_s is +// rb_any_to_s. rbgo printed the short "#" form, so #to_s and #inspect +// disagreed with each other INSIDE rbgo while #inspect agreed with MRI. +// +// The shape is asserted in Ruby rather than by pinning bytes, because the +// address changes every run -- but the PATTERN is exact: "#<", the class name, +// ":0x", sixteen hex digits, ">". A case that merely checked for "0x" would +// pass on a truncated or malformed address. +// +// SCOPE, and it is narrower than MRI's. The fix applies to a plain *RObject +// only. Thirteen core classes reach Kernel#to_s here (Binding, +// ConditionVariable, Dir, File, IO, Monitor, Mutex, Numeric, Object, Queue, +// Random, SizedQueue, TracePoint) and MRI gives all thirteen an address, but +// most of them are Go types whose own ToS() is their #to_s -- rbgo implements +// many objects' to_s as the Go method rather than as a registered Ruby one. +// +// A first attempt applied the identity form to everything reaching Kernel#to_s +// and broke 78 tests, every one of them rightly: Capistrano::Server#to_s is its +// hostname, Net::SMTP::Response#to_s its status line, the top-level self "main". +// So the remaining classes still answer their short form and are out of scope +// here; they need an address their ToS() cannot compute, having no VM. +func TestKernelToSPrintsTheAddress(t *testing.T) { + checkCases(t, []runCase{ + {`class Plain; end +p Plain.new.to_s =~ /\A#\z/ ? :ok : Plain.new.to_s`, ":ok\n"}, + {`p Object.new.to_s =~ /\A#\z/ ? :ok : Object.new.to_s`, ":ok\n"}, + // puts and interpolation dispatch #to_s, so fixing the method fixes them. + // Without these a fix could leave the three paths disagreeing. + {`class P2; end +s = "#{P2.new}" +p s =~ /\A#\z/ ? :ok : s`, ":ok\n"}, + // An object whose class is built in Ruby, to show this is about the + // RECEIVER's representation and not about where the class came from. + {`class Outer; class Inner; end; end +s = Outer::Inner.new.to_s +p s =~ /\A#\z/ ? :ok : s`, ":ok\n"}, + // #to_s and #inspect must now agree with each other, which was the + // internal contradiction #756 opened with. + {`class P4; end +o = P4.new +p o.to_s == o.inspect`, "true\n"}, + }) +} + +// TestKernelToSLeavesEveryTypeThatOwnsOneAlone is the control, and it is the +// half that matters: Kernel#to_s is inherited by everything, so a change to it +// is a change to every receiver that does not override it. These do override +// it, and each expectation is MRI 4.0.7's output. +func TestKernelToSLeavesEveryTypeThatOwnsOneAlone(t *testing.T) { + checkCases(t, []runCase{ + {`print 42.to_s`, "42"}, + {`print 1.5.to_s`, "1.5"}, + {`print [1, 2].to_s`, "[1, 2]"}, + {`print({a: 1}.to_s)`, "{a: 1}"}, + {`print :sym.to_s`, "sym"}, + {`print (1..2).to_s`, "1..2"}, + {`print nil.to_s.inspect`, `""`}, + {`print true.to_s`, "true"}, + {`print String.to_s`, "String"}, + // A built-in value subclass renders as its value, not as an identity: + // it inherits String#to_s rather than Kernel's. + {`class MyStr < String; end +print MyStr.new("x").to_s`, "x"}, + {`class MyArr < Array; end +print MyArr.new([1]).to_s`, "[1]"}, + // A user-defined to_s still wins, for all three paths. + {`class Q; def to_s; "CUSTOM"; end; end +print Q.new.to_s, " ", "#{Q.new}"`, "CUSTOM CUSTOM"}, + }) +} + +// TestThreadNestedClassesCarryTheirQualifiedName (#759, kind A). +// +// Thread::Mutex and three siblings answered their BARE name. In MRI the class +// has one name -- the qualified one -- and the top-level `Mutex` is a second +// constant pointing at the same class, so `Mutex.name` is "Thread::Mutex" +// however it was reached. +// +// This sits with #756 rather than apart from it because the two meet on one +// line of output: once Kernel#to_s builds its string from classOf(self).name +// instead of a hardcoded "#", a bare class name becomes visible +// as `#`. Fixing one without the other trades a missing address for +// a wrong name. +func TestThreadNestedClassesCarryTheirQualifiedName(t *testing.T) { + checkCases(t, []runCase{ + {`p [Mutex.name, Queue.name, SizedQueue.name, ConditionVariable.name]`, + `["Thread::Mutex", "Thread::Queue", "Thread::SizedQueue", "Thread::ConditionVariable"]` + "\n"}, + {`p [Thread::Mutex.name, Thread::Queue.name]`, + `["Thread::Mutex", "Thread::Queue"]` + "\n"}, + // One class, two constants -- the thing that makes a single qualified + // name correct rather than a lie about where it lives. + {`p [Mutex.equal?(Thread::Mutex), Queue.equal?(Thread::Queue)]`, "[true, true]\n"}, + // They still work, which a rename could plausibly have broken. + {`q = Queue.new; q.push 1; p q.pop`, "1\n"}, + {`m = Mutex.new; m.lock; m.unlock; p m.locked?`, "false\n"}, + {`sq = SizedQueue.new(1); sq.push 7; p [sq.max, sq.pop]`, "[1, 7]\n"}, + }) +} diff --git a/internal/vm/phase1_test.go b/internal/vm/phase1_test.go index c534ea05..2171a95c 100644 --- a/internal/vm/phase1_test.go +++ b/internal/vm/phase1_test.go @@ -73,7 +73,9 @@ func TestKernelMethods(t *testing.T) { func TestValueReprAndTruthy(t *testing.T) { cases := []struct{ src, want string }{ - {"class E\nend\nputs E.new", "#\n"}, // RObject.ToS + // puts dispatches #to_s, and Kernel#to_s is MRI's rb_any_to_s, which + // always prints the address (#756). Normalised, since it is not fixed. + {"class E\nend\nputs E.new.to_s.gsub(/0x[0-9a-f]+/, \"0xA\")", "#\n"}, {"class E\nend\nputs(!E.new)", "false\n"}, // RObject.Truthy (objects are truthy) {"class K\nend\np K", "K\n"}, // RClass.Inspect {"class K\nend\nputs(!K)", "false\n"}, // RClass.Truthy diff --git a/internal/vm/queue.go b/internal/vm/queue.go index 77648ff2..8c5592e3 100644 --- a/internal/vm/queue.go +++ b/internal/vm/queue.go @@ -37,7 +37,8 @@ func (q *RQueue) Inspect() string { return q.ToS() } func (q *RQueue) Truthy() bool { return true } func (vm *VM) registerQueue() { - cQueue := newClass("Queue", vm.cObject) + // Qualified name, bare constant: see registerMutex for why (#759 / #756). + cQueue := newClass("Thread::Queue", vm.cObject) vm.consts["Queue"] = cQueue vm.consts["Thread"].(*RClass).consts["Queue"] = cQueue cQueue.smethods["new"] = &Method{name: "new", owner: cQueue, native: func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { @@ -120,7 +121,7 @@ func (vm *VM) registerQueue() { } func (vm *VM) registerSizedQueue(cQueue *RClass) { - cSized := newClass("SizedQueue", cQueue) + cSized := newClass("Thread::SizedQueue", cQueue) vm.consts["SizedQueue"] = cSized vm.consts["Thread"].(*RClass).consts["SizedQueue"] = cSized cSized.smethods["new"] = &Method{name: "new", owner: cSized, native: func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { diff --git a/internal/vm/thread.go b/internal/vm/thread.go index f78d8b49..f87fad3f 100644 --- a/internal/vm/thread.go +++ b/internal/vm/thread.go @@ -1190,7 +1190,14 @@ func (vm *VM) threadLocalKey(k object.Value) object.Value { } func (vm *VM) registerMutex() { - cMutex := newClass("Mutex", vm.cObject) + // The class's name is the QUALIFIED one; `Mutex` at top level is a second + // constant pointing at the same class, which is what MRI does -- + // `Mutex.equal?(Thread::Mutex)` is true in both engines, and `Mutex.name` + // answers "Thread::Mutex" in MRI however it was reached. rbgo answered + // "Mutex" (#759), and that only became visible in OUTPUT once Kernel#to_s + // started building its string from classOf(self).name (#756) rather than + // from the hardcoded "#" in RMutex.ToS. + cMutex := newClass("Thread::Mutex", vm.cObject) vm.consts["Mutex"] = cMutex vm.consts["Thread"].(*RClass).consts["Mutex"] = cMutex cMutex.smethods["new"] = &Method{name: "new", owner: cMutex, native: func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { diff --git a/internal/vm/wave59_puts_to_s_test.go b/internal/vm/wave59_puts_to_s_test.go index ad838ea0..5f43a04c 100644 --- a/internal/vm/wave59_puts_to_s_test.go +++ b/internal/vm/wave59_puts_to_s_test.go @@ -138,7 +138,17 @@ show "REXMLDoc", REXML::Document.new("") show "REXMLElem", REXML::Document.new("").root show "ARGFclass", ARGF.class ` - const want = `Object=# + // Compared line by line against MRI 4.0.7 on this host: 13 of these 14 are + // byte-identical to it. The one that is not is Enum -- rbgo's + // Enumerator#to_s answers MRI's *inspect* form, "#", + // where MRI's #to_s inherits Kernel's and answers "#". A + // divergence of its own, left alone here and recorded rather than quietly + // folded into this expectation. + // + // Object moved to the address form with #756: Kernel#to_s is MRI's + // rb_any_to_s, which always prints one. This test already normalised + // addresses to 0xA, so it was built for exactly that. + const want = `Object=# Encoding=UTF-8 Regexp=(?i-mx:ab) Time=1970-01-01 00:00:00 UTC