Skip to content

feat(ts): translate the methods of std's ranges - #325

Merged
kixelated merged 9 commits into
mainfrom
ts/range-methods
Oct 31, 2026
Merged

kixelated merged 9 commits into
mainfrom
ts/range-methods

Conversation

@kixelated

@kixelated kixelated commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

#320 left std's ranges with gaps no quest tracked. Reproduced in a probe crate, each a refusal with its span or a runtime throw:

  • contains refused on RangeFrom, RangeTo, RangeToInclusive, RangeFull (through RangeBounds), on char and float ranges, and through RangeBounds::contains on every range type.
  • is_empty refused on char and float ranges.
  • == and Hash refused on every range type but RangeFull, and RangeInclusive::into_inner refused.
  • A Range constant was refused, so even (1u8..3).contains(&x) and {:?} of 1.5..2.0 failed to translate.
  • The by_ref char case (('a'..='z').by_ref().zip(0..3).count()) is fixed on main since feat(ts): translate start.. as an iterator, stepping as std's does past its type's maximum #320 passes rt.charStep. But a range of unsigned NonZeros, which std's Step iterates, still translated and then threw rust2-ts: stepping a range of no integer type at runtime (nth of NonZero<u8> ranges, next of start..).

Approach

  • Bounds compare as std's RangeBounds::contains does, by <= and < (a new le beside lt in rt/iter.ts). That way a NaN bound or item contains nothing, and RangeInclusive::is_empty is exhausted || !(start <= end).
  • Each range type has its own contains runtime function. RangeBounds::contains maps to the same function, RangeFull's to true, and a pair of Bounds' to rt.boundsContains.
  • Ranges are keys for ==, !=, Hash, and hash collections through eq_fn, hash_fn, and std_key. rt.rangeEq and rt.rangeHash compare and hash each bound through what the bound's type uses, and an inclusive range's exhausted flag too, as std's derived impls do.
  • Range constants translate as new rt.Range(...), as RangeFrom and RangeInclusive constants already did.
  • A range of unsigned NonZeros takes rt.nonZeroStep(int's Step, MAX). Where the integer would wrap, it stops at MAX or 1, as std's forward_overflowing and backward_overflowing do. Every remaining path to stepping a range of no integer type and stepping a wide range without its type needs a Step type with no JS value: Ipv4Addr and Ipv6Addr, which translation already refuses as a type with a span, or the unstable AsciiChar.
  • Differential fixture iters::range_methods, 1216 cases:
    • Types: each integer width at its ends and past 2^53 and 2^64, char on either side of the surrogates, f32 and f64 with NaN, the infinities, and both zeros, and NonZero of u8, u64, u128, and usize.
    • Methods: every range type's contains (inherent, through RangeBounds, and of Bound pairs), is_empty, ==, !=, Debug, Hash (also as HashSet keys), start, end, start_bound, end_bound, and into_inner.
    • States: an inclusive range is first read each way, by next, next_back, past its end until it wraps, folded until it is exhausted at its end, nth, and nth_back.
    • contains and is_empty use the number comparison only when the item's type matches the bounds' type and neither is behind a &mut. Otherwise the call stays refused with its span. Two reject cases pin this: keyed_across (a crate's cross-type PartialOrd) and place_bounds (&mut bounds).
  • Mutation check: reverting each runtime fix (NaN-aware is_empty and contains, exhausted in == and Hash, RangeToInclusive's inclusive end, NonZero saturation, Bound pairs) makes it report between 20 and 1198 differences.

clone was left to #322 (ts-iter-clone), which has since merged.

Impact

  • Public API: the translator's output. Breaking for a caller that builds a range itself: one of unsigned NonZeros takes rt.nonZeroStep(rt.u8Step, 255) and the like, added to ts/MIGRATE.md.
  • New runtime exports: rt.nonZeroStep, rt.rangeFromContains, rt.rangeToContains, rt.boundsContains, rt.rangeInclusiveIntoInner, rt.rangeEq, and rt.rangeHash.

Alternatives

  • One generic contains over rt.Bounds for every range type. Rejected: it builds two Bound objects per call, on what is often a hot path ((b'0'..=b'9').contains(&c)). Only the Bound pair goes through it.
  • Separate == and Hash functions for each range type. One function each, reading the range's fields, is less code.

Choices made without asking

  • RangeBounds::contains of a pair of Bounds is included, since it is the same trait method as the range types' own contains.
  • Round 4 of the review was the guide's design checkpoint. I judged the approach sound, since its finding needed only a narrower guard, and kept going as the brief asked (review until a round is clean). The design did not change.

Follow-ups

  • Debug of Bound: {:?} of a Bound is refused, so the fixture writes bounds as bytes.
  • Quest file: this PR planned quest/m3/ts-range-methods.md and deleted it once the work was done.

Review

Codex gpt-6.1-sol, medium effort, quest review --base origin/main, 5 rounds in one session. Reviewed head: 3ad03cf1fe9e5c44cc2cc1e587621e24e81591f0. just ci passes on that head.

(written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 9 commits October 30, 2026 19:43
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…onZeros

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated marked this pull request as ready for review October 31, 2026 04:22
@kixelated
kixelated merged commit 7eb1474 into main Oct 31, 2026
1 of 2 checks passed
@kixelated
kixelated deleted the ts/range-methods branch October 31, 2026 04:23
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