Skip to content

feat(ts): translate a slice's parts by inclusive and checked ranges and by fixed size - #279

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

kixelated merged 5 commits into
mainfrom
quest/m3/ts-slice-ranges

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Closes the quest quest/m3/ts-slice-ranges.md.

Problem

rust2-ts refused a slice's remaining parts with a span: &v[a..=b], &v[..=b], and their &mut; get and get_mut of any range; and first_chunk, last_chunk, split_last_chunk, and the _mut forms of those and split_first_chunk. The lang fixture's new case hit 63 such refusals before this change.

Approach

  • a..b, a.., ..b, and .. are still taken apart in place, as indexing already did. get and get_mut of a range now go through that same index path, into new sliceGetRange, sliceGetRangeMut, and byteSliceGetRange. These give undefined unless start <= end <= len, as std's get does.
  • Any other SliceIndex range (a..=b, ..=b, or a pair of Bounds) is passed whole, as the rt.Bounds that feat(ts): translate drains and B-tree ranges of Bound pairs and the crate's own RangeBounds #267 added for drains. collections::plain_bounds is factored out of bounds so both use one check. Two runtime functions turn the range into a start and end:
    • sliceIndexBounds panics as std's index does. For a..=b it checks the range's own fields: b >= len reports slice_index_fail(a, b, len), and an exhausted range starts at b + 1. Every other range goes through boundsRange, which already is std's into_slice_range.
    • sliceGetBounds gives the exclusive start and end without checking them. get then applies the same start <= end <= len rule, which matches std's get for each kind of range: JS numbers don't overflow at usize::MAX + 1.
  • Mutable parts of values other than bytes are feat(ts): translate a slice's exact, reverse, and mutable chunks #270's sliceView. Parts of bytes are already views.
  • first_chunk(_mut)::<N> maps to get_range(_mut)(s, 0, N), and split_first_chunk(_mut) maps to split_at(_mut)_checked(N). This deletes sliceSplitFirstChunk and byteSliceSplitFirstChunk. The last_chunk and split_last_chunk forms get small runtime functions of their own.

In this nightly, &v[a..=usize::MAX] no longer panics with a separate overflow message. It panics with slice_index_fail's range message, and the differential tests match that.

Tests

The new lang case slices::ranges covers bytes, u16, an object struct, and a [u32; 5]. It checks:

  • every pair of bounds up to one past the end, plus usize::MAX
  • get returning None for ranges that are out of range or backwards
  • inclusive ranges stepped past their end by next and exhausted by a fold
  • each Bound kind at either end
  • chunks of length 0 and of exactly the slice's length
  • writes through each _mut part landing in the source

caller.ts enumerates 3,240 targeted inputs. The differential run reports 0 differences. As a mutation check, I made the runtime index a..=b through boundsRange and then dropped the exhausted flag. The cases caught these as 360 and 60 differences.

Impact

  • The translator's output covers the shapes above. No exported API changes.
  • Runtime: adds sliceGetRange, sliceGetRangeMut, byteSliceGetRange, sliceIndexBounds, sliceGetBounds, sliceLastChunk, sliceLastChunkMut, sliceSplitLastChunk, sliceSplitLastChunkMut, byteSliceLastChunk, and byteSliceSplitLastChunk. Removes sliceSplitFirstChunk and byteSliceSplitFirstChunk; generated code calls split_at_checked's functions instead. These are internal runtime functions, so there is no MIGRATE.md entry.
  • Consumer: moq's codecs index and get byte slices by range. This also closes the m3 gap the quest names for std slices.

Alternatives

  • Taking a..=b apart in place, as exclusive ranges are, would avoid allocating a range object. I rejected it because it needs the exhausted flag and std's distinct panic messages for each source of the range. Passing the range whole reuses feat(ts): translate drains and B-tree ranges of Bound pairs and the crate's own RangeBounds #267's rt.Bounds. Inclusive ranges are rare on hot paths.
  • One runtime function per (element kind, mutability, index or get, parts or whole range) would mean about 12 functions. Instead, the whole-range path converts to a start and end and reuses the parts functions.

Follow-ups

  • [u8]::fill is still refused. quest/m3/ts-slice-edits.md already tracks it, so the fixture uses a loop instead.
  • core::range::RangeInclusive (the new range API) as a slice index still refuses with a span, as an unsupported call.

Review

🤖 Generated with Claude Code

(written by Claude Opus 5.5)

kixelated and others added 4 commits October 30, 2026 04:57
…nd by fixed size

`&v[a..=b]`, `&v[..=b]`, a pair of `Bound`s, and their `&mut`, `get` and `get_mut` of
each kind of range, and `first_chunk`, `last_chunk`, `split_first_chunk`,
`split_last_chunk`, and their `_mut` forms translate, of bytes and other values. A range
std's ranges don't take apart in place passes as the `rt.Bounds` drains read, which the
runtime checks as std's `SliceIndex` does, `a..=b` by its own fields. A mutable part of
other values is the runtime's `sliceView`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/caller.ts
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
#	ts/fixtures/lang/src/lib.rs
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
@kixelated kixelated changed the title A slice's parts by inclusive range, checked range, and fixed size feat(ts): translate a slice's parts by inclusive and checked ranges and by fixed size Oct 30, 2026
@kixelated
kixelated marked this pull request as ready for review October 30, 2026 12:50
@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 18 seconds.

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: 88a4731a-6823-4305-8524-5e78cab07782
Commits

Reviewing files that changed from the base of the PR and between 8d5602b and 12c9914.

Files selected for processing: 16
  • quest/m3/README.md
  • quest/m3/ts-slice-ranges.md
  • ts/fixtures/lang/caller.ts
  • ts/fixtures/lang/expected/core/option.ts
  • ts/fixtures/lang/expected/harness.ts
  • ts/fixtures/lang/expected/lib.ts
  • ts/fixtures/lang/expected/slices.ts
  • ts/fixtures/lang/expected/splits.ts
  • ts/fixtures/lang/src/harness.rs
  • ts/fixtures/lang/src/lib.rs
  • ts/fixtures/lang/src/slices.rs
  • ts/runtime/rt.ts
  • ts/runtime/rt/collections.ts
  • ts/src/lower/collections.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.

# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs

@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: 49cc13e.

No actionable bugs found. Reusing the bounds and slice-view machinery is a sound direction without multiplying helpers for every range shape. The separate inclusive-index checks preserve exhausted-range behavior; checked ranges reject invalid bounds, and fixed-size first/last splits preserve tuple ordering and zero-length cases. Mutable byte and non-byte results retain write-through to their source.

The differential fixtures exercise boundary pairs, usize::MAX, exhausted inclusive ranges, fixed-size chunks and mutations; the new range API remains explicitly unsupported rather than silently mislowered.

Verification limits: source/generated-fixture inspection and an independent static review only. I did not run the translator, differential/mutation tests, or CI; reported results were not independently verified.

@kixelated
kixelated merged commit aedf30f into main Oct 30, 2026
2 checks passed
@kixelated
kixelated deleted the quest/m3/ts-slice-ranges branch October 30, 2026 12:55
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