Repository navigation
Array: advance MRI-4.0 conformance (live iteration, coercion, inspect, flatten, sort) - #558
Merged
Merged
Conversation
…_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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)
each/map/select/sum/to_h/take_while/sort_by/map!/each_indexnow 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"). Fixedeachentirely (4→11) and the shared example across the family.keep_if/select!/reject!/delete_ifcompact 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 aBasicObject-safe fallback.#to_sis a true shared-record alias (5 inspect examples).#to_ary/#to_intargument coercion: set operations (|,&,-,union,intersection,difference,intersect?),Array.new/#initialize,#to_h,#concat,#replace,#transpose,#flatten(incl. a#to_aryreached through#method_missing), and count args forfirst/last/pop/shift/drop/permutation.#flattenflattens#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.Array.new/#initializecopy via#to_ary, size via#to_int, arity 0..2, break-safe incremental build, and#initializeis private (14→18 / 15→22).#deletechecks frozen only on an actual removal;#rindexrechecks 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/andgo test ./...green.GOOS=wasip1 GOARCH=wasm go build ./...OK;gofmt -lclean.internal/vm/array_residuals_wave10_test.gocovers 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#hashelement-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