Skip to content

Array: advance MRI-4.0 conformance (live iteration, coercion, inspect, flatten, sort) - #558

Merged
tannevaled merged 11 commits into
mainfrom
feat/agent-array2
Sep 4, 2026
Merged

tannevaled merged 11 commits into
mainfrom
feat/agent-array2

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Advances core/array rubyspec conformance for the pure-Go Ruby VM (rbgo).

Summary of fixes (all verified byte-for-byte against MRI Ruby 4.0.6)

  • Live index-based iteration: each/map/select/sum/to_h/take_while/sort_by/map!/each_index now iterate by a live index so an element the block appends during iteration is visited too (MRI's "tolerates increasing an array size during iteration"). Fixed each entirely (4→11) and the shared example across the family.
  • In-place, panic-safe compaction: keep_if/select!/reject!/delete_if compact in place with a two-pointer walk under a deferred recover, so a block that raises partway leaves the array as MRI leaves it (decided prefix, then the raising element and the rest).
  • Array#inspect/#to_s: render each element through its own Ruby #inspect (honouring user-defined/mocked inspect), following MRI's rb_inspect → rb_obj_as_string, with recursion [...] and a BasicObject-safe fallback. #to_s is a true shared-record alias (5 inspect examples).
  • #to_ary/#to_int argument coercion: set operations (|,&,-,union,intersection,difference,intersect?), Array.new/#initialize, #to_h, #concat, #replace, #transpose, #flatten (incl. a #to_ary reached through #method_missing), and count args for first/last/pop/shift/drop/permutation.
  • #flatten flattens #to_ary-convertible elements (rb_check_array_type): 25→32.
  • #sort/#sort! block comparator turns Integer/Bignum/Comparable results into a sign like rb_cmpint: 34→36.
  • Constructor: Array.new/#initialize copy via #to_ary, size via #to_int, arity 0..2, break-safe incremental build, and #initialize is private (14→18 / 15→22).
  • Smaller fixes: #delete checks frozen only on an actual removal; #rindex rechecks the live length; #append/#prepend/#collect/#filter/#collect!/#filter!/#to_ary (shared-record aliases + identity); pop/shift over-argument errors; first-with-Bignum RangeError.

Result

core/array rubyspec: fail+error dropped from ~166 to 67 (pass 1109, fail 50, error 17) with no regressions.

Verification

  • go test ./internal/vm/ and go test ./... green.
  • GOOS=wasip1 GOARCH=wasm go build ./... OK; gofmt -l clean.
  • New internal/vm/array_residuals_wave10_test.go covers every new branch; all new/changed functions report 100.0% in the merged coverage profile (repo total 99.9%, up from 98.9%); no shadowed dead code.

Not fixed (out of scope / pre-existing): Array#[] with Enumerator::ArithmeticSequence, Array#hash element-hash semantics, +/- operators via literal syntax (arith.go), and a handful of harness-dependent (before :all / describe-body ivar) cases. Also surfaced a pre-existing VM defect unrelated to Array: a block's first != after a top-level != mis-evaluates (returns the operand instead of the comparison) — left untouched as it lives in the opcode/inline-cache layer.

🤖 Generated with Claude Code

tannevaled and others added 11 commits September 4, 2026 12:08
…_while

Array#each and the collect/filter family visited elements by ranging over a
snapshot of the slice header, so elements a block appended during iteration
were not yielded. Ruby's Array iteration is index-based (visit while i <
length), so a growing array has its new tail seen. Switch each, map, select,
sum, to_h, sort_by and take_while to a live-index loop.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…ete_if; live map!/each_index

