Skip to content

feat(ts): type a generic type's runs of its parameter as each instance holds them - #375

Merged
moq-bot[bot] merged 7 commits into
mainfrom
quest/m3/ts-generic-byte-fields
Nov 1, 2026
Merged

moq-bot[bot] merged 7 commits into
mainfrom
quest/m3/ts-generic-byte-fields

Conversation

@kixelated

@kixelated kixelated commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A generic type's definition typed a [T; N], [T] (behind a reference or a Box), Vec<T>, or Cow<[T]> field as T[] or rt.Cow<T>. Its u8 instance stores a Uint8Array, an rt.ByteSlice, an rt.ByteVec, or an rt.ByteCow there. Translation succeeded, but tsc rejected every instance's code that made or read one. Reproduced by adding Runs<u8> to the lang fixture: 16 tsc errors on main.

Approach

Follows #371's rt.Ptr:

  • The definition types each run of an open parameter as a runtime union that also holds the byte form: rt.ArrayOf<T> (T[] | Uint8Array), rt.SliceOf<T> (T[] | ByteSlice), rt.VecOf<T> (T[] | ByteVec), and rt.CowOf<T> (Cow<T> | ByteCow).
  • defined_field_ty records these fields beside rt.Ptr ones (ptr_fields is now instanced_fields). held_field casts each instance's read to what that instance holds, and field_value casts a constructed value where a function type in the field takes one.
  • A write through a &mut to such a field, such as mem::take(&mut runs.list), assigns through the cast. assignable now looks through as, which TS accepts as an assignment target.
  • A generic class's [Symbol.dispose] glue types its fields with no byte form and casts each read. Only an instance whose parameter drops runs the glue, so that parameter is never u8.

Differential test (generics::runs in the lang fixture, against native wasm32): Runs, Run, and Fixed at u8 and at u16. Each is made, written through its fields and through &muts to them, copied, compared, and printed. Runs and Run of a type that logs its drops prove the glue's drop order.

Impact

  • Output: a generic type's field holding a run of its parameter is now an rt.ArrayOf<T>, rt.SliceOf<T>, rt.VecOf<T>, or rt.CowOf<T>, not T[] or rt.Cow<T>. A hand-written caller reading one casts it to its instance's form. This is breaking; there is a MIGRATE.md entry.
  • Runtime: adds the exported types rt.ArrayOf, rt.SliceOf, rt.VecOf, and rt.CowOf.
  • In the fixtures, only strs.Counted<T>'s items: &[T] changes, to rt.SliceOf<T>.

Alternatives

  • Refuse the u8 instance, as generic_erasure refuses an Option of a parameter that can be undefined. Smaller, but it would refuse any generic container instantiated at bytes, which is common in moq's codecs.
  • Inline unions (T[] | Uint8Array) instead of named runtime types. These read the same, but the translator could not tell them apart from other unions with bytes in them, such as Option<&[u8]>, which is rt.ByteSlice | undefined. Named types match rt.Ptr.
  • One alias for all runs (T[] | Uint8Array | ByteSlice | ByteVec). Fewer names, but Cow<[T]> still needs its own, and the separate names say which Rust type each field is.

The four runtime names are an API choice I made without asking. Rename them in review if you prefer others.

Merge with #376

  • Dropped the reject fixture's mistyped case (feat(ts): type-check the whole output before a translation succeeds #376). It wrote through this exact gap, so translation now succeeds. I found no other typing gap that translation still leaves to tsc. The [T], Vec<T>, HashMap<_, Vec<T>>, Rc<RefCell<Vec<T>>>, and Box<dyn Fn(&[T])> fields, and the cases in ts-ptr-replace-unread and ts-box-places, all type-check. So check.rs's "a bug in rust2-ts" path has no fixture until the next gap turns up. The binding check (bind/wrong.toml) still covers the same tsc run.

Follow-ups

  • New quest quest/m3/ts-shared-slices.md: Rc<[T]> and Arc<[T]> made from a Vec or a slice are refused (unsupported call to <Rc<[T]> as From<Vec<T>>>::from). That is a separate gap from this mismatch.
  • quest/m3/ts-ptr-replace-unread.md and quest/m3/ts-box-places.md look stale: their example cases translate and type-check on this branch, the latter likely since fix(ts): a &mut into a Box is the place holding the box #369. They need a /quest-audit pass; I left them untouched to keep this PR to one problem.

Review

mechanical fix by codex / Codex: Merged base, preserved both fixture sets and field-read changes, regenerated snapshots; 11 fixture tests pass, task waker differential also fails on base. (head 85740cf)

🤖 Generated with Claude Code

kixelated and others added 6 commits October 31, 2026 22:57
…e holds them

A generic type's `[T; N]`, `[T]`, `Vec<T>`, and `Cow<[T]>` are `rt.ArrayOf<T>`,
`rt.SliceOf<T>`, `rt.VecOf<T>`, and `rt.CowOf<T>`, which also hold the byte form of
the `u8` instance, and each instance's read casts the field to what it holds, as
`rt.Ptr` fields are.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…olds its runs

Also covers a generic type JS copies and an enum of runs, at bytes, at u16, and at a
type that drops, and records Rc and Arc of a slice as a follow-up quest.

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

# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…yte-fields

Drops the reject fixture's `mistyped` case, which #376 wrote through the typing gap
this branch closes: no other typing gap that translation leaves to `tsc` is known.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title A generic type's bytes at u8 feat(ts): type a generic type's runs of its parameter as each instance holds them Nov 1, 2026
@kixelated
kixelated marked this pull request as ready for review November 1, 2026 07:31
@coderabbitai

coderabbitai Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

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: 7e158846-ffc1-4749-89dc-19944b7addc6
  • 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

@kixelated
kixelated enabled auto-merge (squash) November 1, 2026 13:01
@moq-bot
moq-bot Bot disabled auto-merge November 1, 2026 13:02
@moq-bot moq-bot Bot removed the quest:merge label Nov 1, 2026
@moq-bot

moq-bot Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

quest land removed this pull request from the queue: Conflicts with the base: ts/fixtures/lang/expected/harness.ts, ts/fixtures/lang/src/harness.rs, ts/fixtures/reject/census.md, ts/fixtures/reject/mistyped.txt, ts/fixtures/reject/src/lib.rs, ts/src/lower/drop.rs
.

@kixelated

Copy link
Copy Markdown
Collaborator Author

Maintainer decision, 2026-11-01: keep the four names rt.ArrayOf, rt.SliceOf, rt.VecOf, and rt.CowOf.

(written by Claude Opus 5.5)

@moq-bot
moq-bot Bot enabled auto-merge (squash) November 1, 2026 13:17
@moq-bot moq-bot Bot added the quest:merge label Nov 1, 2026
@moq-bot
moq-bot Bot merged commit 98bef24 into main Nov 1, 2026
1 check passed
@moq-bot
moq-bot Bot deleted the quest/m3/ts-generic-byte-fields branch November 1, 2026 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant