Skip to content

feat(ts): translate Cow<[T]> and Wrapping<T> with their std impls - #326

Merged
kixelated merged 9 commits into
mainfrom
quest/m3/ts-cow-slices
Oct 31, 2026
Merged

kixelated merged 9 commits into
mainfrom
quest/m3/ts-cow-slices

Conversation

@kixelated

@kixelated kixelated commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Closes the quest quest/m3/ts-cow-slices.md.

Problem

Constructing a Cow<[T]> was refused (Cow<str> is already a JS string), and every Wrapping<T> was refused as an unsupported type from another crate, so none of their std impls translated. Reproduced on a scratch crate: 55 refusals, each with its span.

Approach

Cow<[T]>, a representation decided by the orchestrator, pending maintainer review:

  • The class marks the variant. An owned Cow<[T]> is its bare T[], and a borrowed one is an rt.Borrowed holding the slice. A Cow<[u8]> is an rt.ByteVec or an rt.ByteSlice, which are already distinct classes.
  • to_mut stores rt.cowIntoOwned of the Cow in the &mut's place, then lends that Vec. A borrowed one clones each item by its Clone; an owned one is returned as it is.
  • Clone of a borrowed one is a new rt.Borrowed of the same slice, so a &mut to the payload, whether stored directly, passed on as an rt.Place, or both, writes slice in place on its own Cow only. Clone of an owned one clones its Vec. Drop drops only an owned one's items. into_owned, Deref, AsRef, Borrow, Default, From (a Vec, a &Vec, a slice, an array), Vec::from, and collect (std's collects the owned Vec) are all supported. A match and a pattern read the variant by instanceof.
  • PartialEq, PartialOrd, Ord, and Hash go through the slice it derefs to, by rt.cowBy and rt.hashCow. A Cow's ne is !eq, and the slice's eq still calls each item's own ne (feat(ts): compare through a held type's own ne, as std's slices, tuples, and Arcs do #316). This covers slices, Vecs, and arrays on either side, and Cow<[u8]> against every form of bytes.
  • An owned or byte payload is the Cow value itself, so a store to it assigns the variable through a cast to the variant's type. assigned_name in mir/mod.rs now looks through that cast, so the variable is declared let; before, it was const and the store threw at runtime.
  • Cow::is_borrowed and is_owned are unstable at the pinned toolchain, so a slice's are refused as unsupported calls. The Cow<str> refusal message ("either is the same JS string") is no longer used for slices.

Wrapping<T> erases to its integer, as Reverse and NonZero do:

  • +, -, *, and -x call the integer's wrapping* runtime functions.
  • A signed / and % use the new rt.*WrappingDiv and rt.*WrappingRem, so MIN / -1 wraps and only a zero divisor panics, with std's message.
  • Shifts mask the amount by bits - 1; a constant amount is folded.
  • Every operator also translates through references and in its assign form, and !, &, |, and ^ translate too.
  • Comparison, ordering, Hash, Debug, Display, and the radix formats, Sum and Product (wrapping), Default, Clone, and derived impls of structs holding one all translate. Hashed keys look through it as they do through Reverse.

Tests, all differential against native wasm32 in the lang fixture:

  • wraps::{u8s..isizes}: 24 kinds per width. caller.ts adds every kind at every width over operands at 0, 1, either end, and around the sign bit (so MIN / -1 is hit), plus values past 2^64 for the 128-bit widths. Shift amounts are at, past, and many times past the width, including amounts above 2^24.
  • cows::slices: construction each way, a payload replaced directly and through a helper with reads through the same borrow, to_mut of a local, an element, and a field (each checking that the borrowed source is unchanged), comparisons against every sequence on either side, B-tree and hash keys, and match.
  • Tally, an item that logs its clones and drops, shows what a borrowed or owned Cow clones and drops through clone, to_mut, and into_owned.
  • cows::bytes: the same coverage for Cow<[u8]>, payload replacement included.
  • collections::own_nes: Wrapping<Picky> and Cow<[Picky]>, whose items' eq and ne disagree.
  • The reject fixture now covers a slice Cow's unstable is_borrowed in place of the old refusal of Cow<[u8]> itself.

Impact

  • Translator output: Cow<[T]> and Wrapping<T> translate where they were refused.
  • Runtime, all new exports: rt.Borrowed, rt.Cow, rt.ByteCow, rt.cowSlice, rt.byteCowSlice, rt.cowIntoOwned, rt.byteCowIntoOwned, rt.cowClone, rt.byteCowClone, rt.cowDrop, rt.cowBy, rt.hashCow, and rt.{i8,i16,i32,i64,i128,isize}Wrapping{Div,Rem}. Nothing is breaking.
  • ts/README.md documents both types, and its design notes add a line beside Cow<str>'s.

Alternatives

Representation of Cow<[T]>:

  • Untagged, a bare array as Cow<str> is a bare string: lost because to_mut could not tell a borrow apart from an owned Vec, so it would clone on every call (O(n^2) over repeated calls), and a match on the variant would be refused.
  • Tagged, an object { owned, value }: lost because it wraps every Cow, owned ones included, and unwraps it at every read.
  • Class marks variant (chosen): exact, since it clones and drops as std's does. It keeps the owned side bare, and only the borrowed side pays a wrapper.

Follow-ups

None.

Review

  • Reviewer: quest review, Codex gpt-6.1-sol, medium effort.
  • Round 1 on 479369fa: a borrowed Cow's payload replaced through &mut stored to a readonly slice. Fixed in 305e9dc3, which also covered the owned and byte variants assigning to a cast of a const.
  • Round 2 on 305e9dc3: the same payload passed to a helper as an rt.Place still wrote the readonly slice. Fixed in b1f0adf9: slice is writable, each borrowed clone gets its own wrapper, and a cast assignment target counts as a write.
  • Round 3 on ec8c6ed2: 305e9dc3's rebuilding of the whole Cow broke wrapper identity for a borrow a helper had captured. Fixed in eb6083ed, which drops that rebuilding and writes slice in place.
  • Round 4 on f8dd028e (with origin/main merged): no findings.
  • just ci passed locally on f8dd028e.

🤖 Generated with Claude Code

(written by Claude Opus 5.5)

kixelated and others added 9 commits October 30, 2026 20:31
Quest-Claim: fcba5a6fbea67914
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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/reject/census.md
#	ts/src/lower/collections.rs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A borrowed Cow's clone gets its own rt.Borrowed, whose slice a &mut writes, and a
variable assigned through a cast to its variant's type is declared with let.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
#	ts/fixtures/reject/census.md
#	ts/fixtures/reject/expected.txt
…f it shares

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
#	ts/fixtures/reject/census.md
@kixelated kixelated changed the title Cow of a slice, and Wrapping feat(ts): translate Cow<[T]> and Wrapping<T> with their std impls Oct 31, 2026
@kixelated
kixelated marked this pull request as ready for review October 31, 2026 05:09
@kixelated
kixelated merged commit 41f114e into main Oct 31, 2026
2 checks passed
@kixelated
kixelated deleted the quest/m3/ts-cow-slices branch October 31, 2026 05:10
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