arrayKeepIf now compacts in place with a two-pointer walk under a deferred
recover, so a block that raises partway leaves the array as MRI leaves it — the
decided prefix, then the raising element and the untouched tail. delete_if
routes through it for the same semantics. map! and each_index use a live-index
loop so a growing array has its new tail visited.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Array#inspect now renders each element through its own Ruby-level #inspect
(honouring user-defined and mocked #inspect), following MRI's rb_inspect →
rb_obj_as_string: a non-String #inspect result passes through #to_s, then falls
back to the #<Class:0x…> identity; a self-referential array renders as [...].
#to_s aliases it.

Array#|, #&, #-, #union, #intersection, #difference and #intersect? now coerce
each argument with #to_ary (Array subclasses used directly, never converted),
matching MRI's rb_to_array_type, instead of rejecting every non-Array.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
… private

Array.new / Array#initialize now match MRI's rb_ary_initialize: a single
non-Integer argument is copied as another array through #to_ary (Array
subclasses directly); a size argument is coerced with #to_int; 3+ arguments
raise ArgumentError; the array is built incrementally so a block break keeps the
elements produced so far; and #initialize is marked private.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…inspect elements

Array#to_h now converts a non-Array pair through #to_ary (matching MRI) and
raises ArgumentError when given a positional argument. Array#inspect renders an
element that does not answer #inspect (a bare BasicObject subclass, which lacks
even #respond_to?) through its native representation via a static method lookup,
rather than raising NoMethodError.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- first/last/pop/shift/drop/permutation coerce a count argument through #to_int
  (Float and #to_int-responders), instead of accepting only Integer.
- concat/replace coerce arguments through #to_ary (Array subclasses directly),
  and concat snapshots each argument before appending so concat(self) matches MRI.
- pop/shift raise ArgumentError on more than one argument; first raises RangeError
  for a Bignum count.
- rindex rechecks the live length each step so a block that shrinks the array can
  no longer index out of range.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…n/frozen

- #append, #prepend, #to_s, #collect, #collect!, #filter, #filter! now share the
  method record of #push/#unshift/#inspect/#map/#map!/#select/#select! so their
  UnboundMethods are identical, as MRI's aliases are; #to_ary returns self.
- #transpose coerces each row through #to_ary (subclasses directly).
- #delete checks frozen only when an element is actually removed, so deleting an
  absent element from a frozen array returns nil instead of raising.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Array#flatten / #flatten! now flatten any element that converts to an Array
through #to_ary — a real #to_ary, or one reached through #method_missing when
respond_to_missing?(:to_ary, true) is true — following MRI's rb_check_array_type
via rb_check_funcall. A #to_ary returning nil leaves the element in place; a
non-nil non-Array result raises TypeError. Immediates and receivers without even
#respond_to_missing? (a bare BasicObject) are left untouched rather than sent a
message they cannot answer. The two flatten helpers are unified into one
vm-method that also reports whether anything expanded (for flatten!).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…duct via checkArrayType

Array#sort / #sort! with a block now turn the block's result into a sign the way
MRI's rb_cmpint does: an Integer or Bignum contributes its own sign, and any
other result is compared against 0 through #<=> (a non-Integer #<=> is
"comparison of <class> with 0 failed"). Previously only a small Integer result
was accepted, so a Bignum or Comparable result raised. #product routes argument
coercion through checkArrayType so a #to_ary reached through #method_missing is
honoured.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…flatten, sort cmp)

Adds internal/vm/array_residuals_wave10_test.go exercising every new branch:
live-index iteration with a growing array; arrayKeepIf's in-place compaction,
no-change nil, panic-safe partial retention and full-truncate; #inspect element
dispatch with #to_s and identity fall-backs, recursion and BasicObject; set-op
and constructor/#to_h/#concat/#replace/#transpose #to_ary and #to_int coercion;
count-arg coercion and arity errors; the shared-record aliases' identity;
#flatten #to_ary coercion (real, method_missing, nil, TypeError, recursion); and
the sort block comparator over Integer/Bignum/Comparable/non-comparable results.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…g branches

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 451bc21 into main Sep 4, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-array2 branch September 4, 2026 12:10
tannevaled added a commit that referenced this pull request Sep 4, 2026
Locks in #557 (Proc/Method/UnboundMethod), #558 (Array) and #559
(BasicObject). Measured 18582 passing on the pinned corpus, up from 18354.

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