Skip to content

feat(ts): translate drains and B-tree ranges of Bound pairs and the crate's own RangeBounds - #267

Merged
kixelated merged 11 commits into
mainfrom
quest/m3/ts-bound-ranges
Oct 30, 2026
Merged

kixelated merged 11 commits into
mainfrom
quest/m3/ts-bound-ranges

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

rust2-ts refused a range given as a (Bound<T>, Bound<T>) pair or as a crate's own RangeBounds wherever Vec::drain, VecDeque::drain, or a B-tree's range or range_mut takes one (#259 added the refusal). moq's model census had 15 of these refusals, and moq-net's whole-crate census had 18. They come from moq-net's ledger.rs, model/track.rs, and model/expiry.rs, and from rangemap, which ranges its BTreeMaps by (Bound<&Wrapper>, Bound<&Wrapper>).

Moved from m3 to m2 under ts-model (first commit). The quest sat in m3 as a generic std gap. But the model's census, which ts-model measures its children by, needs it now, so it is ts-model work. This PR finishes the quest, so the second commit deletes it again.

Approach

  • Translator (collections::bounds): every drain and B-tree range goes through one path.
    • std's range types and a pair of Bounds pass as they are, since a pair's JS value already holds its two rt.RangeBounds.
    • Any other RangeBounds<T> becomes rt.boundsOf(range, start_bound, end_bound). T comes from the callee's own R: RangeBounds<T> clause, and the two methods resolve through fn_value, so the crate's own impl, generic or not, translates as its other methods do.
    • A generic instance in a census decides per instance.
  • Runtime (rt/collections.ts):
    • bounds returns start_bound and then end_bound for every form. A crate's own pair of bounds is computed only when read.
    • An empty B-tree reads neither, as std's range does not.
    • A B-tree's range now handles an excluded start. It checks the two ends as std's search_tree_for_bifurcation does: equal excluded ends with ==, then start > end with >, each panicking with std's message. For a target holding the crate's own type, the translator passes that type's PartialEq::eq and PartialOrd::gt (its own override, or partial_cmp). A target of std types alone checks by the map's Ord, which matches std's == and > with no side effects.
    • sliceRange now follows try_into_slice_range step by step: the end first, then the start, with Excluded(start) >= end panicking. Panic messages come from std's slice_index_fail, including its fallback that names the end.
  • What is still refused: a range that itself drops, at every drain and B-tree range, with the existing "passing R, which drops, to ..." message. The reject fixture's Spent range shows it for Vec::drain, VecDeque::drain, and BTreeMap::range. It is listed in the drop-order quest.

Census

Measured with just ts census at the pinned moq revision, on this branch before it merged main's later commits (head 5dc4bac), against main at #259:

census refusals before after range refusals before after
model 431 419 12 0
moq-net 1506 1491 15 0
kio 152 152 0 0

The brief's "15" for the model counted lines in its census file: 3 table rows and 12 refusals. On head 486f363, which includes main through #265, the totals are 411, 1482, and 144; the rest of that movement comes from #262, #263, and #265. No range refusal is left in any of the three, and this PR adds no other refusal.

Tests

  • New lang case bounds::bounds, run differentially against native wasm32. Named cases cover all 9 pairs of bound kinds, 36 value pairs around and past 4 items, and 0 items, so ranges are empty, backwards, equal, excluded on both ends at one value, and out of bounds. Panics are compared by message.
    • Vec drains of items that log their drops, of bytes, and of words, by pairs of Bound<usize> and Bound<&usize>.
    • A crate's own generic RangeBounds that logs when it is asked for each bound. The log shows it is asked before the drain yields or drops anything, and not at all for an empty B-tree.
    • VecDeque drains whose items wrap around the buffer.
    • BTreeMap::range and range_mut and BTreeSet::range, including through a generic fn<R: RangeBounds<u8>>.
    • Keys of the crate's own with a reversed Ord, ranged by (Bound<&Key>, Bound<&Key>) as rangemap ranges, and the same keys held in (u8, Key) tuples. Key logs each == and partial_cmp, so the test shows which comparisons std makes of the ends.
  • The case clears its drop log on entry. Native traps on a panic and is re-instantiated for the next case, but the translated module keeps its thread-local log, so without the clear a case after a panic would see the panicking case's log.

Impact

  • Translator output: drains and B-tree ranges of a Bound pair or a crate's own RangeBounds now translate instead of being refused.
  • Runtime: rt.boundsOf is new. rt.Bounds also accepts a Bound pair and a function returning one. rangeCheck is now exported from rt.ts. Nothing breaks.

Alternatives

  • Sending tuples through boundsOf too would be fully uniform, but it costs a closure and an array on every call on moq's ledger path. A tuple's start_bound and end_bound are pure, so passing the pair directly is not observably different.
  • Calling the crate's own start_bound/end_bound eagerly at the call site would be simpler, but it would ask an empty B-tree for its bounds, which std does not.

Choices made without asking

  • The quest is deleted rather than left in m2, since nothing is left of it. The m3 → m2 move is the first commit, as asked.
  • A pair of Bounds passes as its own value instead of through boundsOf (see Alternatives).

Follow-ups

  • A B-tree's drop order (new). Found while writing the fixture: at the pinned nightly, std drops a whole BTreeMap node by node in postorder (drop_tree), but the runtime drops in key order. So a map of 12 or more entries that drop logs a separator key at a different point. This is a silent divergence, not a refusal, and the model may reach it through B-trees of values that drop.
  • The drop-order quest gains a bullet for a range that drops.

Review

Local review with quest review: Codex, gpt-6.1-sol, medium effort. Reviewed head 5d5f2f7: no findings.

  • Round 1 (5dc4bac): a crate's own range that drops was accepted by the B-tree and VecDeque paths without dropping it. Fixed: bounds refuses it at every call site, covered by the reject fixture.
  • Round 2 (13fdb13): the B-tree checked equal excluded ends with Ord::cmp, but std uses PartialEq::eq and >. Fixed: the crate's own eq and gt are passed, and Key logs its comparisons in the differential test.
  • Round 3 (cd413d9): keys wrapped in std types (Option<Key>, tuples) skipped those callbacks. Fixed: any owned type in the target passes them, with a (u8, Key) differential case.
  • Round 4 (486f363): no findings. This was the guide's design checkpoint; the approach held, so I continued without a rethink.
  • Round 5 (8f693b1): merge of origin/main (feat(ts): translate a slice's exact, reverse, and mutable chunks #270), with a hand-resolved doc conflict in the lang fixture. No findings.
  • Round 6 (5d5f2f7): merge of origin/main (feat(ts): translate fmt::Write #269), with a hand-resolved case-list conflict in the lang harness. No findings.

just ci passes locally on 5d5f2f7.

🤖 Generated with Claude Code

(written by Claude Opus 5.5)

kixelated and others added 10 commits October 30, 2026 01:56
… census needs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rate's own RangeBounds

The runtime's Bounds reads a (Bound, Bound) as the pair it is, and any other
RangeBounds through rt.boundsOf, which calls its start_bound and then its
end_bound where std's does: not at all for an empty B-tree. B-tree ranges
panic on equal excluded ends, and slice::range checks an excluded start as
std's does, with std's messages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…est reads as a message

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…akes one

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>
…td does

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…wn by its comparisons

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Ranges of Bounds feat(ts): translate drains and B-tree ranges of Bound pairs and the crate's own RangeBounds Oct 30, 2026
@kixelated
kixelated marked this pull request as ready for review October 30, 2026 10:47
@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 36 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2f47b4c7-be7e-4503-9a7c-15eb0c625ad8
Commits

Reviewing files that changed from the base of the PR and between c8c7cbf and 5d5f2f7.

Files selected for processing: 24
  • quest/m3/README.md
  • quest/m3/ts-bound-ranges.md
  • quest/m3/ts-btree-drop.md
  • quest/m3/ts-drop-order.md
  • ts/README.md
  • ts/fixtures/lang/caller.ts
  • ts/fixtures/lang/expected/bounds.Asked.ts
  • ts/fixtures/lang/expected/bounds.Item.ts
  • ts/fixtures/lang/expected/bounds.Key.ts
  • ts/fixtures/lang/expected/bounds.ts
  • ts/fixtures/lang/expected/core/option.ts
  • ts/fixtures/lang/expected/harness.ts
  • ts/fixtures/lang/expected/lib.ts
  • ts/fixtures/lang/src/bounds.rs
  • ts/fixtures/lang/src/harness.rs
  • ts/fixtures/lang/src/lib.rs
  • ts/fixtures/reject/census.md
  • ts/fixtures/reject/expected.txt
  • ts/fixtures/reject/src/lib.rs
  • ts/fixtures/reject/ts.toml
  • ts/runtime/rt.ts
  • ts/runtime/rt/collections.ts
  • ts/src/lower/collections.rs
  • ts/src/lower/library.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 8f693b143b929878949aeeb4c26e1a887e59e5ad.

No actionable bugs found in the changed areas. Centralizing range lowering while deferring custom bound evaluation preserves the empty-tree and callback-order distinctions without eagerly evaluating every range. Excluded endpoints and custom comparison callbacks—including wrapped keys—are handled coherently, and refusing drop-bearing ranges avoids silently losing their destructors. The differential cases meaningfully exercise endpoint combinations, borrowed bounds and drain/drop behavior. The separately documented whole-B-tree drop-order gap is not introduced by this change.

Verification: independent static review of lowering, runtime range checks, generated output and fixtures. I did not run CI, differential tests or census measurements; reported results were not independently verified.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated merged commit 520e0d9 into main Oct 30, 2026
2 checks passed
@kixelated
kixelated deleted the quest/m3/ts-bound-ranges branch October 30, 2026 10:53
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