Repository navigation
Range: construction #<=> dispatch, #step #coerce, MRI 4.0.6 conformance - #571
Merged
Merged
Conversation
…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>
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.
Summary
Advances
core/rangeMRI 4.0.6 conformance: 586 → 591 passing ruby/spec examples (+5), with a bonus +1 incore/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 <=> enddispatch (newRange)MRI's
range_initcompares 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 onRange.new/#initialize. Now a singlenewRangehelper:Range.new/Range#initializeare strict (matching MRI): an unordered pair (#<=>nil and not#==) isArgumentError: bad value for range. It also mirrors MRI'sObject#<=>, which answers0for two#==objects with no ordering of their own — soRange.new(//, //)builds, exactly as under MRI. TwoIntegerbounds take MRI's Fixnum fast path (no#<=>).(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#eachraises), andRange#step"return nil … if the first element is not of compatible type".2.
Range#stepcoerces a non-numeric step (coerceRangeStep)(1..3).step(obj)whereobj.coerce(1) => [1, 2]now walks1, 3(MRI 4.0). The coercion lives innumericStep, which onlyRange#stepreaches with a non-numeric step —Integer#step/Float#stepreject it innumericStepArgCheckfirst — matching MRI, whereRange#stepcoerces butNumeric#stepdoes 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/minmaxmock-ordering cases: shim mockshould_receive(:<=>).with(x)shadows.with(y); MRI+shim fails them too.case_compare(nil..nil) === 1: the shim'sShouldProxyhas no#===, so it falls toObject#===→==; MRI+shim fails identically.bsearch"infinity bounds": the assertion is… .should != inf, and rbgo compilesa != bas!(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=wasmbuild OK;gofmt -lclean.newRange,coerceRangeStep,numericStepall 100.0%; editedRange.new/#initializelines covered; no shadowed dead code (edited in place). New branches covered byinternal/vm/range_residuals_wave13_test.go.🤖 Generated with Claude Code