Skip to content

feat(ts): translate slice windows and chunks, Vec::drain, and the order of bytes - #259

Merged
kixelated merged 6 commits into
mainfrom
quest/m3/ts-slice-gaps
Oct 30, 2026
Merged

kixelated merged 6 commits into
mainfrom
quest/m3/ts-slice-gaps

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

rust2-ts refused a slice's windows and chunks, Vec::drain, and <, <=, >, >=, and cmp of byte arrays, slices, and vectors (quest m3/ts-slice-gaps, filed by #252). moq reaches the drains: kio's task.rs, and moq-net's model/track.rs and priority.rs. zlib-rs reaches windows.

Approach

  • windows and chunks map to rt.PartsIter in rt/iter.ts, which yields byte views or array copies, as the slice is. Each part comes from its index, so it reads from both ends, has a len, and skips without making a part. The last chunk is the remainder, a window larger than the slice yields nothing, and a size of 0 panics with std's message. They are runtime iterators because std's iterators are rt.Iters by design (README, "Iterators are the runtime's lazy classes"): std's Windows and Chunks types don't translate.
  • Vec::drain maps to rt.vecDrain and rt.byteVecDrain, which port std's Drain. At creation the vector keeps only what precedes the range. When the drain drops, it drops what it still holds front to back, then moves the tail back. A forgotten drain therefore leaves the vector what came before the range and leaks the rest, as std's does. Because its drop does something JS can see, vec::Drain now always drops, even when its items don't. It is a port and not std's MIR because std's Drain reads elements out and moves the tail through raw pointers. Replacing it is recorded in quest/m3/ts-unsafe.md, as feat(ts): translate cmp::max and min, is_sorted, and BinaryHeap #252 did for BinaryHeap.
  • Byte arrays, slices, and vectors order through rt.cmpBytes: by their bytes, then by length. It reads whichever form a key or operand holds (a Uint8Array, an rt.ByteSlice, or an rt.ByteVec), so it covers direct comparisons, Ord's max/min/clamp, B-tree keys, heap items, sorts, and tuples holding owned or borrowed bytes. slice::range's check, shared with VecDeque::drain, moved into one sliceRange.
  • Vec::drain, VecDeque::drain, and a B-tree's range now refuse, with a span, a range argument the runtime can't read: a tuple of Bounds or a crate's own RangeBounds. The runtime's bounds used to read those as unbounded, which silently drained or ranged over everything (found in review).

Proof: the lang fixture adds slices::parts, slices::drains, and orders::bytes, run differentially against native wasm32. They cover drop order, nth/nth_back skips, last, count, forgotten drains, out-of-range and backwards ranges (panic messages compared), chunk remainders, windows larger than the slice, and prefixes of different lengths. Mutating the runtime showed the cases catch these mistakes: reversing the drop order, restoring the tail eagerly so a forgotten drain no longer leaks, yielding a window past the end, dropping the remainder chunk, and dropping the length tiebreak. One mutation survived: changing sliceChunks's zero-size panic message. In that case the byte chunks(0) runs first and panics with its own copy of the same message.

Census against main at 9df3118: moq-net 1495 to 1506, model 420 to 431, kio 153 to 152.

  • Gone: the three drains (kio's task.rs, and moq-net's model/track.rs and priority.rs) and zlib-rs's windows.
  • New: 15 refusals of a B-tree range over a (Bound, Bound) tuple, in moq-net's ledger.rs, model/track.rs, and model/expiry.rs, and in rangemap's map.rs and inclusive_map.rs. Before this PR those calls passed the tuple to a runtime that reads it as unbounded, in a call tsc rejects. Now translation refuses them with a span. quest/m3/ts-bound-ranges.md translates them, and moq's model needs it.

Impact

  • Translator output: these calls translate instead of refusing. New runtime exports: sliceWindows, sliceChunks, byteSliceWindows, byteSliceChunks, vecDrain, byteVecDrain, and cmpBytes. Nothing breaks, so there is no MIGRATE.md entry.
  • New refusals: a (Bound, Bound) or custom-RangeBounds argument to VecDeque::drain and B-tree range/range_mut, which translated wrongly before.

Choices made without asking

  • Scope: windows, chunks, Vec::drain, and ordering of all three byte forms, keys included. The quest named byte arrays only; slices and vectors share the same comparator. == and hashing of byte arrays stay refused, for a follow-up.
  • rt.cmpBytes dispatches on the byte form at run time instead of statically, because one type, such as [u8; 2], is a Uint8Array when owned and an rt.ByteSlice behind a reference, and peel loses which. It costs two instanceof checks per comparison. No bench was added: nothing performance-sensitive changed, and no moq hot path compares bytes.

Alternatives

  • Lowering std's Windows, Chunks, and Drain from MIR. That would mean translating std's iterator structs, against the iterator design. For Drain it would also mean the raw pointers ts-unsafe covers.
  • Restoring the drain's tail eagerly and special-casing mem::forget. That would be wrong for every other leak, such as Box::leak.

