Skip to content

Advance Range MRI conformance: cover?/include?/===, step, min/max/first - #509

Merged
tannevaled merged 2 commits into
mainfrom
feat/agent-range
Aug 30, 2026
Merged

tannevaled merged 2 commits into
mainfrom
feat/agent-range

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Fixes 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). Every behavior was checked byte-for-byte against ruby -e.

What changed

  • #cover? / #include? / #member? / #=== now dispatch #<=> through the VM, so Numeric subclasses, Time and custom Comparable objects order by their own comparison rather than only the built-in numeric/string fast paths. #include? follows MRI's range_include_internal (cover logic for a linear or numeric-argument endpoint, a discrete #succ+#== walk for String/custom ranges, TypeError for beginless/endless non-numeric ranges). #cover? with a Range argument handles a generic #succ end via self.end.succ. All four require exactly one argument.
  • #member? is a true alias of #include? (shared method entry).
  • ASCII multi-character String ranges walk the full #succ sequence like MRI's rb_str_upto, so ('a'..'ab').to_a yields a..z, aa, ab (and #include? answers accordingly). Non-ASCII ranges keep the conservative walk.
  • #each stops at the inclusive end without asking it for a successor.
  • #to_a raises RangeError (not TypeError) 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-correct RangeError/TypeError at the edges.
  • #min / #max with a block reduce a non-Integer comparison result via #>/#< and raise RangeError for 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 raises ArgumentError eagerly while a zero step over a non-numeric range yields nothing; a beginless range raises ArgumentError.

Per-file ruby/spec deltas (core/range), +74 total, no regressions

file before after
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

Verified no regressions across all of core/range and a broad sweep of core/{comparable,integer,float,symbol,enumerable,kernel,numeric,string,array}.

Verification

  • go test ./... green (internal/vm ok, 99.9% overall); new range code 100% covered.
  • GOOS=wasip1 GOARCH=wasm go build ./... OK.
  • gofmt -l clean on changed files.

Remaining out-of-scope core/range/step failures (12) are the 3.4 String #+ stepping semantics and Enumerator::ArithmeticSequence, plus a couple of examples that rely on the spec-harness mock distinguishing .with(a) from .with(b) (not yet supported) — left for follow-ups.

🤖 Generated with Claude Code

tannevaled and others added 2 commits August 30, 2026 16:04
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>
@tannevaled
tannevaled merged commit 271a63d into main Aug 30, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-range branch August 30, 2026 15:48
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