From eb4454bcaefb9cd86f7e97a0a85941e28d6034e8 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 21:02:09 +0200 Subject: [PATCH 1/2] Struct/Data: advance MRI 4.0.6 conformance (keyword auto-detect, each_pair, dig, to_h, inspect) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Struct#initialize (keyword_init unset) now takes a lone Hash whose keys all name members as keyword init (Ruby 3.2 auto-detect), and is private. Struct#each_pair yields a single [name, value] Array (Hash#each style) so an Enumerator consumer receives the pair whole. Struct#dig with a non-member name returns nil instead of raising NameError. Struct#to_h with a block coerces the returned pair via #to_ary (never #to_a) and names the real class in its TypeError. deconstruct_keys reports "expected Array or nil" and names the real class in the #to_int TypeError. Struct/Data #inspect omit the class name for a class nested in an anonymous namespace (permanent-classpath check), and a recursive anonymous Data renders the member class as "#". Co-Authored-By: Claude Opus 4.8 --- internal/vm/data.go | 22 ++++-- internal/vm/struct.go | 97 ++++++++++++++++++++++---- internal/vm/struct_conformance_test.go | 2 +- 3 files changed, 102 insertions(+), 19 deletions(-) diff --git a/internal/vm/data.go b/internal/vm/data.go index 2932ba2a..d2e39ba4 100644 --- a/internal/vm/data.go +++ b/internal/vm/data.go @@ -1,11 +1,25 @@ package vm import ( + "fmt" + "reflect" "strings" "github.com/go-embedded-ruby/ruby/internal/object" ) +// anonClassRepr renders an anonymous class/module the way MRI does inside a +// recursive Data #inspect sentinel — "#" / "#" with the +// class's identity address — so a self-referential anonymous Data prints +// "#:...>" (matching MRI's rb_class_name fallback). +func anonClassRepr(c *RClass) string { + kind := "Class" + if c.isModule { + kind = "Module" + } + return fmt.Sprintf("#<%s:0x%016x>", kind, reflect.ValueOf(c).Pointer()) +} + // dataDef records the member layout of a Data subclass minted by Data.define. // Unlike structDef there is no keyword-init tri-state: a Data class always // accepts both positional and keyword construction. It is stored on the RClass @@ -413,7 +427,7 @@ func dataToHPair(vm *VM, res object.Value) (object.Value, object.Value) { } } if !ok { - raise("TypeError", "wrong element type %s (expected array)", classNameOf(res)) + raise("TypeError", "wrong element type %s (expected array)", vm.classOf(res).name) } } if len(arr.Elems) != 2 { @@ -430,15 +444,15 @@ func (vm *VM) dataInspect(o *RObject) string { names := dataDefOf(o.class).names if !object.ReprEnter(o) { nm := o.class.name - if nm == "" { - nm = o.class.ToS() // anonymous: # + if !namedThroughNesting(o.class) { + nm = anonClassRepr(o.class) // anonymous / non-permanent path: # } return "#" } defer object.ReprLeave(o) var b strings.Builder b.WriteString("# 0 { + if h, ok := a[0].(*object.Hash); ok && structHashAllMembers(h, names) { + for i, nm := range names { + if v, present := h.Get(object.Symbol(nm)); present { + o.structVals[i] = v + } else { + o.structVals[i] = object.NilV + } + } + return object.NilV + } + } if len(a) > len(names) { raise("ArgumentError", "struct size differs") } @@ -209,6 +227,9 @@ func setupStruct(vm *VM) { } return object.NilV }) + // MRI keeps Struct#initialize private (Car.private_instance_methods includes + // :initialize). + vm.setInstanceVisibility(cStruct, "initialize", visPrivate) cStruct.define("members", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return structMemberSyms(structDefOf(self.(*RObject).class).names) @@ -231,7 +252,11 @@ func setupStruct(vm *VM) { names := structDefOf(self.(*RObject).class).names vals := structValues(self) for i, nm := range names { - vm.callBlock(blk, []object.Value{object.Symbol(nm), vals[i]}) + // MRI yields ONE two-element Array per pair (like Hash#each): a `|k, v|` + // block auto-splats it, while a single-parameter block (or an Enumerator + // consumer, e.g. each_pair.map { |var| var }) receives the [name, value] + // Array whole. + vm.callBlock(blk, []object.Value{object.NewArrayFromSlice([]object.Value{object.Symbol(nm), vals[i]})}) } return self }) @@ -293,14 +318,10 @@ func setupStruct(vm *VM) { for i, nm := range names { if blk != nil { res := vm.callBlock(blk, []object.Value{object.Symbol(nm), vals[i]}) - pair, ok := res.(*object.Array) - if !ok { - raise("TypeError", "wrong element type %s (expected array)", classNameOf(res)) - } - if len(pair.Elems) != 2 { - raise("ArgumentError", "element has wrong array length (expected 2, was %d)", len(pair.Elems)) - } - h.Set(pair.Elems[0], pair.Elems[1]) + // MRI coerces the returned pair with #to_ary (never #to_a); a non-Array, + // non-to_ary object is a TypeError naming the object's real class. + key, val := dataToHPair(vm, res) + h.Set(key, val) } else { h.Set(object.Symbol(nm), vals[i]) } @@ -323,7 +344,7 @@ func setupStruct(vm *VM) { } keys, ok := a[0].(*object.Array) if !ok { - raise("TypeError", "wrong argument type %s (expected Array)", classNameOf(a[0])) + raise("TypeError", "wrong argument type %s (expected Array or nil)", classNameOf(a[0])) } if len(keys.Elems) > len(names) { return h // more keys than members: MRI returns {} without inspecting them @@ -373,9 +394,19 @@ func setupStruct(vm *VM) { } v = vals[idx] case object.Symbol: - v = vals[structNameIndex(string(k), names)] + // Unlike #[], #dig with a name that is not a member returns nil (treating + // the missing member as nil) rather than raising NameError. + if i, found := structFind(string(k), names); found { + v = vals[i] + } else { + v = object.NilV + } case *object.String: - v = vals[structNameIndex(k.Str(), names)] + if i, found := structFind(k.Str(), names); found { + v = vals[i] + } else { + v = object.NilV + } default: raise("TypeError", "no implicit conversion of %s into Integer", classNameOf(a[0])) } @@ -413,7 +444,7 @@ func setupStruct(vm *VM) { vals := o.structVals var b strings.Builder b.WriteString("# Date: Mon, 31 Aug 2026 21:14:45 +0200 Subject: [PATCH 2/2] Struct#hash: class-only hash for recursive structs; residual tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit rb_struct_hash runs under rb_exec_recursive_outer, which on a cycle unwinds to the outermost struct and hashes from its class alone (members skipped). Replicate that: structHashWalk folds member hashes while tracking the path, and any back-edge makes structHash return the class-only value — so #eql? recursive structs (which short-circuit on the shared cyclic member) hash alike, matching core/struct/hash_spec "recursive structs". Adds struct_residuals_test.go covering the changed branches. Co-Authored-By: Claude Opus 4.8 --- internal/vm/data.go | 15 ++--- internal/vm/struct.go | 55 ++++++++++++------ internal/vm/struct_residuals_test.go | 83 ++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 26 deletions(-) create mode 100644 internal/vm/struct_residuals_test.go diff --git a/internal/vm/data.go b/internal/vm/data.go index d2e39ba4..3deaae6c 100644 --- a/internal/vm/data.go +++ b/internal/vm/data.go @@ -8,16 +8,13 @@ import ( "github.com/go-embedded-ruby/ruby/internal/object" ) -// anonClassRepr renders an anonymous class/module the way MRI does inside a -// recursive Data #inspect sentinel — "#" / "#" with the -// class's identity address — so a self-referential anonymous Data prints -// "#:...>" (matching MRI's rb_class_name fallback). +// anonClassRepr renders an anonymous Data subclass the way MRI does inside a +// recursive Data #inspect sentinel — "#" with the class's identity +// address — so a self-referential anonymous Data prints "#:...>" +// (matching MRI's rb_class_name fallback). It is only ever called for a Data +// class, which is always a Class (never a module). func anonClassRepr(c *RClass) string { - kind := "Class" - if c.isModule { - kind = "Module" - } - return fmt.Sprintf("#<%s:0x%016x>", kind, reflect.ValueOf(c).Pointer()) + return fmt.Sprintf("#", reflect.ValueOf(c).Pointer()) } // dataDef records the member layout of a Data subclass minted by Data.define. diff --git a/internal/vm/struct.go b/internal/vm/struct.go index 22b69ba8..3dc8770d 100644 --- a/internal/vm/struct.go +++ b/internal/vm/struct.go @@ -747,29 +747,50 @@ func (vm *VM) structEqual(a, b object.Value, eql bool, seen map[[2]*RObject]bool return true } -// structHash combines the struct class identity with each member's hash. A cycle -// guard contributes a fixed sentinel for a re-entered struct so a self- or -// mutually-recursive struct hashes without looping. -func (vm *VM) structHash(o *RObject, seen map[*RObject]bool) int64 { - if seen[o] { - return 0x27d4eb2f - } - if seen == nil { - seen = map[*RObject]bool{} +// structHash combines the struct class identity with each member's hash. When the +// member graph contains a cycle (a self- or mutually-recursive struct) the whole +// hash collapses to a class-only value: MRI's rb_struct_hash runs under +// rb_exec_recursive_outer, which on detecting recursion unwinds to the OUTERMOST +// struct and returns a hash computed from its class alone (members skipped). That +// makes every recursive struct of a class hash alike — necessary because such +// structs are #eql? by short-circuiting on the shared cyclic member, and #eql? +// objects must hash equal (see core/struct/hash_spec "recursive structs"). +func (vm *VM) structHash(o *RObject, _ map[*RObject]bool) int64 { + if h, recursed := vm.structHashWalk(o, map[*RObject]bool{}); !recursed { + return h } - seen[o] = true - defer delete(seen, o) - h := int64(reflect.ValueOf(o.class).Pointer()) + return structClassOnlyHash(o.class) +} + +// structHashWalk folds the class identity with each member's hash, tracking the +// set of structs on the current path so a back-edge to one of them is reported as +// recursion (recursed=true) rather than followed into a loop. The fold value is +// meaningful only when recursed is false. +func (vm *VM) structHashWalk(o *RObject, path map[*RObject]bool) (int64, bool) { + if path[o] { + return 0, true + } + path[o] = true + defer delete(path, o) + h := structClassOnlyHash(o.class) for _, v := range o.structVals { - var hv int64 if so, ok := v.(*RObject); ok && hasMemberDef(so.class) { - hv = vm.structHash(so, seen) + hv, recursed := vm.structHashWalk(so, path) + if recursed { + return 0, true + } + h = h*31 + hv } else { - hv = vm.hashValue(v) + h = h*31 + vm.hashValue(v) } - h = h*31 + hv } - return h + return h, false +} + +// structClassOnlyHash is the hash contributed by a Struct/Data class's identity — +// the base of the member fold, and also the whole hash of a recursive struct. +func structClassOnlyHash(c *RClass) int64 { + return int64(reflect.ValueOf(c).Pointer()) } // validConstName reports whether s is a valid (ASCII) Ruby constant name: an diff --git a/internal/vm/struct_residuals_test.go b/internal/vm/struct_residuals_test.go new file mode 100644 index 00000000..635d098c --- /dev/null +++ b/internal/vm/struct_residuals_test.go @@ -0,0 +1,83 @@ +package vm_test + +import ( + "strings" + "testing" +) + +// TestStructDataResiduals locks in the Struct/Data conformance residuals fixed in +// this change: keyword_init-unset auto-detect, #each_pair pair yielding, #dig on a +// missing member, #to_h block coercion, #inspect naming for anonymous-namespace +// classes, and the class-only hash of a recursive struct. Every expectation is +// byte-for-byte against MRI Ruby 4.0. +func TestStructDataResiduals(t *testing.T) { + cases := []struct{ src, want string }{ + // Struct#initialize is private (MRI). + {`S = Struct.new(:a); p S.private_instance_methods(true).include?(:initialize)`, "true\n"}, + + // #each_pair yields ONE [name, value] Array per pair, so an Enumerator + // consumer (to_a / single-parameter map) gets the pair whole. + {`S = Struct.new(:a, :b); p S.new(1, 2).each_pair.to_a`, "[[:a, 1], [:b, 2]]\n"}, + {`S = Struct.new(:a, :b); p S.new(1, 2).each_pair.map { |v| v }`, "[[:a, 1], [:b, 2]]\n"}, + + // #dig with a missing member name returns nil (unlike #[], which raises). + {`S = Struct.new(:a); p S.new(1).dig(:zzz)`, "nil\n"}, + {`S = Struct.new(:a); p S.new(1).dig("zzz")`, "nil\n"}, + + // keyword_init unset auto-detect (Ruby 3.2): a lone Hash whose keys all name + // members is keyword init; any non-member or non-Symbol key keeps it + // positional (matching MRI for an explicit-brace hash). + {`S = Struct.new(:a, :b); p S.new(a: 1, b: 2).to_a`, "[1, 2]\n"}, + {`S = Struct.new(:a, :b); p S.new(a: 5).to_a`, "[5, nil]\n"}, + {`S = Struct.new(:a); p S.new({b: 1}).a`, "{b: 1}\n"}, + {`S = Struct.new(:a); p S.new({0 => 1}).a`, "{0 => 1}\n"}, + {`S = Struct.new(:a); p S.new({}).a`, "{}\n"}, + + // #to_h block coerces the returned pair with #to_ary (never #to_a). + {`S = Struct.new(:a); class Ary1; def to_ary; [:k, 9]; end; end; p S.new(1).to_h { |k, v| Ary1.new }`, "{k: 9}\n"}, + + // #to_s / #inspect: name shown only for a permanent classpath — omitted for + // an anonymous class or one nested in an anonymous namespace. + {`puts Struct.new(:a).new("").to_s`, "#\n"}, + {`S = Struct.new(:a); puts S.new("x").to_s`, "#\n"}, + {`module NS1; B = Struct.new(:a); end; puts NS1::B.new("z").to_s`, "#\n"}, + {`m = Module.new; m.module_eval "Foo = Struct.new(:a)"; puts m::Foo.new("").to_s`, "#\n"}, + + // Recursive struct hashes collapse to a class-only value (so #eql? recursive + // structs hash alike); non-recursive nested structs still fold member hashes. + {`R = Struct.new(:a, :b); x = R.new(1, 2); y = R.new(1, 2); x[:a] = x; y[:a] = x; p x.hash == y.hash`, "true\n"}, + {`R = Struct.new(:a); o1 = R.new(R.new(1)); o2 = R.new(R.new(2)); p o1.hash == o2.hash`, "false\n"}, + + // Data#to_s naming and recursive sentinel. + {`M = Data.define(:x); puts M.new(x: 1).to_s`, "#\n"}, + {`M = Data.define(:x); k = Class.new(M); puts k.new(x: 1).to_s`, "#\n"}, + {`M = Data.define(:x); a = M.allocate; a.send(:initialize, x: a); puts a.to_s`, "#>\n"}, + // Recursive anonymous Data renders the member class as #. + {`M = Data.define(:x); k = Class.new(M); b = k.allocate; b.send(:initialize, x: b); p !!(b.to_s =~ /\A#:\.\.\.>>\z/)`, "true\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got %q\nwant %q", c.src, got, c.want) + } + } +} + +// TestStructDataResidualErrors covers the TypeError paths whose message wording or +// class-naming this change corrected. +func TestStructDataResidualErrors(t *testing.T) { + cases := []struct{ src, want string }{ + // deconstruct_keys of a non-Array, non-nil argument names "Array or nil". + {`Struct.new(:a).new(1).deconstruct_keys("x")`, "wrong argument type String (expected Array or nil)"}, + // A #to_int that returns a non-Integer names the key's real class. + {`class KK2; def to_int; "x"; end; end; Struct.new(:a, :b).new(1, 2).deconstruct_keys([KK2.new])`, "can't convert KK2 into Integer"}, + // #to_h block pair that is neither Array nor #to_ary-able names the real class. + {`Struct.new(:a).new(1).to_h { |k, v| Object.new }`, "wrong element type Object (expected array)"}, + {`Data.define(:a).new(a: 1).to_h { |k, v| Object.new }`, "wrong element type Object (expected array)"}, + } + for _, c := range cases { + err := runErr(t, c.src) + if err == nil || !strings.Contains(err.Error(), c.want) { + t.Errorf("src=%q: got err=%v, want containing %q", c.src, err, c.want) + } + } +}