Follow-ups

  • quest/m3/ts-byte-keys.md: == of byte arrays, and byte arrays, slices, and vectors as hash keys.
  • quest/m3/ts-slice-chunks.md: chunks_exact, rchunks, and the mutable chunks (zlib-rs and uuid reach these).
  • quest/m3/ts-bound-ranges.md: drains and B-tree ranges of a (Bound, Bound).
  • quest/m3/ts-unsafe.md: Vec::drain from std's MIR once raw pointers into a Vec translate.
  • Not recorded: adding an instance merges identical generic instances under the alphabetically first name, so it renames another module's import. A fixture closure here first renamed floats.ts's mapOr$Option_Ordering_u8_floats_closure0, and I changed the fixture to avoid it.

Review

Codex (quest review), gpt-6.1-sol, medium effort. Reviewed head e508d45, round 3: no findings. This head merges main at 9df3118 (#257, #260, #261), with hand-resolved conflicts in rt/collections.ts's imports (ByteSlice/ByteVec beside #261's Hashing) and in the lang harness (main's cases first). #261's hash_fn still refuses byte containers, so this branch's routing of them through std_key, for ordering, intercepts no hash mapping.

Round 2, on 9dd9b65: no findings.

Round 1, on fe31040:

Local checks: just ci passed on e508d45. Since then main gained only #264, which changes quests alone and merges without conflicts.

(written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 5 commits October 30, 2026 00:02
Quest-Claim: b05241a68bd19e74
A slice's `windows` and `chunks` map to `rt.PartsIter`, of byte views or
array copies as the slice is, panicking on a size of 0 as std's do.
`Vec::drain` maps to a runtime port that leaves the vector what precedes
the range until the drain drops, then drops what it holds and moves the
tail back, so a forgotten drain leaks the rest as std's does; its `Drain`
therefore always drops. Byte arrays, slices, and vectors order through
`rt.cmpBytes`, which reads each byte form a key or operand holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
`Vec::drain`, `VecDeque::drain`, and a B-tree's `range` passed a tuple of
`Bound`s, or a crate's own `RangeBounds`, to the runtime's `bounds`, which
read it as unbounded. Each now refuses it with its span.

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 feat(ts): translate slice windows and chunks, Vec::drain, and byte array order feat(ts): translate slice windows and chunks, Vec::drain, and the order of bytes Oct 30, 2026
@kixelated
kixelated marked this pull request as ready for review October 30, 2026 08:19
@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 27 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: ab3f2e98-c364-44aa-8c00-0bc2122413dc
Commits

Reviewing files that changed from the base of the PR and between 3b74e5f and 9dd9b65.

Files selected for processing: 27
  • quest/m3/README.md
  • quest/m3/ts-bound-ranges.md
  • quest/m3/ts-byte-keys.md
  • quest/m3/ts-slice-chunks.md
  • quest/m3/ts-slice-gaps.md
  • quest/m3/ts-unsafe.md
  • ts/README.md
  • ts/fixtures/lang/expected/core/cmp.ts
  • ts/fixtures/lang/expected/core/option.ts
  • ts/fixtures/lang/expected/harness.ts
  • ts/fixtures/lang/expected/orders.ts
  • ts/fixtures/lang/expected/slices.Item.ts
  • ts/fixtures/lang/expected/slices.ts
  • ts/fixtures/lang/src/harness.rs
  • ts/fixtures/lang/src/lib.rs
  • ts/fixtures/lang/src/orders.rs
  • ts/fixtures/lang/src/slices.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/collections.ts
  • ts/runtime/rt/iter.ts
  • ts/src/lower/collections.rs
  • ts/src/lower/drop.rs
  • ts/src/lower/library.rs
  • ts/src/lower/mir/call.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: 9dd9b657a8d75e2b64a9a4d4ad6c157cb96ce1db.

No actionable findings from this review. The parts iterator's front/back accounting preserves chunk remainders and empty oversized windows, and byte ordering respects view offsets and logical lengths. The drain keeps only the source prefix until disposal, drops the remaining range before restoring the tail, and is marked as dropping even for Copy elements. The earlier local range-bounds finding is addressed by explicit refusal instead of treating unsupported bounds as unbounded.

Direction: runtime iterators and one byte comparator fit the existing representations; deferred drain restoration is simpler and more faithful than special-casing mem::forget. The differential cases cover partial consumption, reverse/skipping consumers, destructor order and forgotten drains. One small coverage improvement: exercise zero-sized non-byte chunks independently, so the earlier byte-chunks panic cannot mask its panic-message mutation.

Verification limits: static GitHub review, including an independent drain ownership/drop pass. I did not execute the translator, native/TS differential suite, mutation tests, census or just ci; the reported results are not independently verified.

(Written by OpenAI)

# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
#	ts/fixtures/lang/src/lib.rs
#	ts/fixtures/reject/census.md
#	ts/runtime/rt/collections.ts
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