Skip to content

Range + Comparable: advance MRI 4.0.6 conformance - #539

Merged
tannevaled merged 5 commits into
mainfrom
feat/agent-rangecmp
Aug 31, 2026
Merged

tannevaled merged 5 commits into
mainfrom
feat/agent-rangecmp

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Advances MRI 4.0.6 conformance of the Range + Comparable clusters
(core/range/, core/comparable/). Measured against ruby/spec and verified
byte-for-byte against MRI 4.0.6.

Range

  • == / eql? / hash: compare by endpoint value (dispatching #== /
    #eql? and mixing both bounds + exclude_end? into the hash) rather than by
    object identity, so custom Comparable endpoints, Range subclasses and endless
    ranges compare/hash like MRI (via message send).
  • #inspect: (nil..nil).inspect is now "nil..nil" (MRI's range_inspect
    rule); one-sided unbounded forms and #to_s are unchanged.
  • #initialize: added the private native initializer behind Range.new —
    arity 2..3, comparable-endpoints check, FrozenError on an already-constructed
    Range, fills a Range.allocate'd instance.
  • #count: returns Float::INFINITY for any unbounded range.
  • #step: a non-Integer step over a String range advances by #+ (MRI 4.0),
    e.g. ("A".."AAA").step("A"); without a block a numeric range returns an
    Enumerator::ArithmeticSequence (String range stays a plain Enumerator).
  • #%: Range#step's alias, producing an ArithmeticSequence whose #inspect
    reads ((1..10).%(2)); the % operator on a Range now dispatches the method.
  • Enumerator::ArithmeticSequence#inspect/#to_s: MRI format
    (((b..e).step(s)) / ((b..e).%(s)), (b.step(e, s))).
  • #reverse_each: supports beginless Integer ranges (descend from the end,
    bounded via take), Symbol and generic #succ ranges; its Enumerator #size
    names the end's class and is Infinity for a beginless Integer end.
  • #entries is now a true alias of #to_a; rangeElemsV names the
    offending endpoint's class (fixes Range#to_set's message).

Comparable

  • #== follows rb_cmpint: nil is unequal, a zero (incl. 0.0) result is
    equal, and a non-nil non-comparison result (a String) raises ArgumentError.

Spec deltas (core/range + core/comparable)

~28 more examples pass. Remaining failures are almost entirely blocked by an
mspec mock .with() argument-matching bug (out of the Range/Comparable scope):
x <=> y returns the wrong stubbed value, which sinks the mock-driven each /
minmax / reverse_each / step cases. One bsearch case depends on repeated eval
in a single example (eval-cache infra), and one step case needs the #coerce
step protocol.

Gates

  • go test ./internal/vm/ green; go test ./... green
  • MERGED coverage tool cover -func | tail -1 = 100.0%
  • GOOS=wasip1 GOARCH=wasm go build ./... OK; gofmt -l clean
  • FLOOR untouched

Adds internal/vm/range_comparable_residuals_test.go; updates
range_edges_test.go and step_test.go from pre-4.0 to MRI 4.0.6 expectations.

🤖 Generated with Claude Code

tannevaled and others added 5 commits August 31, 2026 17:20
Range#== / #eql? / #hash now compare by endpoint value (dispatching #==
/#eql? and mixing the two bounds + exclude_end? into the hash) rather than
by object identity, so custom Comparable endpoints, Range subclasses and
endless ranges compare and hash like MRI. Range#inspect renders (nil..nil)
as "nil..nil" while keeping (1..) and (..1) unchanged, matching MRI's
range_inspect rule (to_s is unchanged).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…mpint ==

- Range#initialize: private native initializer behind Range.new — arity 2..3,
  comparable-endpoints check, FrozenError on an already-constructed (frozen)
  Range, fills a Range.allocate'd instance.
- Range#count returns Float::INFINITY for any unbounded range (nil begin or
  end), even a String/Float bound whose #size is nil.
- Range#step: a non-Integer step over a String range advances by #+ (MRI 4.0),
  e.g. ("A".."AAA").step("A") => "A","AA","AAA"; an incompatible step
  raises TypeError via String#+.
- Range#entries is now a true alias of #to_a (shared method object).
- rangeElemsV names the offending endpoint's class ("can't iterate from
  NilClass") rather than its value, matching MRI (fixes Range#to_set message).
- Comparable#== follows rb_cmpint: nil is unequal, a zero (incl. 0.0) result is
  equal, and a non-nil non-comparison result (a String) raises ArgumentError.

Adds internal/vm/range_comparable_residuals_test.go.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- Range#reverse_each: supports beginless Integer ranges (descends from the end
  forever; bounded via take), Symbol and generic #succ ranges (via rangeElemsV),
  and its Enumerator #size names the END's class for a non-countable begin,
  returns Infinity for a beginless Integer end (MRI 4.0).
- Range#step without a block returns an Enumerator::ArithmeticSequence for a
  numeric range (Numeric begin, or beginless with Numeric end) and a plain
  Enumerator for a String range, matching MRI 2.6+.
- Range#% is Range#step's alias, producing an ArithmeticSequence whose #inspect
  reads ((1..10).%(2)); the % operator on a Range now dispatches the method.
- Enumerator::ArithmeticSequence#inspect/#to_s follow MRI: ((b..e).step(s)) /
  ((b..e).%(s)) for a Range receiver, (b.step(e, s)) for a Numeric receiver.
- Updated range_edges_test.go and step_test.go, which encoded pre-4.0 behavior
  (beginless reverse_each raising; blockless numeric step returning a plain
  Enumerator), to the MRI 4.0.6 results.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…rd case

The String-only #+ walk always compares two Strings, so rangeCmpV never
reports incomparability; drop the dead guard and add a backward-range test
(( 'D'..'A' ).step('A') => []) covering the overshoot return. Keeps the
changed function at 100% coverage.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 0843580 into main Aug 31, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-rangecmp branch August 31, 2026 22:25
tannevaled added a commit that referenced this pull request Sep 1, 2026
Locks in #535 (String methods: index/casecmp/chomp/gsub/scan/tr/... ~+110),
#536 (IO/StringIO reader+writer), #538 (Array#[]= auto-grow + mutators),
#539 (Range + Comparable), #540 (Struct + Data), #541 (Time). Measured
17983 passing on the pinned corpus, up from 17654.

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