From a6ab8dbe5e37495fcc97ac8f8a7106956bb7f251 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 21:10:18 -0700 Subject: [PATCH 1/2] chore(quest): claim quest/m3/ts-harness.md Quest-Claim: 41d91de2d6de9d75 From c247b8e7895e11feb58fcdd718863f24e7e22c86 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 21:51:22 -0700 Subject: [PATCH 2/2] test(ts): check the differential harnesses' step lists, named cases, and kept references The task harness logs each step's name on both sides, so a step list that drifts, or a name that no longer picks its step, fails the differential run. stock names its cases and its caller targets `buf::reserves` by name, as lang's does. lang's `refs` gains references kept in a tuple and read after the index or reference they were borrowed through changes. Co-Authored-By: Claude Opus 5.5 --- quest/m3/README.md | 1 - quest/m3/ts-harness.md | 20 ------------ ts/fixtures/lang/expected/refs.Held.ts | 10 ++++++ ts/fixtures/lang/expected/refs.Nest.ts | 10 ++++++ ts/fixtures/lang/expected/refs.ts | 45 ++++++++++++++++++++++++++ ts/fixtures/lang/src/refs.rs | 45 ++++++++++++++++++++++++++ ts/fixtures/stock/caller.ts | 4 +-- ts/fixtures/stock/expected/harness.ts | 27 ++++++++++++---- ts/fixtures/stock/src/harness.rs | 35 ++++++++++++-------- ts/fixtures/stock/ts.toml | 2 +- ts/fixtures/task/src/harness.rs | 10 +++--- ts/tests/task.ts | 3 +- 12 files changed, 163 insertions(+), 49 deletions(-) delete mode 100644 quest/m3/ts-harness.md create mode 100644 ts/fixtures/lang/expected/refs.Held.ts create mode 100644 ts/fixtures/lang/expected/refs.Nest.ts diff --git a/quest/m3/README.md b/quest/m3/README.md index 0d20fa142..976018fb0 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -24,7 +24,6 @@ generic std gap is planned here even when moq never calls it. - [An inclusive range folded through an adaptor](/quest/m3/ts-range-fold.md) - a `RangeInclusive` folded through `map`, `filter`, or `rev` ends in core's state, and `by_ref` of one type-checks - [Binding gaps](/quest/m3/ts-bind-gaps.md) - bound types as keys, unit returns checked, renamed dependencies - [Drop order in std's folds and iterators](/quest/m3/ts-drop-order.md) - the values that drop still refused in std's folds, zips, flattens, and collects drop as std's do -- [Checks on the differential harnesses](/quest/m3/ts-harness.md) - step lists that drift, literal op bytes, and `refs`'s coverage fail a test or get cases - [Ordering in std](/quest/m3/ts-ordering.md) - `cmp::max`/`min`, `is_sorted`, and `BinaryHeap`, and #173's cost on integers measured - [Arc's unique-access functions](/quest/m3/ts-arc-mut.md) - `Arc::get_mut`, `make_mut`, `pin`, and `as_deref` of an `Option` translate - [Debug and Display of Arc and ranges](/quest/m3/ts-fmt-wrappers.md) - `Arc` formats as `T`, and ranges as std's `Debug` diff --git a/quest/m3/ts-harness.md b/quest/m3/ts-harness.md deleted file mode 100644 index e4957132d..000000000 --- a/quest/m3/ts-harness.md +++ /dev/null @@ -1,20 +0,0 @@ -# [S] Checks on the differential harnesses - -## Goal - -The differential harnesses can't silently test less than they claim: a test fails when the -task fixture's step list in Rust (`harness.rs`) and TypeScript (`tests/task.ts`) drift -apart, when a scripted case's literal op bytes stop picking the step or case it names, and -an audit of `lang`'s `refs` case shows which of the place lowering's branches it reaches. - -## Plan - -Planned on 2026-10-28. Both lists are matched by order today, so a step added to one side -only shifts every op after it, and a script written as bytes rather than by name keeps -running whatever its bytes now pick. - -- Compare the lists by name in a test, rather than generating one from the other. -- The `refs` audit adds cases for the branches it misses, such as #148's review asked: - reborrows through a reassigned reference, and indexed borrows. - -Public API: none. diff --git a/ts/fixtures/lang/expected/refs.Held.ts b/ts/fixtures/lang/expected/refs.Held.ts new file mode 100644 index 000000000..9e550f50f --- /dev/null +++ b/ts/fixtures/lang/expected/refs.Held.ts @@ -0,0 +1,10 @@ +// Generated by rust2-ts. Do not edit. + +import type * as Nest from "./refs.Nest.ts"; + +/** A struct holding a reference, which is replaced. */ +export interface Held { + at: Nest.Nest; +} + +//# sourceMappingURL=refs.Held.ts.map diff --git a/ts/fixtures/lang/expected/refs.Nest.ts b/ts/fixtures/lang/expected/refs.Nest.ts new file mode 100644 index 000000000..b9bfc788e --- /dev/null +++ b/ts/fixtures/lang/expected/refs.Nest.ts @@ -0,0 +1,10 @@ +// Generated by rust2-ts. Do not edit. + +import type * as Pair from "./refs.Pair.ts"; + +/** A struct a reference points into. */ +export interface Nest { + pair: Pair.Pair; +} + +//# sourceMappingURL=refs.Nest.ts.map diff --git a/ts/fixtures/lang/expected/refs.ts b/ts/fixtures/lang/expected/refs.ts index eddf7610b..5a82d688d 100644 --- a/ts/fixtures/lang/expected/refs.ts +++ b/ts/fixtures/lang/expected/refs.ts @@ -7,7 +7,9 @@ import { unwrapOr$Option_u32, unwrapOr$Option_u8, unwrapOr$Option_u8_u8, unwrapO import * as Counts from "./refs.Counts.ts"; import * as Cursor from "./refs.Cursor.ts"; import type * as Fetch from "./refs.Fetch.ts"; +import type * as Held from "./refs.Held.ts"; import * as Listing from "./refs.Listing.ts"; +import type * as Nest from "./refs.Nest.ts"; import type * as Pair from "./refs.Pair.ts"; import type * as Pair2 from "./refs.Pair2.ts"; @@ -484,6 +486,7 @@ export function lent(data: rt.ByteSlice, out: rt.ByteVec): void { defaulted(data, out); taken(data, out); elements(data, out); + kept(data, out); } /** Filled in place by its default, through a `&mut` and from a closure, then reset. */ @@ -1045,6 +1048,48 @@ export function elements(data: rt.ByteSlice, out: rt.ByteVec): void { } } +/** + * References kept in a tuple, read after the index or reference they were borrowed + * through changes: each part reads what it borrowed when the tuple was made. + */ +export function kept(data: rt.ByteSlice, out: rt.ByteVec): void { + const by: rt.u8 = unwrapOr$Option_u8(rt.byteSliceFirst(data), 4); + const v: Uint8Array = Uint8Array.of(1, 2, 3, 4); + const iter: rt.Iter<[rt.Place, rt.u8]> = rt.iterZip(rt.iterSliceMut(rt.byteSliceView(rt.byteSliceFrom(v))), rt.iterSlice(rt.byteSliceView(data)), true); + while (true) { + const _13: [rt.Place, rt.u8] | rt.Done = iter.next(); + if (_13 === rt.DONE) { + break; + } + _13[0].v = _13[0].v ^ _13[1]; + } + const _20: [Nest.Nest, Nest.Nest] = [{ pair: { n: by } }, { pair: { n: by ^ 255 } }]; + const x: Nest.Nest = _20[0]; + const y: Nest.Nest = _20[1]; + let i: rt.usize = rt.usizeRem(by as rt.usize, rt.byteSliceFrom(v).length); + let r: Nest.Nest = x; + const held: Held.Held = { at: x }; + const copied: Nest.Nest = r; + const _42: rt.usize = i; + if (!(_42 < 4)) { + rt.panic("index out of bounds: the len is " + 4 + " but the index is " + _42); + } + const _44: Pair.Pair = r.pair; + const _82: Nest.Nest = held.at; + const _48: Nest.Nest = held.at; + const part = v[_42]; + i = rt.usizeRem(rt.usizeAdd(i, 1), rt.byteSliceFrom(v).length); + r = y; + held.at = y; + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(part, _44.n, y.pair.n, _82.pair.n, copied.pair.n, _48.pair.n, copied.pair.n))); + const _73: rt.u8 = part ^ _44.n; + const _77: rt.usize = i; + if (!(_77 < 4)) { + rt.panic("index out of bounds: the len is " + 4 + " but the index is " + _77); + } + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(_73, v[_77], r.pair.n, held.at.pair.n))); +} + export function seenOf(data: rt.ByteSlice): rt.u8 | undefined { return rt.byteSliceGet(data, 2); } diff --git a/ts/fixtures/lang/src/refs.rs b/ts/fixtures/lang/src/refs.rs index cc2c96508..67b69142d 100644 --- a/ts/fixtures/lang/src/refs.rs +++ b/ts/fixtures/lang/src/refs.rs @@ -352,6 +352,7 @@ fn lent(data: &[u8], out: &mut Vec) { defaulted(data, out); taken(data, out); elements(data, out); + kept(data, out); } /// moq-net's listing: a crate type's default, made in place only while there is none. @@ -663,6 +664,50 @@ fn elements(data: &[u8], out: &mut Vec) { } } +/// A struct a reference points into. +struct Nest { + pair: Pair, +} + +/// A struct holding a reference, which is replaced. +struct Held<'a> { + at: &'a Nest, +} + +/// References kept in a tuple, read after the index or reference they were borrowed +/// through changes: each part reads what it borrowed when the tuple was made. +fn kept(data: &[u8], out: &mut Vec) { + let by = data.first().copied().unwrap_or(4); + let mut v = [1u8, 2, 3, 4]; + for (x, &b) in v.iter_mut().zip(data) { + *x ^= b; + } + let (x, y) = ( + Nest { pair: Pair { n: by } }, + Nest { + pair: Pair { n: by ^ 0xff }, + }, + ); + let mut i = usize::from(by) % v.len(); + let (mut r, q) = (&x, &y); + let mut held = Held { at: &x }; + let copied = r; + let parts = (&v[i], &r.pair, &q.pair, &held.at.pair, &copied.pair, held.at, copied); + i = (i + 1) % v.len(); + r = &y; + held.at = &y; + out.extend_from_slice(&[ + *parts.0, + parts.1.n, + parts.2.n, + parts.3.n, + parts.4.n, + parts.5.pair.n, + parts.6.pair.n, + ]); + out.extend_from_slice(&[*parts.0 ^ parts.1.n, v[i], r.pair.n, held.at.pair.n]); +} + fn seen_of(data: &[u8]) -> Option { data.get(2).copied() } diff --git a/ts/fixtures/stock/caller.ts b/ts/fixtures/stock/caller.ts index 96748e5ad..50699cfd0 100644 --- a/ts/fixtures/stock/caller.ts +++ b/ts/fixtures/stock/caller.ts @@ -1,11 +1,11 @@ // The harness, with inputs it names for `differential.ts`: `buf::reserves`' edges of // `rt.BytesMut`'s limit, which only these reach, as each allocates 512 MiB. -import { cases as all, run } from "./harness.ts"; +import { op, run } from "./harness.ts"; export { run }; -const reserves = all().length - 1; +const reserves = op("buf::reserves"); /** Each edge but shared storage past the limit, which throws, read past by none and by some. */ export const cases: Uint8Array[] = [0, 1, 2].flatMap((kind) => [0, 3].map((read) => Uint8Array.of(reserves, ...new TextEncoder().encode("edge"), kind, read))); diff --git a/ts/fixtures/stock/expected/harness.ts b/ts/fixtures/stock/expected/harness.ts index c1d739567..b7b124562 100644 --- a/ts/fixtures/stock/expected/harness.ts +++ b/ts/fixtures/stock/expected/harness.ts @@ -5,25 +5,38 @@ import { bytesMut, reserves, reads, writes, edits } from "./buf.ts"; import * as Owner from "./harness.Owner.ts"; /** - * Each case, which the first byte picks modulo their count, so a case added at the end - * moves no other. + * Each case's name and what it runs. The first byte picks one modulo their count, so a + * case added at the end moves no other. */ -export function cases(): Array<(a0: rt.ByteSlice, a1: rt.ByteVec) => void> { - return [slices, made, events, random, bytesMut, reads, writes, edits, reserves]; +export function cases(): [string, (a0: rt.ByteSlice, a1: rt.ByteVec) => void][] { + return [["slices", slices] as [string, (a0: rt.ByteSlice, a1: rt.ByteVec) => void], ["made", made] as [string, (a0: rt.ByteSlice, a1: rt.ByteVec) => void], ["events", events] as [string, (a0: rt.ByteSlice, a1: rt.ByteVec) => void], ["random", random] as [string, (a0: rt.ByteSlice, a1: rt.ByteVec) => void], ["buf::bytes_mut", bytesMut] as [string, (a0: rt.ByteSlice, a1: rt.ByteVec) => void], ["buf::reads", reads] as [string, (a0: rt.ByteSlice, a1: rt.ByteVec) => void], ["buf::writes", writes] as [string, (a0: rt.ByteSlice, a1: rt.ByteVec) => void], ["buf::edits", edits] as [string, (a0: rt.ByteSlice, a1: rt.ByteVec) => void], ["buf::reserves", reserves] as [string, (a0: rt.ByteSlice, a1: rt.ByteVec) => void]]; } /** Run one case. */ export function run(input: rt.ByteSlice): rt.ByteVec { const out: rt.ByteVec = new rt.ByteVec(); if (input.length !== 0) { - const op: rt.u8 = input.data[input.start]; - const cases_: Array<(a0: rt.ByteSlice, a1: rt.ByteVec) => void> = cases(); - cases_[rt.idx(cases_, rt.usizeRem(op as rt.usize, cases_.length))](new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out); + const op_: rt.u8 = input.data[input.start]; + const cases_: [string, (a0: rt.ByteSlice, a1: rt.ByteVec) => void][] = cases(); + cases_[rt.idx(cases_, rt.usizeRem(op_ as rt.usize, cases_.length))][1](new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out); return out; } return out; } +/** The first byte that picks the case named `name`, which `caller.ts` targets by. */ +export function op(name: string): rt.u8 { + const _2: rt.usize | undefined = rt.iterPosition(rt.iterSlice(cases()), (_22: [string, (a0: rt.ByteSlice, a1: rt.ByteVec) => void]): boolean => { + return _22[0] === name; + }); + if (_2 === undefined) { + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtPad(f, name)]; + rt.panicFmt(["no case ", args2[0]]); + } else { + return _2 & 0xff; + } +} + /** * `Bytes`'s slices by every kind of range, out of bounds included, which panic as * `bytes` does, and what they compare equal to. diff --git a/ts/fixtures/stock/src/harness.rs b/ts/fixtures/stock/src/harness.rs index 61c241f83..610fdedee 100644 --- a/ts/fixtures/stock/src/harness.rs +++ b/ts/fixtures/stock/src/harness.rs @@ -7,19 +7,19 @@ use rand::RngExt; /// What a case runs on the rest of the input. type Case = fn(&[u8], &mut Vec); -/// Each case, which the first byte picks modulo their count, so a case added at the end -/// moves no other. -fn cases() -> Vec { +/// Each case's name and what it runs. The first byte picks one modulo their count, so a +/// case added at the end moves no other. +fn cases() -> Vec<(&'static str, Case)> { vec![ - slices, - made, - events, - random, - crate::buf::bytes_mut, - crate::buf::reads, - crate::buf::writes, - crate::buf::edits, - crate::buf::reserves, + ("slices", slices), + ("made", made), + ("events", events), + ("random", random), + ("buf::bytes_mut", crate::buf::bytes_mut), + ("buf::reads", crate::buf::reads), + ("buf::writes", crate::buf::writes), + ("buf::edits", crate::buf::edits), + ("buf::reserves", crate::buf::reserves), ] } @@ -30,10 +30,19 @@ pub fn run(input: &[u8]) -> Vec { return out; }; let cases = cases(); - cases[usize::from(op) % cases.len()](data, &mut out); + let (_, case) = cases[usize::from(op) % cases.len()]; + case(data, &mut out); out } +/// The first byte that picks the case named `name`, which `caller.ts` targets by. +pub fn op(name: &str) -> u8 { + match cases().iter().position(|&(n, _)| n == name) { + Some(i) => i as u8, + None => panic!("no case {name}"), + } +} + /// `Bytes`'s slices by every kind of range, out of bounds included, which panic as /// `bytes` does, and what they compare equal to. fn slices(data: &[u8], out: &mut Vec) { diff --git a/ts/fixtures/stock/ts.toml b/ts/fixtures/stock/ts.toml index dfa8bacfd..bd9cdfacf 100644 --- a/ts/fixtures/stock/ts.toml +++ b/ts/fixtures/stock/ts.toml @@ -1,2 +1,2 @@ package = "stock-fixture" -roots = ["harness::run"] +roots = ["harness::run", "harness::op"] diff --git a/ts/fixtures/task/src/harness.rs b/ts/fixtures/task/src/harness.rs index ce1d6be46..4d45f77a2 100644 --- a/ts/fixtures/task/src/harness.rs +++ b/ts/fixtures/task/src/harness.rs @@ -1,9 +1,10 @@ //! Runs a script of executor steps and returns the log: the native side of the //! differential test, which `tests/task.ts` mirrors step for step on the translation. //! -//! Each step is two bytes, an op and its argument. Channels and tasks are picked by the -//! argument, modulo how many there are; a step with nothing to pick does nothing. Once -//! the script ends, every task drops in order, then every channel. +//! Each step is two bytes, an op and its argument, and logs its name, so the two sides +//! differ wherever their lists do. Channels and tasks are picked by the argument, modulo +//! how many there are; a step with nothing to pick does nothing. Once the script ends, +//! every task drops in order, then every channel. use crate::drops; use crate::exec::Executor; @@ -19,7 +20,8 @@ pub fn run(input: &[u8]) -> Vec { let steps = steps(); for step in input.chunks(2) { let (op, arg) = (step[0], step.get(1).copied().unwrap_or(0)); - let (_, run_step) = steps[usize::from(op) % steps.len()]; + let (name, run_step) = steps[usize::from(op) % steps.len()]; + log.note(format!("step {name}")); run_step(&log, &mut exec, &mut channels, arg); } for t in 0..exec.len() { diff --git a/ts/tests/task.ts b/ts/tests/task.ts index c3ea6854e..5cf1e2f3b 100644 --- a/ts/tests/task.ts +++ b/ts/tests/task.ts @@ -319,7 +319,8 @@ export function harness(f: Fixture): (input: Uint8Array) => Bytes { const exec = new Executor(note, f.Pending); const r: Run = { f, log, note, exec, channels: [] }; for (let i = 0; i < input.length; i += 2) { - const [, step] = steps[input[i] % steps.length]; + const [name, step] = steps[input[i] % steps.length]; + note(`step ${name}`); step(r, input[i + 1] ?? 0); } for (let t = 0; t < exec.tasks.length; t++) exec.drop(t);