Repository navigation
Advance Range MRI conformance: cover?/include?/===, step, min/max/first - #509
Merged
Merged
Conversation
Fix a cluster of Range methods to match MRI (rbgo reports RUBY_VERSION
3.4.1, so version-gated ruby/spec examples follow the 3.4 branch):
- #cover?, #include?, #member? and #=== now dispatch #<=> through the VM,
so Numeric subclasses, Time and custom Comparable objects order by their
own comparison instead of only the built-in numeric/string fast paths.
#include? follows MRI's range_include_internal with string_use_cover=0:
cover (pure #<=>) logic for a linear (Numeric/Time) or numeric-argument
endpoint, a discrete #succ walk compared with #== for a String or custom
range, and a TypeError for a beginless/endless non-numeric range.
#cover? with a Range argument handles a generic #succ end by comparing
self.end.succ against the argument's end. All four require exactly one
argument (ArgumentError otherwise).
- #member? is a true alias of #include? (shared method entry, so
Range.instance_method(:member?) == Range.instance_method(:include?)).
- ASCII multi-character String ranges walk the full #succ sequence, as
MRI's rb_str_upto does, so ('a'..'ab').to_a yields a..z, aa, ab (and
#include? answers accordingly). Non-ASCII ranges keep the conservative
walk, since rbgo's byte-wise #succ diverges there.
- #each stops at the inclusive end without asking it for a successor, so a
range whose end has no usable #succ past itself (a Time-like object) is
enumerated without error.
- #to_a raises RangeError (not TypeError) for an endless range.
- #first, #min(n) and #max(n) coerce the count with #to_int (truncating a
Float), count up without materialising so a Bignum-bounded or endless
integer range answers quickly ((0...2**64).min(2) == [0, 1]), and raise
the MRI-correct RangeError/TypeError at the edges; exclusive beginless
#max and beginless #max(n) raise per MRI 3.4.
- #min/#max with a block reduce a non-Integer comparison result via #>/#<
(MRI's rb_cmpint) and raise RangeError for an unbounded range.
- #step: the Enumerator size is derived from the same in-range predicate the
walk uses, so a term landing just inside or outside an exclusive float
boundary is counted consistently ((1.0...55.6).step(18.2).size == 4). A
zero numeric step raises ArgumentError eagerly, while a zero step over a
bounded non-numeric range yields nothing; a beginless range raises
ArgumentError with MRI's message.
+74 ruby/spec examples across core/range (include 49->78, cover 97->109,
case_compare 30->40, step 58->66, min 26->30, max 28->32, first 8->12,
each 8->9, to_a 6->7, member 0->1), no regressions across core/range or a
broad sweep of core/{comparable,integer,float,symbol,enumerable,kernel,
numeric,string,array}. New code is 100% covered.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The merged-coverage gate (go test -coverpkg across all internal packages) measures the union of every test binary, where the range helpers my earlier commit added or reshaped had uncovered branches, dropping the total below 100.0%. This adds targeted vm_test cases and removes branches my change made unreachable: - blockCmpSign: cover the Integer zero-sign result and the non-Integer results decided by #> / #< (positive, negative, and neither). - strRangeElems: the non-ASCII #succ walk (Greek range, a backward range, and a successor that overshoots the end length) — the ASCII split had left the lexical path reached only by non-ASCII inputs no Go test used. - isASCIIStr: the non-ASCII early return (via the same non-ASCII ranges). - rangeElemsV: a custom #succ element overshooting the end, and an endless Symbol range (moving the endless RangeError into the Integer/String case so the Symbol guard stays reachable). - strIncludeRangeP: a #to_str that returns a non-String (TypeError). - stepSize / rangeStepSize / rangeStepSize's size Enumerator: nil for a non-numeric step or limit. - numericStep / numericStepEndless: their zero-step guards were made dead by the eager zero-step check; gate that eager raise to the no-block path so a zero step with a block still reaches (and is raised by) the walkers. - Remove the now-unreachable zero-step guards in stepSize and rangeStepSize (every caller rejects a zero step before the size Enumerator is built) and the redundant NaN guard (the index scan already yields 0 for a NaN estimate). Merged total returns to 100.0% (go tool cover -func). All values checked against MRI where the range is well-formed. 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.
Fixes a cluster of
Rangemethods to match MRI (rbgo reportsRUBY_VERSION3.4.1, so version-gated ruby/spec examples follow the 3.4 branch). Every behavior was checked byte-for-byte againstruby -e.What changed
#cover?/#include?/#member?/#===now dispatch#<=>through the VM, so Numeric subclasses,Timeand customComparableobjects order by their own comparison rather than only the built-in numeric/string fast paths.#include?follows MRI'srange_include_internal(cover logic for a linear or numeric-argument endpoint, a discrete#succ+#==walk for String/custom ranges,TypeErrorfor beginless/endless non-numeric ranges).#cover?with aRangeargument handles a generic#succend viaself.end.succ. All four require exactly one argument.#member?is a true alias of#include?(shared method entry).#succsequence like MRI'srb_str_upto, so('a'..'ab').to_ayieldsa..z, aa, ab(and#include?answers accordingly). Non-ASCII ranges keep the conservative walk.#eachstops at the inclusive end without asking it for a successor.#to_araisesRangeError(notTypeError) for an endless range.#first/#min(n)/#max(n)coerce the count with#to_int, count up without materialising (so(0...2**64).min(2)and endless integer ranges work), and raise the MRI-correctRangeError/TypeErrorat the edges.#min/#maxwith a block reduce a non-Integer comparison result via#>/#<and raiseRangeErrorfor an unbounded range.#step: Enumerator size is derived from the same in-range predicate the walk uses (so an exclusive term on the float boundary is counted consistently); a zero numeric step raisesArgumentErroreagerly while a zero step over a non-numeric range yields nothing; a beginless range raisesArgumentError.Per-file ruby/spec deltas (core/range), +74 total, no regressions
Verified no regressions across all of
core/rangeand a broad sweep ofcore/{comparable,integer,float,symbol,enumerable,kernel,numeric,string,array}.Verification
go test ./...green (internal/vmok, 99.9% overall); new range code 100% covered.GOOS=wasip1 GOARCH=wasm go build ./...OK.gofmt -lclean on changed files.Remaining out-of-scope
core/range/stepfailures (12) are the 3.4 String#+stepping semantics andEnumerator::ArithmeticSequence, plus a couple of examples that rely on the spec-harnessmockdistinguishing.with(a)from.with(b)(not yet supported) — left for follow-ups.🤖 Generated with Claude Code