Skip to content

Struct/Data: advance MRI 4.0.6 conformance (keyword auto-detect, each_pair, dig, to_h, inspect, recursive hash) - #540

Merged
tannevaled merged 3 commits into
mainfrom
feat/agent-struct
Aug 31, 2026
Merged

tannevaled merged 3 commits into
mainfrom
feat/agent-struct

Conversation

@tannevaled

@tannevaled tannevaled commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Advances MRI Ruby 4.0.6 conformance for core/struct/* and core/data/*.
Corpus (rubyspec) result went from 230 pass / 18 fail+error to 246 pass / 2 fail across the two suites (16 examples fixed); the 2 residual failures are out of scope (see below).

All work is confined to internal/vm/struct.go, internal/vm/data.go, and tests.

Fixes

  • Struct#initialize keyword_init auto-detect (Ruby 3.2) — when keyword_init: is unset, a lone Hash argument whose keys all name members is taken as keyword initialisation; a Hash with any non-member or non-Symbol key stays a positional value. MRI decides this from the caller's keyword flag, which our calling convention does not carry to a native method, so we approximate by matching keys against members (preserves e.g. Struct.new(:a).new({b: 1}) as positional).
  • Struct#initialize is now private (private_instance_methods includes :initialize).
  • Struct#each_pair yields a single [name, value] Array per pair (Hash#each style), so an Enumerator consumer such as each_pair.map { |v| v } receives the pair whole.
  • Struct#dig with a name that is not a member returns nil instead of raising NameError (unlike #[]).
  • Struct#to_h with a block coerces the returned pair via #to_ary (never #to_a) and names the real class in its TypeError.
  • Struct#deconstruct_keys reports expected Array or nil, and names the real class in the #to_int conversion TypeError.
  • Struct#to_s / Data#to_s omit the class name for a class nested in an anonymous namespace (permanent-classpath check), matching MRI's rb_class_path_cached. A recursive anonymous Data renders the member class as #<Class:0x…>.
  • Struct#hash for recursive structs collapses to a class-only value, replicating MRI's rb_exec_recursive_outer (unwind to the outermost struct, hash from its class alone). Necessary because #eql? recursive structs must hash alike.

Out of scope (reported, not fixed)

The 2 remaining corpus failures — Struct#hash/Data#hash "returns different hashes for different classes" — are not hash bugs (the hashes are genuinely different). They fail because these specs use x.should != y, and the VM compiles a != b as !(a == b) without dispatching a user-defined #!=, so the spec harness's ShouldProxy#== runs and raises. This is compiler/operator-dispatch machinery outside struct.go.

Verification

  • go test ./internal/vm/ green; go test ./... green.
  • GOOS=wasip1 GOARCH=wasm go build ./... OK.
  • gofmt -l clean on changed files.
  • Every function in struct.go and data.go is at 100.0% coverage under the merged (-coverpkg ./internal/...) profile; repo total unchanged at ~99.9%.

🤖 Generated with Claude Code

tannevaled and others added 3 commits August 31, 2026 21:02
…_pair, dig, to_h, inspect)

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 "#<Class:0x…>".

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 1fd225c into main Aug 31, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-struct branch August 31, 2026 22:26
tannevaled added a commit that referenced this pull request Sep 1, 2026
Locks in #535 (String methods: index/casecmp/chomp/gsub/scan/tr/... ~+110),
#536 (IO/StringIO reader+writer), #538 (Array#[]= auto-grow + mutators),
#539 (Range + Comparable), #540 (Struct + Data), #541 (Time). Measured
17983 passing on the pinned corpus, up from 17654.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
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