Skip to content

Range: construction #<=> dispatch, #step #coerce, MRI 4.0.6 conformance - #571

Merged
tannevaled merged 1 commit into
mainfrom
feat/agent-range2
Sep 6, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/agent-range2

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Summary

Advances core/range MRI 4.0.6 conformance: 586 → 591 passing ruby/spec examples (+5), with a bonus +1 in core/string/element_reference, and no regressions anywhere in a focused baseline-vs-branch sweep of core/{range,comparable,kernel,integer,float,string,…} + language.

Two fixes, both in internal/vm/range_edges.go (helpers) with minimal call-site edits:

1. Construction-time begin <=> end dispatch (newRange)

MRI's range_init compares the endpoints when constructing a range — a dispatch several specs observe through a mocked #<=>. rbgo skipped it on the literal path and duplicated it inline on Range.new/#initialize. Now a single newRange helper:

  • Range.new / Range#initialize are strict (matching MRI): an unordered pair (#<=> nil and not #==) is ArgumentError: bad value for range. It also mirrors MRI's Object#<=>, which answers 0 for two #== objects with no ordering of their own — so Range.new(//, //) builds, exactly as under MRI. Two Integer bounds take MRI's Fixnum fast path (no #<=>).
  • The range literal performs the comparison for its dispatch but keeps rbgo's tolerance for an unordered literal, so a discarded (1.."z") still builds (no spec asserts the literal raises; internal tests rely on it).

This closes: Range#cover? / #=== "uses the range element's #<=>" (the construction call is the 2nd #<=>), Range#each "raises TypeError if the first element does not respond to #succ" (the range constructs, then #each raises), and Range#step "return nil … if the first element is not of compatible type".

2. Range#step coerces a non-numeric step (coerceRangeStep)

(1..3).step(obj) where obj.coerce(1) => [1, 2] now walks 1, 3 (MRI 4.0). The coercion lives in numericStep, which only Range#step reaches with a non-numeric step — Integer#step/Float#step reject it in numericStepArgCheck first — matching MRI, where Range#step coerces but Numeric#step does not.

Remaining core/range failures (not fixed)

All are shim limitations (the ruby/spec scoring shim's mock #<=> does not dispatch on argument, so MRI fails them identically under the same shim) or an out-of-scope VM bug:

  • each/reverse_each/minmax mock-ordering cases: shim mock should_receive(:<=>).with(x) shadows .with(y); MRI+shim fails them too.
  • case_compare (nil..nil) === 1: the shim's ShouldProxy has no #===, so it falls to Object#===→==; MRI+shim fails identically.
  • bsearch "infinity bounds": the assertion is … .should != inf, and rbgo compiles a != b as !(a == b) (ignoring a user-defined #!=) — a VM/compiler bug outside the range scope.

Verification

  • core/range: 586 → 591; focused sweep (742 files) shows only positive deltas, zero regressions.
  • go test ./... green; GOOS=js GOARCH=wasm build OK; gofmt -l clean.
  • Coverage: newRange, coerceRangeStep, numericStep all 100.0%; edited Range.new/#initialize lines covered; no shadowed dead code (edited in place). New branches covered by internal/vm/range_residuals_wave13_test.go.

🤖 Generated with Claude Code

…nce)

Range construction now performs MRI's construction-time `begin <=> end`
comparison — a dispatch several specs observe through a mocked `#<=>`:

- `Range.new` / `Range#initialize` are strict, mirroring MRI's range_init:
  an unordered pair (`#<=>` nil and not `#==`) is "bad value for range"; a
  pair that is `#==` without its own `#<=>` (two identical Regexps, whose
  only comparison is Object's identity default) still builds, matching MRI's
  Object#<=> answering 0 for `#==` objects.
- The range literal performs the comparison for its dispatch but keeps
  rbgo's tolerance for an unordered literal, so a discarded `(1.."z")` still
  builds (no spec asserts the literal raises; internal tests rely on it).

This closes the "uses the range element's #<=>" count on Range#cover? / #===
(the construction call is the second `#<=>`), the each/step "first element"
TypeError-after-construction cases, and Range#step's non-numeric #size case.

Range#step now coerces a non-numeric step against the range begin via MRI's
numeric coercion — (1..3).step(obj) with obj.coerce(1) => [1, 2] walks 1, 3 —
in numericStep (only reached by Range#step; Numeric#step rejects such a step
first), matching MRI where Range#step coerces but Numeric#step does not.

core/range: 586 -> 591 (case_compare +1, cover +1, each +1, step +2), no
regressions.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 7b059c8 into main Sep 6, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-range2 branch September 6, 2026 09:28
tannevaled added a commit that referenced this pull request Sep 6, 2026
Locks in #569 (Hash), #570 (Encoding subsystem), #571 (Range) and #572 (the
!= opcode dispatching a user-defined #!=). Measured 18790 passing on the pinned
corpus, up from 18767.

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