Skip to content

Array & Array#pack: advance MRI 4.0.6 conformance (element_reference ArithmeticSequence, sum Kahan, pack U/P/p/buffer, residuals) - #567

Merged
tannevaled merged 5 commits into
mainfrom
feat/agent-array3
Sep 5, 2026
Merged

tannevaled merged 5 commits into
mainfrom
feat/agent-array3

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Advances MRI 4.0.6 conformance for the core/array residual cluster, clearing the entire core/array/pack backlog and a range of core/array method residuals. All changes are pure-Go (CGO=0) and confined to internal/vm/builtins.go, internal/vm/array_edges.go, and internal/vm/pack.go.

Array (internal/vm/builtins.go, array_edges.go)

  • Array#[] with an Enumerator::ArithmeticSequence (a[(m..n).step(k)]): positive/negative steps, endless/beginless ranges, exclusive endpoints, negative indices, the begin-past-end nil result, and MRI's RangeError when |step| > 1 and the declared span reaches past the array — verified byte-for-byte against MRI. (element_reference 56 → 69 pass.)
  • Array#[] also accepts a user subclass of Range; a Range passed with a length is a TypeError; an index/length beyond the Fixnum range raises RangeError instead of silently overflowing.
  • Array#sum uses MRI's exact-then-Kahan folding: integer/bignum/rational elements accumulate exactly, the first Float switches to Kahan–Babuška–Neumaier compensated summation ([floats].sum is exact where reduce(:+) drifts), a non-numeric element falls back to #+, and NaN/Infinity follow MRI's rules.
  • Array#index is a full #find_index (value / block / no-arg Enumerator forms) and a true alias of #find_index (shared method record).
  • Array#length is a true alias of #size; #clear / #map reject stray positional arguments; Array.allocate returns a fully-formed empty Array and takes no arguments.
  • Array#permutation without a block returns a lazy Enumerator that re-reads self when iterated (so a later mutation is seen) and reports the descending-factorial #size.
  • Block-less #bsearch / #bsearch_index / #rindex Enumerators report an unknown (nil) size.

Array#pack / String#unpack (internal/vm/pack.go) — pack backlog cleared (35 → 0)

  • U packs the full MRI extended-UTF-8 range (1..6 bytes, past U+10FFFF), coerces via #to_int, and raises RangeError/TypeError per MRI.
  • P/p (pointer): a native-width word backed by a process registry so pack/unpack round-trips ("hello" → P → P5 → "hello"); nil is a null pointer; too few bytes on unpack yields no element.
  • a/A/Z pack a nil argument as an empty (padded) string rather than raising.
  • Array#pack :buffer option: directs bytes into an existing String (mutated and returned), packing from its end with @ repositioning absolutely; TypeError/FrozenError guards; encoding preserved.

Verification

  • core/array/* + core/array/pack/*: 2615 pass, 37 fail, 6 error (down from ~102 fail+error). Every remaining failure is a spec-harness fidelity artifact (e.g. before-block ivars such as @never_passed/@value_to_return not propagating across sibling/nested describes — fill, the "size increasing during iteration" shared examples) or requires touching out-of-scope shared code (Array #hash, the #eql?/#hash-mock set operations, arith.go operators, the Enumerable prelude, the shared checkArrayType coercion); rbgo's own behaviour there matches MRI in direct tests.
  • go test ./internal/vm/ green; go test ./... green; GOOS=wasip1 GOARCH=wasm go build ./... OK; gofmt -l empty.
  • New/changed functions are exercised by internal/vm/array_residuals_wave12_test.go (every expectation checked against MRI 4.0.6); the changed named helpers report 100% coverage with no shadowed dead code.

🤖 Generated with Claude Code

tannevaled and others added 5 commits September 5, 2026 21:29
…uence, aliases, arity, enumerator size)

core/array residuals wave 12:

- Array#[] can be sliced with an Enumerator::ArithmeticSequence
  (a[(m..n).step(k)]): positive/negative steps, endless/beginless ranges,
  exclusive endpoints, negative indices, the begin-past-end nil result, and
  MRI's RangeError guard when |step| > 1 and the declared span reaches past
  the array. Verified byte-for-byte against MRI 4.0.6 (element_reference
  56 -> 69 pass, 0 fail/error).
- Array#[] accepts a user subclass of Range; a Range passed with a length is
  a TypeError; an index/length beyond the Fixnum range raises RangeError
  (arrayArefLong) instead of silently overflowing.
- Array#length is a true alias of Array#size (shared method record).
- Array#clear / Array#map reject stray positional arguments (ArgumentError).
- Array.allocate returns a fully-formed empty Array and takes no arguments.
- Block-less Array#bsearch / #bsearch_index / #rindex Enumerators report an
  unknown (nil) size, matching MRI.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
More core/array residuals (wave 12):

- Array#sum uses MRI's exact-then-Kahan folding: integer/bignum/rational
  elements accumulate exactly, the first Float switches to Kahan–Babuška–
  Neumaier compensated summation ([floats].sum is exact where reduce(:+)
  drifts), a non-numeric element falls back to #+, and NaN/Infinity follow
  MRI's rules. (sum 12 -> 13 pass.)
- Array#index is a full #find_index (value / block / no-arg Enumerator forms)
  and a true alias of #find_index (shared method record, so
  Array.instance_method(:index) == Array.instance_method(:find_index)).
- Array#permutation without a block returns a lazy Enumerator that re-reads
  self when iterated (so a later mutation is seen) and reports the
  descending-factorial #size. (permutation 15 -> 16 pass.)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Clears the entire core/array/pack conformance backlog (35 fail+error -> 0):

- 'U' (Unicode) packs the full MRI extended-UTF-8 range (1..6 bytes, past
  U+10FFFF where Go's own encoder clamps), coerces the argument via #to_int
  (TypeError if it does not return an Integer), and raises RangeError for a
  negative codepoint or one above 0x7FFFFFFF. (pack/u 35 -> 43, +8.)
- 'P'/'p' (pointer): a native-width word standing in for a pointer to the
  argument string, backed by a process registry so pack/unpack round-trips
  ("hello" -> P -> P5 -> "hello"); nil is a null pointer. (pack/p 2 -> 14, +12.)
- 'a'/'A'/'Z' pack a nil argument as an empty string (space/NUL padded) rather
  than raising. (pack/a +6, pack/z +3.)
- Array#pack :buffer option: directs the bytes into an existing String
  (mutated and returned), packing from its current end with @ repositioning
  absolutely; TypeError for a non-String buffer, FrozenError for a frozen one,
  and the buffer's encoding is preserved. (pack/buffer 2 -> 8, +6.)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…r for |step|>1)

Match MRI: a[(-9..).step(1)] resolves the begin to a still-negative index and
returns nil (like a[-9..]), rather than clamping to 0; with |step| > 1 the same
out-of-range begin raises RangeError (a[(-9..).step(2)]). Adds the covering tests
and the Array.allocate subclass / permutation(k>n) enumerator cases.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
String#unpack("P"/"p") with fewer bytes than a pointer produces no element
("".unpack("P") == []), matching MRI, instead of a nil. Adds the covering tests
(short data, and a 'P' count clamped to the string length).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit b35dc2a into main Sep 5, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-array3 branch September 5, 2026 21:04
tannevaled added a commit that referenced this pull request Sep 5, 2026
Locks in #565 (MatchData / regexp-engine \u escapes + duplicate/non-ASCII
capture names), #566 (Method/UnboundMethod) and #567 (Array + pack). Measured
18767 passing on the pinned corpus, up from 18650.

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