diff --git a/quest/m2/README.md b/quest/m2/README.md index 743dee7de..024118fea 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -18,7 +18,6 @@ is not a goal, and types must be solid at the API boundary ([design](/ts/README. ## Required -- [Async and drop spike](/quest/m2/ts-async-spike.md) - generators from THIR against MIR state machines, deciding async, drop points, and the lowering - [MIR lowering spike](/quest/m2/ts-mir-spike.md) - the whole lowering ported to MIR on two fixtures decides whether the translator moves - [Fail the TS benchmark when Chrome hangs](/quest/m2/ts-bench-chrome-timeout.md) - a hung renderer fails the run loudly - [A/B routes in the TS benchmark](/quest/m2/ts-bench-routes.md) - `--route name=dir` loads another translation for reproducible A/B runs @@ -27,8 +26,8 @@ is not a goal, and types must be solid at the API boundary ([design](/ts/README. - [Cheaper byte slices](/quest/m2/ts-byte-views.md) - splitting a `&[u8]` stops making a `Uint8Array` view per split - [Wide-integer fast paths](/quest/m2/ts-wide-fast.md) - a fused `from_halves` and an unboxed limb product, kept only if measured faster - [Error-path workload](/quest/m2/ts-bench-error-path.md) - the benchmark prices `?` on a frame split across reads -- [Drop in the TypeScript translator](/quest/m2/ts-drop.md) - a `Drop` type's `drop()` runs where Rust drops it -- [Async in the TypeScript translator](/quest/m2/ts-async.md) - async code translates with Rust's lazy futures +- [Drop in the TypeScript translator](/quest/m2/ts-drop.md) - every value that drops in moq-net and kio drops where Rust drops it +- [Async in the TypeScript translator](/quest/m2/ts-async.md) - moq-net's and kio's async code translates with Rust's lazy futures - [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values - [Collections](/quest/m2/ts-collections.md) - maps, sets, and queues keep Rust's key semantics - [Tree-shaking spike](/quest/m2/ts-shake-spike.md) - classes, free functions, or free functions behind root facades, measured on speed, shaken size, and caller code diff --git a/quest/m2/ts-async-spike.md b/quest/m2/ts-async-spike.md deleted file mode 100644 index 2f9af8444..000000000 --- a/quest/m2/ts-async-spike.md +++ /dev/null @@ -1,58 +0,0 @@ -# [L] Async and drop lowering spike - -## Goal - -A report picks how [rust2-ts](/ts/README.md) translates async code and where it -reads drop points, from two prototypes run on the same kio-shaped channel: -JS generators lowered from THIR, and state machines lowered from MIR after the -coroutine transform. The report also decides whether the whole lowering moves -from THIR to MIR, and rewrites [Drop](/quest/m2/ts-drop.md) and -[Async](/quest/m2/ts-async.md) to the winner. - -## Plan - -Decided with the maintainer on 2026-10-23: prototype both, and measure one -spike for async and drop together, since both hinge on THIR versus MIR. - -Research (2026-10-23): - -- Nearly all of moq's async is `poll_*` functions behind one-line - `kio::wait(|w| self.poll_x(w)).await` wrappers. The hard parts are kio's - internals: `Waiter` holds `Arc`, `WaiterList` holds `Weak` and - frees slots when `Weak::upgrade` fails, plus `Mutex`, atomics, and a - thread-local coop budget. No `select!`, `join!`, or tokio in kio or the model. -- Generators: rustc desugars `.await` before THIR into a poll loop around - `yield`, which maps to `cx = yield` and `gen.next(cx)`. Laziness is free, JS - objects do not move so `Pin` is moot, and `gen.return()` runs `finally` and - `using` disposals. Gaps: the first `next()` drops its argument, an unstarted - generator's `return()` runs no `finally` (its captured arguments must still - drop), and polling after completion must panic. Output stays readable. -- MIR state machines: rustc's own transform, with its drop shim giving exact - drops on cancel, and the fastest shape. The output needs a structurizer and - reads poorly, and lowering MIR means mapping `Pin::new_unchecked` and raw - pointers. -- Eager `async`/`await` (rust-js's approach) is out: it breaks laziness, poll - order, and cancel-by-drop. -- A microbenchmark of a two-level await handoff measured a hand-written state - machine at 75 ns, generators at about 180 ns, and `async`/`await` at about - 95 ns in Node 24. Generators still sustain millions of wakeups per second. - -Prototype shape: - -- Translate a small kio-like `Producer`/`Consumer`, `Waiter`/`WaiterList`, - `kio::wait`, and one `async fn` consumer loop. Each prototype gets only the - `Arc`/`Weak` counting, `Drop`, and `Mutex` it needs. -- A deterministic FIFO executor in both Rust and TS logs every poll, wake, and - drop; the differential test requires identical traces. Cases: a never-polled - future has no effects, a future dropped mid-await drops in Rust's order, - dropping an unstarted future drops its arguments, and polling after `Ready` - panics. -- Executor integration: woken tasks run in a batch from one `queueMicrotask`; - the coop budget yields to a macrotask; promises appear only at the edge. -- Measure on the [TS benchmark](/ts/README.md#bench) against hand-written TS - and moq's WASM: handoff cost at await depths 1, 4, and 16, plus output size. - -Land the harness and the winning prototype's tests; record the loser's numbers -in the report and delete its code. - -Public API: none until Drop and Async land. diff --git a/quest/m2/ts-async.md b/quest/m2/ts-async.md index c6335565f..0dd1180c1 100644 --- a/quest/m2/ts-async.md +++ b/quest/m2/ts-async.md @@ -1,19 +1,38 @@ -# [L] Async in the TypeScript translator +# [M] Async in the TypeScript translator ## Goal -[rust2-ts](/ts/README.md) translates `async fn`, `.await`, and the `Future` and `Waker` -machinery kio is built on, keeping Rust's lazy futures: nothing runs until polled. -Differential tests against native Rust prove the same poll order and results. +[rust2-ts](/ts/README.md) translates the async code in moq-net's model and kio, keeping +Rust's lazy futures: nothing runs until polled, and dropping a future cancels it. +Differential tests against native Rust prove the same polls, wakes, drops, and results. ## Plan -Decided with the maintainer on 2026-10-23: async must keep Rust's lazy futures, so -an eager `async`/`await` mapping, as rust-js makes, is out. A future is a state -machine its executor polls; JS promises can sit at the edge, for callers. - -The [spike](/quest/m2/ts-async-spike.md) picks between generators and MIR state -machines; rewrite this plan to its report before starting. +Decided by the async spike ([#16](https://github.com/kixelated/rust2/pull/16)) on +2026-10-24: an async body is a generator lowered from THIR, not a state machine lowered +from MIR. MIR's were about twice as fast per await level, but would need a second +lowering of everything an async body can hold, and read as basic blocks. What landed +there: an `async fn` returns `rt.future(body, drop, ...upvars)`, whose body is a +module-level `function*` of what it captured (a generator function made per call costs +V8 about 25 times as much), `.await` is a poll loop around `yield`, and `rt.spawn` runs +a future from a microtask as a promise. The task fixture's trace test covers a +kio-shaped channel at await depths 1, 4, and 16. + +What moq-net still needs, by its census at the pinned revision: + +- `Poll`'s helpers (about 270 calls: `is_ready`, `is_pending`, `map`, `map_ok`, + `map_err`), `pin!` (47), `poll_fn` (51 with its `poll`), and `Box::pin` (17, a boxed + future that binds to hand-written TS at the boundary, per the + [model](/quest/m2/ts-model.md)). +- An async block that captures part of a variable (47), or borrows a number mutably + (4); async closures. +- kio's own API, reached across crates (`Pending`, `Consumer::poll`, + `Waiter::poll_future`, `Tasks`), with the atomics, `OnceLock`, and coop budget behind + it, which the model quest brings along. +- The edge: `rt.spawn`'s shape for callers, and cancelling a spawned task. + +If a profile of moq's client finds await overhead, `yield*` between translated futures +would skip each level's `poll`; measure it on the [TS benchmark](/ts/README.md#bench). Decided with the maintainer on 2026-10-24: the async spike ([#16](https://github.com/kixelated/rust2/pull/16)) merges with generators from @@ -30,6 +49,5 @@ Public API: the translator's output for async code. ## Required -- [Async and drop spike](/quest/m2/ts-async-spike.md) - the lowering this builds on - [MIR lowering spike](/quest/m2/ts-mir-spike.md) - rewrite to the spike's report before starting -- [Drop in the TypeScript translator](/quest/m2/ts-drop.md) - a dropped future drops its state +- [Drop in the TypeScript translator](/quest/m2/ts-drop.md) - async bodies hold the values that drop it still refuses diff --git a/quest/m2/ts-drop.md b/quest/m2/ts-drop.md index edf7f1a8a..2f118f21f 100644 --- a/quest/m2/ts-drop.md +++ b/quest/m2/ts-drop.md @@ -2,20 +2,48 @@ ## Goal -[rust2-ts](/ts/README.md) translates types that implement `Drop`, which it refuses -today: each value's `drop()` runs where Rust drops it, in Rust's drop order, including -on early return, `?`, and panic, proven by differential tests against native Rust. +[rust2-ts](/ts/README.md) translates every value that drops in moq-net's model and kio: +each `drop()` runs where Rust drops it, in Rust's drop order, including on early +return, `?`, panic, and a cancelled future, proven by differential tests against +native Rust. ## Plan -Decided with the maintainer on 2026-10-23: `Drop` becomes an explicit `drop()`, -exposed as `[Symbol.dispose]` for JS callers. kio's producers close their channel -when the last one drops, so moq-net's model needs this first. +Decided by the async spike ([#16](https://github.com/kixelated/rust2/pull/16)) on +2026-10-24: drop points come from the THIR lowering's own scopes, not MIR. What landed +there: a class that drops gets `[Symbol.dispose]` as drop glue (its `Drop` impl, then +each field); a variable drops in a `finally` at the end of its scope; a move leaves +`undefined` behind, so a variable moved on some paths drops on the others. The task +fixture's trace test proves it on kio's waiters, wakers, and channels. -THIR does not say where drops happen; MIR does, after drop elaboration. The -[spike](/quest/m2/ts-async-spike.md) decides whether to read drop points from MIR -beside the THIR lowering or move the lowering itself; rewrite this plan to its -report before starting. +What still fails translation, each with its span (the reject fixture has one of each), +ranked by the census of moq-net at the pinned revision (kio's has 28 in all): + +- Dropping a value whose drop no `[Symbol.dispose]` carries (657): a `Result`, an + `Option` or `Poll` of one, an enum or tuple holding one, a `Vec` of one. Each needs + glue of its own, switching on the variant. +- A method taking `self` by value that drops (155), which can't leave `this` undefined. +- A temporary that drops, unless a call or binding takes it whole (280): borrowed, a + field or index of one, a `match`, `if let`, `let`, or let chain that moves only part + of it, and a guarded arm that binds one. Rust drops it at the end of its statement, or + of the `match`. +- A `for` loop over values that drop (60): each at the end of its iteration, and the + rest on a `break`. +- An iterator that owns a value that drops (55): a `Vec` of one moved into it, or an + adaptor over items that drop. An `rt.Iter` drops nothing it skips, consumes, or still + holds when it is dropped. +- A std mapping that may discard an operand that drops (19): `bool::then_some`, + `Option::ok_or`, `map_or`, and `unwrap_or`, and `Result::ok`. Each refuses any operand + that drops today, even one it always keeps, such as `ok_or`'s option: `keeps_operands` + in `library.rs` should name the operands each mapping may discard instead. +- Struct update syntax from a value that drops (16), a closure capturing one (13), a + generic type that implements `Drop` (11), a parameter that drops bound by a pattern, + a rest pattern that moves values that drop out of a place, and an array repeat of a + value that drops. +- A value that drops, moved ahead of an `.await` in the same call (3) or in the place + an assignment writes it to, which a cancel would leave in a temporary no scope drops, + and an `Arc` or `Mutex` holding a borrow of a value that drops, which JS would drop + as the value itself. Decided with the maintainer on 2026-10-24: the async spike ([#16](https://github.com/kixelated/rust2/pull/16)) merges with drop points from @@ -25,9 +53,13 @@ too. Also in scope, found since: a temporary that drops and is held across an `.await` is owned instead of refused (3 refusal sites), which needs declarations at scope level. -Public API: the translator's output for `Drop` types. +Watch the cost: each move of a value that drops writes `undefined` to where it was, and +each scope holding one becomes a `try`/`finally`. A pass that proves a variable moved on +every path, or on none, would drop most of both; measure it on the +[TS benchmark](/ts/README.md#bench) before adding it. + +Public API: the translator's output for values that drop. ## Required -- [Async and drop spike](/quest/m2/ts-async-spike.md) - where drop points come from - [MIR lowering spike](/quest/m2/ts-mir-spike.md) - rewrite to the spike's report before starting diff --git a/quest/m2/ts-mir-spike.md b/quest/m2/ts-mir-spike.md index 2b12d38a8..bae266c32 100644 --- a/quest/m2/ts-mir-spike.md +++ b/quest/m2/ts-mir-spike.md @@ -33,7 +33,3 @@ exception would skip drops that THIR's `finally` blocks run. That its output rea basic blocks no longer counts against it. Public API: none unless the translator moves; then its whole output. - -## Required - -- [Async and drop spike](/quest/m2/ts-async-spike.md) - the task fixture, its trace test, and the THIR lowering this compares against diff --git a/quest/m2/ts-shared.md b/quest/m2/ts-shared.md index 3e98205a1..4045e3622 100644 --- a/quest/m2/ts-shared.md +++ b/quest/m2/ts-shared.md @@ -14,6 +14,12 @@ Decided with the maintainer on 2026-10-23: deterministic reference counts in `rt.ts`, not GC-backed `WeakRef`. kio's `WaiterList` frees slots when `Weak::upgrade` fails and reads `weak_count`, so drop timing is observable. +The async spike ([#16](https://github.com/kixelated/rust2/pull/16)) landed what its +kio-shaped fixture needs, in `rt/task.ts`: `rt.Arc` counts strong references and +drops its value with the last, `rt.Weak` upgrades while one is left, and `rt.Mutex` +is its own guard. Left: `Rc`, `weak_count` (a dropped `Weak` counts nothing yet), +`RwLock`, atomics, `OnceLock`, `Cell`, and `RefCell`. + - `Send` and `Sync` bounds have no meaning in JS; accept them. - Keep Rust's failure modes where they are defined: a `RefCell` double borrow panics. A re-entrant `Mutex` lock on one thread is unspecified in Rust; pick diff --git a/ts/Cargo.lock b/ts/Cargo.lock index 77315fae0..2744d27e9 100644 --- a/ts/Cargo.lock +++ b/ts/Cargo.lock @@ -222,6 +222,10 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "task-fixture" +version = "0.0.0" + [[package]] name = "thiserror" version = "2.0.21" diff --git a/ts/README.md b/ts/README.md index fe6efc7e0..7c6f6565c 100644 --- a/ts/README.md +++ b/ts/README.md @@ -45,7 +45,8 @@ it finds; translation still fails on any refusal. Each Rust module with translated items becomes a file (`lib.ts`, `coding/varint.ts`), with a `.ts.map` source map into the Rust it came from. `rt.ts` beside them is the -runtime: what std does that JS does not, with iterators in `rt/iter.ts`. +runtime: what std does that JS does not, with iterators in `rt/iter.ts`, and ownership +and async in `rt/task.ts`. | Rust | TypeScript | | --- | --- | @@ -55,7 +56,7 @@ runtime: what std does that JS does not, with iterators in `rt/iter.ts`. | `struct` | a class; methods take `this`, trait methods included | | `enum` without data | a union of strings: `"Lite01" \| "Lite02"`, or `never` without variants | | `enum` | a union of objects tagged by variant, with a companion object of functions | -| `Option` | `T \| undefined`; an `Option` of `()` or an `Option` is refused | +| `Option` | `T \| undefined`; refused if `T` can already be `undefined`, as `()` and `Option` can | | `Result` | `rt.Result`: `{ ok: true, value }` or `{ ok: false, error }` | | tuple, `[T; N]`, `&[T]`, `Vec` | an array, `Uint8Array` for bytes, `rt.ByteVec` for `Vec` | | `&T`, `&mut T`, `Box` | the value: a `&mut` must point at an object JS can change in place | @@ -63,6 +64,10 @@ runtime: what std does that JS does not, with iterators in `rt/iter.ts`. | `a..b`, `a..=b` | `rt.Range`, `rt.RangeInclusive`, which are iterators too | | closure | an arrow function | | constant | its value, inlined as Rust inlines it | +| a type that drops | a class with `[Symbol.dispose]`: its `Drop` impl, then its fields | +| `Arc`, `Weak`, `Mutex` | `rt.Arc`, counted; `rt.Weak`; `rt.Mutex`, its own guard | +| `async fn` | a function returning `rt.Future`, around a generator | +| `Poll`, `Waker`, `Context` | `T \| typeof rt.Pending`, refused if `T` can already be pending; `rt.Waker`, which is also the context | `usize` is 32 bits, as on wasm32, so it is exact in a `number`. Arithmetic is checked as a debug build checks it, so translated code panics where native code would: it never @@ -109,6 +114,19 @@ generic class holds a method per instance, each declaring the instance it takes array, or an `Option` or `Result` holding one, is copied where Rust copies it, since JS would share the object. +A value drops where Rust drops it, if dropping it does something JS can see: a `Drop` +impl, an `Arc`'s count, or a waker. A variable drops at the end of its scope through a +`finally`, so an early return, `?`, a panic, or a cancelled future drops it too, and a +move leaves `undefined` where it was, which drops as nothing. + +An `async fn` returns a future without running anything: `rt.future(body, drop, +...upvars)`, whose body is a module-level generator of what the function captured. Each +`.await` polls in a loop around `yield`. A poll after the future returned panics, and +dropping it cancels it: unpolled, it drops what it captured by value; suspended, its +generator returns through the `finally` blocks that drop its locals and captures. +`rt.spawn(future)` runs one as a promise, polling woken tasks in a batch from a +microtask and yielding to a macrotask after 128 polls. + A file's items, and a class's or companion's methods, follow the Rust source, whatever order translation reaches them in, so adding a root moves nothing it does not reach. What a macro makes sits where the macro is called. The instances of one function sit @@ -154,9 +172,11 @@ they need. (`rev`, `zip`, `peekable`), and a `next` returning an item or `rt.DONE` allocates nothing per item. `just ts bench` measures chains of `map`, `filter`, `any`, `skip`, and `take` 1.4-8x faster than the same ES helpers in Node, Bun, and Chrome. -- Bodies lower from THIR, which keeps loops, `match`, and `?` shaped as written. It does - not say where values drop; [Drop](/quest/m2/ts-drop.md) decides how to get that from - MIR. +- Bodies lower from THIR, which keeps loops, `match`, and `?` shaped as written. Drops + come from its scopes, and async bodies are generators: MIR state machines were about + twice as fast per await level, but would need a second lowering + ([#16](https://github.com/kixelated/rust2/pull/16) measured both). The + [MIR spike](/quest/m2/ts-mir-spike.md) decides whether the whole lowering moves. ## Never @@ -166,14 +186,19 @@ bind to hand-written TS through config ([model](/quest/m2/ts-model.md)). ## Not yet -These fail translation: floats, `char`, statics, `async`, -types that implement `Drop`, iterators of std's collections and the adaptors without a -mapping in `src/lower/iter.rs` (such as `cycle`, `unzip`, and `partition`), an own -`impl Iterator` with `next_back`, `len`, or another provided method of its own, -`while let` or `match` on the `next` of an iterator of `Option`, or-patterns that bind, other format specs (a precision, `+`, `{:b}`, `{:x?}`, -and widths from arguments), writing to a `String` with `write!`, a `move` closure -copying a mutable variable, `mem::swap`, `replace`, or `take` of a reference's whole -pointee, and std functions without a mapping in `src/lower/library.rs`. +These fail translation: floats, `char`, statics, async blocks that borrow a number +mutably and async closures, values that drop as temporaries not moved whole, in a +closure's captures, in an iterator, in an enum, tuple, or generic `Drop` type, passed to +a std function that may discard them (`unwrap_or`), borrowed in an `Arc` or `Mutex`, or +moved ahead of an `.await` in the same call or assignment ([Drop](/quest/m2/ts-drop.md)), iterators of std's collections and the adaptors without +a mapping in `src/lower/iter.rs` (such as `cycle`, `unzip`, and `partition`), an own +`impl Iterator` with `next_back`, `len`, or another provided method of its own, `while +let` or `match` on the `next` of an iterator of `Option`, or-patterns that bind, other +format specs (a precision, `+`, `{:b}`, `{:x?}`, and widths from arguments), writing to a +`String` with `write!`, a `move` closure copying a mutable variable, `mem::swap`, +`replace`, or `take` of a reference's whole pointee, assigning an `Arc`, a waker, a +`MutexGuard`, a pinned reference, or a future through a reference, and std functions +without a mapping in `src/lower/library.rs`. ## Tests @@ -199,9 +224,16 @@ built natively for wasm32 and run beside its translation on the same inputs by and adds every 64- and 128-bit case over each pair of values next to a power of two, where the runtime switches between numbers and limbs, and its limbs carry and borrow. `brands.ts`, another caller, mixes integer types where `tsc` must refuse them. +- `task` trims moq's kio to a channel of `Arc`ed state, waiters, and wakers, under async + functions. Its caller is hand-written TS (`tests/task.ts`): a FIFO executor that runs a + script of spawns, sends, polls, and drops, and logs every poll, wake, and drop, as the + fixture's native executor does. The logs must match byte for byte. - `reject` holds constructs outside the subset; its expected errors are `expected.txt`, and its expected census is `census.md`. +`tests/spawn.ts` checks that `rt.spawn` yields to a timer after its 128-poll budget, and +rejects when a future panics as it drops. + ## Bench ```bash @@ -219,4 +251,9 @@ translation to show the noise. The report records the host and engine versions, each route's bundle size raw and gzipped. It sets no threshold; the nightly workflow runs it and posts the report to its summary. +The task fixture's `bench::run` times an async handoff: a value sent to a task parked on +its channel, read through 1, 4, or 16 async functions. Its translation runs polled at +once, as natively, and on `rt.spawn`; its hand-written route is the same handoff with +promises, as moq's TypeScript writes it. + The bench shell adds Bun, Chromium, and the wasm-bindgen that builds moq-wasm's package. diff --git a/ts/bench/entry.ts b/ts/bench/entry.ts index 31d23af54..ed2fcada1 100644 --- a/ts/bench/entry.ts +++ b/ts/bench/entry.ts @@ -5,10 +5,14 @@ import { Wasm } from "../tests/wasm.ts"; import { measure, type Route } from "./time.ts"; import type { Workload } from "./workloads.ts"; +type Workloads = { name: string; op: Workload["op"]; items: number; input: string }[]; + export interface Config { sampleMs: number; rounds: number; - workloads: { name: string; op: Workload["op"]; items: number; input: string }[]; + workloads: Workloads; + /** The async handoff's, on the task fixture. */ + handoffs: Workloads; } const browser = typeof window !== "undefined"; @@ -22,9 +26,9 @@ async function load(name: string): Promise { return new Uint8Array(await (await fetch(url)).arrayBuffer()); } -/** A bundled route, whose `run` returns its output as `view` reads it. */ -async function route(name: string, file: string, view: (out: T) => Uint8Array): Promise { - const { run } = await import(new URL(file, import.meta.url).href); +/** A bundled route, whose `run` (or `exported`) returns its output as `view` reads it. */ +async function route(name: string, file: string, view: (out: T) => Uint8Array, exported = "run"): Promise { + const run = (await import(new URL(file, import.meta.url).href))[exported]; return { name, run, output: async (input) => view(await run(input)) }; } @@ -35,9 +39,13 @@ function engine(): string { return `Node ${process.version} (V8 ${process.versions.v8})`; } +function parse(workloads: Workloads): Workload[] { + return workloads.map((w) => ({ ...w, input: Uint8Array.from(w.input.match(/../g) ?? [], (b) => parseInt(b, 16)) })); +} + async function main() { const config: Config = JSON.parse(new TextDecoder().decode(await load("workloads.json"))); - const workloads = config.workloads.map((w) => ({ ...w, input: Uint8Array.from(w.input.match(/../g) ?? [], (b) => parseInt(b, 16)) })); + const workloads = parse(config.workloads); const wasm = new Wasm(await WebAssembly.compile(await load("bench.wasm"))); // The A/A route is the translation again, loaded as its own module: its spread from the // first is the measurement's noise. @@ -48,6 +56,18 @@ async function main() { await route("A/A", "translated-aa.js", translated), ]; const timings = await measure(wasm, routes, workloads, config); + + // The async handoff: the translation polled at once by a one-task executor, as the + // native and WASM routes are, and on the runtime's microtask executor. + const bytes = (out: Uint8Array) => out; + const handoffRoutes = [ + await route("hand-written", "task-hand.js", bytes, "handWritten"), + await route("translated", "task-translated.js", bytes), + await route("spawned", "task-translated.js", bytes, "spawn"), + await route("A/A", "task-translated-aa.js", bytes), + ]; + const taskWasm = new Wasm(await WebAssembly.compile(await load("task.wasm"))); + timings.push(...(await measure(taskWasm, handoffRoutes, parse(config.handoffs), config))); return { engine: engine(), timings }; } diff --git a/ts/bench/main.ts b/ts/bench/main.ts index 3c83244bc..0a5aca525 100644 --- a/ts/bench/main.ts +++ b/ts/bench/main.ts @@ -2,7 +2,10 @@ // and Chrome, and reports the numbers with each route's bundle size. // // usage: bun main.ts --translated --wasm --native --moq -// --moq-wasm --out [--sample-ms 10] [--rounds 15] +// --moq-wasm --task --task-wasm --task-native +// --out [--sample-ms 10] [--rounds 15] +// +// The task fixture's async handoff runs beside the codec, against moq's way with promises. import { cpus, release, totalmem } from "node:os"; import { join, resolve } from "node:path"; @@ -12,7 +15,7 @@ import type { BunPlugin } from "bun"; import { Wasm } from "../tests/wasm.ts"; import type { Config } from "./entry.ts"; import { hex, type Timing } from "./time.ts"; -import { type Workload, workloads } from "./workloads.ts"; +import { handoffs, type Workload, workloads } from "./workloads.ts"; interface Engine { engine: string; @@ -26,6 +29,9 @@ const { values: args } = parseArgs({ native: { type: "string" }, moq: { type: "string" }, "moq-wasm": { type: "string" }, + task: { type: "string" }, + "task-wasm": { type: "string" }, + "task-native": { type: "string" }, out: { type: "string" }, "sample-ms": { type: "string", default: "10" }, rounds: { type: "string", default: "15" }, @@ -72,12 +78,37 @@ const handWritten = join(import.meta.dir, "moq.ts"); const translated = join(arg("translated"), "bench.ts"); const wasmBytes = await read(arg("wasm")); const translation = await bundle(translated); + +// The task fixture's translation, as `bench/task.ts` drives it. +async function taskEntry(dir: string): Promise { + const entry = join(dir, "bench-entry.ts"); + await Bun.write( + entry, + `import * as rt from "./rt.ts"; +import { Channel, drain } from "./model.ts"; +import { polled, spawned } from ${JSON.stringify(join(import.meta.dir, "task.ts"))}; + +const fixture = { Pending: rt.Pending, spawn: rt.spawn, u64: rt.u64, Channel, drain }; +export const run = polled(fixture); +export const spawn = spawned(fixture); +`, + ); + return entry; +} +const taskHand = join(import.meta.dir, "task.ts"); +const taskTranslated = await taskEntry(arg("task")); +const taskWasmBytes = await read(arg("task-wasm")); +const taskTranslation = await bundle(taskTranslated); await Promise.all([ Bun.write(join(out, "entry.js"), await bundle(join(import.meta.dir, "entry.ts"))), Bun.write(join(out, "moq.js"), await bundle(handWritten, { plugins: [moq] })), Bun.write(join(out, "translated.js"), translation), Bun.write(join(out, "translated-aa.js"), translation), Bun.write(join(out, "bench.wasm"), wasmBytes), + Bun.write(join(out, "task-hand.js"), await bundle(taskHand)), + Bun.write(join(out, "task-translated.js"), taskTranslation), + Bun.write(join(out, "task-translated-aa.js"), taskTranslation), + Bun.write(join(out, "task.wasm"), taskWasmBytes), Bun.write(join(out, "package.json"), '{ "type": "module" }\n'), Bun.write(join(out, "index.html"), page), ]); @@ -88,10 +119,19 @@ const sizes = [ { route: "hand-written", files: [await bundle(handWritten, { minify: true, plugins: [moq] })] }, { route: "translated", files: [await bundle(translated, { minify: true })] }, { route: "moq-wasm package", files: [await read(join(moqWasm, "moq_bg.wasm")), await read(join(moqWasm, "moq.js"))] }, -].map(({ route, files }) => { +].map(measureSize); + +// The task fixture: its channel, its async functions, and their runtime, as each route ships them. +const taskSizes = [ + { route: "wasm", files: [taskWasmBytes] }, + { route: "hand-written", files: [await bundle(taskHand, { minify: true })] }, + { route: "translated", files: [await bundle(taskTranslated, { minify: true })] }, +].map(measureSize); + +function measureSize({ route, files }: { route: string; files: Uint8Array[] }) { const sum = (f: (b: Uint8Array) => number) => files.reduce((n, b) => n + f(b), 0); return { route, raw: sum((b) => b.byteLength), gzip: sum((b) => Bun.gzipSync(b, { level: 9 }).byteLength) }; -}); +} // The workloads, made by the wasm32 build, which also gives each its expected output. const wasm = new Wasm(await WebAssembly.compile(wasmBytes)); @@ -101,25 +141,43 @@ function expected(input: Uint8Array): Uint8Array { return output; } const list = workloads(expected); +const handoffList = handoffs(); const config: Config = { sampleMs, rounds, workloads: list.map((w) => ({ name: w.name, op: w.op, items: w.items, input: hex(w.input) })), + handoffs: handoffList.map((w) => ({ name: w.name, op: w.op, items: w.items, input: hex(w.input) })), }; await Bun.write(join(out, "workloads.json"), JSON.stringify(config)); -async function native(): Promise { - const stdin = Buffer.from(list.map((w) => `${w.name}/${w.op} ${hex(w.input)}\n`).join("")); - const proc = Bun.spawn([arg("native"), String(sampleMs), String(rounds)], { stdin, stderr: "inherit" }); +/** The native route of `list`: `bin` times each on the host, which must match `expected`. */ +async function nativeRoute(bin: string, list: Workload[], expected: (input: Uint8Array) => Uint8Array): Promise { + // Names are one word on the binary's lines. + const key = (w: Workload) => `${w.name}/${w.op}`.replaceAll(" ", "-"); + const stdin = Buffer.from(list.map((w) => `${key(w)} ${hex(w.input)}\n`).join("")); + const proc = Bun.spawn([bin, String(sampleMs), String(rounds)], { stdin, stderr: "inherit" }); const lines = (await new Response(proc.stdout).text()).trim().split("\n"); - if ((await proc.exited) !== 0) throw new Error("the native benchmark failed"); - const timings = lines.map((line, i): Timing => { + if ((await proc.exited) !== 0) throw new Error(`the native benchmark ${bin} failed`); + return lines.map((line, i): Timing => { const [name, calls, ns, output] = line.split(" "); const w = list[i]; - if (name !== `${w.name}/${w.op}`) throw new Error(`the native benchmark printed ${name} for ${w.name}/${w.op}`); + if (name !== key(w)) throw new Error(`the native benchmark printed ${name} for ${key(w)}`); if (output !== hex(expected(w.input))) throw new Error(`native Rust on the host differs from wasm32 on ${name}`); return { workload: w.name, op: w.op, route: "native", calls: Number(calls), ns: ns.split(",").map((n) => Number(n) / w.items) }; }); +} + +async function native(): Promise { + const taskWasm = new Wasm(await WebAssembly.compile(taskWasmBytes)); + const handoff = (input: Uint8Array) => { + const output = taskWasm.call(input); + if (output === "panic") throw new Error(`native Rust panics on ${hex(input)}`); + return output; + }; + const timings = [ + ...(await nativeRoute(arg("native"), list, expected)), + ...(await nativeRoute(arg("task-native"), handoffList, handoff)), + ]; return { engine: "native", timings }; } @@ -177,7 +235,7 @@ for (const run of [() => script("node"), () => script(process.execPath), chrome] const report = render(); await Bun.write(join(out, "report.md"), report); -await Bun.write(join(out, "report.json"), JSON.stringify({ native: host, engines, sizes }, null, "\t")); +await Bun.write(join(out, "report.json"), JSON.stringify({ native: host, engines, sizes, taskSizes }, null, "\t")); console.log(report); function render(): string { @@ -232,9 +290,49 @@ function render(): string { }), ); } + lines.push(...renderHandoffs()); return `${lines.join("\n")}\n`; } +/** The async handoff on every route, and each route's size. */ +function renderHandoffs(): string[] { + const routes = ["native", "wasm", "hand-written", "translated", "spawned", "A/A"]; + const lines = [ + "", + "## Async handoff", + "", + "Nanoseconds per handoff: a value sent to a task parked on a kio-shaped channel, which", + "wakes it, polls it through `depth` async functions to the channel, and parks it again.", + "`native` and `wasm` run the task fixture's `bench.rs`, which polls the task at once on", + "each wake, and `translated` its translation polled the same way. `spawned` runs the", + "translation on the runtime's `spawn`, which polls woken tasks from a microtask.", + "`hand-written` is the handoff with promises and `async` functions, as moq's TypeScript", + "writes it, where each handoff waits for the reader to park again. Bundle sizes are of", + "the channel, the async functions, and their runtime.", + "", + "| route | raw | gzip |", + "| --- | ---: | ---: |", + ...taskSizes.map((s) => `| ${s.route} | ${s.raw} | ${s.gzip} |`), + ]; + for (const engine of engines) { + const all = [...host.timings, ...engine.timings]; + lines.push( + "", + `### ${engine.engine}`, + "", + `| depth | ${routes.join(" | ")} | translated รท hand-written |`, + `| --- |${" ---: |".repeat(routes.length + 1)}`, + ...handoffList.map((w) => { + const cells = routes.map((r) => cell(find(all, w, r).ns)); + const median = (route: string) => stats(find(all, w, route).ns).median; + const ratio = (median("translated") / median("hand-written")).toFixed(2); + return `| ${w.name.replace("depth ", "")} | ${cells.join(" | ")} | ${ratio} |`; + }), + ); + } + return lines; +} + function find(timings: Timing[], w: Workload, route: string): Timing { const timing = timings.find((t) => t.workload === w.name && t.op === w.op && t.route === route); if (!timing) throw new Error(`no ${route} timing for ${w.name} ${w.op}`); diff --git a/ts/bench/task.ts b/ts/bench/task.ts new file mode 100644 index 000000000..62b3bd8a5 --- /dev/null +++ b/ts/bench/task.ts @@ -0,0 +1,192 @@ +// The async handoff, as the task fixture's `bench.rs` runs it natively: a value sent to a +// task parked on a channel wakes it, which reads the value through `depth` async +// functions and parks again. `main.ts` bundles three routes from here: moq's way, with +// promises and `async`/`await`; the translation on a one-task executor that polls it +// right away, as `bench.rs` does; and the translation on the runtime's `spawn`, which +// polls woken tasks from a microtask. +// +// Each run's input is the depth, then the count of values as a big-endian `u32`; its +// output is the task's sum, as a big-endian `u64`. + +/** The translation's waker, by shape. */ +interface Waker extends Disposable { + wake(): void; + wakeByRef(): void; + clone(): Waker; + willWake(other: Waker): boolean; +} + +interface Future extends Disposable { + poll(cx: Waker): T | symbol; +} + +/** What a route drives: the translation's channel and task, and its runtime, whose `u64` values are `V`. */ +export interface Fixture { + Pending: symbol; + spawn(future: Future): Promise; + u64: { of(value: number): V; toBeBytes(a: V): Uint8Array }; + Channel: { open(): Channel }; + drain(rx: Disposable, depth: number): Future; +} + +interface Channel extends Disposable { + consumer(): Disposable; + send(value: V): boolean; +} + +function parse(input: Uint8Array): [depth: number, count: number] { + return [input[0], new DataView(input.buffer, input.byteOffset + 1, 4).getUint32(0)]; +} + +function output(total: bigint): Uint8Array { + const out = new Uint8Array(8); + new DataView(out.buffer).setBigUint64(0, total); + return out; +} + +/** A waker that only records that it was woken: the executor of one task. */ +class Woken implements Waker { + woken = true; + + wake(): void { + this.woken = true; + } + + wakeByRef(): void { + this.woken = true; + } + + clone(): Waker { + return this; + } + + willWake(other: Waker): boolean { + return other === this; + } + + [Symbol.dispose](): void {} +} + +/** The translation, polled right away whenever it is woken, as `bench.rs` polls it. */ +export function polled(f: Fixture): (input: Uint8Array) => Uint8Array { + return (input) => { + const [depth, count] = parse(input); + const channel = f.Channel.open(); + const task = f.drain(channel.consumer(), depth); + const waker = new Woken(); + const run = (): V | undefined => { + while (waker.woken) { + waker.woken = false; + const out = task.poll(waker); + if (out !== f.Pending) return out as V; + } + return undefined; + }; + run(); + for (let value = 0; value < count; value++) { + channel.send(f.u64.of(value)); + run(); + } + channel[Symbol.dispose](); + const total = run(); + task[Symbol.dispose](); + if (total === undefined) throw new Error("the task did not finish once its channel closed"); + return f.u64.toBeBytes(total); + }; +} + +/** The translation on the runtime's executor, which polls woken tasks from a microtask. */ +export function spawned(f: Fixture): (input: Uint8Array) => Promise { + return async (input) => { + const [depth, count] = parse(input); + const channel = f.Channel.open(); + const done = f.spawn(f.drain(channel.consumer(), depth)); + for (let value = 0; value < count; value++) { + // The task's poll is queued first, so it parks before the next send. + await undefined; + channel.send(f.u64.of(value)); + } + await undefined; + channel[Symbol.dispose](); + return f.u64.toBeBytes(await done); + }; +} + +/** moq's way: a channel whose reader awaits a promise, under `async` functions. */ +class PromiseChannel { + #queue: bigint[] = []; + #next = 0; + #closed = false; + #wake: (() => void) | undefined; + #parked: (() => void) | undefined; + + send(value: bigint): void { + this.#queue.push(value); + this.#notify(); + } + + close(): void { + this.#closed = true; + this.#notify(); + } + + #notify(): void { + const wake = this.#wake; + this.#wake = undefined; + wake?.(); + } + + async recv(): Promise { + for (;;) { + if (this.#next < this.#queue.length) return this.#queue[this.#next++]; + if (this.#closed) return undefined; + await new Promise((resolve) => { + this.#wake = resolve; + const parked = this.#parked; + this.#parked = undefined; + parked?.(); + }); + } + } + + /** Resolves once the reader next waits. */ + parked(): Promise { + return new Promise((resolve) => { + this.#parked = resolve; + }); + } +} + +/** The next value, read through `depth` async functions. */ +function read(rx: PromiseChannel, depth: number): Promise { + return depth === 1 ? rx.recv() : level(rx, depth); +} + +async function level(rx: PromiseChannel, depth: number): Promise { + return await read(rx, depth - 1); +} + +async function drainPromises(rx: PromiseChannel, depth: number): Promise { + let total = 0n; + for (;;) { + const value = await read(rx, depth); + if (value === undefined) return total; + total += value; + } +} + +/** The handoff, written with promises. */ +export async function handWritten(input: Uint8Array): Promise { + const [depth, count] = parse(input); + const channel = new PromiseChannel(); + let parked = channel.parked(); + const done = drainPromises(channel, depth); + for (let value = 0; value < count; value++) { + await parked; + parked = channel.parked(); + channel.send(BigInt(value)); + } + await parked; + channel.close(); + return output(await done); +} diff --git a/ts/bench/workloads.ts b/ts/bench/workloads.ts index 9baec325a..a703d54a7 100644 --- a/ts/bench/workloads.ts +++ b/ts/bench/workloads.ts @@ -5,8 +5,8 @@ export interface Workload { /** What is coded, such as `varint/quic/2B` or `frames/video/lite-07`, or iterated. */ name: string; - op: "encode" | "decode" | "iterate"; - /** Varints, frame headers, messages, or items per call, which each time is divided by. */ + op: "encode" | "decode" | "iterate" | "handoff"; + /** Varints, frame headers, messages, items, or handoffs per call, which each time is divided by. */ items: number; input: Uint8Array; } @@ -55,6 +55,24 @@ export function workloads(encode: (input: Uint8Array) => Uint8Array): Workload[] return out; } +/** The async handoff at each await depth: `bench::run` of the task fixture. */ +export function handoffs(): Workload[] { + // One call hands this many values over. + const count = 1000; + return [1, 4, 16].map((depth) => ({ + name: `depth ${depth}`, + op: "handoff", + items: count, + input: Uint8Array.of(depth, ...u32(count)), + })); +} + +function u32(v: number): number[] { + const bytes = new Uint8Array(4); + new DataView(bytes.buffer).setUint32(0, v); + return [...bytes]; +} + function u64(v: bigint): number[] { const bytes = new Uint8Array(8); new DataView(bytes.buffer).setBigUint64(0, v); diff --git a/ts/fixtures/bench.rs b/ts/fixtures/bench.rs new file mode 100644 index 000000000..a53075660 --- /dev/null +++ b/ts/fixtures/bench.rs @@ -0,0 +1,73 @@ +//! A fixture's native benchmark route: times its `bench::run` on the host, as +//! `ts/bench/time.ts` times the other routes. Each fixture's `examples/bench.rs` runs it. +//! +//! usage: bench < workloads +//! +//! Each input line is ` `. Each output line is ` `. + +use std::hint::black_box; +use std::io::{BufRead, Write}; +use std::time::{Duration, Instant}; + +/// Time `run` on the workloads read from stdin. +pub fn main(run: fn(&[u8]) -> Vec) { + let args: Vec = std::env::args().skip(1).collect(); + let [sample, rounds] = args.as_slice() else { + panic!("usage: bench < workloads"); + }; + let sample = Duration::from_millis(sample.parse().expect("sample ms")); + let rounds: usize = rounds.parse().expect("rounds"); + + let workloads: Vec<(String, Vec)> = std::io::stdin() + .lock() + .lines() + .map(|line| { + let line = line.expect("reading stdin"); + let (name, hex) = line.split_once(' ').expect("a line is ` `"); + (name.to_string(), unhex(hex)) + }) + .collect(); + + // Calibrate each workload to a sample of at least `sample`, which also warms it. + let calls: Vec = workloads + .iter() + .map(|(_, input)| { + let mut calls = 1; + while time(run, input, calls) < sample { + calls *= 2; + } + calls + }) + .collect(); + + // Interleave the workloads in each round, so drift spreads over all of them. + let mut ns = vec![Vec::with_capacity(rounds); workloads.len()]; + for _ in 0..rounds { + for (i, (_, input)) in workloads.iter().enumerate() { + ns[i].push(time(run, input, calls[i]).as_nanos() as f64 / calls[i] as f64); + } + } + + let mut stdout = std::io::stdout().lock(); + for (i, (name, input)) in workloads.iter().enumerate() { + let samples: Vec = ns[i].iter().map(|ns| format!("{ns:.3}")).collect(); + let output: String = run(input).iter().map(|b| format!("{b:02x}")).collect(); + writeln!(stdout, "{name} {} {} {output}", calls[i], samples.join(",")).unwrap(); + } +} + +fn time(run: fn(&[u8]) -> Vec, input: &[u8], calls: u64) -> Duration { + let start = Instant::now(); + for _ in 0..calls { + black_box(run(black_box(input))); + } + start.elapsed() +} + +fn unhex(hex: &str) -> Vec { + (0..hex.len()) + .step_by(2) + .map(|i| u8::from_str_radix(&hex[i..i + 2], 16).expect("input hex")) + .collect() +} diff --git a/ts/fixtures/codec/examples/bench.rs b/ts/fixtures/codec/examples/bench.rs index 89a48bb2c..32f49469a 100644 --- a/ts/fixtures/codec/examples/bench.rs +++ b/ts/fixtures/codec/examples/bench.rs @@ -1,74 +1,8 @@ -//! The benchmark's native route: times `bench::run` on the host, as `ts/bench/time.ts` -//! times the other routes. -//! -//! usage: bench < workloads -//! -//! Each input line is ` `. Each output line is ` `. +//! The benchmark's native route for this fixture: see `fixtures/bench.rs`. -use std::hint::black_box; -use std::io::{BufRead, Write}; -use std::time::{Duration, Instant}; - -use codec_fixture::bench; +#[path = "../../bench.rs"] +mod native; fn main() { - let args: Vec = std::env::args().skip(1).collect(); - let [sample, rounds] = args.as_slice() else { - panic!("usage: bench < workloads"); - }; - let sample = Duration::from_millis(sample.parse().expect("sample ms")); - let rounds: usize = rounds.parse().expect("rounds"); - - let workloads: Vec<(String, Vec)> = std::io::stdin() - .lock() - .lines() - .map(|line| { - let line = line.expect("reading stdin"); - let (name, hex) = line.split_once(' ').expect("a line is ` `"); - (name.to_string(), unhex(hex)) - }) - .collect(); - - // Calibrate each workload to a sample of at least `sample`, which also warms it. - let calls: Vec = workloads - .iter() - .map(|(_, input)| { - let mut calls = 1; - while time(input, calls) < sample { - calls *= 2; - } - calls - }) - .collect(); - - // Interleave the workloads in each round, so drift spreads over all of them. - let mut ns = vec![Vec::with_capacity(rounds); workloads.len()]; - for _ in 0..rounds { - for (i, (_, input)) in workloads.iter().enumerate() { - ns[i].push(time(input, calls[i]).as_nanos() as f64 / calls[i] as f64); - } - } - - let mut stdout = std::io::stdout().lock(); - for (i, (name, input)) in workloads.iter().enumerate() { - let samples: Vec = ns[i].iter().map(|ns| format!("{ns:.3}")).collect(); - let output: String = bench::run(input).iter().map(|b| format!("{b:02x}")).collect(); - writeln!(stdout, "{name} {} {} {output}", calls[i], samples.join(",")).unwrap(); - } -} - -fn time(input: &[u8], calls: u64) -> Duration { - let start = Instant::now(); - for _ in 0..calls { - black_box(bench::run(black_box(input))); - } - start.elapsed() -} - -fn unhex(hex: &str) -> Vec { - (0..hex.len()) - .step_by(2) - .map(|i| u8::from_str_radix(&hex[i..i + 2], 16).expect("input hex")) - .collect() + native::main(codec_fixture::bench::run); } diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index cb28a3c5c..74333ff31 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,41 +1,61 @@ # Census of `reject-fixture` -- Refusals: 44 -- Function instances: 33 -- Generic functions no instance reached, lowered over their type parameters: 2 +- Refusals: 76 +- Function instances: 66 +- Generic functions no instance reached, lowered over their type parameters: 3 ## By construct | Count | Construct | First | | ---: | --- | --- | -| 5 | unsupported type `_` | fixtures/reject/src/lib.rs:6:1 | +| 6 | unsupported type `_` | fixtures/reject/src/lib.rs:6:1 | +| 4 | replacing the `_` behind a reference, whose JS object is shared or private | fixtures/reject/src/lib.rs:225:2 | | 4 | unsupported call to `_` | fixtures/reject/src/lib.rs:36:4 | +| 3 | `_` is not supported: its `None` and `Some` would both be `undefined` | fixtures/reject/src/lib.rs:31:15 | | 3 | a `_` binding: a JS value of `_` is a copy, so it can't change | fixtures/reject/src/lib.rs:19:13 | -| 3 | replacing the value behind a reference to `_` | fixtures/reject/src/lib.rs:99:17 | -| 2 | `_` is not supported: its `None` and `Some` would both be `undefined` | fixtures/reject/src/lib.rs:31:15 | +| 3 | an iterator that owns `_`, which drops, which is not supported yet | fixtures/reject/src/lib.rs:371:4 | +| 3 | dropping `_`, which is not supported yet | fixtures/reject/src/lib.rs:66:31 | +| 3 | replacing the value behind a reference to `_` | fixtures/reject/src/lib.rs:168:17 | | 2 | `_`: a JS value of `_` is a copy, so a reference can't change it | fixtures/reject/src/lib.rs:19:13 | -| 2 | an iterator of the crate implementing `_`, which is not supported yet | fixtures/reject/src/lib.rs:148:1 | -| 2 | overriding `_`, which std's adaptors call in place of `next` or beside it | fixtures/reject/src/lib.rs:128:2 | -| 1 | `Binary` formatting, as in `{:b}`, is not supported | fixtures/reject/src/lib.rs:63:3 | -| 1 | `LowerExp` formatting, as in `{:e}`, is not supported | fixtures/reject/src/lib.rs:65:3 | -| 1 | `Octal` formatting, as in `{:o}`, is not supported | fixtures/reject/src/lib.rs:64:3 | -| 1 | `Pointer` formatting, as in `{:p}`, is not supported | fixtures/reject/src/lib.rs:68:3 | -| 1 | `_` implements `Drop`, which is not supported yet | fixtures/reject/src/lib.rs:43:1 | -| 1 | `_` is async, which is not supported yet | fixtures/reject/src/lib.rs:53:1 | -| 1 | a `zip` of `_`, whose closures std calls one item further through `next` than through `fold` | fixtures/reject/src/lib.rs:161:51 | +| 2 | a `for` loop over values that drop, which is not supported yet | fixtures/reject/src/lib.rs:76:6 | +| 2 | a `match` on a temporary that drops, which it doesn't move out of whole | fixtures/reject/src/lib.rs:217:2 | +| 2 | a field of a temporary that drops: bind it to a variable first | fixtures/reject/src/lib.rs:62:2 | +| 2 | an iterator of the crate implementing `_`, which is not supported yet | fixtures/reject/src/lib.rs:309:1 | +| 2 | overriding `_`, which std's adaptors call in place of `next` or beside it | fixtures/reject/src/lib.rs:289:2 | +| 1 | `Binary` formatting, as in `{:b}`, is not supported | fixtures/reject/src/lib.rs:132:3 | +| 1 | `LowerExp` formatting, as in `{:e}`, is not supported | fixtures/reject/src/lib.rs:134:3 | +| 1 | `Octal` formatting, as in `{:o}`, is not supported | fixtures/reject/src/lib.rs:133:3 | +| 1 | `Pointer` formatting, as in `{:p}`, is not supported | fixtures/reject/src/lib.rs:137:3 | +| 1 | `_` is generic and implements `Drop`, which is not supported yet | fixtures/reject/src/lib.rs:97:1 | +| 1 | `_` is not supported: it would be pending two ways with one value | fixtures/reject/src/lib.rs:229:20 | +| 1 | a `let` on a temporary that drops, which it doesn't move out of whole | fixtures/reject/src/lib.rs:200:2 | +| 1 | a `zip` of `_`, whose closures std calls one item further through `next` than through `fold` | fixtures/reject/src/lib.rs:322:51 | | 1 | a cast from `_` | fixtures/reject/src/lib.rs:11:2 | -| 1 | a cast to `_` | fixtures/reject/src/lib.rs:110:2 | -| 1 | a closure that copies a mutable variable | fixtures/reject/src/lib.rs:80:18 | -| 1 | a mutable reference to `_`, which is no object it can change in place | fixtures/reject/src/lib.rs:75:17 | -| 1 | a precision, as in `{:.2}`, is not supported | fixtures/reject/src/lib.rs:60:3 | -| 1 | a printable type must implement `Display` or `Debug` | fixtures/reject/src/lib.rs:106:1 | -| 1 | a width or precision from an argument, as in `{:1$}`, is not supported | fixtures/reject/src/lib.rs:67:3 | -| 1 | an or-pattern with bindings | fixtures/reject/src/lib.rs:87:3 | +| 1 | a cast to `_` | fixtures/reject/src/lib.rs:179:2 | +| 1 | a closure of type `_` | fixtures/reject/src/lib.rs:121:10 | +| 1 | a closure that captures a value that drops, which is not supported yet | fixtures/reject/src/lib.rs:68:23 | +| 1 | a closure that copies a mutable variable | fixtures/reject/src/lib.rs:149:18 | +| 1 | a method that takes `self` by value that drops, which is not supported yet | fixtures/reject/src/lib.rs:51:20 | +| 1 | a mutable reference to `_`, which is no object it can change in place | fixtures/reject/src/lib.rs:144:17 | +| 1 | a parameter that drops bound by a pattern, which is not supported yet | fixtures/reject/src/lib.rs:83:21 | +| 1 | a precision, as in `{:.2}`, is not supported | fixtures/reject/src/lib.rs:129:3 | +| 1 | a printable type must implement `Display` or `Debug` | fixtures/reject/src/lib.rs:175:1 | +| 1 | a rest pattern that moves values that drop out of a place, which is not supported yet | fixtures/reject/src/lib.rs:405:14 | +| 1 | a variable from outside the function | fixtures/reject/src/lib.rs:84:2 | +| 1 | a width or precision from an argument, as in `{:1$}`, is not supported | fixtures/reject/src/lib.rs:136:3 | +| 1 | an `.await` in an operand after one that drops, which is not supported yet: bind the awaited value first | fixtures/reject/src/lib.rs:190:13 | +| 1 | an `.await` in the place an assignment of a value that drops writes to, which is not supported yet: bind the awaited value first | fixtures/reject/src/lib.rs:207:2 | +| 1 | an `if let` on a temporary that drops, which it doesn't move out of whole | fixtures/reject/src/lib.rs:241:9 | +| 1 | an array repeat of a value that drops, which is not supported yet | fixtures/reject/src/lib.rs:416:14 | +| 1 | an async body returning `_` is not supported: its `Ready` could be pending | fixtures/reject/src/lib.rs:245:54 | +| 1 | an async body that borrows `_` mutably, which is no object it can change in place | fixtures/reject/src/lib.rs:113:3 | +| 1 | an or-pattern with bindings | fixtures/reject/src/lib.rs:156:3 | | 1 | arithmetic on `_` | fixtures/reject/src/lib.rs:7:2 | -| 1 | hex `Debug`, as in `{:x?}`, is not supported | fixtures/reject/src/lib.rs:66:3 | -| 1 | the `+` flag of a format spec is not supported | fixtures/reject/src/lib.rs:61:3 | -| 1 | the `-` flag of a format spec is not supported | fixtures/reject/src/lib.rs:62:3 | -| 1 | unsupported expression: StaticRef | fixtures/reject/src/lib.rs:94:2 | +| 1 | hex `Debug`, as in `{:x?}`, is not supported | fixtures/reject/src/lib.rs:135:3 | +| 1 | passing `_`, which drops, to `_`, which is not supported yet | fixtures/reject/src/lib.rs:400:4 | +| 1 | the `+` flag of a format spec is not supported | fixtures/reject/src/lib.rs:130:3 | +| 1 | the `-` flag of a format spec is not supported | fixtures/reject/src/lib.rs:131:3 | +| 1 | unsupported expression: StaticRef | fixtures/reject/src/lib.rs:163:2 | | 1 | unsupported literal of type `_` | fixtures/reject/src/lib.rs:7:6 | ## By API @@ -45,70 +65,112 @@ The type or module each refused type or path belongs to. | Count | API | First | | ---: | --- | --- | | 6 | `f64` | fixtures/reject/src/lib.rs:6:1 | -| 4 | `std::string::String` | fixtures/reject/src/lib.rs:73:16 | -| 3 | `Meters` | fixtures/reject/src/lib.rs:99:17 | +| 4 | `Option` | fixtures/reject/src/lib.rs:31:15 | +| 4 | `std::string::String` | fixtures/reject/src/lib.rs:142:16 | +| 3 | `Meters` | fixtures/reject/src/lib.rs:168:17 | | 3 | `u32` | fixtures/reject/src/lib.rs:19:13 | -| 2 | `Iterator` | fixtures/reject/src/lib.rs:128:2 | -| 2 | `Option` | fixtures/reject/src/lib.rs:31:15 | +| 2 | `Iterator` | fixtures/reject/src/lib.rs:289:2 | +| 2 | `Noisy` | fixtures/reject/src/lib.rs:371:16 | | 2 | `char` | fixtures/reject/src/lib.rs:10:18 | -| 1 | `DoubleEndedIterator` | fixtures/reject/src/lib.rs:148:1 | -| 1 | `FusedIterator` | fixtures/reject/src/lib.rs:202:1 | -| 1 | `Guard` | fixtures/reject/src/lib.rs:43:1 | +| 2 | `std::pin::Pin` | fixtures/reject/src/lib.rs:261:2 | +| 2 | `std::sync::Arc` | fixtures/reject/src/lib.rs:183:42 | +| 2 | `{async closure}` | fixtures/reject/src/lib.rs:121:6 | +| 1 | `Boxed` | fixtures/reject/src/lib.rs:97:1 | +| 1 | `DoubleEndedIterator` | fixtures/reject/src/lib.rs:309:1 | +| 1 | `FusedIterator` | fixtures/reject/src/lib.rs:363:1 | +| 1 | `Poll` | fixtures/reject/src/lib.rs:229:20 | +| 1 | `Slot` | fixtures/reject/src/lib.rs:93:1 | | 1 | `dyn std::ops::Fn(u8) -> u8` | fixtures/reject/src/lib.rs:14:16 | -| 1 | `later` | fixtures/reject/src/lib.rs:53:1 | -| 1 | `std::iter::Chain` | fixtures/reject/src/lib.rs:161:51 | -| 1 | `std::ops::Range` | fixtures/reject/src/lib.rs:169:8 | -| 1 | `std::slice::Iter` | fixtures/reject/src/lib.rs:115:11 | +| 1 | `std::iter::Chain` | fixtures/reject/src/lib.rs:322:51 | +| 1 | `std::ops::Range` | fixtures/reject/src/lib.rs:330:8 | +| 1 | `std::option::Option` | fixtures/reject/src/lib.rs:400:4 | +| 1 | `std::slice::Iter` | fixtures/reject/src/lib.rs:276:11 | | 1 | `std::slice` | fixtures/reject/src/lib.rs:36:4 | +| 1 | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:271:2 | +| 1 | `std::task::Poll` | fixtures/reject/src/lib.rs:245:54 | +| 1 | `std::vec::Vec` | fixtures/reject/src/lib.rs:371:4 | +| 1 | `u8` | fixtures/reject/src/lib.rs:113:3 | +| 1 | `{closure}` | fixtures/reject/src/lib.rs:66:31 | ## By type or path | Count | Construct | Type or path | First | | ---: | --- | --- | --- | -| 3 | replacing the value behind a reference to `_` | `Meters` | fixtures/reject/src/lib.rs:99:17 | +| 3 | `_` is not supported: its `None` and `Some` would both be `undefined` | `Option` | fixtures/reject/src/lib.rs:31:15 | +| 3 | replacing the value behind a reference to `_` | `Meters` | fixtures/reject/src/lib.rs:168:17 | | 3 | unsupported type `_` | `f64` | fixtures/reject/src/lib.rs:6:1 | -| 2 | `_` is not supported: its `None` and `Some` would both be `undefined` | `Option` | fixtures/reject/src/lib.rs:31:15 | | 2 | a `_` binding: a JS value of `_` is a copy, so it can't change | `&mut u32`, `u32` | fixtures/reject/src/lib.rs:19:13 | -| 1 | `_` implements `Drop`, which is not supported yet | `Guard` | fixtures/reject/src/lib.rs:43:1 | -| 1 | `_` is async, which is not supported yet | `later` | fixtures/reject/src/lib.rs:53:1 | -| 1 | `_`: a JS value of `_` is a copy, so a reference can't change it | `&mut std::string::String`, `std::string::String` | fixtures/reject/src/lib.rs:73:16 | +| 2 | an iterator that owns `_`, which drops, which is not supported yet | `Noisy` | fixtures/reject/src/lib.rs:371:16 | +| 2 | replacing the `_` behind a reference, whose JS object is shared or private | `std::pin::Pin` | fixtures/reject/src/lib.rs:261:2 | +| 1 | `_` is generic and implements `Drop`, which is not supported yet | `Boxed` | fixtures/reject/src/lib.rs:97:1 | +| 1 | `_` is not supported: it would be pending two ways with one value | `Poll` | fixtures/reject/src/lib.rs:229:20 | +| 1 | `_`: a JS value of `_` is a copy, so a reference can't change it | `&mut std::string::String`, `std::string::String` | fixtures/reject/src/lib.rs:142:16 | | 1 | `_`: a JS value of `_` is a copy, so a reference can't change it | `&mut u32`, `u32` | fixtures/reject/src/lib.rs:19:13 | -| 1 | a `_` binding: a JS value of `_` is a copy, so it can't change | `&mut std::string::String`, `std::string::String` | fixtures/reject/src/lib.rs:73:16 | -| 1 | a `zip` of `_`, whose closures std calls one item further through `next` than through `fold` | `std::iter::Chain` | fixtures/reject/src/lib.rs:161:51 | +| 1 | a `_` binding: a JS value of `_` is a copy, so it can't change | `&mut std::string::String`, `std::string::String` | fixtures/reject/src/lib.rs:142:16 | +| 1 | a `zip` of `_`, whose closures std calls one item further through `next` than through `fold` | `std::iter::Chain` | fixtures/reject/src/lib.rs:322:51 | | 1 | a cast from `_` | `char` | fixtures/reject/src/lib.rs:11:2 | -| 1 | a cast to `_` | `f64` | fixtures/reject/src/lib.rs:110:2 | -| 1 | a mutable reference to `_`, which is no object it can change in place | `std::string::String` | fixtures/reject/src/lib.rs:75:17 | -| 1 | an iterator of the crate implementing `_`, which is not supported yet | `DoubleEndedIterator` | fixtures/reject/src/lib.rs:148:1 | -| 1 | an iterator of the crate implementing `_`, which is not supported yet | `FusedIterator` | fixtures/reject/src/lib.rs:202:1 | +| 1 | a cast to `_` | `f64` | fixtures/reject/src/lib.rs:179:2 | +| 1 | a closure of type `_` | `{async closure}` | fixtures/reject/src/lib.rs:121:10 | +| 1 | a mutable reference to `_`, which is no object it can change in place | `std::string::String` | fixtures/reject/src/lib.rs:144:17 | +| 1 | an async body returning `_` is not supported: its `Ready` could be pending | `std::task::Poll` | fixtures/reject/src/lib.rs:245:54 | +| 1 | an async body that borrows `_` mutably, which is no object it can change in place | `u8` | fixtures/reject/src/lib.rs:113:3 | +| 1 | an iterator of the crate implementing `_`, which is not supported yet | `DoubleEndedIterator` | fixtures/reject/src/lib.rs:309:1 | +| 1 | an iterator of the crate implementing `_`, which is not supported yet | `FusedIterator` | fixtures/reject/src/lib.rs:363:1 | +| 1 | an iterator that owns `_`, which drops, which is not supported yet | `std::vec::Vec` | fixtures/reject/src/lib.rs:371:4 | | 1 | arithmetic on `_` | `f64` | fixtures/reject/src/lib.rs:7:2 | -| 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::fold` | fixtures/reject/src/lib.rs:128:2 | -| 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::size_hint` | fixtures/reject/src/lib.rs:182:2 | -| 1 | unsupported call to `_` | `::cycle` | fixtures/reject/src/lib.rs:115:11 | -| 1 | unsupported call to `_` | `::write_fmt` | fixtures/reject/src/lib.rs:75:10 | -| 1 | unsupported call to `_` | `std::ops::Range::contains` | fixtures/reject/src/lib.rs:169:8 | +| 1 | dropping `_`, which is not supported yet | `Slot` | fixtures/reject/src/lib.rs:93:1 | +| 1 | dropping `_`, which is not supported yet | `std::sync::Arc` | fixtures/reject/src/lib.rs:183:42 | +| 1 | dropping `_`, which is not supported yet | `{closure}` | fixtures/reject/src/lib.rs:66:31 | +| 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::fold` | fixtures/reject/src/lib.rs:289:2 | +| 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::size_hint` | fixtures/reject/src/lib.rs:343:2 | +| 1 | passing `_`, which drops, to `_`, which is not supported yet | `std::option::Option`, `Option::unwrap_or` | fixtures/reject/src/lib.rs:400:4 | +| 1 | replacing the `_` behind a reference, whose JS object is shared or private | `std::sync::Arc` | fixtures/reject/src/lib.rs:225:2 | +| 1 | replacing the `_` behind a reference, whose JS object is shared or private | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:271:2 | +| 1 | unsupported call to `_` | `::cycle` | fixtures/reject/src/lib.rs:276:11 | +| 1 | unsupported call to `_` | `::write_fmt` | fixtures/reject/src/lib.rs:144:10 | +| 1 | unsupported call to `_` | `std::ops::Range::contains` | fixtures/reject/src/lib.rs:330:8 | | 1 | unsupported call to `_` | `std::slice::sort` | fixtures/reject/src/lib.rs:36:4 | | 1 | unsupported literal of type `_` | `f64` | fixtures/reject/src/lib.rs:7:6 | | 1 | unsupported type `_` | `char` | fixtures/reject/src/lib.rs:10:18 | | 1 | unsupported type `_` | `dyn std::ops::Fn(u8) -> u8` | fixtures/reject/src/lib.rs:14:16 | +| 1 | unsupported type `_` | `{async closure}` | fixtures/reject/src/lib.rs:121:6 | ## Spans -
unsupported type `_` (5) +
unsupported type `_` (6) - fixtures/reject/src/lib.rs:6:1: unsupported type `f64` - fixtures/reject/src/lib.rs:6:14: unsupported type `f64` - fixtures/reject/src/lib.rs:10:18: unsupported type `char` - fixtures/reject/src/lib.rs:14:16: unsupported type `dyn std::ops::Fn(u8) -> u8` -- fixtures/reject/src/lib.rs:108:1: unsupported type `f64` +- fixtures/reject/src/lib.rs:121:6: unsupported type `{async closure@fixtures/reject/src/lib.rs:121:10: 121:18}` +- fixtures/reject/src/lib.rs:177:1: unsupported type `f64` + +
+ +
replacing the `_` behind a reference, whose JS object is shared or private (4) + +- fixtures/reject/src/lib.rs:225:2: replacing the `std::sync::Arc` behind a reference, whose JS object is shared or private +- fixtures/reject/src/lib.rs:261:2: replacing the `std::pin::Pin>` behind a reference, whose JS object is shared or private +- fixtures/reject/src/lib.rs:271:2: replacing the `std::sync::MutexGuard<'_, u8>` behind a reference, whose JS object is shared or private +- fixtures/reject/src/lib.rs:411:2: replacing the `std::pin::Pin<&mut Noisy>` behind a reference, whose JS object is shared or private
unsupported call to `_` (4) - fixtures/reject/src/lib.rs:36:4: unsupported call to `std::slice::::sort` -- fixtures/reject/src/lib.rs:75:10: unsupported call to `::write_fmt` -- fixtures/reject/src/lib.rs:115:11: unsupported call to ` as std::iter::Iterator>::cycle` -- fixtures/reject/src/lib.rs:169:8: unsupported call to `std::ops::Range::::contains::` +- fixtures/reject/src/lib.rs:144:10: unsupported call to `::write_fmt` +- fixtures/reject/src/lib.rs:276:11: unsupported call to ` as std::iter::Iterator>::cycle` +- fixtures/reject/src/lib.rs:330:8: unsupported call to `std::ops::Range::::contains::` + +
+ +
`_` is not supported: its `None` and `Some` would both be `undefined` (3) + +- fixtures/reject/src/lib.rs:31:15: `Option>` is not supported: its `None` and `Some` would both be `undefined` +- fixtures/reject/src/lib.rs:40:42: `Option>` is not supported: its `None` and `Some` would both be `undefined` +- fixtures/reject/src/lib.rs:234:19: `Option>` is not supported: its `None` and `Some` would both be `undefined`
@@ -116,85 +178,121 @@ The type or module each refused type or path belongs to. - fixtures/reject/src/lib.rs:19:13: a `&mut u32` binding: a JS value of `u32` is a copy, so it can't change - fixtures/reject/src/lib.rs:25:6: a `&mut u32` binding: a JS value of `u32` is a copy, so it can't change -- fixtures/reject/src/lib.rs:73:16: a `&mut std::string::String` binding: a JS value of `std::string::String` is a copy, so it can't change +- fixtures/reject/src/lib.rs:142:16: a `&mut std::string::String` binding: a JS value of `std::string::String` is a copy, so it can't change
-
replacing the value behind a reference to `_` (3) +
an iterator that owns `_`, which drops, which is not supported yet (3) -- fixtures/reject/src/lib.rs:99:17: replacing the value behind a reference to `Meters` -- fixtures/reject/src/lib.rs:99:20: replacing the value behind a reference to `Meters` -- fixtures/reject/src/lib.rs:103:25: replacing the value behind a reference to `Meters` +- fixtures/reject/src/lib.rs:371:4: an iterator that owns `std::vec::Vec`, which drops, which is not supported yet +- fixtures/reject/src/lib.rs:371:16: an iterator that owns `Noisy`, which drops, which is not supported yet +- fixtures/reject/src/lib.rs:376:20: an iterator that owns `Noisy`, which drops, which is not supported yet
-
`_` is not supported: its `None` and `Some` would both be `undefined` (2) +
dropping `_`, which is not supported yet (3) -- fixtures/reject/src/lib.rs:31:15: `Option>` is not supported: its `None` and `Some` would both be `undefined` -- fixtures/reject/src/lib.rs:40:42: `Option>` is not supported: its `None` and `Some` would both be `undefined` +- fixtures/reject/src/lib.rs:66:31: dropping `{closure@fixtures/reject/src/lib.rs:68:10: 68:17}`, which is not supported yet +- fixtures/reject/src/lib.rs:93:1: dropping `Slot`, which is not supported yet +- fixtures/reject/src/lib.rs:183:42: dropping `std::sync::Arc<&Noisy>`, which is not supported yet + +
+ +
replacing the value behind a reference to `_` (3) + +- fixtures/reject/src/lib.rs:168:17: replacing the value behind a reference to `Meters` +- fixtures/reject/src/lib.rs:168:20: replacing the value behind a reference to `Meters` +- fixtures/reject/src/lib.rs:172:25: replacing the value behind a reference to `Meters`
`_`: a JS value of `_` is a copy, so a reference can't change it (2) - fixtures/reject/src/lib.rs:19:13: `&mut u32`: a JS value of `u32` is a copy, so a reference can't change it -- fixtures/reject/src/lib.rs:73:16: `&mut std::string::String`: a JS value of `std::string::String` is a copy, so a reference can't change it +- fixtures/reject/src/lib.rs:142:16: `&mut std::string::String`: a JS value of `std::string::String` is a copy, so a reference can't change it + +
+ +
a `for` loop over values that drop, which is not supported yet (2) + +- fixtures/reject/src/lib.rs:76:6: a `for` loop over values that drop, which is not supported yet +- fixtures/reject/src/lib.rs:392:6: a `for` loop over values that drop, which is not supported yet + +
+ +
a `match` on a temporary that drops, which it doesn't move out of whole (2) + +- fixtures/reject/src/lib.rs:217:2: a `match` on a temporary that drops, which it doesn't move out of whole +- fixtures/reject/src/lib.rs:252:16: a `match` on a temporary that drops, which it doesn't move out of whole + +
+ +
a field of a temporary that drops: bind it to a variable first (2) + +- fixtures/reject/src/lib.rs:62:2: a field of a temporary that drops: bind it to a variable first +- fixtures/reject/src/lib.rs:266:2: a field of a temporary that drops: bind it to a variable first
an iterator of the crate implementing `_`, which is not supported yet (2) -- fixtures/reject/src/lib.rs:148:1: an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet -- fixtures/reject/src/lib.rs:202:1: an iterator of the crate implementing `FusedIterator`, which is not supported yet +- fixtures/reject/src/lib.rs:309:1: an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet +- fixtures/reject/src/lib.rs:363:1: an iterator of the crate implementing `FusedIterator`, which is not supported yet
overriding `_`, which std's adaptors call in place of `next` or beside it (2) -- fixtures/reject/src/lib.rs:128:2: overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it -- fixtures/reject/src/lib.rs:182:2: overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it +- fixtures/reject/src/lib.rs:289:2: overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it +- fixtures/reject/src/lib.rs:343:2: overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it
`Binary` formatting, as in `{:b}`, is not supported (1) -- fixtures/reject/src/lib.rs:63:3: `Binary` formatting, as in `{:b}`, is not supported +- fixtures/reject/src/lib.rs:132:3: `Binary` formatting, as in `{:b}`, is not supported
`LowerExp` formatting, as in `{:e}`, is not supported (1) -- fixtures/reject/src/lib.rs:65:3: `LowerExp` formatting, as in `{:e}`, is not supported +- fixtures/reject/src/lib.rs:134:3: `LowerExp` formatting, as in `{:e}`, is not supported
`Octal` formatting, as in `{:o}`, is not supported (1) -- fixtures/reject/src/lib.rs:64:3: `Octal` formatting, as in `{:o}`, is not supported +- fixtures/reject/src/lib.rs:133:3: `Octal` formatting, as in `{:o}`, is not supported
`Pointer` formatting, as in `{:p}`, is not supported (1) -- fixtures/reject/src/lib.rs:68:3: `Pointer` formatting, as in `{:p}`, is not supported +- fixtures/reject/src/lib.rs:137:3: `Pointer` formatting, as in `{:p}`, is not supported
-
`_` implements `Drop`, which is not supported yet (1) +
`_` is generic and implements `Drop`, which is not supported yet (1) -- fixtures/reject/src/lib.rs:43:1: `Guard` implements `Drop`, which is not supported yet +- fixtures/reject/src/lib.rs:97:1: `Boxed` is generic and implements `Drop`, which is not supported yet
-
`_` is async, which is not supported yet (1) +
`_` is not supported: it would be pending two ways with one value (1) -- fixtures/reject/src/lib.rs:53:1: `later` is async, which is not supported yet +- fixtures/reject/src/lib.rs:229:20: `Poll>>` is not supported: it would be pending two ways with one value + +
+ +
a `let` on a temporary that drops, which it doesn't move out of whole (1) + +- fixtures/reject/src/lib.rs:200:2: a `let` on a temporary that drops, which it doesn't move out of whole
a `zip` of `_`, whose closures std calls one item further through `next` than through `fold` (1) -- fixtures/reject/src/lib.rs:161:51: a `zip` of `std::iter::Chain, {closure@fixtures/reject/src/lib.rs:161:15: 161:18}>, std::iter::Copied>>`, whose closures std calls one item further through `next` than through `fold` +- fixtures/reject/src/lib.rs:322:51: a `zip` of `std::iter::Chain, {closure@fixtures/reject/src/lib.rs:322:15: 322:18}>, std::iter::Copied>>`, whose closures std calls one item further through `next` than through `fold`
@@ -206,43 +304,115 @@ The type or module each refused type or path belongs to.
a cast to `_` (1) -- fixtures/reject/src/lib.rs:110:2: a cast to `f64` +- fixtures/reject/src/lib.rs:179:2: a cast to `f64` + +
+ +
a closure of type `_` (1) + +- fixtures/reject/src/lib.rs:121:10: a closure of type `{async closure@fixtures/reject/src/lib.rs:121:10: 121:18}` + +
+ +
a closure that captures a value that drops, which is not supported yet (1) + +- fixtures/reject/src/lib.rs:68:23: a closure that captures a value that drops, which is not supported yet
a closure that copies a mutable variable (1) -- fixtures/reject/src/lib.rs:80:18: a closure that copies a mutable variable +- fixtures/reject/src/lib.rs:149:18: a closure that copies a mutable variable + +
+ +
a method that takes `self` by value that drops, which is not supported yet (1) + +- fixtures/reject/src/lib.rs:51:20: a method that takes `self` by value that drops, which is not supported yet
a mutable reference to `_`, which is no object it can change in place (1) -- fixtures/reject/src/lib.rs:75:17: a mutable reference to `std::string::String`, which is no object it can change in place +- fixtures/reject/src/lib.rs:144:17: a mutable reference to `std::string::String`, which is no object it can change in place + +
+ +
a parameter that drops bound by a pattern, which is not supported yet (1) + +- fixtures/reject/src/lib.rs:83:21: a parameter that drops bound by a pattern, which is not supported yet
a precision, as in `{:.2}`, is not supported (1) -- fixtures/reject/src/lib.rs:60:3: a precision, as in `{:.2}`, is not supported +- fixtures/reject/src/lib.rs:129:3: a precision, as in `{:.2}`, is not supported
a printable type must implement `Display` or `Debug` (1) -- fixtures/reject/src/lib.rs:106:1: a printable type must implement `Display` or `Debug` +- fixtures/reject/src/lib.rs:175:1: a printable type must implement `Display` or `Debug` + +
+ +
a rest pattern that moves values that drop out of a place, which is not supported yet (1) + +- fixtures/reject/src/lib.rs:405:14: a rest pattern that moves values that drop out of a place, which is not supported yet + +
+ +
a variable from outside the function (1) + +- fixtures/reject/src/lib.rs:84:2: a variable from outside the function
a width or precision from an argument, as in `{:1$}`, is not supported (1) -- fixtures/reject/src/lib.rs:67:3: a width or precision from an argument, as in `{:1$}`, is not supported +- fixtures/reject/src/lib.rs:136:3: a width or precision from an argument, as in `{:1$}`, is not supported + +
+ +
an `.await` in an operand after one that drops, which is not supported yet: bind the awaited value first (1) + +- fixtures/reject/src/lib.rs:190:13: an `.await` in an operand after one that drops, which is not supported yet: bind the awaited value first + +
+ +
an `.await` in the place an assignment of a value that drops writes to, which is not supported yet: bind the awaited value first (1) + +- fixtures/reject/src/lib.rs:207:2: an `.await` in the place an assignment of a value that drops writes to, which is not supported yet: bind the awaited value first + +
+ +
an `if let` on a temporary that drops, which it doesn't move out of whole (1) + +- fixtures/reject/src/lib.rs:241:9: an `if let` on a temporary that drops, which it doesn't move out of whole + +
+ +
an array repeat of a value that drops, which is not supported yet (1) + +- fixtures/reject/src/lib.rs:416:14: an array repeat of a value that drops, which is not supported yet + +
+ +
an async body returning `_` is not supported: its `Ready` could be pending (1) + +- fixtures/reject/src/lib.rs:245:54: an async body returning `std::task::Poll` is not supported: its `Ready` could be pending + +
+ +
an async body that borrows `_` mutably, which is no object it can change in place (1) + +- fixtures/reject/src/lib.rs:113:3: an async body that borrows `u8` mutably, which is no object it can change in place
an or-pattern with bindings (1) -- fixtures/reject/src/lib.rs:87:3: an or-pattern with bindings +- fixtures/reject/src/lib.rs:156:3: an or-pattern with bindings
@@ -254,25 +424,31 @@ The type or module each refused type or path belongs to.
hex `Debug`, as in `{:x?}`, is not supported (1) -- fixtures/reject/src/lib.rs:66:3: hex `Debug`, as in `{:x?}`, is not supported +- fixtures/reject/src/lib.rs:135:3: hex `Debug`, as in `{:x?}`, is not supported + +
+ +
passing `_`, which drops, to `_`, which is not supported yet (1) + +- fixtures/reject/src/lib.rs:400:4: passing `std::option::Option`, which drops, to `Option::unwrap_or`, which is not supported yet
the `+` flag of a format spec is not supported (1) -- fixtures/reject/src/lib.rs:61:3: the `+` flag of a format spec is not supported +- fixtures/reject/src/lib.rs:130:3: the `+` flag of a format spec is not supported
the `-` flag of a format spec is not supported (1) -- fixtures/reject/src/lib.rs:62:3: the `-` flag of a format spec is not supported +- fixtures/reject/src/lib.rs:131:3: the `-` flag of a format spec is not supported
unsupported expression: StaticRef (1) -- fixtures/reject/src/lib.rs:94:2: unsupported expression: StaticRef +- fixtures/reject/src/lib.rs:163:2: unsupported expression: StaticRef
diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index 2e550a9eb..df3e8e335 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -1,6 +1,6 @@ -a root must not be generic at fixtures/reject/src/lib.rs:108:1 +a root must not be generic at fixtures/reject/src/lib.rs:177:1 no item `missing` in the crate -a printable type must implement `Display` or `Debug` at fixtures/reject/src/lib.rs:106:1 +a printable type must implement `Display` or `Debug` at fixtures/reject/src/lib.rs:175:1 unsupported type `f64` at fixtures/reject/src/lib.rs:6:14 unsupported literal of type `f64` at fixtures/reject/src/lib.rs:7:6 arithmetic on `f64` at fixtures/reject/src/lib.rs:7:2 @@ -14,31 +14,63 @@ a `&mut u32` binding: a JS value of `u32` is a copy, so it can't change at fixtu `Option>` is not supported: its `None` and `Some` would both be `undefined` at fixtures/reject/src/lib.rs:31:15 unsupported call to `std::slice::::sort` at fixtures/reject/src/lib.rs:36:4 `Option>` is not supported: its `None` and `Some` would both be `undefined` at fixtures/reject/src/lib.rs:40:42 -`Guard` implements `Drop`, which is not supported yet at fixtures/reject/src/lib.rs:50:2 -`later` is async, which is not supported yet at fixtures/reject/src/lib.rs:53:1 -a precision, as in `{:.2}`, is not supported at fixtures/reject/src/lib.rs:60:3 -the `+` flag of a format spec is not supported at fixtures/reject/src/lib.rs:61:3 -the `-` flag of a format spec is not supported at fixtures/reject/src/lib.rs:62:3 -`Binary` formatting, as in `{:b}`, is not supported at fixtures/reject/src/lib.rs:63:3 -`Octal` formatting, as in `{:o}`, is not supported at fixtures/reject/src/lib.rs:64:3 -`LowerExp` formatting, as in `{:e}`, is not supported at fixtures/reject/src/lib.rs:65:3 -hex `Debug`, as in `{:x?}`, is not supported at fixtures/reject/src/lib.rs:66:3 -a width or precision from an argument, as in `{:1$}`, is not supported at fixtures/reject/src/lib.rs:67:3 -`Pointer` formatting, as in `{:p}`, is not supported at fixtures/reject/src/lib.rs:68:3 -a `&mut std::string::String` binding: a JS value of `std::string::String` is a copy, so it can't change at fixtures/reject/src/lib.rs:73:16 -`&mut std::string::String`: a JS value of `std::string::String` is a copy, so a reference can't change it at fixtures/reject/src/lib.rs:73:16 -a mutable reference to `std::string::String`, which is no object it can change in place at fixtures/reject/src/lib.rs:75:17 -unsupported call to `::write_fmt` at fixtures/reject/src/lib.rs:75:10 -a closure that copies a mutable variable at fixtures/reject/src/lib.rs:80:18 -an or-pattern with bindings at fixtures/reject/src/lib.rs:87:3 -unsupported expression: StaticRef at fixtures/reject/src/lib.rs:94:2 -replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs:99:17 -replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs:99:20 -replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs:103:25 -unsupported call to ` as std::iter::Iterator>::cycle` at fixtures/reject/src/lib.rs:115:11 -a `zip` of `std::iter::Chain, {closure@fixtures/reject/src/lib.rs:161:15: 161:18}>, std::iter::Copied>>`, whose closures std calls one item further through `next` than through `fold` at fixtures/reject/src/lib.rs:161:51 -unsupported call to `std::ops::Range::::contains::` at fixtures/reject/src/lib.rs:169:8 -overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:128:2 -an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:148:1 -overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:182:2 -an iterator of the crate implementing `FusedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:202:1 +a method that takes `self` by value that drops, which is not supported yet at fixtures/reject/src/lib.rs:51:20 +a field of a temporary that drops: bind it to a variable first at fixtures/reject/src/lib.rs:62:2 +a closure that captures a value that drops, which is not supported yet at fixtures/reject/src/lib.rs:68:23 +dropping `{closure@fixtures/reject/src/lib.rs:68:10: 68:17}`, which is not supported yet at fixtures/reject/src/lib.rs:66:31 +a `for` loop over values that drop, which is not supported yet at fixtures/reject/src/lib.rs:76:6 +a parameter that drops bound by a pattern, which is not supported yet at fixtures/reject/src/lib.rs:83:21 +a variable from outside the function at fixtures/reject/src/lib.rs:84:2 +dropping `Slot`, which is not supported yet at fixtures/reject/src/lib.rs:93:1 +`Boxed` is generic and implements `Drop`, which is not supported yet at fixtures/reject/src/lib.rs:105:6 +an async body that borrows `u8` mutably, which is no object it can change in place at fixtures/reject/src/lib.rs:113:3 +unsupported type `{async closure@fixtures/reject/src/lib.rs:121:10: 121:18}` at fixtures/reject/src/lib.rs:121:6 +a closure of type `{async closure@fixtures/reject/src/lib.rs:121:10: 121:18}` at fixtures/reject/src/lib.rs:121:10 +a precision, as in `{:.2}`, is not supported at fixtures/reject/src/lib.rs:129:3 +the `+` flag of a format spec is not supported at fixtures/reject/src/lib.rs:130:3 +the `-` flag of a format spec is not supported at fixtures/reject/src/lib.rs:131:3 +`Binary` formatting, as in `{:b}`, is not supported at fixtures/reject/src/lib.rs:132:3 +`Octal` formatting, as in `{:o}`, is not supported at fixtures/reject/src/lib.rs:133:3 +`LowerExp` formatting, as in `{:e}`, is not supported at fixtures/reject/src/lib.rs:134:3 +hex `Debug`, as in `{:x?}`, is not supported at fixtures/reject/src/lib.rs:135:3 +a width or precision from an argument, as in `{:1$}`, is not supported at fixtures/reject/src/lib.rs:136:3 +`Pointer` formatting, as in `{:p}`, is not supported at fixtures/reject/src/lib.rs:137:3 +a `&mut std::string::String` binding: a JS value of `std::string::String` is a copy, so it can't change at fixtures/reject/src/lib.rs:142:16 +`&mut std::string::String`: a JS value of `std::string::String` is a copy, so a reference can't change it at fixtures/reject/src/lib.rs:142:16 +a mutable reference to `std::string::String`, which is no object it can change in place at fixtures/reject/src/lib.rs:144:17 +unsupported call to `::write_fmt` at fixtures/reject/src/lib.rs:144:10 +a closure that copies a mutable variable at fixtures/reject/src/lib.rs:149:18 +an or-pattern with bindings at fixtures/reject/src/lib.rs:156:3 +unsupported expression: StaticRef at fixtures/reject/src/lib.rs:163:2 +replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs:168:17 +replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs:168:20 +replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs:172:25 +dropping `std::sync::Arc<&Noisy>`, which is not supported yet at fixtures/reject/src/lib.rs:183:42 +an `.await` in an operand after one that drops, which is not supported yet: bind the awaited value first at fixtures/reject/src/lib.rs:190:13 +a `let` on a temporary that drops, which it doesn't move out of whole at fixtures/reject/src/lib.rs:200:2 +an `.await` in the place an assignment of a value that drops writes to, which is not supported yet: bind the awaited value first at fixtures/reject/src/lib.rs:207:2 +a `match` on a temporary that drops, which it doesn't move out of whole at fixtures/reject/src/lib.rs:217:2 +replacing the `std::sync::Arc` behind a reference, whose JS object is shared or private at fixtures/reject/src/lib.rs:225:2 +`Poll>>` is not supported: it would be pending two ways with one value at fixtures/reject/src/lib.rs:229:20 +`Option>` is not supported: its `None` and `Some` would both be `undefined` at fixtures/reject/src/lib.rs:234:19 +an `if let` on a temporary that drops, which it doesn't move out of whole at fixtures/reject/src/lib.rs:241:9 +an async body returning `std::task::Poll` is not supported: its `Ready` could be pending at fixtures/reject/src/lib.rs:245:54 +a `match` on a temporary that drops, which it doesn't move out of whole at fixtures/reject/src/lib.rs:252:16 +replacing the `std::pin::Pin>` behind a reference, whose JS object is shared or private at fixtures/reject/src/lib.rs:261:2 +a field of a temporary that drops: bind it to a variable first at fixtures/reject/src/lib.rs:266:2 +replacing the `std::sync::MutexGuard<'_, u8>` behind a reference, whose JS object is shared or private at fixtures/reject/src/lib.rs:271:2 +unsupported call to ` as std::iter::Iterator>::cycle` at fixtures/reject/src/lib.rs:276:11 +a `zip` of `std::iter::Chain, {closure@fixtures/reject/src/lib.rs:322:15: 322:18}>, std::iter::Copied>>`, whose closures std calls one item further through `next` than through `fold` at fixtures/reject/src/lib.rs:322:51 +unsupported call to `std::ops::Range::::contains::` at fixtures/reject/src/lib.rs:330:8 +an iterator that owns `Noisy`, which drops, which is not supported yet at fixtures/reject/src/lib.rs:371:16 +an iterator that owns `std::vec::Vec`, which drops, which is not supported yet at fixtures/reject/src/lib.rs:371:4 +an iterator that owns `Noisy`, which drops, which is not supported yet at fixtures/reject/src/lib.rs:376:20 +a `for` loop over values that drop, which is not supported yet at fixtures/reject/src/lib.rs:392:6 +passing `std::option::Option`, which drops, to `Option::unwrap_or`, which is not supported yet at fixtures/reject/src/lib.rs:400:4 +a rest pattern that moves values that drop out of a place, which is not supported yet at fixtures/reject/src/lib.rs:405:14 +replacing the `std::pin::Pin<&mut Noisy>` behind a reference, whose JS object is shared or private at fixtures/reject/src/lib.rs:411:2 +an array repeat of a value that drops, which is not supported yet at fixtures/reject/src/lib.rs:416:14 +overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:289:2 +an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:309:1 +overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:343:2 +an iterator of the crate implementing `FusedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:363:1 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index 6b6ba014a..62ac830ca 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -40,18 +40,87 @@ pub fn iterators(v: &[u8]) -> bool { v.iter().map(|&b| (b > 1).then_some(b)).next().is_some() } -pub struct Guard; +pub struct Noisy(pub u8); -impl Drop for Guard { +impl Drop for Noisy { fn drop(&mut self) {} } -pub fn guard() -> Guard { - Guard +impl Noisy { + /// A move out of `self` would leave `this` undefined, which JS can't. + pub fn into_inner(self) -> u8 { + self.0 + } } -pub async fn later() -> u8 { - 1 +pub fn make() -> Noisy { + Noisy(1) +} + +/// The temporary drops at the end of the statement, which nothing in JS marks. +pub fn temporary() -> u8 { + make().0 +} + +/// Dropping the closure would drop what it captured. +pub fn owning_closure() -> u8 { + let n = make(); + let f = move || drop(n); + f(); + 0 +} + +/// Each element drops at the end of its iteration, and the rest on a `break`. +pub fn each(v: Vec) -> u8 { + let mut sum = 0; + for n in v { + sum += n.0; + } + sum +} + +/// The part the pattern skips drops at the end of the function, which nothing marks. +pub fn destructured((a, _): (Noisy, Noisy)) -> u8 { + a.0 +} + +pub enum Slot { + Empty, + Full(Noisy), +} + +/// An enum's drop glue would switch on its variant. +pub fn slot(s: Slot) -> bool { + matches!(s, Slot::Empty) +} + +pub struct Boxed(pub T); + +impl Drop for Boxed { + fn drop(&mut self) {} +} + +/// A generic `Drop` impl has an instance per type. +pub fn boxed() -> u8 { + let b = Boxed(1u8); + b.0 +} + +/// The async block's generator would change its own copy of `n`. +pub fn async_borrow() -> u8 { + let mut n = 1u8; + let f = async { + n += 1; + }; + drop(f); + n +} + +/// An async closure is a coroutine closure, not an async body. +pub fn async_closure() -> u8 { + let f = async || 1u8; + drop(f); + 0 } /// Format specs outside what moq uses. @@ -110,6 +179,98 @@ pub fn generic>(t: &mut T) -> f64 { (*t).into() as f64 } +/// An `Arc` of a borrow is the borrowed value in JS: dropping the last one would drop it. +pub fn shared_borrow(n: &Noisy) -> usize { + let a = std::sync::Arc::new(n); + std::sync::Arc::strong_count(&a) +} + +/// `n` would wait out the `.await` where no scope drops it if the future is cancelled. +pub async fn held_across(n: Noisy) -> u8 { + consume(n, async { 1u8 }.await) +} + +fn consume(n: Noisy, v: u8) -> u8 { + drop(n); + v +} + +/// The skipped element drops at the end of the statement, which nothing marks. +pub fn skipped() -> u8 { + let [kept, _] = [make(), make()]; + kept.0 +} + +/// `n` would wait out the `.await` in the index where no scope drops it on a cancel. +pub async fn assigned(n: Noisy) -> u8 { + let mut slots = [make(), make()]; + slots[index().await] = n; + slots[0].0 +} + +async fn index() -> usize { + 1 +} + +/// `Some(..)` leaves the payload in the temporary, which drops after the `match`. +pub fn unnamed() -> u8 { + match Some(make()) { + Some(..) => 1, + None => 0, + } +} + +/// Every clone of an `Arc` is one JS object: replacing the payload would reach them all. +pub fn replace_shared(dst: &mut std::sync::Arc, src: std::sync::Arc) { + *dst = src; +} + +/// `Ready(Some(Pending))` would be `rt.Pending`, as the outer `Pending` is. +pub fn nested_poll(p: std::task::Poll>>) -> bool { + matches!(p, std::task::Poll::Pending) +} + +/// `None` and `Some(Ready(()))` would both be `undefined`. +pub fn ready_unit(p: Option>) -> bool { + matches!(p, None) +} + +/// The payload of the `Some` that fails the test drops after the `if let`. +#[allow(clippy::redundant_pattern_matching, reason = "the `if let` is under test")] +pub fn unmatched() -> u8 { + if let None = Some(make()) { 1 } else { 0 } +} + +/// A future's `Ready(Pending)` would be `rt.Pending`, as its own pending is. +pub async fn pending_output() -> std::task::Poll { + std::task::Poll::Pending +} + +/// The temporary drops after the `match`, which nothing marks. +#[allow(clippy::match_like_matches_macro, reason = "the `match` is under test")] +pub fn tested() -> u8 { + let matched = match Some(make()) { + Some(_) => 1, + None => 0, + }; + matched + 1 +} + +/// A `Pin` is its pointer in JS: replacing the `Arc` would reach every clone. +pub fn replace_pinned(dst: &mut std::pin::Pin>, src: std::pin::Pin>) { + *dst = src; +} + +/// The box drops its `Noisy` at the end of the statement, which nothing marks. +pub fn boxed_field() -> u8 { + Box::new(make()).0 +} + +/// A guard is its mutex in JS: replacing it would copy the other mutex's value in. +pub fn replace_guard<'a>(dst: &mut std::sync::MutexGuard<'a, u8>, src: std::sync::MutexGuard<'a, u8>) { + *dst = src; +} + /// An adaptor without a mapping. pub fn cycled(v: &[u8]) -> usize { v.iter().cycle().take(3).count() @@ -204,3 +365,54 @@ impl std::iter::FusedIterator for Fused {} pub fn fused() -> usize { Fused.fuse().count() } + +/// The items `next` leaves drop with the iterator, which an `rt.Iter` never drops. +pub fn drained(v: Vec) -> Option { + v.into_iter().next() +} + +/// `count` drops each item it consumes. +pub fn counted() -> usize { + (0..2).map(Noisy).count() +} + +/// An iterator that drops, after the loop. +pub struct Countdown(pub Noisy); + +impl Iterator for Countdown { + type Item = u8; + + fn next(&mut self) -> Option { + None + } +} + +pub fn looped(c: Countdown) -> u8 { + let mut n = 0; + for i in c { + n += i; + } + n +} + +/// `unwrap_or` drops its fallback when the option holds a value. +pub fn fallback(a: Option, b: Noisy) -> Noisy { + a.unwrap_or(b) +} + +/// `rest` moves the tail out of `pair`, which would still drop it. +pub fn rest_moved(pair: [Noisy; 2]) -> u8 { + let [first, rest @ ..] = pair; + first.0 + rest[0].0 +} + +/// A pinned reference is its referent in JS: replacing it would change what it points at. +pub fn replace_pinned_ref<'a>(dst: &mut std::pin::Pin<&'a mut Noisy>, src: std::pin::Pin<&'a mut Noisy>) { + *dst = src; +} + +/// `[n; 0]` drops `n` at once, which no element of the array would. +pub fn repeated(n: Noisy) -> usize { + let empty = [n; 0]; + empty.len() +} diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index 2c44978e7..21325e271 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -8,8 +8,15 @@ roots = [ "nested", "sorted", "iterators", - "guard", - "later", + "Noisy", + "temporary", + "owning_closure", + "each", + "destructured", + "slot", + "boxed", + "async_borrow", + "async_closure", "specs", "written", "captured", @@ -18,6 +25,20 @@ roots = [ "swapped", "replaced", "generic", + "shared_borrow", + "held_across", + "skipped", + "assigned", + "unnamed", + "replace_shared", + "nested_poll", + "ready_unit", + "unmatched", + "pending_output", + "tested", + "replace_pinned", + "boxed_field", + "replace_guard", "cycled", "folds", "backs", @@ -25,6 +46,13 @@ roots = [ "keyed", "hinted", "fused", + "drained", + "counted", + "looped", + "fallback", + "rest_moved", + "replace_pinned_ref", + "repeated", "missing", ] printable = ["Meters"] diff --git a/ts/fixtures/task/Cargo.toml b/ts/fixtures/task/Cargo.toml new file mode 100644 index 000000000..b58b1039f --- /dev/null +++ b/ts/fixtures/task/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "task-fixture" +description = "A kio-shaped channel and async code, to test rust2-ts's futures and drops against native Rust" +version = "0.0.0" +edition.workspace = true +license.workspace = true +publish = false + +[lib] +# The cdylib is the native side of the differential test, built for wasm32. +crate-type = ["lib", "cdylib"] + +[features] +# The wasm32 build exports the benchmark's handoff in place of the harness. +bench = [] + +[lints] +workspace = true diff --git a/ts/fixtures/task/examples/handoff.rs b/ts/fixtures/task/examples/handoff.rs new file mode 100644 index 000000000..2b3506c34 --- /dev/null +++ b/ts/fixtures/task/examples/handoff.rs @@ -0,0 +1,8 @@ +//! The benchmark's native route for this fixture: see `fixtures/bench.rs`. + +#[path = "../../bench.rs"] +mod native; + +fn main() { + native::main(task_fixture::bench::run); +} diff --git a/ts/fixtures/task/expected/kio.ts b/ts/fixtures/task/expected/kio.ts new file mode 100644 index 000000000..30cdb7cdc --- /dev/null +++ b/ts/fixtures/task/expected/kio.ts @@ -0,0 +1,301 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +/** A task's waker, shared weakly with every list it registers on. */ +export class Waiter { + waker: rt.Waker; + shared: rt.Arc; + + constructor(fields: { waker: rt.Waker; shared: rt.Arc }) { + this.waker = fields.waker; + this.shared = fields.shared; + } + + [Symbol.dispose](): void { + rt.drop(this.waker); + rt.drop(this.shared); + } + + static new(waker: rt.Waker): Waiter { + try { + let shared = new rt.Arc(waker.clone()); + try { + const waker2 = waker; + waker = undefined!; + const arg = waker2; + const shared2 = shared; + shared = undefined!; + return new Waiter({ waker: arg, shared: shared2 }); + } finally { + rt.drop(shared); + } + } finally { + rt.drop(waker); + } + } + + /** Register with `list`, a no-op while the list still holds this waiter. */ + register(list: WaiterList): void { + list.register(this); + } +} + +/** Wakers waiting for one event, held weakly. */ +export class WaiterList { + entries: rt.Weak[]; + /** Where the next registration looks for a dead slot to reuse. */ + cursor: rt.usize; + + constructor(fields: { entries: rt.Weak[]; cursor: rt.usize }) { + this.entries = fields.entries; + this.cursor = fields.cursor; + } + + static new(): WaiterList { + return new WaiterList({ entries: [], cursor: 0 }); + } + + register(waiter: Waiter): void { + const weak = new rt.Weak(waiter.shared); + for (const entry of this.entries) { + if (entry.arc === weak.arc) { + return; + } + } + let probes: rt.usize = rt.min(this.entries.length, 2); + while (probes > 0) { + if (rt.at(this.entries, this.cursor).arc.strong === 0) { + const value = weak; + this.entries[rt.idx(this.entries, this.cursor)] = value; + return; + } + this.cursor = rt.usize.rem(rt.usize.add(this.cursor, 1), this.entries.length); + probes = rt.usize.sub(probes, 1); + } + if (this.entries.length >= 4) { + let live: rt.Weak[] = []; + const taken = this.entries; + this.entries = []; + for (const entry2 of taken) { + if (entry2.arc.strong > 0) { + live.push(entry2); + } + } + this.entries = live; + this.cursor = 0; + } + this.entries.push(weak); + } + + /** Drain every entry into a new list, leaving this one empty. */ + take(): WaiterList { + this.cursor = 0; + const taken = this.entries; + this.entries = []; + return new WaiterList({ entries: taken, cursor: 0 }); + } + + /** Wake every live waiter, draining the list. */ + wake(): void { + this.cursor = 0; + const taken = this.entries; + this.entries = []; + for (const entry of taken) { + const upgrade = entry.upgrade(); + if (upgrade !== undefined) { + const waker = upgrade; + try { + waker.value.wakeByRef(); + } finally { + rt.drop(waker); + } + } + } + } +} + +/** Holds a parked waiter between polls, so its registrations outlive the poll. */ +export class Park { + _0: Waiter | undefined; + + constructor(_0: Waiter | undefined) { + this._0 = _0; + } + + [Symbol.dispose](): void { + rt.drop(this._0); + } + + static default(): Park { + return new Park(undefined); + } + + /** + * The waiter for this poll: the one held if it wakes the same task, else a new one, + * dropping the old and so its registrations. + */ + hold(cx: rt.Context): Waiter { + let reuse: boolean; + if (this._0 !== undefined) { + const waiter = this._0; + reuse = cx.willWake(waiter.waker); + } else { + reuse = false; + } + if (!reuse) { + const value = Waiter.new(cx.clone()); + rt.drop(this._0); + this._0 = value; + } + return rt.option.unwrap(this._0); + } +} + +/** A future from a poll function that receives a [`Waiter`]. */ +export function wait(poll: (a0: Waiter) => rt.Poll): Pending<(a0: Waiter) => rt.Poll> { + return new Pending({ _poll: poll, park: Park.default() }); +} + +/** What [`wait`] returns. */ +export class Pending { + _poll: F; + park: Park; + + constructor(fields: { _poll: F; park: Park }) { + this._poll = fields._poll; + this.park = fields.park; + } + + [Symbol.dispose](): void { + rt.drop(this._poll); + rt.drop(this.park); + } + + poll(this: Pending<(a0: Waiter) => rt.Poll>, cx: rt.Context): rt.Poll { + const this_ = this; + const waiter = this_.park.hold(cx); + return this_._poll(waiter); + } +} + +export class State { + queue: rt.u64[]; + closed: boolean; + producers: rt.usize; + waiters: WaiterList; + + constructor(fields: { queue: rt.u64[]; closed: boolean; producers: rt.usize; waiters: WaiterList }) { + this.queue = fields.queue; + this.closed = fields.closed; + this.producers = fields.producers; + this.waiters = fields.waiters; + } +} + +/** The sending side of a channel, which closes when the last one drops. */ +export class Producer { + state: rt.Arc>; + + constructor(fields: { state: rt.Arc> }) { + this.state = fields.state; + } + + [Symbol.dispose](): void { + this.drop(); + rt.drop(this.state); + } + + static new(): Producer { + const state = new State({ queue: [], closed: false, producers: 1, waiters: WaiterList.new() }); + return new Producer({ state: new rt.Arc(new rt.Mutex(state)) }); + } + + /** Queue `value` and wake every consumer, after releasing the lock. */ + send(value: rt.u64): void { + let state: rt.Mutex = this.state.value; + state.value.queue.push(value); + let waiters: WaiterList = state.value.waiters.take(); + waiters.wake(); + } + + consume(): Consumer { + return new Consumer({ state: this.state.clone(), next: 0 }); + } + + clone(): Producer { + this.state.value.value.producers = rt.usize.add(this.state.value.value.producers, 1); + return new Producer({ state: this.state.clone() }); + } + + drop(): void { + let state: rt.Mutex = this.state.value; + state.value.producers = rt.usize.sub(state.value.producers, 1); + if (state.value.producers > 0) { + return; + } + state.value.closed = true; + let waiters: WaiterList = state.value.waiters.take(); + waiters.wake(); + } +} + +/** The receiving side of a channel: every value sent, in order. */ +export class Consumer { + state: rt.Arc>; + next: rt.usize; + + constructor(fields: { state: rt.Arc>; next: rt.usize }) { + this.state = fields.state; + this.next = fields.next; + } + + [Symbol.dispose](): void { + rt.drop(this.state); + } + + clone(): Consumer { + return new Consumer({ state: this.state.clone(), next: this.next }); + } + + /** The next value, or `None` once the channel closed and every value was read. */ + pollRecv(waiter: Waiter): rt.Poll { + let state: rt.Mutex = this.state.value; + if (this.next < state.value.queue.length) { + const value = rt.at(state.value.queue, this.next); + this.next = rt.usize.add(this.next, 1); + return value; + } + if (state.value.closed) { + return undefined; + } + waiter.register(state.value.waiters); + return rt.Pending; + } + + /** kio's one-line wrapper over a `poll_*` method. */ + recv(): rt.Future { + return rt.future(recvBody, undefined, this); + } +} + +export function* recvBody(_taskContext: rt.Context, self: Consumer): Generator { + const self2 = self; + let __awaitee: Pending<(a0: Waiter) => rt.Poll> = wait((waiter: Waiter): rt.Poll => { + return self2.pollRecv(waiter); + }); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +//# sourceMappingURL=kio.ts.map diff --git a/ts/fixtures/task/expected/log.ts b/ts/fixtures/task/expected/log.ts new file mode 100644 index 000000000..2bd381560 --- /dev/null +++ b/ts/fixtures/task/expected/log.ts @@ -0,0 +1,42 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +/** A shared, append-only list of events. */ +export class Log { + _0: rt.Arc>; + + constructor(_0: rt.Arc>) { + this._0 = _0; + } + + [Symbol.dispose](): void { + rt.drop(this._0); + } + + clone(): Log { + return new Log(this._0.clone()); + } + + static new(): Log { + return new Log(new rt.Arc(new rt.Mutex([]))); + } + + /** Append one event. */ + note(event: string): void { + this._0.value.value.push(event); + } + + /** Every event so far, one per line. */ + text(): rt.ByteVec { + const events = this._0.value; + let out: rt.ByteVec = new rt.ByteVec(); + for (const event of events.value) { + out.extendFromSlice(rt.string.asBytes(event)); + out.push(10); + } + return out; + } +} + +//# sourceMappingURL=log.ts.map diff --git a/ts/fixtures/task/expected/model.ts b/ts/fixtures/task/expected/model.ts new file mode 100644 index 000000000..e027632a7 --- /dev/null +++ b/ts/fixtures/task/expected/model.ts @@ -0,0 +1,968 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { Producer, type Consumer } from "./kio.ts"; +import { type Log } from "./log.ts"; + +/** A channel's open producers, and a consumer to clone for each task. */ +export class Channel { + producers: Producer[]; + _consumer: Consumer; + + constructor(fields: { producers: Producer[]; _consumer: Consumer }) { + this.producers = fields.producers; + this._consumer = fields._consumer; + } + + [Symbol.dispose](): void { + rt.dropAll(this.producers); + rt.drop(this._consumer); + } + + static open(): Channel { + let producer = Producer.new(); + try { + let consumer = producer.consume(); + try { + let producers: Producer[] = []; + try { + const arg = producers; + const producer2 = producer; + producer = undefined!; + arg.push(producer2); + const producers2 = producers; + producers = undefined!; + const arg2 = producers2; + const consumer2 = consumer; + consumer = undefined!; + return new Channel({ producers: arg2, _consumer: consumer2 }); + } finally { + rt.dropAll(producers); + } + } finally { + rt.drop(consumer); + } + } finally { + rt.drop(producer); + } + } + + /** A consumer that reads every value from the first. */ + consumer(): Consumer { + return this._consumer.clone(); + } + + /** Send `value` through the first producer, if one is open. */ + send(value: rt.u64): boolean { + const first = rt.slice.first(this.producers); + if (first !== undefined) { + const producer = first; + producer.send(value); + return true; + } else { + return false; + } + } + + /** Drop the last producer, if one is open: the last closes the channel. */ + close(): boolean { + const pop = this.producers.pop(); + if (pop !== undefined) { + let producer = pop; + try { + const producer2 = producer; + producer = undefined!; + rt.drop(producer2); + return true; + } finally { + rt.drop(producer); + } + } else { + return false; + } + } + + /** Open another producer, if one is open to clone. */ + cloneProducer(): boolean { + let clone: Producer = undefined!; + try { + const first = rt.slice.first(this.producers); + if (first !== undefined) { + const producer = first; + clone = producer.clone(); + } else { + return false; + } + const arg = this.producers; + const clone2 = clone; + clone = undefined!; + arg.push(clone2); + return true; + } finally { + rt.drop(clone); + } + } +} + +/** Logs its own drop. */ +export class Guard { + id: rt.u32; + log: Log; + + constructor(fields: { id: rt.u32; log: Log }) { + this.id = fields.id; + this.log = fields.log; + } + + [Symbol.dispose](): void { + this.drop(); + rt.drop(this.log); + } + + static new(id: rt.u32, log: Log): Guard { + try { + const arg = id; + const log2 = log; + log = undefined!; + return new Guard({ id: arg, log: log2 }); + } finally { + rt.drop(log); + } + } + + /** Log `event` as this guard's. */ + note(event: string): void { + this.log.note(rt.fmt.format(["g", (f: rt.Formatter) => rt.u32.display(this.id, f), " ", (f: rt.Formatter) => f.pad(event)])); + } + + drop(): void { + this.log.note(rt.fmt.format(["g", (f: rt.Formatter) => rt.u32.display(this.id, f), " drop"])); + } +} + +/** + * Every value until the channel closes, summed, holding a guard of its own across + * each await beside the one it was given. + */ +export function sum(rx: Consumer, guard: Guard): rt.Future { + return rt.future(sumBody, sumDrop, rx, guard); +} + +export function* sumBody(_taskContext: rt.Context, rx: Consumer, guard: Guard): Generator { + let rx2: Consumer = rx; + rx = undefined!; + try { + const guard2 = guard; + guard = undefined!; + try { + const inner = Guard.new(rt.u32.add(guard2.id, 100), guard2.log.clone()); + try { + let total: rt.u64 = 0; + while (true) { + let option: rt.u64 | undefined; + let __awaitee: rt.Future = rx2.recv(); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + option = result; + break; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } + if (option !== undefined) { + const value = option; + inner.log.note(rt.fmt.format(["g", (f: rt.Formatter) => rt.u32.display(guard2.id, f), " got ", (f: rt.Formatter) => rt.u64.display(value, f)])); + total = rt.u64.add(total, value); + } else { + break; + } + } + return total; + } finally { + rt.drop(inner); + } + } finally { + rt.drop(guard2); + } + } finally { + rt.drop(rx2); + } +} + +export function sumDrop(rx: Consumer, guard: Guard): void { + rt.drop(rx); + rt.drop(guard); +} + +/** + * One value read through the captures only an async block makes: a borrow, which + * dropping the block must not drop; a copy, which a later change to the original must + * not reach; a mutable borrow the block replaces whole, which the caller must see; a + * move on one path only; and a move the block only borrows, which drops when the block + * returns or is cancelled. Between them, a value of two types that hold each other. + */ +export function blocks(rx: Consumer, guard: Guard, moves: boolean): rt.Future { + return rt.future(blocksBody, blocksDrop, rx, guard, moves); +} + +export function* blocksBody(_taskContext: rt.Context, rx: Consumer, guard: Guard, moves: boolean): Generator { + let rx2: Consumer = rx; + rx = undefined!; + try { + let guard2 = guard; + guard = undefined!; + try { + const moves2 = moves; + let first = Guard.new(rt.u32.add(guard2.id, 200), guard2.log.clone()); + try { + let second = Guard.new(rt.u32.add(guard2.id, 300), guard2.log.clone()); + try { + rt.drop(rt.future(blocksBody$2, undefined, first)); + turns(guard2.log, rt.u32.rem(guard2.id, 5)); + let tally: Tally = new Tally({ n: guard2.id }); + let seen = rt.future(blocksBody$3, undefined, rt.copy(tally)); + try { + tally.n = rt.u32.add(tally.n, 1000); + const arg2 = guard2; + const arg = tally.n; + let value: rt.u32; + const seen2 = seen; + seen = undefined!; + let __awaitee: rt.Future = seen2; + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + value = result; + break; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } + arg2.note(rt.fmt.format(["tally ", (f: rt.Formatter) => rt.u32.display(arg, f), " then ", (f: rt.Formatter) => rt.u32.display(value, f)])); + let mark: Mark = new Mark({ n: 1 }); + let replaced: rt.u32; + let __awaitee2: rt.Future = rt.future(blocksBody$4, undefined, mark); + try { + while (true) { + const poll2 = __awaitee2.poll(_taskContext); + if (poll2 !== rt.Pending) { + const result2 = poll2; + replaced = result2; + break; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee2); + } + guard2.note(rt.fmt.format(["mark ", (f: rt.Formatter) => rt.u32.display(replaced, f), " ", (f: rt.Formatter) => rt.u32.display(mark.n, f)])); + let cycle = new Outer({ inner: undefined, guard: Guard.new(rt.u32.add(guard2.id, 800), guard2.log.clone()) }); + try { + const cycle2 = cycle; + cycle = undefined!; + const _nested = new Inner({ outer: cycle2 }); + try { + let third = Guard.new(rt.u32.add(guard2.id, 400), guard2.log.clone()); + try { + let chosen: Guard = undefined!; + try { + if (moves2) { + const third2 = third; + third = undefined!; + chosen = third2; + } else { + const third3 = third; + third = undefined!; + chosen = third3; + } + chosen.note("chosen"); + if (moves2) { + const guard22 = guard2; + guard2 = undefined!; + rt.drop(rt.future(blocksBody$5, blocksDrop$2, guard22)); + } + const first2 = first; + first = undefined!; + const second2 = second; + second = undefined!; + const rx22 = rx2; + rx2 = undefined!; + let read_ = rt.future(blocksBody$6, blocksDrop$3, first2, second2, rx22); + try { + const read2 = read_; + read_ = undefined!; + let __awaitee4: rt.Future = read2; + try { + while (true) { + const poll4 = __awaitee4.poll(_taskContext); + if (poll4 !== rt.Pending) { + const result4 = poll4; + return result4; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee4); + } + } finally { + rt.drop(read_); + } + } finally { + rt.drop(chosen); + } + } finally { + rt.drop(third); + } + } finally { + rt.drop(_nested); + } + } finally { + rt.drop(cycle); + } + } finally { + rt.drop(seen); + } + } finally { + rt.drop(second); + } + } finally { + rt.drop(first); + } + } finally { + rt.drop(guard2); + } + } finally { + rt.drop(rx2); + } +} + +export function blocksDrop(rx: Consumer, guard: Guard, _moves: boolean): void { + rt.drop(rx); + rt.drop(guard); +} + +export function* blocksBody$2(_taskContext2: rt.Context, first: Guard): Generator { + first.note("borrowed"); +} + +export function* blocksBody$3(_taskContext3: rt.Context, tally: Tally): Generator { + return read(rt.copy(tally)); +} + +export function* blocksBody$4(_taskContext4: rt.Context, mark: Mark): Generator { + Object.assign(mark, new Mark({ n: 2 })); + return mark.n; +} + +export function* blocksBody$5(_taskContext5: rt.Context, guard2: Guard): Generator { + try { + guard2.note("moved"); + } finally { + rt.drop(guard2); + } +} + +export function blocksDrop$2(guard2: Guard): void { + rt.drop(guard2); +} + +export function* blocksBody$6(_taskContext6: rt.Context, first: Guard, second: Guard, rx2: Consumer): Generator { + try { + try { + try { + first.note("reads"); + second.note("reads"); + const _held = Guard.new(rt.u32.add(second.id, 100), second.log.clone()); + try { + let option: rt.u64 | undefined; + let __awaitee3: rt.Future = rx2.recv(); + try { + while (true) { + const poll3 = __awaitee3.poll(_taskContext6); + if (poll3 !== rt.Pending) { + const result3 = poll3; + option = result3; + break; + } + _taskContext6 = yield; + } + } finally { + rt.drop(__awaitee3); + } + if (option !== undefined) { + const value2 = option; + return value2; + } else { + return 0; + } + } finally { + rt.drop(_held); + } + } finally { + rt.drop(first); + } + } finally { + rt.drop(second); + } + } finally { + rt.drop(rx2); + } +} + +export function blocksDrop$3(first: Guard, second: Guard, rx2: Consumer): void { + rt.drop(first); + rt.drop(second); + rt.drop(rx2); +} + +export class Mark { + n: rt.u32; + + constructor(fields: { n: rt.u32 }) { + this.n = fields.n; + } +} + +/** + * Drops through `guard`, and `Inner` through it: deciding `Outer` first must not leave + * `Inner` decided as dropping nothing. + */ +export class Outer { + inner: Inner | undefined; + guard: Guard; + + constructor(fields: { inner: Inner | undefined; guard: Guard }) { + this.inner = fields.inner; + this.guard = fields.guard; + } + + [Symbol.dispose](): void { + rt.drop(this.inner); + rt.drop(this.guard); + } +} + +export class Inner { + outer: Outer | undefined; + + constructor(fields: { outer: Outer | undefined }) { + this.outer = fields.outer; + } + + [Symbol.dispose](): void { + rt.drop(this.outer); + } +} + +/** A `Copy` value an `async move` block takes its own copy of. */ +export class Tally { + n: rt.u32; + + constructor(fields: { n: rt.u32 }) { + this.n = fields.n; + } +} + +export function read(t: Tally): rt.u32 { + return t.n; +} + +/** `i`, noted as picked. */ +export function pick(log: Log, i: rt.usize): rt.usize { + log.note(rt.fmt.format(["pick ", (f: rt.Formatter) => rt.usize.display(i, f)])); + return i; +} + +/** + * A guard per turn of a loop that ends a guarded `match` arm, so leaving the loop from + * inside a turn's scope must still skip the arms after it. Before them, a pair of + * guards destructured in part: the one left in the array drops with it, after the other; + * a pinned box of one, which drops it; `ref` bindings, which borrow a variable and keep a + * temporary to the end of the block; a `_` binding of an element, which evaluates its + * index and moves nothing; and a boxed guard read twice, which a read leaves boxed. + */ +export function turns(log: Log, count: rt.u32): void { + const pair = [Guard.new(rt.u32.add(count, 10), log.clone()), Guard.new(rt.u32.add(count, 20), log.clone())]; + try { + const kept = pair[0]; + pair[0] = undefined!; + try { + kept.note("kept"); + const pinned = Guard.new(rt.u32.add(count, 40), log.clone()); + try { + pinned.note("pinned"); + const held = Guard.new(rt.u32.add(count, 50), log.clone()); + try { + const borrowed = held; + borrowed.note("borrowed"); + const extended = Guard.new(rt.u32.add(count, 60), log.clone()); + try { + extended.note("extended"); + let value: rt.u32; + const inner = Guard.new(rt.u32.add(count, 90), log.clone()); + try { + value = inner.id; + } finally { + rt.drop(inner); + } + const scoped = value; + log.note(rt.fmt.format(["after ", (f: rt.Formatter) => rt.u32.display(scoped, f)])); + const boxed = Guard.new(rt.u32.add(count, 100), log.clone()); + try { + const once = boxed.id; + log.note(rt.fmt.format(["boxed ", (f: rt.Formatter) => rt.u32.display(once, f), " ", (f: rt.Formatter) => rt.u32.display(boxed.id, f)])); + const spares = [Guard.new(rt.u32.add(count, 70), log.clone()), Guard.new(rt.u32.add(count, 80), log.clone())]; + try { + const index = pick(log, rt.usize.rem(count as rt.usize, 2)); + spares[rt.idx(spares, index)]; + arms: { + if (count === 0) { + log.note("no turns"); + break arms; + } + const n = count; + if (n < 4) { + let left: rt.u32 = n; + while (true) { + const turn = Guard.new(left, log.clone()); + try { + if (left === 1) { + break; + } + turn.note("turn"); + left = rt.u32.sub(left, 1); + } finally { + rt.drop(turn); + } + } + break arms; + } + log.note("many turns"); + } + } finally { + rt.dropAll(spares); + } + } finally { + rt.drop(boxed); + } + } finally { + rt.drop(extended); + } + } finally { + rt.drop(held); + } + } finally { + rt.drop(pinned); + } + } finally { + rt.drop(kept); + } + } finally { + rt.dropAll(pair); + } +} + +/** + * Every value until the channel closes, summed, each read `depth` async functions + * deep: 1, 4, or 16. + */ +export function drain(rx: Consumer, depth: rt.u32): rt.Future { + return rt.future(drainBody, drainDrop, rx, depth); +} + +export function* drainBody(_taskContext: rt.Context, rx: Consumer, depth: rt.u32): Generator { + let rx2: Consumer = rx; + rx = undefined!; + try { + const depth2 = depth; + let total: rt.u64 = 0; + while (true) { + let next: rt.u64 | undefined; + if (depth2 === 1) { + let __awaitee: rt.Future = rx2.recv(); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + next = result; + break; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } + } else if (depth2 === 4) { + let __awaitee2: rt.Future = level3(rx2); + try { + while (true) { + const poll2 = __awaitee2.poll(_taskContext); + if (poll2 !== rt.Pending) { + const result2 = poll2; + next = result2; + break; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee2); + } + } else { + let __awaitee3: rt.Future = level15(rx2); + try { + while (true) { + const poll3 = __awaitee3.poll(_taskContext); + if (poll3 !== rt.Pending) { + const result3 = poll3; + next = result3; + break; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee3); + } + } + if (next !== undefined) { + const value = next; + total = rt.u64.add(total, value); + } else { + break; + } + } + return total; + } finally { + rt.drop(rx2); + } +} + +export function drainDrop(rx: Consumer, _depth: rt.u32): void { + rt.drop(rx); +} + +export function level1(rx: Consumer): rt.Future { + return rt.future(level1Body, undefined, rx); +} + +export function* level1Body(_taskContext: rt.Context, rx: Consumer): Generator { + const rx2 = rx; + let __awaitee: rt.Future = rx2.recv(); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +export function level10(rx: Consumer): rt.Future { + return rt.future(level10Body, undefined, rx); +} + +export function* level10Body(_taskContext: rt.Context, rx: Consumer): Generator { + const rx2 = rx; + let __awaitee: rt.Future = level9(rx2); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +export function level11(rx: Consumer): rt.Future { + return rt.future(level11Body, undefined, rx); +} + +export function* level11Body(_taskContext: rt.Context, rx: Consumer): Generator { + const rx2 = rx; + let __awaitee: rt.Future = level10(rx2); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +export function level12(rx: Consumer): rt.Future { + return rt.future(level12Body, undefined, rx); +} + +export function* level12Body(_taskContext: rt.Context, rx: Consumer): Generator { + const rx2 = rx; + let __awaitee: rt.Future = level11(rx2); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +export function level13(rx: Consumer): rt.Future { + return rt.future(level13Body, undefined, rx); +} + +export function* level13Body(_taskContext: rt.Context, rx: Consumer): Generator { + const rx2 = rx; + let __awaitee: rt.Future = level12(rx2); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +export function level14(rx: Consumer): rt.Future { + return rt.future(level14Body, undefined, rx); +} + +export function* level14Body(_taskContext: rt.Context, rx: Consumer): Generator { + const rx2 = rx; + let __awaitee: rt.Future = level13(rx2); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +export function level15(rx: Consumer): rt.Future { + return rt.future(level15Body, undefined, rx); +} + +export function* level15Body(_taskContext: rt.Context, rx: Consumer): Generator { + const rx2 = rx; + let __awaitee: rt.Future = level14(rx2); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +export function level2(rx: Consumer): rt.Future { + return rt.future(level2Body, undefined, rx); +} + +export function* level2Body(_taskContext: rt.Context, rx: Consumer): Generator { + const rx2 = rx; + let __awaitee: rt.Future = level1(rx2); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +export function level3(rx: Consumer): rt.Future { + return rt.future(level3Body, undefined, rx); +} + +export function* level3Body(_taskContext: rt.Context, rx: Consumer): Generator { + const rx2 = rx; + let __awaitee: rt.Future = level2(rx2); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +export function level4(rx: Consumer): rt.Future { + return rt.future(level4Body, undefined, rx); +} + +export function* level4Body(_taskContext: rt.Context, rx: Consumer): Generator { + const rx2 = rx; + let __awaitee: rt.Future = level3(rx2); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +export function level5(rx: Consumer): rt.Future { + return rt.future(level5Body, undefined, rx); +} + +export function* level5Body(_taskContext: rt.Context, rx: Consumer): Generator { + const rx2 = rx; + let __awaitee: rt.Future = level4(rx2); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +export function level6(rx: Consumer): rt.Future { + return rt.future(level6Body, undefined, rx); +} + +export function* level6Body(_taskContext: rt.Context, rx: Consumer): Generator { + const rx2 = rx; + let __awaitee: rt.Future = level5(rx2); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +export function level7(rx: Consumer): rt.Future { + return rt.future(level7Body, undefined, rx); +} + +export function* level7Body(_taskContext: rt.Context, rx: Consumer): Generator { + const rx2 = rx; + let __awaitee: rt.Future = level6(rx2); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +export function level8(rx: Consumer): rt.Future { + return rt.future(level8Body, undefined, rx); +} + +export function* level8Body(_taskContext: rt.Context, rx: Consumer): Generator { + const rx2 = rx; + let __awaitee: rt.Future = level7(rx2); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +export function level9(rx: Consumer): rt.Future { + return rt.future(level9Body, undefined, rx); +} + +export function* level9Body(_taskContext: rt.Context, rx: Consumer): Generator { + const rx2 = rx; + let __awaitee: rt.Future = level8(rx2); + try { + while (true) { + const poll = __awaitee.poll(_taskContext); + if (poll !== rt.Pending) { + const result = poll; + return result; + } + _taskContext = yield; + } + } finally { + rt.drop(__awaitee); + } +} + +//# sourceMappingURL=model.ts.map diff --git a/ts/fixtures/task/src/bench.rs b/ts/fixtures/task/src/bench.rs new file mode 100644 index 000000000..0c7e22e54 --- /dev/null +++ b/ts/fixtures/task/src/bench.rs @@ -0,0 +1,71 @@ +//! The handoff `just ts bench` times on every route: a value sent to a task parked on the +//! channel, which wakes it, polls it through `depth` async functions, and parks it again. +//! Not translated: `bench/task.ts` runs the same loop on the translation with its own +//! executor. +//! +//! The input is the depth, then the count of values as a big-endian `u32`; the output is +//! the task's sum, as a big-endian `u64`. + +use std::future::Future; +use std::pin::Pin; +use std::sync::{Arc, Mutex}; +use std::task::{Context, Poll, Wake, Waker}; + +use crate::model::{self, Channel}; + +/// Run one handoff workload. +pub fn run(input: &[u8]) -> Vec { + let depth = u32::from(input[0]); + let count = u32::from_be_bytes([input[1], input[2], input[3], input[4]]); + let channel = Channel::open(); + let mut task = Task::new(model::drain(channel.consumer(), depth)); + task.run(); + for value in 0..count { + channel.send(u64::from(value)); + task.run(); + } + drop(channel); + let total = task.run().expect("the task finishes once its channel closes"); + total.to_be_bytes().to_vec() +} + +/// One task, polled whenever its waker was woken: an executor that only ever runs one. +struct Task { + future: Pin>, + woken: Arc, + waker: Waker, +} + +struct Woken(Mutex); + +impl Wake for Woken { + fn wake(self: Arc) { + self.wake_by_ref(); + } + + fn wake_by_ref(self: &Arc) { + *self.0.lock().unwrap() = true; + } +} + +impl Task { + fn new(future: F) -> Self { + let woken = Arc::new(Woken(Mutex::new(true))); + Self { + future: Box::pin(future), + waker: Waker::from(woken.clone()), + woken, + } + } + + /// Poll while woken, returning the output once it is ready. + fn run(&mut self) -> Option { + while std::mem::take(&mut *self.woken.0.lock().unwrap()) { + let mut cx = Context::from_waker(&self.waker); + if let Poll::Ready(output) = self.future.as_mut().poll(&mut cx) { + return Some(output); + } + } + None + } +} diff --git a/ts/fixtures/task/src/exec.rs b/ts/fixtures/task/src/exec.rs new file mode 100644 index 000000000..f880393f3 --- /dev/null +++ b/ts/fixtures/task/src/exec.rs @@ -0,0 +1,162 @@ +//! A deterministic FIFO executor that logs every poll, wake, and waker clone and drop: +//! the native side of the executor `tests/task.ts` runs the translation on, which logs +//! the same events. Not translated: it is the boundary, built on `dyn Future` and a +//! hand-made waker. + +use std::collections::VecDeque; +use std::future::Future; +use std::pin::Pin; +use std::sync::{Arc, Mutex}; +use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker}; + +use crate::log::Log; + +/// What every waker of one task points at. +struct Wake { + task: usize, + log: Log, + queue: Arc>>, + /// Whether the task is in the queue, so a second wake before its poll adds nothing. + queued: Mutex, +} + +impl Wake { + fn schedule(&self) { + let mut queued = self.queued.lock().unwrap(); + if !*queued { + *queued = true; + self.queue.lock().unwrap().push_back(self.task); + } + } +} + +static VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake_by_ref, drop); + +/// # Safety +/// `data` is an `Arc` that `waker` made. +unsafe fn clone(data: *const ()) -> RawWaker { + // SAFETY: the waker being cloned holds a count. + unsafe { Arc::increment_strong_count(data.cast::()) }; + // SAFETY: as above, it is live. + let wake = unsafe { &*data.cast::() }; + wake.log.note(format!("t{} waker clone", wake.task)); + RawWaker::new(data, &VTABLE) +} + +unsafe fn wake(data: *const ()) { + // SAFETY: the waker being consumed holds a count, which this takes. + let wake = unsafe { Arc::from_raw(data.cast::()) }; + wake.log.note(format!("t{} wake", wake.task)); + wake.schedule(); +} + +unsafe fn wake_by_ref(data: *const ()) { + // SAFETY: the waker holds a count. + let wake = unsafe { &*data.cast::() }; + wake.log.note(format!("t{} wake", wake.task)); + wake.schedule(); +} + +unsafe fn drop(data: *const ()) { + // SAFETY: the waker being dropped holds a count, which this releases. + let wake = unsafe { Arc::from_raw(data.cast::()) }; + wake.log.note(format!("t{} waker drop", wake.task)); +} + +fn waker(wake: Arc) -> Waker { + let data = Arc::into_raw(wake).cast::<()>(); + // SAFETY: the vtable's functions treat `data` as the `Arc` it is. + unsafe { Waker::from_raw(RawWaker::new(data, &VTABLE)) } +} + +struct Task { + future: Pin>>, + waker: Waker, + wake: Arc, + done: bool, +} + +/// Runs tasks in the order they were woken. +pub struct Executor { + log: Log, + queue: Arc>>, + /// Every task spawned, by id; `None` once dropped. + tasks: Vec>, +} + +impl Executor { + pub fn new(log: Log) -> Self { + Self { + log, + queue: Arc::new(Mutex::new(VecDeque::new())), + tasks: Vec::new(), + } + } + + /// How many tasks were spawned, dropped ones included. + pub fn len(&self) -> usize { + self.tasks.len() + } + + /// Spawn a task and queue its first poll. + pub fn spawn(&mut self, future: impl Future + 'static) { + let task = self.tasks.len(); + self.log.note(format!("t{task} spawn")); + let wake = Arc::new(Wake { + task, + log: self.log.clone(), + queue: self.queue.clone(), + queued: Mutex::new(false), + }); + wake.schedule(); + self.tasks.push(Some(Task { + future: Box::pin(future), + waker: waker(wake.clone()), + wake, + done: false, + })); + } + + /// Poll queued tasks until none is left. + pub fn run(&mut self) { + while self.step() {} + } + + /// Poll the next queued task, if any. A dropped or finished one is skipped. + pub fn step(&mut self) -> bool { + let Some(task) = self.queue.lock().unwrap().pop_front() else { + return false; + }; + if self.tasks[task].as_ref().is_some_and(|t| !t.done) { + self.poll(task); + } + true + } + + /// Poll a task whether or not it is queued or finished: polling a finished one panics. + pub fn poll(&mut self, task: usize) { + let Some(t) = self.tasks[task].as_mut() else { + return; + }; + *t.wake.queued.lock().unwrap() = false; + self.log.note(format!("t{task} poll")); + let mut cx = Context::from_waker(&t.waker); + match t.future.as_mut().poll(&mut cx) { + Poll::Ready(value) => { + t.done = true; + self.log.note(format!("t{task} ready {value}")); + } + Poll::Pending => self.log.note(format!("t{task} pending")), + } + } + + /// Drop a task's future, then its waker. + pub fn drop(&mut self, task: usize) { + let Some(t) = self.tasks[task].take() else { + return; + }; + self.log.note(format!("t{task} drop")); + std::mem::drop(t.future); + std::mem::drop(t.waker); + } +} diff --git a/ts/fixtures/task/src/harness.rs b/ts/fixtures/task/src/harness.rs new file mode 100644 index 000000000..ca40a6255 --- /dev/null +++ b/ts/fixtures/task/src/harness.rs @@ -0,0 +1,85 @@ +//! 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. + +use crate::exec::Executor; +use crate::log::Log; +use crate::model::{self, Channel, Guard}; + +/// Run one script. +pub fn run(input: &[u8]) -> Vec { + let log = Log::new(); + let mut exec = Executor::new(log.clone()); + let mut channels: Vec = Vec::new(); + for step in input.chunks(2) { + let (op, arg) = (step[0], step.get(1).copied().unwrap_or(0)); + let pick = |len: usize| (len > 0).then(|| usize::from(arg) % len); + match op % 11 { + 0 => { + log.note(format!("c{} open", channels.len())); + channels.push(Channel::open()); + } + 1 => { + if let Some(c) = pick(channels.len()) { + let guard = Guard::new(exec.len() as u32, log.clone()); + exec.spawn(model::sum(channels[c].consumer(), guard)); + } + } + 2 => { + if let Some(c) = pick(channels.len()) { + let depth = [1, 4, 16][usize::from(arg / 16) % 3]; + exec.spawn(model::drain(channels[c].consumer(), depth)); + } + } + 3 => { + if let Some(c) = pick(channels.len()) { + log.note(format!("c{c} send {arg}")); + if !channels[c].send(u64::from(arg)) { + log.note(format!("c{c} closed")); + } + } + } + 4 => { + if let Some(c) = pick(channels.len()) { + log.note(format!("c{c} close")); + channels[c].close(); + } + } + 5 => { + if let Some(c) = pick(channels.len()) { + log.note(format!("c{c} clone")); + channels[c].clone_producer(); + } + } + 6 => exec.run(), + 7 => _ = exec.step(), + 8 => { + if let Some(t) = pick(exec.len()) { + exec.poll(t); + } + } + 9 => { + if let Some(t) = pick(exec.len()) { + exec.drop(t); + } + } + _ => { + if let Some(c) = pick(channels.len()) { + let guard = Guard::new(exec.len() as u32, log.clone()); + exec.spawn(model::blocks(channels[c].consumer(), guard, arg >= 128)); + } + } + } + } + for t in 0..exec.len() { + exec.drop(t); + } + for (c, channel) in channels.into_iter().enumerate() { + log.note(format!("c{c} drop")); + drop(channel); + } + log.text() +} diff --git a/ts/fixtures/task/src/kio.rs b/ts/fixtures/task/src/kio.rs new file mode 100644 index 000000000..f14620b99 --- /dev/null +++ b/ts/fixtures/task/src/kio.rs @@ -0,0 +1,241 @@ +//! moq's kio, trimmed to what its futures hinge on, at the moq revision `just ts moq` +//! pins (`rs/kio/src/waiter.rs`, `pending.rs`, `producer.rs`, and `consumer.rs`). +//! +//! A `WaiterList` holds each waiter's waker weakly, so a waiter that drops, as its +//! `Park` replaces it or its future is cancelled, frees its slots with no deregister. +//! The channel closes when its last producer drops. kio's atomics, tag records, and +//! inline storage are left out: they change no event the test logs. + +use std::future::Future; +use std::pin::Pin; +use std::sync::{Arc, Mutex, Weak}; +use std::task::{Context, Poll, Waker}; + +/// A task's waker, shared weakly with every list it registers on. +pub struct Waiter { + waker: Waker, + shared: Arc, +} + +impl Waiter { + pub fn new(waker: Waker) -> Self { + let shared = Arc::new(waker.clone()); + Self { waker, shared } + } + + /// Register with `list`, a no-op while the list still holds this waiter. + pub fn register(&self, list: &mut WaiterList) { + list.register(self); + } +} + +/// Wakers waiting for one event, held weakly. +pub struct WaiterList { + entries: Vec>, + /// Where the next registration looks for a dead slot to reuse. + cursor: usize, +} + +impl WaiterList { + pub fn new() -> Self { + Self { + entries: Vec::new(), + cursor: 0, + } + } + + pub fn register(&mut self, waiter: &Waiter) { + let weak = Arc::downgrade(&waiter.shared); + for entry in &self.entries { + if Weak::ptr_eq(entry, &weak) { + return; + } + } + // Probe two slots at the cursor for one whose waiter dropped. + let mut probes = self.entries.len().min(2); + while probes > 0 { + if self.entries[self.cursor].strong_count() == 0 { + self.entries[self.cursor] = weak; + return; + } + self.cursor = (self.cursor + 1) % self.entries.len(); + probes -= 1; + } + if self.entries.len() >= 4 { + // Sweep every dead slot before growing. + let mut live = Vec::new(); + for entry in std::mem::take(&mut self.entries) { + if entry.strong_count() > 0 { + live.push(entry); + } + } + self.entries = live; + self.cursor = 0; + } + self.entries.push(weak); + } + + /// Drain every entry into a new list, leaving this one empty. + pub fn take(&mut self) -> Self { + self.cursor = 0; + Self { + entries: std::mem::take(&mut self.entries), + cursor: 0, + } + } + + /// Wake every live waiter, draining the list. + pub fn wake(&mut self) { + self.cursor = 0; + for entry in std::mem::take(&mut self.entries) { + if let Some(waker) = entry.upgrade() { + waker.wake_by_ref(); + } + } + } +} + +/// Holds a parked waiter between polls, so its registrations outlive the poll. +#[derive(Default)] +pub struct Park(Option); + +impl Park { + /// The waiter for this poll: the one held if it wakes the same task, else a new one, + /// dropping the old and so its registrations. + pub fn hold(&mut self, cx: &Context<'_>) -> &Waiter { + let reuse = match &self.0 { + Some(waiter) => cx.waker().will_wake(&waiter.waker), + None => false, + }; + if !reuse { + self.0 = Some(Waiter::new(cx.waker().clone())); + } + self.0.as_ref().unwrap() + } +} + +/// A future from a poll function that receives a [`Waiter`]. +pub fn wait(poll: F) -> Pending +where + F: FnMut(&Waiter) -> Poll + Unpin, +{ + Pending { + poll, + park: Park::default(), + } +} + +/// What [`wait`] returns. +pub struct Pending { + poll: F, + park: Park, +} + +impl Future for Pending +where + F: FnMut(&Waiter) -> Poll + Unpin, +{ + type Output = R; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let this = &mut *self; + let waiter = this.park.hold(cx); + (this.poll)(waiter) + } +} + +struct State { + queue: Vec, + closed: bool, + producers: usize, + waiters: WaiterList, +} + +/// The sending side of a channel, which closes when the last one drops. +pub struct Producer { + state: Arc>, +} + +impl Producer { + pub fn new() -> Self { + let state = State { + queue: Vec::new(), + closed: false, + producers: 1, + waiters: WaiterList::new(), + }; + Self { + state: Arc::new(Mutex::new(state)), + } + } + + /// Queue `value` and wake every consumer, after releasing the lock. + pub fn send(&self, value: u64) { + let mut waiters = { + let mut state = self.state.lock().unwrap(); + state.queue.push(value); + state.waiters.take() + }; + waiters.wake(); + } + + pub fn consume(&self) -> Consumer { + Consumer { + state: self.state.clone(), + next: 0, + } + } +} + +impl Clone for Producer { + fn clone(&self) -> Self { + self.state.lock().unwrap().producers += 1; + Self { + state: self.state.clone(), + } + } +} + +impl Drop for Producer { + fn drop(&mut self) { + let mut waiters = { + let mut state = self.state.lock().unwrap(); + state.producers -= 1; + if state.producers > 0 { + return; + } + state.closed = true; + state.waiters.take() + }; + waiters.wake(); + } +} + +/// The receiving side of a channel: every value sent, in order. +#[derive(Clone)] +pub struct Consumer { + state: Arc>, + next: usize, +} + +impl Consumer { + /// The next value, or `None` once the channel closed and every value was read. + pub fn poll_recv(&mut self, waiter: &Waiter) -> Poll> { + let mut state = self.state.lock().unwrap(); + if self.next < state.queue.len() { + let value = state.queue[self.next]; + self.next += 1; + return Poll::Ready(Some(value)); + } + if state.closed { + return Poll::Ready(None); + } + waiter.register(&mut state.waiters); + Poll::Pending + } + + /// kio's one-line wrapper over a `poll_*` method. + pub async fn recv(&mut self) -> Option { + wait(|waiter| self.poll_recv(waiter)).await + } +} diff --git a/ts/fixtures/task/src/lib.rs b/ts/fixtures/task/src/lib.rs new file mode 100644 index 000000000..aca8645c2 --- /dev/null +++ b/ts/fixtures/task/src/lib.rs @@ -0,0 +1,25 @@ +//! A kio-shaped channel and the async code moq builds on it, exercised against native +//! Rust: laziness, poll and wake order, and every drop, including a future's on cancel. +//! +//! `kio` trims moq's kio to what its futures hinge on: a `Waiter` holding the task's +//! waker behind an `Arc` that `WaiterList`s hold weakly, `wait`, and a channel whose +//! last producer closes it on drop. `model` holds the async functions. `exec` is a +//! deterministic FIFO executor that logs every poll, wake, and waker clone and drop, and +//! `harness` runs a script of executor steps through it; `tests/task.ts` runs the same +//! script on the translation with its own executor. `bench` holds the handoff `just ts +//! bench` times. None of `exec`, `harness`, and `bench` is translated. + +pub mod bench; +pub mod exec; +pub mod harness; +pub mod kio; +pub mod log; +pub mod model; +#[cfg(target_arch = "wasm32")] +#[path = "../../wasm.rs"] +mod wasm; +/// What the wasm32 build exports: the benchmark's handoff with `bench`, else the harness. +#[cfg(all(target_arch = "wasm32", feature = "bench"))] +use bench::run as entry; +#[cfg(all(target_arch = "wasm32", not(feature = "bench")))] +use harness::run as entry; diff --git a/ts/fixtures/task/src/log.rs b/ts/fixtures/task/src/log.rs new file mode 100644 index 000000000..bcc3c9aa9 --- /dev/null +++ b/ts/fixtures/task/src/log.rs @@ -0,0 +1,29 @@ +//! The trace both sides of the differential test write, one event per line. + +use std::sync::{Arc, Mutex}; + +/// A shared, append-only list of events. +#[derive(Clone, Default)] +pub struct Log(Arc>>); + +impl Log { + pub fn new() -> Self { + Self(Arc::new(Mutex::new(Vec::new()))) + } + + /// Append one event. + pub fn note(&self, event: String) { + self.0.lock().unwrap().push(event); + } + + /// Every event so far, one per line. + pub fn text(&self) -> Vec { + let events = self.0.lock().unwrap(); + let mut out = Vec::new(); + for event in events.iter() { + out.extend_from_slice(event.as_bytes()); + out.push(b'\n'); + } + out + } +} diff --git a/ts/fixtures/task/src/model.rs b/ts/fixtures/task/src/model.rs new file mode 100644 index 000000000..8d54111dd --- /dev/null +++ b/ts/fixtures/task/src/model.rs @@ -0,0 +1,267 @@ +//! What the harness spawns and drives: a consumer loop holding guards across its awaits, +//! the same loop under a stack of async functions, and the channels they read. + +use crate::kio::{Consumer, Producer}; +use crate::log::Log; + +/// A channel's open producers, and a consumer to clone for each task. +pub struct Channel { + producers: Vec, + consumer: Consumer, +} + +impl Channel { + pub fn open() -> Self { + let producer = Producer::new(); + let consumer = producer.consume(); + let mut producers = Vec::new(); + producers.push(producer); + Self { producers, consumer } + } + + /// A consumer that reads every value from the first. + pub fn consumer(&self) -> Consumer { + self.consumer.clone() + } + + /// Send `value` through the first producer, if one is open. + pub fn send(&self, value: u64) -> bool { + match self.producers.first() { + Some(producer) => { + producer.send(value); + true + } + None => false, + } + } + + /// Drop the last producer, if one is open: the last closes the channel. + pub fn close(&mut self) -> bool { + match self.producers.pop() { + Some(producer) => { + drop(producer); + true + } + None => false, + } + } + + /// Open another producer, if one is open to clone. + pub fn clone_producer(&mut self) -> bool { + let clone = match self.producers.first() { + Some(producer) => producer.clone(), + None => return false, + }; + self.producers.push(clone); + true + } +} + +/// Logs its own drop. +pub struct Guard { + id: u32, + log: Log, +} + +impl Guard { + pub fn new(id: u32, log: Log) -> Self { + Self { id, log } + } + + /// Log `event` as this guard's. + fn note(&self, event: &str) { + self.log.note(format!("g{} {event}", self.id)); + } +} + +impl Drop for Guard { + fn drop(&mut self) { + self.log.note(format!("g{} drop", self.id)); + } +} + +/// Every value until the channel closes, summed, holding a guard of its own across +/// each await beside the one it was given. +pub async fn sum(mut rx: Consumer, guard: Guard) -> u64 { + let inner = Guard::new(guard.id + 100, guard.log.clone()); + let mut total = 0; + while let Some(value) = rx.recv().await { + inner.log.note(format!("g{} got {value}", guard.id)); + total += value; + } + total +} + +/// One value read through the captures only an async block makes: a borrow, which +/// dropping the block must not drop; a copy, which a later change to the original must +/// not reach; a mutable borrow the block replaces whole, which the caller must see; a +/// move on one path only; and a move the block only borrows, which drops when the block +/// returns or is cancelled. Between them, a value of two types that hold each other. +pub async fn blocks(mut rx: Consumer, guard: Guard, moves: bool) -> u64 { + let first = Guard::new(guard.id + 200, guard.log.clone()); + let second = Guard::new(guard.id + 300, guard.log.clone()); + drop(async { first.note("borrowed") }); + turns(&guard.log, guard.id % 5); + let mut tally = Tally { n: guard.id }; + let seen = async move { read(tally) }; + tally.n += 1000; + guard.note(&format!("tally {} then {}", tally.n, seen.await)); + let mut mark = Mark { n: 1 }; + let replaced = async { + mark = Mark { n: 2 }; + mark.n + } + .await; + guard.note(&format!("mark {replaced} {}", mark.n)); + let cycle = Outer { + inner: None, + guard: Guard::new(guard.id + 800, guard.log.clone()), + }; + let _nested = Inner { + outer: Some(Box::new(cycle)), + }; + // A move on each arm of a `match` simple enough to be an expression. + let third = Guard::new(guard.id + 400, guard.log.clone()); + let chosen = match moves { + true => third, + false => third, + }; + chosen.note("chosen"); + if moves { + drop(async move { guard.note("moved") }); + } + let read = async move { + first.note("reads"); + second.note("reads"); + let _held = Guard::new(second.id + 100, second.log.clone()); + match rx.recv().await { + Some(value) => value, + None => 0, + } + }; + read.await +} + +struct Mark { + n: u32, +} + +/// Drops through `guard`, and `Inner` through it: deciding `Outer` first must not leave +/// `Inner` decided as dropping nothing. +struct Outer { + inner: Option>, + guard: Guard, +} + +struct Inner { + outer: Option>, +} + +/// A `Copy` value an `async move` block takes its own copy of. +#[derive(Clone, Copy)] +struct Tally { + n: u32, +} + +fn read(t: Tally) -> u32 { + t.n +} + +/// `i`, noted as picked. +fn pick(log: &Log, i: usize) -> usize { + log.note(format!("pick {i}")); + i +} + +/// A guard per turn of a loop that ends a guarded `match` arm, so leaving the loop from +/// inside a turn's scope must still skip the arms after it. Before them, a pair of +/// guards destructured in part: the one left in the array drops with it, after the other; +/// a pinned box of one, which drops it; `ref` bindings, which borrow a variable and keep a +/// temporary to the end of the block; a `_` binding of an element, which evaluates its +/// index and moves nothing; and a boxed guard read twice, which a read leaves boxed. +#[expect(clippy::toplevel_ref_arg, reason = "the `ref` bindings are under test")] +fn turns(log: &Log, count: u32) { + let pair = [Guard::new(count + 10, log.clone()), Guard::new(count + 20, log.clone())]; + let [kept, _] = pair; + kept.note("kept"); + let pinned = std::pin::Pin::new(Box::new(Guard::new(count + 40, log.clone()))); + pinned.note("pinned"); + let held = Guard::new(count + 50, log.clone()); + let ref borrowed = held; + borrowed.note("borrowed"); + let ref extended = Guard::new(count + 60, log.clone()); + extended.note("extended"); + let scoped = { + let ref inner = Guard::new(count + 90, log.clone()); + inner.id + }; + log.note(format!("after {scoped}")); + let boxed = Box::new(Guard::new(count + 100, log.clone())); + let once = boxed.id; + log.note(format!("boxed {once} {}", boxed.id)); + let spares = [Guard::new(count + 70, log.clone()), Guard::new(count + 80, log.clone())]; + let _ = spares[pick(log, count as usize % 2)]; + match count { + 0 => log.note(format!("no turns")), + n if n < 4 => { + let mut left = n; + loop { + let turn = Guard::new(left, log.clone()); + if left == 1 { + break; + } + turn.note("turn"); + left -= 1; + } + } + _ => log.note(format!("many turns")), + } +} + +/// Every value until the channel closes, summed, each read `depth` async functions +/// deep: 1, 4, or 16. +pub async fn drain(mut rx: Consumer, depth: u32) -> u64 { + let mut total = 0; + loop { + let next = match depth { + 1 => rx.recv().await, + 4 => level3(&mut rx).await, + _ => level15(&mut rx).await, + }; + match next { + Some(value) => total += value, + None => break, + } + } + total +} + +/// `levelN` reads through N async functions over `recv`. +macro_rules! levels { + ($($name:ident => $next:ident,)*) => { + $(async fn $name(rx: &mut Consumer) -> Option { + $next(rx).await + })* + }; +} + +async fn level1(rx: &mut Consumer) -> Option { + rx.recv().await +} + +levels! { + level2 => level1, + level3 => level2, + level4 => level3, + level5 => level4, + level6 => level5, + level7 => level6, + level8 => level7, + level9 => level8, + level10 => level9, + level11 => level10, + level12 => level11, + level13 => level12, + level14 => level13, + level15 => level14, +} diff --git a/ts/fixtures/task/ts.toml b/ts/fixtures/task/ts.toml new file mode 100644 index 000000000..21343ef6d --- /dev/null +++ b/ts/fixtures/task/ts.toml @@ -0,0 +1,4 @@ +package = "task-fixture" +# What the hand-written harness, tests/task.ts, calls: the futures it spawns, and the +# channels and log it drives them with. +roots = ["log::Log", "model::Channel", "model::Guard", "model::sum", "model::drain", "model::blocks"] diff --git a/ts/justfile b/ts/justfile index 037a37bf9..580570991 100644 --- a/ts/justfile +++ b/ts/justfile @@ -84,13 +84,18 @@ bench checkout="": (moq checkout) cargo run --locked --quiet -- --config fixtures/codec/ts.toml --out "$out/translated" cargo build --locked --quiet --package codec-fixture --features bench --target wasm32-unknown-unknown --profile wasm-release cargo build --locked --quiet --package codec-fixture --example bench --release + cargo run --locked --quiet -- --config fixtures/task/ts.toml --out "$out/task" + cargo build --locked --quiet --package task-fixture --features bench --target wasm32-unknown-unknown --profile wasm-release + cargo build --locked --quiet --package task-fixture --example handoff --release # moq's hand-written TS imports its npm dependencies; its postinstall builds libav. (cd "$src" && bun install --frozen-lockfile --ignore-scripts --filter @moq/net) # moq-wasm's package, as moq's `just js wasm` builds it. (cd "$src" && cargo build --locked --quiet --package moq-wasm --target wasm32-unknown-unknown --profile wasm-release) wasm-bindgen --target web --out-name moq --out-dir "$out/moq-wasm" "$src/target/wasm32-unknown-unknown/wasm-release/moq_wasm.wasm" bun bench/main.ts --translated "$out/translated" --moq "$src" --moq-wasm "$out/moq-wasm" --out "$out" \ - --wasm target/wasm32-unknown-unknown/wasm-release/codec_fixture.wasm --native target/release/examples/bench + --wasm target/wasm32-unknown-unknown/wasm-release/codec_fixture.wasm --native target/release/examples/bench \ + --task "$out/task" \ + --task-wasm target/wasm32-unknown-unknown/wasm-release/task_fixture.wasm --task-native target/release/examples/handoff # Run cargo on the pinned nightly. cargo *args: diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index c12c94828..8bbd20f7c 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -5,6 +5,7 @@ // never wraps silently. Every panic's message is the one native Rust panics with. export * from "./rt/iter.ts"; +export * from "./rt/task.ts"; /** A Rust panic. */ export class Panic extends Error { diff --git a/ts/runtime/rt/task.ts b/ts/runtime/rt/task.ts new file mode 100644 index 000000000..0309952fb --- /dev/null +++ b/ts/runtime/rt/task.ts @@ -0,0 +1,260 @@ +// Ownership and async, for rust2-ts output: what Rust's `Drop`, `Arc`, `Mutex`, and +// futures do that plain JS does not. `rt.ts` re-exports all of it. +// +// A value whose drop JS can observe has `[Symbol.dispose]`, which runs its `Drop` impl +// and drops its fields. A moved-out place holds `undefined`, which drops as nothing. + +import { Panic, panic } from "../rt.ts"; + +// Drops + +/** Drop `value`: run its `[Symbol.dispose]`, if it has one. */ +export function drop(value: unknown): void { + const dispose = (value as Partial | null | undefined)?.[Symbol.dispose]; + if (dispose !== undefined) dispose.call(value); +} + +/** Drop every element of `values`, in order. */ +export function dropAll(values: readonly unknown[] | undefined): void { + if (values === undefined) return; + for (const value of values) drop(value); +} + +// Shared ownership + +/** `Arc`: every clone is this one object, counted. */ +export class Arc implements Disposable { + strong = 1; + value: T; + + constructor(value: T) { + this.value = value; + } + + clone(): Arc { + this.strong++; + return this; + } + + [Symbol.dispose](): void { + if (--this.strong === 0) drop(this.value); + } +} + +/** `Weak`, which nothing observes dropping. */ +export class Weak { + readonly arc: Arc; + + constructor(arc: Arc) { + this.arc = arc; + } + + upgrade(): Arc | undefined { + return this.arc.strong > 0 ? this.arc.clone() : undefined; + } + + clone(): Weak { + return new Weak(this.arc); + } +} + +/** + * `Mutex`, which a single thread never contends, and its guard: a lock is the mutex + * itself, and an unlock is nothing. + */ +export class Mutex implements Disposable { + value: T; + + constructor(value: T) { + this.value = value; + } + + [Symbol.dispose](): void { + drop(this.value); + } +} + +// Futures + +/** `Poll::Pending`; `Poll::Ready(v)` is `v` itself. */ +export const Pending: unique symbol = Symbol("Pending"); + +/** `Poll`. */ +export type Poll = T | typeof Pending; + +/** `Waker`: what an executor hands a future to wake its task. */ +export interface Waker extends Disposable { + wake(): void; + wakeByRef(): void; + clone(): Waker; + willWake(other: Waker): boolean; +} + +/** `Context`, which carries only its waker. */ +export type Context = Waker; + +/** A future, as executors poll it. Dropping it cancels it. */ +export interface Future extends Disposable { + poll(cx: Context): Poll; +} + +/** + * The body of an `async fn` or block, a generator of what it captured: it yields at + * each `.await` that is pending. + */ +export type Body = (cx: Context, ...upvars: A) => Generator; + +const Unresumed = 0; +const Suspended = 1; +const Returned = 2; +const Panicked = 3; + +/** + * An `async fn`'s future, around its body as a generator. Nothing runs until the first + * poll. Dropping it unresumed drops what it captured, and suspended runs the `finally` + * blocks that drop its live locals. + */ +class Async implements Future { + #state = Unresumed; + #body: Body; + #drop: ((...upvars: A) => void) | undefined; + #upvars: A | undefined; + #gen: Generator | undefined; + + constructor(body: Body, drop: ((...upvars: A) => void) | undefined, upvars: A) { + this.#body = body; + this.#drop = drop; + this.#upvars = upvars; + } + + poll(cx: Context): Poll { + const state = this.#state; + if (state === Returned) panic("`async fn` resumed after completion"); + if (state === Panicked) panic("`async fn` resumed after panicking"); + this.#state = Panicked; + let next: IteratorResult; + if (state === Unresumed) { + const gen = this.#body(cx, ...this.#upvars!); + this.#upvars = undefined; + this.#gen = gen; + next = gen.next(); + } else { + next = this.#gen!.next(cx); + } + if (next.done) { + this.#state = Returned; + this.#gen = undefined; + return next.value; + } + this.#state = Suspended; + return Pending; + } + + [Symbol.dispose](): void { + const state = this.#state; + this.#state = Returned; + if (state === Unresumed) this.#drop?.(...this.#upvars!); + else if (state === Suspended) this.#gen!.return(undefined as T); + } +} + +/** + * An `async fn`'s future: `body` runs on what it captured at the first poll, and `drop` + * drops that if it never does. + */ +export function future( + body: Body, + drop: ((...upvars: A) => void) | undefined, + ...upvars: A +): Future { + return new Async(body, drop, upvars); +} + +// Executor + +/** + * Run `future` to completion on the microtask queue, as a promise of its output. A + * panic rejects the promise with the `Panic`. + */ +export function spawn(future: Future): Promise { + return new Promise((resolve, reject) => new Task(future, resolve, reject).schedule()); +} + +/** How many polls one microtask runs before it yields to a macrotask, so a task that keeps waking itself cannot starve I/O. */ +const BUDGET = 128; + +/** Tasks woken since the last run, in order. */ +const queue: { run(): void }[] = []; +let scheduled = false; + +// `scheduled` stays set while a batch runs, so a task it wakes joins the batch rather +// than queueing a microtask that would run ahead of the macrotask. +function run(): void { + try { + for (let polls = 0; polls < BUDGET && queue.length > 0; polls++) queue.shift()!.run(); + } finally { + if (queue.length > 0) setTimeout(run, 0); + else scheduled = false; + } +} + +/** A spawned future, which is also the waker of every poll it gets. */ +class Task implements Waker { + #future: Future | undefined; + #resolve: (value: T) => void; + #reject: (err: unknown) => void; + #queued = false; + + constructor(future: Future, resolve: (value: T) => void, reject: (err: unknown) => void) { + this.#future = future; + this.#resolve = resolve; + this.#reject = reject; + } + + schedule(): void { + if (this.#queued || this.#future === undefined) return; + this.#queued = true; + queue.push(this); + if (!scheduled) { + scheduled = true; + queueMicrotask(run); + } + } + + run(): void { + this.#queued = false; + const future = this.#future; + if (future === undefined) return; + let output: Poll; + try { + output = future.poll(this); + if (output === Pending) return; + this.#future = undefined; + future[Symbol.dispose](); + } catch (err) { + this.#future = undefined; + this.#reject(err); + if (!(err instanceof Panic)) throw err; + return; + } + this.#resolve(output); + } + + wake(): void { + this.schedule(); + } + + wakeByRef(): void { + this.schedule(); + } + + clone(): Waker { + return this; + } + + willWake(other: Waker): boolean { + return other === this; + } + + [Symbol.dispose](): void {} +} diff --git a/ts/runtime/tsconfig.json b/ts/runtime/tsconfig.json index b45ac6972..3bd1f2377 100644 --- a/ts/runtime/tsconfig.json +++ b/ts/runtime/tsconfig.json @@ -5,6 +5,7 @@ "noUnusedLocals": true, "noUnusedParameters": true, "target": "es2022", + "lib": ["es2022", "esnext.disposable", "dom"], "module": "esnext", "moduleResolution": "bundler", "allowImportingTsExtensions": true, diff --git a/ts/src/census.rs b/ts/src/census.rs index 564ef1985..384b428a1 100644 --- a/ts/src/census.rs +++ b/ts/src/census.rs @@ -134,6 +134,7 @@ const VERBATIM: &[&str] = &[ "()", "+", "-", + ".await", "?", "Binary", "Copy", @@ -147,6 +148,7 @@ const VERBATIM: &[&str] = &[ "Octal", "Option", "Pointer", + "Ready", "Result", "Some", "UpperExp", @@ -156,8 +158,10 @@ const VERBATIM: &[&str] = &[ "for", "format_args!", "if", + "if let", "let", "let ... else", + "match", "next", "ref mut", "self", diff --git a/ts/src/lower/body.rs b/ts/src/lower/body.rs index 9d91c0682..12afe0c62 100644 --- a/ts/src/lower/body.rs +++ b/ts/src/lower/body.rs @@ -15,6 +15,7 @@ use rustc_middle::ty::adjustment::PointerCoercion; use rustc_middle::ty::{self, GenericArgsRef, Instance, Ty, TypeVisitableExt}; use rustc_span::{Span, Symbol}; +use super::drop::Owned; use super::library::{self, Args}; use super::{AdtRepr, ObjectKind, Translator, camel, escape}; use crate::program::SymKind; @@ -42,6 +43,10 @@ pub fn function<'a, 'tcx>( breakables: Vec::new(), ret: sig.output(), visited: FxHashSet::default(), + scopes: Vec::new(), + moved: FxHashSet::default(), + borrowed: FxHashSet::default(), + yields: 0, done: super::iter::is_next(tcx, def_id), }; let span = tcx.def_span(def_id); @@ -50,9 +55,11 @@ pub fn function<'a, 'tcx>( } let mut body = Vec::new(); let mut params = Vec::new(); + b.open_scope(); for (i, param) in thir.params.iter().enumerate() { let ty = b.subst(param.ty); - let this = i == 0 && param.self_kind.is_some(); + // An explicit `self: Pin<&mut Self>` has no implicit self kind. + let this = i == 0 && (param.self_kind.is_some() || method); match (¶m.pat, this) { (Some(pat), true) => { let PatKind::Binding { var, .. } = pat.kind else { @@ -68,6 +75,13 @@ pub fn function<'a, 'tcx>( mutable: false, }, ); + // `this` can't be left `undefined`, as a move out of it would. + if method && !matches!(ty.kind(), ty::Ref(..)) && b.t.drops(ty) { + b.t.err( + pat.span, + "a method that takes `self` by value that drops, which is not supported yet", + ); + } match method { // A generic class's method is one instance's, so `this` is that // instance's type, under which its fields have their types. @@ -108,6 +122,7 @@ pub fn function<'a, 'tcx>( false => Dest::Return, }; b.stmt(expr, dest, &mut body); + b.close_scope(&mut body, 0, span); let ret = match (b.done, ret.kind()) { (true, ty::Adt(_, args)) => Type::Union(vec![b.t.ty(args.type_at(0), span), Type::Rt("Done", Vec::new())]), _ => b.t.ret_ty(ret, span), @@ -120,6 +135,7 @@ pub fn function<'a, 'tcx>( body, loc: None, doc: None, + generator: false, }) } @@ -168,6 +184,15 @@ pub struct Body<'b, 'a, 'tcx> { /// Expressions lowered so far, by THIR and id, so a refused call lowers only the /// arguments its mapping did not. visited: FxHashSet<(*const Thir<'tcx>, ExprId)>, + /// The variables each open scope drops when it closes, innermost last. + pub(super) scopes: Vec>>, + /// The variables a move left `undefined`, which are declared `let` for it. + pub(super) moved: FxHashSet, + /// The variables a generator lowered so far borrows mutably: each is a parameter + /// holding the caller's object, so replacing it whole must change that object. + borrowed: FxHashSet, + /// How many `yield`s are lowered so far, so an operand can tell whether it awaits. + yields: usize, /// Whether this is an `Iterator::next`, which returns an item or `rt.DONE` rather /// than an `Option`: its closures are not. done: bool, @@ -249,8 +274,8 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } } - /// Declare a binding for a Rust variable. - /// Declare a binding for a Rust variable of type `ty`. + /// Declare a binding for a Rust variable of type `ty`, which its scope drops if it + /// `owns` the value rather than borrowing it. pub(super) fn bind_var( &mut self, var: LocalVarId, @@ -258,6 +283,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { mutable: bool, ty: Ty<'tcx>, span: Span, + owns: bool, ) -> String { // A JS variable holding a number is a copy: writing through it reaches nothing. if let ty::Ref(_, inner, rustc_ast::Mutability::Mut) = self.subst(ty).kind() @@ -276,6 +302,10 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { mutable, }, ); + if owns { + let ty = self.subst(ty); + self.own(&name, ty); + } name } @@ -296,7 +326,14 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { true => *name, false => Symbol::intern(&format!("_{name}")), }; - self.bind_var(*var, name, mutable, pat.ty, pat.span) + self.bind_var(*var, name, mutable, pat.ty, pat.span, true) + } + _ if self.t.drops(self.subst(pat.ty)) => { + self.t.err( + pat.span, + "a parameter that drops bound by a pattern, which is not supported yet", + ); + self.fresh("arg") } PatKind::Wild => self.fresh("_"), _ => { @@ -370,7 +407,9 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { Some(body) => self.lp(*region_scope, body, dest, out), None => self.stmt(*value, dest, out), }, - ExprKind::ValueTypeAscription { source, .. } => self.stmt(*source, dest, out), + ExprKind::ValueTypeAscription { source, .. } | ExprKind::ValueExpr { source } => { + self.stmt(*source, dest, out) + } ExprKind::NeverToAny { source } => self.stmt(*source, Dest::Discard, out), ExprKind::Block { block } => self.block(*block, dest, out), ExprKind::If { @@ -409,6 +448,13 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { return; } let value = self.expr(id, out); + // A value that goes nowhere drops here. + if let Dest::Discard = dest + && let Some(dropped) = self.t.drop_value(value.clone(), ty, span) + { + self.push(out, StmtKind::Expr(dropped), span); + return; + } self.deliver(value, &dest, span, out); } } @@ -471,12 +517,15 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { fn block_body(&mut self, id: BlockId, dest: Dest, out: &mut Vec) { let block = &self.thir[id]; + let start = out.len(); + self.open_scope(); for &stmt in &block.stmts { self.stmt_item(stmt, out); } if let Some(expr) = block.expr { self.stmt(expr, dest, out); } + self.close_scope(out, start, block.span); } fn stmt_item(&mut self, id: StmtId, out: &mut Vec) { @@ -530,20 +579,37 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { self.ref_binding(mode.0, pat_ty, pat.span); } let mutable = mode.1 == rustc_ast::Mutability::Mut; + let by_value = mode.0 == rustc_hir::ByRef::No; let ty = self.t.ty(pat_ty, pat.span); let Some(init) = init else { - let name = self.bind_var(*var, *name, true, pat_ty, pat.span); + let name = self.bind_var(*var, *name, true, pat_ty, pat.span, by_value); self.push(out, StmtKind::decl(true, name, Some(ty), None), span); return; }; + // A `ref` binding borrows a place where it is, and keeps a temporary alive to the + // end of the block, which then drops it as it would a variable. + let borrows = !by_value && self.is_place(init); + let owns = !borrows; if self.needs_dest(init) { - let name = self.bind_var(*var, *name, true, pat_ty, pat.span); + let name = self.bind_var(*var, *name, true, pat_ty, pat.span, owns); self.push(out, StmtKind::decl(true, name.clone(), Some(ty), None), span); self.stmt(init, Dest::Assign(name), out); return; } - let value = self.operand(init, out); - let name = self.bind_var(*var, *name, mutable, pat_ty, pat.span); + // A move from another variable or field: it is left `undefined`. + if by_value && self.moves(init) { + let place = self.place(init, out); + let name = self.bind_var(*var, *name, mutable, pat_ty, pat.span, owns); + let annotate = mutable.then_some(ty); + self.push(out, StmtKind::decl(mutable, name, annotate, Some(place.clone())), span); + self.clear(place, out); + return; + } + let value = match borrows { + true => self.place(init, out), + false => self.operand(init, out), + }; + let name = self.bind_var(*var, *name, mutable, pat_ty, pat.span, owns); let annotate = mutable || needs_annotation(&value); self.push( out, @@ -560,8 +626,40 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } return; }; + // `let _ = value;` drops a value at once, and evaluates a place without moving it: + // an index still runs, and checks its bounds. + if let PatKind::Wild = pat.kind + && els.is_none() + && self.t.drops(pat_ty) + { + match self.is_place(init) { + true => { + let place = self.place(init, out); + if !place.is_path() { + self.push(out, StmtKind::Expr(place), span); + } + } + false => { + let value = self.operand(init, out); + if let Some(dropped) = self.t.drop_value(value, pat_ty, span) { + self.push(out, StmtKind::Expr(dropped), span); + } + } + } + return; + } + // What the pattern leaves unbound drops with its place, or at the end of the + // statement from a temporary, which nothing marks. The shortcut below would move a + // place out whole, so a place binds part by part instead. + let leaves = self.leaves_drop(pat); + if leaves && !self.is_place(init) { + self.t.err( + span, + "a `let` on a temporary that drops, which it doesn't move out of whole", + ); + } // `const [a, b] = ...` for a tuple or array of plain bindings. - if els.is_none() && self.destructurable(pat, true) { + if els.is_none() && !leaves && self.destructurable(pat, true) { let value = self.operand(init, out); let mut mutable = false; let target = self.destructure(pat, &mut mutable); @@ -607,6 +705,61 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { self.pat_bind(pat, &scrut, owned, out); } + /// Whether binding `pat` by value leaves part of a value that drops unbound: under a + /// `_` or a `..`, in a field it doesn't name, or in another variant than the one it + /// matches. + fn leaves_drop(&mut self, pat: &Pat<'tcx>) -> bool { + let ty = self.subst(pat.ty); + if !self.t.drops(ty) { + return false; + } + let tcx = self.t.tcx; + match &pat.kind { + PatKind::Binding { + mode, subpattern: None, .. + } => mode.0 != rustc_hir::ByRef::No, + PatKind::Leaf { subpatterns } | PatKind::Variant { subpatterns, .. } => { + let (variant, others) = match (ty.kind(), &pat.kind) { + (ty::Tuple(tys), _) => (tys.to_vec(), Vec::new()), + (ty::Adt(adt, args), PatKind::Variant { variant_index, .. }) => { + let fields = |v: &ty::VariantDef| -> Vec> { + v.fields.iter().map(|f| f.ty(tcx, args).skip_norm_wip()).collect() + }; + let others = adt + .variants() + .iter_enumerated() + .filter(|(i, _)| i != variant_index) + .flat_map(|(_, v)| fields(v)) + .collect(); + (fields(adt.variant(*variant_index)), others) + } + (ty::Adt(adt, args), _) => ( + adt.non_enum_variant() + .fields + .iter() + .map(|f| f.ty(tcx, args).skip_norm_wip()) + .collect(), + Vec::new(), + ), + _ => return true, + }; + others.into_iter().any(|t| self.t.drops(t)) + || variant.into_iter().enumerate().any(|(i, field)| { + match subpatterns.iter().find(|f| f.field.index() == i) { + Some(f) => self.leaves_drop(&f.pattern), + None => self.t.drops(field), + } + }) + } + PatKind::Array { + prefix, + slice: None, + suffix, + } => prefix.iter().chain(suffix.iter()).any(|p| self.leaves_drop(p)), + _ => true, + } + } + /// Whether `pat` is a tuple or array of plain bindings, which JS destructures. fn destructurable(&self, pat: &Pat<'tcx>, top: bool) -> bool { let ty = self.subst(pat.ty); @@ -666,7 +819,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { PatKind::Binding { name, var, mode, .. } => { let is_mut = mode.1 == rustc_ast::Mutability::Mut; *mutable |= is_mut; - Target::Name(self.bind_var(*var, *name, is_mut, pat.ty, pat.span)) + Target::Name(self.bind_var(*var, *name, is_mut, pat.ty, pat.span, mode.0 == rustc_hir::ByRef::No)) } _ => unreachable!("checked by destructurable"), } @@ -681,7 +834,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { /// Whether `id` is control flow that reads best delivering into a variable. One that /// never makes a value leaves the variable unassigned. - fn needs_dest(&self, id: ExprId) -> bool { + fn needs_dest(&mut self, id: ExprId) -> bool { match &self.thir[id].kind { ExprKind::Scope { value, .. } => self.needs_dest(*value), ExprKind::If { @@ -696,24 +849,32 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } /// Whether `id` lowers to an expression with no statements before it. - fn simple(&self, id: ExprId) -> bool { + fn simple(&mut self, id: ExprId) -> bool { + self.simple_at(id, false) + } + + /// [`Self::simple`], for `id` read as a place, which a move out of would clear. + fn simple_at(&mut self, id: ExprId, place: bool) -> bool { + // A move leaves `undefined` behind with a statement of its own. + if !place && self.moves(id) { + return false; + } match &self.thir[id].kind { ExprKind::Scope { value, .. } | ExprKind::ValueTypeAscription { source: value, .. } - | ExprKind::PlaceTypeAscription { source: value, .. } - | ExprKind::Borrow { arg: value, .. } + | ExprKind::PlaceTypeAscription { source: value, .. } => self.simple_at(*value, place), + ExprKind::Borrow { arg: value, .. } | ExprKind::Deref { arg: value } - | ExprKind::Cast { source: value } + | ExprKind::Field { lhs: value, .. } => self.simple_at(*value, true), + ExprKind::Cast { source: value } | ExprKind::PointerCoercion { source: value, .. } - | ExprKind::Field { lhs: value, .. } | ExprKind::Unary { arg: value, .. } => self.simple(*value), ExprKind::Block { block } => { let block = &self.thir[*block]; block.stmts.is_empty() && !block.targeted_by_break && block.expr.is_some_and(|e| self.simple(e)) } - ExprKind::Binary { lhs, rhs, .. } | ExprKind::Index { lhs, index: rhs } => { - self.simple(*lhs) && self.simple(*rhs) - } + ExprKind::Binary { lhs, rhs, .. } => self.simple(*lhs) && self.simple(*rhs), + ExprKind::Index { lhs, index } => self.simple_at(*lhs, true) && self.simple(*index), ExprKind::LogicalOp { lhs, rhs, .. } => self.simple(*lhs) && self.simple(*rhs), ExprKind::Call { args, fun, .. } => self.simple(*fun) && args.iter().all(|a| self.simple(*a)), ExprKind::Tuple { fields } | ExprKind::Array { fields } => fields.iter().all(|f| self.simple(*f)), @@ -898,6 +1059,14 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { match part { CondPart::Bool(id) => Some(self.expr(*id, pre)), CondPart::Let(expr, pat) => { + // A failed test leaves the value whole, so the pattern must take all of it + // from every variant: a `let` with no other arm to take the rest. + if !self.is_place(*expr) && self.leaves_drop(pat) { + self.t.err( + pat.span, + "an `if let` on a temporary that drops, which it doesn't move out of whole", + ); + } let reads = self.test_reads(pat) + bindings(pat); let (scrut, owned) = self.scrutinee(*expr, reads, pre); let test = self.pat_test(pat, &scrut); @@ -955,8 +1124,10 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { // One condition, as JS writes it. if let [part] = parts.as_slice() { let mut then_stmts = Vec::new(); + self.open_scope(); let test = self.cond_part(part, out, &mut then_stmts); self.stmt(then, dest.clone(), &mut then_stmts); + self.close_scope(&mut then_stmts, 0, span); let mut else_stmts = Vec::new(); if let Some(els) = els { self.stmt(els, dest, &mut else_stmts); @@ -980,12 +1151,17 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { // A let chain: each part nests in the last, and any failing runs the else. let label = self.fresh("chain"); let mut levels = Vec::new(); + self.open_scope(); for part in &parts { let mut pre = Vec::new(); let mut binds = Vec::new(); let test = self.cond_part(part, &mut pre, &mut binds); levels.push((pre, test, binds)); } + if self.scope_owns() { + self.t.err(span, "a let chain that binds a value that drops"); + } + self.scopes.pop(); let mut then_stmts = Vec::new(); self.stmt(then, dest.clone(), &mut then_stmts); let mut else_stmts = Vec::new(); @@ -1054,6 +1230,32 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } let thir: &'a Thir<'tcx> = self.thir; + // One arm binding the whole value, as `.await` desugars to, is a `let` scoped to it. + if let [arm] = arms + && thir[*arm].guard.is_none() + && let PatKind::Binding { + mode, subpattern: None, .. + } = thir[*arm].pattern.kind + && mode.0 == rustc_hir::ByRef::No + { + let start = out.len(); + self.open_scope(); + self.let_(&thir[*arm].pattern, Some(scrutinee), None, span, out); + self.stmt(thir[*arm].body, dest, out); + self.close_scope(out, start, span); + return; + } + if !self.is_place(scrutinee) + && self.t.drops(self.ty_of(scrutinee)) + && !arms + .iter() + .all(|&arm| thir[arm].guard.is_none() && takes_all(&thir[arm].pattern)) + { + self.t.err( + span, + "a `match` on a temporary that drops, which it doesn't move out of whole", + ); + } let pats: Vec<&Pat<'tcx>> = arms.iter().map(|&arm| &*thir[arm].pattern).collect(); // The last arm is the else, whose test never runs, unless it is guarded. let tested = match arms.last().is_some_and(|&arm| thir[arm].guard.is_none()) { @@ -1080,6 +1282,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { let arm = &self.thir[arm]; let test = self.pat_test(&arm.pattern, &scrut); let mut binds = Vec::new(); + self.open_scope(); self.pat_bind(&arm.pattern, &scrut, owned, &mut binds); let guard = arm.guard.map(|g| { let mut pre = Vec::new(); @@ -1088,6 +1291,15 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { }); let mut body = Vec::new(); self.stmt(arm.body, dest.clone(), &mut body); + // What the arm binds that drops is dropped around its bindings and body. + if self.scope_owns() { + if guard.is_some() { + self.t.err(arm.span, "a guarded arm that binds a value that drops"); + } + binds.append(&mut body); + body = std::mem::take(&mut binds); + } + self.close_scope(&mut body, 0, arm.span); lowered.push(Lowered { test, binds, @@ -1190,7 +1402,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { /// Whether a `match` reads as a ternary chain: no bindings or guards, and arms that /// are each a simple value. - fn ternary_match(&self, id: ExprId) -> bool { + fn ternary_match(&mut self, id: ExprId) -> bool { let ExprKind::Match { arms, .. } = &self.thir[self.strip(id)].kind else { return false; }; @@ -1371,6 +1583,14 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { is_loop: true, }); let mut stmts = Vec::new(); + // The iterator drops after the loop, with what it did not yield. + if self.t.drops(self.subst(pat.ty)) || self.t.drops(iter_ty) { + self.t.err( + pat.span, + "a `for` loop over values that drop, which is not supported yet", + ); + } + self.open_scope(); let simple_name = match &pat.kind { PatKind::Binding { name, @@ -1379,7 +1599,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { subpattern: None, .. } if mode.1 == rustc_ast::Mutability::Not && !self.needs_copy(self.subst(pat.ty)) => { - Some(self.bind_var(*var, *name, false, pat.ty, pat.span)) + Some(self.bind_var(*var, *name, false, pat.ty, pat.span, mode.0 == rustc_hir::ByRef::No)) } PatKind::Wild => Some(self.fresh("_")), _ => None, @@ -1397,6 +1617,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } }; self.stmt(loop_body, Dest::Discard, &mut stmts); + self.close_scope(&mut stmts, 0, span); let breakable = self.breakables.pop().unwrap(); let label = breakable.used.then_some(breakable.label); let kind = match source { @@ -1553,18 +1774,70 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { fn assign(&mut self, lhs: ExprId, rhs: ExprId, span: Span, out: &mut Vec) { let mut value = self.operand(rhs, out); let mut pre = Vec::new(); + let yields = self.yields; let target = self.place(lhs, &mut pre); + // The value waits out the `.await` where no scope drops it if the future is cancelled. + if self.yields != yields && self.t.drops(self.ty_of(rhs)) { + self.t.err( + self.span(lhs), + "an `.await` in the place an assignment of a value that drops writes to, which is not supported yet: bind the awaited value first", + ); + } if (!pre.is_empty() || !target.is_path()) && !value.is_constant() { value = self.spill(value, "value", out); } out.extend(pre); - // `*r = v` through a reference replaces what it points to. - if let ExprKind::Deref { arg } = self.thir[self.strip(lhs)].kind - && matches!(self.ty_of(arg).kind(), ty::Ref(..)) - && self.is_place(arg) - { + // The old value drops once the new one is made. + let lty = self.ty_of(lhs); + if self.t.drops(lty) { + if !target.is_path() && !matches!(target, Expr::Index(..)) { + self.t + .err(span, "an assignment that drops the old value of a computed place"); + return; + } + if !value.is_path() && !value.is_constant() { + value = self.spill(value, "value", out); + } + if let Some(dropped) = self.t.drop_value(target.clone(), lty, span) { + self.push(out, StmtKind::Expr(dropped), span); + } + } + // `*r = v` through a reference replaces what it points to, as does replacing a + // variable a generator borrows mutably. + let through_ref = match self.thir[self.strip(lhs)].kind { + ExprKind::Deref { arg } => matches!(self.ty_of(arg).kind(), ty::Ref(..)) && self.is_place(arg), + ExprKind::UpvarRef { var_hir_id, .. } => self.borrowed.contains(&var_hir_id.0), + _ => false, + }; + if through_ref { let pointee = self.ty_of(lhs); + // Every clone of an `Arc` or waker is one JS object, a guard is its mutex, a + // reference is its referent, and a future keeps its state private: copying fields + // in would change the other handles, or nothing. A `Pin` or `Box` is its pointee + // in JS. + let mut erased = pointee; + while let ty::Adt(adt, args) = erased.kind() + && matches!( + library::Std::of(self.t.tcx, adt.did()), + Some(library::Std::Pin | library::Std::Box) + ) { + erased = args.type_at(0); + } + let shared = match erased.kind() { + ty::Adt(adt, _) => matches!( + library::Std::of(self.t.tcx, adt.did()), + Some(library::Std::Arc | library::Std::Waker | library::Std::Context | library::Std::MutexGuard) + ), + ty::Ref(..) | ty::Coroutine(..) => true, + _ => false, + }; match self.t.object_kind(pointee) { + Some(ObjectKind::Class) if shared => { + self.t.err( + span, + format!("replacing the `{pointee}` behind a reference, whose JS object is shared or private"), + ); + } Some(ObjectKind::Class) => { let e = Expr::ident("Object").member("assign").call(vec![target, value]); self.push(out, StmtKind::Expr(e), span); @@ -1627,9 +1900,20 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { /// could change. pub fn operands(&mut self, ids: &[ExprId], out: &mut Vec) -> Vec { let mut values: Vec = Vec::with_capacity(ids.len()); + // Whether an earlier operand is a value that drops, which a later `.await` would + // hold where no scope drops it if the future is cancelled. + let mut holds = false; for &id in ids { let mut pre = Vec::new(); + let yields = self.yields; let value = self.operand(id, &mut pre); + if holds && self.yields != yields { + self.t.err( + self.span(id), + "an `.await` in an operand after one that drops, which is not supported yet: bind the awaited value first", + ); + } + holds |= self.t.drops(self.ty_of(id)); if !pre.is_empty() { for earlier in values.iter_mut() { if !earlier.is_constant() { @@ -1653,11 +1937,54 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { ExprKind::VarRef { .. } | ExprKind::UpvarRef { .. } | ExprKind::Field { .. } | ExprKind::Index { .. } => { true } - ExprKind::Deref { .. } => true, + // What a `Box` made in place points at is part of that temporary. + ExprKind::Deref { arg } => matches!(self.ty_of(*arg).kind(), ty::Ref(..)) || self.is_place(*arg), _ => false, } } + /// Whether `id` makes a temporary that drops, which only a statement could drop. + fn temporary_drops(&mut self, id: ExprId) -> bool { + !self.is_place(id) && self.t.drops(self.ty_of(id)) + } + + /// Whether lowering `id` for its value moves it out of a place that drops it. + pub fn moves(&mut self, id: ExprId) -> bool { + let ty = self.ty_of(id); + self.is_place(id) && !self.t.is_copy(ty) && self.t.drops(ty) + } + + /// Whether a block binds a variable that drops, so its value can't outlive it. + fn block_owns(&mut self, id: BlockId) -> bool { + let thir: &'a Thir<'tcx> = self.thir; + let mut owns = false; + for &stmt in &thir[id].stmts { + if let thir::StmtKind::Let { + pattern, initializer, .. + } = &thir[stmt].kind + { + // A `ref` binding keeps a temporary alive to the end of the block. + if let PatKind::Binding { + mode, subpattern: None, .. + } = pattern.kind + && mode.0 != rustc_hir::ByRef::No + && let Some(init) = *initializer + && !self.is_place(init) + { + owns |= self.t.drops(self.ty_of(init)); + } + pattern.walk_always(|p| { + if let PatKind::Binding { mode, ty, .. } = p.kind + && mode.0 == rustc_hir::ByRef::No + { + owns |= self.t.drops(self.t.subst(self.args, ty)); + } + }); + } + } + owns + } + pub fn needs_copy(&self, ty: Ty<'tcx>) -> bool { if !self.t.is_copy(ty) { return false; @@ -1718,7 +2045,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { ExprKind::ValueTypeAscription { source, .. } | ExprKind::ValueExpr { source } => self.expr(*source, out), // The temp is never assigned: control never gets past the source. ExprKind::NeverToAny { .. } => self.via_temp(id, ty, span, out), - ExprKind::Block { block } if !self.thir[*block].targeted_by_break => { + ExprKind::Block { block } if !self.thir[*block].targeted_by_break && !self.block_owns(*block) => { let block = &self.thir[*block]; for &stmt in &block.stmts { self.stmt_item(stmt, out); @@ -1798,10 +2125,17 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { }; return self.fn_value(def_id, args.skip_binder(), span); } + if self.moves(id) { + let place = self.place(id, out); + return self.move_out(place, span, out); + } let value = self.place(id, out); self.copied(value, ty) } ExprKind::Borrow { borrow_kind, arg } => { + if self.temporary_drops(*arg) { + return self.err(span, "a borrow of a temporary that drops: bind it to a variable first"); + } if let BorrowKind::Mut { .. } = borrow_kind { let pointee = self.ty_of(*arg); if self.t.object_kind(pointee).is_none() @@ -1914,6 +2248,13 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { None => return self.err(span, "an array length that is not a constant"), }; let elem_ty = self.ty_of(*value); + // `[v; 0]` drops `v` at once, which no element of the array would. + if self.t.drops(elem_ty) { + return self.err( + span, + "an array repeat of a value that drops, which is not supported yet", + ); + } let v = self.operand(*value, out); match elem_ty.kind() { ty::Uint(ty::UintTy::U8) => { @@ -1935,7 +2276,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } } ExprKind::Adt(adt) => self.adt_expr(adt, ty, span, out), - ExprKind::Closure(closure) => self.closure(closure, span), + ExprKind::Closure(closure) => self.closure(closure.closure_id, span, out), ExprKind::Assign { .. } | ExprKind::AssignOp { .. } | ExprKind::Break { .. } @@ -1945,6 +2286,11 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { Expr::undefined() } ExprKind::Let { .. } => self.err(span, "a `let` outside an `if` condition"), + ExprKind::Yield { value } => { + self.stmt(*value, Dest::Discard, out); + self.yields += 1; + Expr::Yield + } kind => self.err(span, format!("unsupported expression: {}", kind_name(kind))), } } @@ -1987,6 +2333,13 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { unreachable!() }; let thir: &'a Thir<'tcx> = self.thir; + // Its arms bind nothing, so a temporary that drops is left for nothing to drop. + if self.temporary_drops(*scrutinee) { + return self.err( + self.span(id), + "a `match` on a temporary that drops, which it doesn't move out of whole", + ); + } let reads: usize = arms .iter() .take(arms.len().saturating_sub(1)) @@ -2025,12 +2378,17 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } ExprKind::VarRef { id: var } => self.local(var.0, span), ExprKind::UpvarRef { var_hir_id, .. } => self.local(var_hir_id.0, span), + // Reading through a `Box` that drops reads its pointee: the box stays where it is. + ExprKind::Deref { arg } if self.is_place(*arg) && self.t.drops(self.ty_of(*arg)) => self.place(*arg, out), ExprKind::Deref { arg } => self.expr(*arg, out), ExprKind::Field { lhs, variant_index, name, } => { + if self.temporary_drops(*lhs) { + return self.err(span, "a field of a temporary that drops: bind it to a variable first"); + } // A field of a tuple made in place, `(a, b).0`: its other fields still run. if let ExprKind::Tuple { fields } = &self.thir[self.strip(*lhs)].kind { let fields = fields.clone(); @@ -2053,6 +2411,12 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { self.project(base, lty, *variant_index, *name, span) } ExprKind::Index { lhs, index } => { + if self.temporary_drops(*lhs) { + return self.err( + span, + "an index into a temporary that drops: bind it to a variable first", + ); + } let base = self.place(*lhs, out); let base = self.path(base, *lhs, out); let lty = self.ty_of(*lhs); @@ -2085,7 +2449,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { match ty.kind() { ty::Tuple(_) => base.index(Expr::lit(field.index().to_string())), ty::Adt(adt, _) => match library::Std::of(self.t.tcx, adt.did()) { - Some(library::Std::Option) => base, + Some(library::Std::Option | library::Std::Poll) => base, Some(library::Std::Result) => match variant.index() { 0 => base.member("value"), _ => base.member("error"), @@ -2116,8 +2480,11 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { let mut values = self.operands(&ids, out); if let Some(std) = library::Std::of(tcx, def.did()) { return match (std, adt.variant_index.index()) { - (library::Std::Option, 0) => Expr::undefined(), - (library::Std::Option, _) => values.pop().unwrap(), + (library::Std::Option, 0) | (library::Std::Poll, 1) => match std { + library::Std::Poll => Expr::Rt("Pending"), + _ => Expr::undefined(), + }, + (library::Std::Option | library::Std::Poll, _) => values.pop().unwrap(), (library::Std::Result, 0) => Expr::rt("Ok", values), (library::Std::Result, _) => Expr::rt("Err", values), (library::Std::FmtError, _) => Expr::New(Box::new(Expr::Rt("FmtError")), Vec::new()), @@ -2151,6 +2518,9 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { let info = self.t.adt(def, span); let base = match &adt.base { thir::AdtExprBase::None => None, + thir::AdtExprBase::Base(fru) if self.t.drops(self.ty_of(fru.base)) => { + return self.err(span, "struct update syntax from a value that drops"); + } thir::AdtExprBase::Base(fru) => { // The base is read after the fields, so earlier fields are saved first. let mut pre = Vec::new(); @@ -2243,40 +2613,138 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } } - fn closure(&mut self, closure: &thir::ClosureExpr<'tcx>, span: Span) -> Expr { + /// A closure as an arrow function, or an `async` body as a future around a generator. + /// + /// The generator is a function of its own, taking its upvars as parameters: a + /// generator function made per call costs V8 a prototype object and a new shape for + /// each generator, about 25 times what one made once does. + fn closure(&mut self, local: rustc_hir::def_id::LocalDefId, span: Span, out: &mut Vec) -> Expr { let tcx = self.t.tcx; - let local = closure.closure_id; let Some((thir, body)) = self.t.body(local) else { return self.err(span, "no body for a closure"); }; - // A copy captured by `move` would be shared in JS: refuse one that can change. + let closure_ty = self.subst(tcx.type_of(local).instantiate_identity().skip_norm_wip()); + let (ret, coroutine) = match closure_ty.kind() { + ty::Closure(_, args) => ( + tcx.instantiate_bound_regions_with_erased(args.as_closure().sig()) + .output(), + false, + ), + ty::Coroutine(_, args) if tcx.coroutine_is_async(local.to_def_id()) => { + (args.as_coroutine().return_ty(), true) + } + _ => return self.err(span, format!("a closure of type `{closure_ty}`")), + }; + if coroutine && self.t.erases_to(ret).pending { + self.t.err( + span, + format!("an async body returning `{ret}` is not supported: its `Ready` could be pending"), + ); + } + // An async fn's body is this block alone, which rustc starts by moving each argument + // into a local: the function gives its arguments up whole, and those locals drop + // them. A block moves what it owns out of its place, as any move does, and drops it. + let fn_body = matches!( + tcx.coroutine_kind(local), + Some(rustc_hir::CoroutineKind::Desugared(_, rustc_hir::CoroutineSource::Fn)) + ); + // A generator's upvars, each a parameter: its name, its value here, its type, and + // whether the future owns it and so drops it. + let mut upvars: Vec<(String, Expr, Ty<'tcx>, bool)> = Vec::new(); + // What the generator renames, to put back once it is lowered. + let mut renamed = Vec::new(); for capture in tcx.closure_captures(local) { - if let ty::UpvarCapture::ByValue = capture.info.capture_kind { - let var = capture.get_root_variable(); - let mutable = matches!(self.locals.get(&var), Some(Local::Name { mutable: true, .. })); - if mutable && self.t.is_copy(self.subst(capture.place.ty())) { - self.t - .err(capture.get_path_span(tcx), "a closure that copies a mutable variable"); + let var = capture.get_root_variable(); + let ty = self.subst(capture.place.ty()); + let by_value = matches!(capture.info.capture_kind, ty::UpvarCapture::ByValue); + let mutable = matches!(self.locals.get(&var), Some(Local::Name { mutable: true, .. })); + // A copy captured by `move` would be shared in JS: refuse one that can change. + if !coroutine && by_value && mutable && self.t.is_copy(ty) { + self.t + .err(capture.get_path_span(tcx), "a closure that copies a mutable variable"); + } + if !coroutine { + if by_value && self.t.drops(ty) { + self.t.err( + capture.get_path_span(tcx), + "a closure that captures a value that drops, which is not supported yet", + ); + } + continue; + } + if !capture.place.projections.is_empty() { + self.t.err( + capture.get_path_span(tcx), + "an async body that captures part of a variable", + ); + continue; + } + // Its generator takes the value: a change through the borrow would change a copy. + if let ty::UpvarCapture::ByRef(kind) = capture.info.capture_kind + && kind != ty::BorrowKind::Immutable + { + if self.t.object_kind(ty).is_none() { + self.t.err( + capture.get_path_span(tcx), + format!("an async body that borrows `{ty}` mutably, which is no object it can change in place"), + ); + continue; } + self.borrowed.insert(var); } + let Some(Local::Name { name, .. }) = self.locals.get(&var).cloned() else { + self.t + .err(capture.get_path_span(tcx), "an async body capturing a function item"); + continue; + }; + let owns = by_value && self.t.drops(ty); + let place = match name == "this" { + true => Expr::This, + false => Expr::ident(&name), + }; + let value = match (owns, fn_body) { + (true, true) => { + self.disown(&name); + place + } + (true, false) => self.move_out(place, capture.get_path_span(tcx), out), + // A `Copy` aggregate taken by value is the future's own, as JS would share it. + (false, _) if by_value => self.copied(place, ty), + (false, _) => place, + }; + let param = match name == "this" { + true => { + let alias = self.fresh("self"); + let saved = self.locals.insert( + var, + Local::Name { + name: alias.clone(), + mutable: false, + }, + ); + renamed.push((var, saved)); + alias + } + false => name, + }; + upvars.push((param, value, ty, owns)); } let saved_thir = std::mem::replace(&mut self.thir, thir); self.read.extend(reads(tcx, thir)); let saved_breakables = std::mem::take(&mut self.breakables); - let closure_ty = self.subst(tcx.type_of(local).instantiate_identity().skip_norm_wip()); - let sig = match closure_ty.kind() { - ty::Closure(_, args) => tcx.instantiate_bound_regions_with_erased(args.as_closure().sig()), - _ => { - self.thir = saved_thir; - self.breakables = saved_breakables; - return self.err(span, format!("a closure of type `{closure_ty}`")); - } - }; - let saved_ret = std::mem::replace(&mut self.ret, sig.output()); + let saved_ret = std::mem::replace(&mut self.ret, ret); let saved_done = std::mem::replace(&mut self.done, false); let mut body_stmts = Vec::new(); let mut params = Vec::new(); - // The first parameter is the closure's environment. + self.open_scope(); + // Upvars drop in capture order, after the locals: the first is the innermost. + for (name, _, ty, owns) in upvars.iter().rev() { + if *owns && !fn_body { + self.own(name, *ty); + } + } + // The first parameter is the closure's environment; a coroutine's second is the + // context each poll resumes it with. for param in thir.params.iter().skip(1) { let name = match ¶m.pat { Some(pat) => self.param(pat, &mut body_stmts), @@ -2286,18 +2754,110 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { let ty = self.t.ty(ty, span); params.push(Param { name, ty: Some(ty) }); } - let ret = self.ret; let dest = match ret.is_unit() || ret.is_never() { true => Dest::Discard, false => Dest::Return, }; self.stmt(body, dest, &mut body_stmts); + self.close_scope(&mut body_stmts, 0, span); let ret_ty = self.t.ret_ty(ret, span); self.thir = saved_thir; self.breakables = saved_breakables; self.ret = saved_ret; self.done = saved_done; - Expr::Arrow(params, ret_ty, body_stmts) + for (var, saved) in renamed { + match saved { + Some(saved) => self.locals.insert(var, saved), + None => self.locals.remove(&var), + }; + } + if !coroutine { + return Expr::Arrow(params, ret_ty, body_stmts); + } + + // `rt.future(body, drop, ...upvars)`: the body and what drops the upvars if it + // never runs, each a function of the upvars. + let parent = tcx.typeck_root_def_id(local.to_def_id()); + let base = camel(tcx.item_name(parent).as_str()); + let mut drops = Vec::new(); + for (name, _, ty, owns) in &upvars { + if *owns && let Some(dropped) = self.t.drop_value(Expr::ident(name), *ty, span) { + drops.push(StmtKind::Expr(dropped).at(None)); + } + } + let upvar_params: Vec = upvars + .iter() + .map(|(name, _, ty, _)| Param { + name: name.clone(), + ty: Some(self.t.ty(*ty, span)), + }) + .collect(); + params.extend(upvar_params.iter().cloned()); + let body_fn = self.hoist(parent, format!("{base}Body"), params, ret_ty, body_stmts, true, span); + // The drop reads only the upvars it drops: TypeScript lets the rest go unread as `_`s. + let drop_params = upvars + .iter() + .zip(upvar_params) + .map(|((_, _, _, owns), param)| match owns { + true => param, + false => Param { + name: format!("_{}", param.name), + ..param + }, + }) + .collect(); + let drop_fn = match drops.is_empty() { + true => Expr::undefined(), + false => self.hoist( + parent, + format!("{base}Drop"), + drop_params, + Type::Builtin("void"), + drops, + false, + span, + ), + }; + let mut args = vec![body_fn, drop_fn]; + args.extend(upvars.into_iter().map(|(_, value, _, _)| value)); + Expr::rt("future", args) + } + + /// A module-level function in `parent`'s module, by the name `base`. + #[expect(clippy::too_many_arguments, reason = "a function's parts")] + fn hoist( + &mut self, + parent: rustc_hir::def_id::DefId, + base: String, + params: Vec, + ret: Type, + body: Vec, + generator: bool, + span: Span, + ) -> Expr { + let order = self.t.order(span); + let sym = self.t.program.sym(crate::program::Symbol { + module: self.t.module_of(parent), + base, + suffix: String::new(), + owner: None, + kind: SymKind::Function, + order, + instance: String::new(), + name: String::new(), + }); + let loc = self.t.loc(span); + self.t.program.items.push(ts::Item::Function(ts::Function { + sym, + method: false, + params, + ret, + body, + loc, + doc: None, + generator, + })); + Expr::Sym(sym) } // Calls @@ -2370,6 +2930,14 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } _ => return self.err(span, format!("`{}` does not resolve", tcx.def_path_str(def_id))), }; + // An `async` body's `Future::poll`, which its future has itself. + if let ty::InstanceKind::Item(did) = instance.def + && tcx.is_coroutine(did) + { + let mut values = self.args(args, out); + let future = values.remove(0); + return future.member("poll").call(values); + } if let ty::InstanceKind::Item(did) = instance.def && did.is_local() { @@ -2445,7 +3013,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { if let Some(std) = library::Std::of(tcx, adt_id) { let mut values = values; return match (std, variant.index()) { - (library::Std::Option, 1) => values.pop().unwrap(), + (library::Std::Option, 1) | (library::Std::Poll, 0) => values.pop().unwrap(), (library::Std::Result, 0) => Expr::rt("Ok", values), (library::Std::Result, _) => Expr::rt("Err", values), _ => self.err(span, format!("constructing `{}`", tcx.def_path_str(adt_id))), @@ -2612,6 +3180,27 @@ fn needs_annotation(value: &Expr) -> bool { } } +/// Whether matching `pat` moves its whole value into a binding, or matches only a variant +/// with nothing in it: either way nothing is left to drop. +fn takes_all(pat: &Pat<'_>) -> bool { + match &pat.kind { + PatKind::Binding { + mode, subpattern: None, .. + } => mode.0 == rustc_hir::ByRef::No, + // `Some(..)` names no field, which then stays in the temporary. + PatKind::Variant { + adt_def, + variant_index, + subpatterns, + .. + } => { + subpatterns.len() == adt_def.variant(*variant_index).fields.len() + && subpatterns.iter().all(|f| takes_all(&f.pattern)) + } + _ => false, + } +} + pub fn has_bindings(pat: &Pat<'_>) -> bool { bindings(pat) > 0 } diff --git a/ts/src/lower/drop.rs b/ts/src/lower/drop.rs new file mode 100644 index 000000000..66258221a --- /dev/null +++ b/ts/src/lower/drop.rs @@ -0,0 +1,343 @@ +//! Drops: which values JS can see drop, and where each one drops. +//! +//! A type drops if dropping it does something JS can see: runs a `Drop` impl, counts an +//! `Arc` down, or drops a waker. Such a value has `[Symbol.dispose]`, which a class gets +//! as drop glue: its `Drop` impl, then each field in order. A variable drops at the end +//! of its scope through a `finally`, so an early exit, a panic, or a cancelled future +//! drops it too. A move leaves `undefined` in the place it left, which drops as nothing, +//! so a variable moved on only some paths still drops on the others. + +use rustc_middle::ty::{self, AdtDef, Instance, Ty, TypeVisitableExt}; +use rustc_span::Span; + +use super::body::Body; +use super::library::Std; +use super::{Adt, AdtRepr, Translator}; +use crate::program::{SymKind, Symbol}; +use crate::ts::{self, Expr, Stmt, StmtKind, Target, Type}; + +/// A variable its scope drops when it ends. +pub struct Owned<'tcx> { + pub name: String, + pub ty: Ty<'tcx>, +} + +impl<'a, 'tcx> Translator<'a, 'tcx> { + /// Whether dropping a `ty` does something JS can see. + pub fn drops(&mut self, ty: Ty<'tcx>) -> bool { + if let Some(&drops) = self.drops.get(&ty) { + return drops; + } + // A type that holds itself drops through another field if at all. + if !self.drops_open.insert(ty) { + self.drops_cycle = true; + return false; + } + let outer_cycle = std::mem::replace(&mut self.drops_cycle, false); + let tcx = self.tcx; + let drops = match ty.kind() { + ty::Adt(adt, args) => match Std::of(tcx, adt.did()) { + Some(Std::Arc | Std::Waker) => true, + Some(Std::Option | Std::Result | Std::Box | Std::Vec | Std::Mutex | Std::Poll | Std::Pin) => { + args.types().any(|t| self.drops(t)) + } + Some(_) => false, + None if adt.did().is_local() => adt.has_dtor(tcx) || self.fields_drop(*adt, args, true), + None => false, + }, + ty::Tuple(tys) => tys.iter().any(|t| self.drops(t)), + ty::Array(elem, _) => self.drops(*elem), + ty::Closure(_, args) => args.as_closure().upvar_tys().iter().any(|t| self.drops(t)), + ty::Coroutine(..) | ty::Param(_) => true, + _ => false, + }; + self.drops_open.remove(&ty); + // A `false` that read a type still being decided holds only once the first of + // them is decided: if it drops, so does every type it reached that reaches it. + let cycle = std::mem::replace(&mut self.drops_cycle, outer_cycle); + if drops || !cycle || self.drops_open.is_empty() { + self.drops.insert(ty, drops); + } else { + self.drops_cycle = true; + } + drops + } + + /// Whether any field of `adt` with `args` drops, counting one of a type parameter only + /// if `params`. + fn fields_drop(&mut self, adt: AdtDef<'tcx>, args: ty::GenericArgsRef<'tcx>, params: bool) -> bool { + let tcx = self.tcx; + let env = self.typing_env_pub(); + adt.all_fields().any(|field| { + // A projection from a type parameter normalizes only where its bounds are known. + let field = ty::EarlyBinder::bind(tcx, tcx.type_of(field.did).instantiate_identity().skip_norm_wip()); + let ty = tcx + .try_instantiate_and_normalize_erasing_regions(args, env, field) + .unwrap_or_else(|_| field.instantiate(tcx, args).skip_norm_wip()); + (params || !ty.has_param()) && self.drops(ty) + }) + } + + /// Whether whatever holds a `ty` can drop it without knowing its type, through the + /// `[Symbol.dispose]` its JS value has, as an `Arc` and a generic class's glue do. + fn disposable(&mut self, ty: Ty<'tcx>) -> bool { + if !self.drops(ty) { + return self.inert(ty); + } + match ty.kind() { + ty::Adt(adt, args) => match Std::of(self.tcx, adt.did()) { + Some(Std::Waker) => true, + Some(Std::Arc | Std::Mutex | Std::Option | Std::Box | Std::Pin) => self.disposable(args.type_at(0)), + Some(_) => false, + None => adt.is_struct() && args.types().all(|t| self.disposable(t)), + }, + ty::Coroutine(..) | ty::Param(_) => true, + _ => false, + } + } + + /// Whether dropping a `ty` through its JS value's `[Symbol.dispose]` does nothing. A + /// reference is its referent in JS, so dropping a borrow of a value that drops would + /// drop the value. + fn inert(&mut self, ty: Ty<'tcx>) -> bool { + if self.drops(ty) { + return false; + } + match ty.kind() { + ty::Ref(_, inner, _) => self.inert(*inner), + ty::Adt(adt, args) => match Std::of(self.tcx, adt.did()) { + // Each is its contents in JS. + Some(Std::Option | Std::Box | Std::Mutex | Std::MutexGuard | Std::Poll | Std::Pin) => { + args.types().all(|t| self.inert(t)) + } + // Its waker. + Some(Std::Context) => false, + Some(_) => true, + // Its glue, if an instance that drops gave it one, drops its type parameters' fields. + None if adt.is_struct() => args.types().all(|t| self.inert(t)), + None => true, + }, + _ => true, + } + } + + /// What drops the `ty` in `value`, or `None` if JS would see nothing drop. + pub fn drop_value(&mut self, value: Expr, ty: Ty<'tcx>, span: Span) -> Option { + if !self.drops(ty) { + return None; + } + let supported = match ty.kind() { + ty::Adt(adt, args) => match Std::of(self.tcx, adt.did()) { + Some(Std::Vec) if self.disposable(args.type_at(0)) => { + return Some(Expr::rt("dropAll", vec![value])); + } + Some(Std::Arc | Std::Mutex | Std::Option | Std::Box | Std::Pin) => self.disposable(args.type_at(0)), + Some(Std::Waker) => true, + Some(_) => false, + None if adt.is_struct() => { + let info = self.adt(*adt, span); + self.glue(*adt, &info, span); + args.types().all(|t| self.disposable(t)) + } + None => false, + }, + ty::Array(elem, _) if self.disposable(*elem) => return Some(Expr::rt("dropAll", vec![value])), + ty::Coroutine(..) | ty::Param(_) => true, + _ => false, + }; + if !supported { + self.err(span, format!("dropping `{ty}`, which is not supported yet")); + return None; + } + Some(Expr::rt("drop", vec![value])) + } + + /// Give a class whose own fields or `Drop` impl drop its drop glue up front, so JS + /// that holds one can drop it. One that drops only through a type parameter gets + /// it once an instance that drops does. + pub fn drop_glue(&mut self, adt: AdtDef<'tcx>, info: &Adt, span: Span) { + let identity = ty::GenericArgs::identity_for_item(self.tcx, adt.did()); + if adt.has_dtor(self.tcx) || self.fields_drop(adt, identity, false) { + self.glue(adt, info, span); + } + } + + /// The `[Symbol.dispose]` of a class: its `Drop` impl, then each field that drops. + fn glue(&mut self, adt: AdtDef<'tcx>, info: &Adt, span: Span) { + let tcx = self.tcx; + if !self.glued.insert(adt.did()) { + return; + } + // An enum that drops is refused where it drops: its glue would switch on its variant. + let AdtRepr::Class { fields, .. } = &info.repr else { + if adt.has_dtor(tcx) { + self.err( + span, + format!( + "`{}` is an enum that implements `Drop`, which is not supported yet", + tcx.def_path_str(adt.did()) + ), + ); + } + return; + }; + let mut body: Vec = Vec::new(); + if let Some(dtor) = adt.destructor(tcx) { + match tcx.generics_of(adt.did()).requires_monomorphization(tcx) { + true => self.err( + span, + format!( + "`{}` is generic and implements `Drop`, which is not supported yet", + tcx.def_path_str(adt.did()) + ), + ), + false => { + let sym = self.instance(Instance::mono(tcx, dtor.did)); + body.push(StmtKind::Expr(Expr::Method(Box::new(Expr::This), sym, Vec::new())).at(None)); + } + } + } + for (field, &id) in adt.non_enum_variant().fields.iter().zip(fields) { + let ty = tcx.type_of(field.did).instantiate_identity().skip_norm_wip(); + if let Some(dropped) = self.drop_value(Expr::This.field(id), ty, span) { + body.push(StmtKind::Expr(dropped).at(None)); + } + } + let sym = self.program.sym(Symbol { + module: self.module_of(adt.did()), + base: "[Symbol.dispose]".into(), + suffix: String::new(), + owner: Some(info.sym), + kind: SymKind::Method { instance: true }, + order: 0, + instance: String::new(), + name: String::new(), + }); + self.program.methods.entry(info.sym).or_default().push(ts::Function { + sym, + method: true, + params: Vec::new(), + ret: Type::Builtin("void"), + body, + loc: None, + doc: None, + generator: false, + }); + } +} + +impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { + /// Start a scope: the variables bound until it closes drop when it does. + pub fn open_scope(&mut self) { + self.scopes.push(Vec::new()); + } + + /// Drop `name`, a `ty`, when the innermost scope closes, if JS would see it drop. + pub fn own(&mut self, name: &str, ty: Ty<'tcx>) { + if !self.t.drops(ty) { + return; + } + if let Some(scope) = self.scopes.last_mut() { + scope.push(Owned { + name: name.to_string(), + ty, + }); + } + } + + /// Stop dropping `name` here: it moved somewhere that drops it itself. + pub fn disown(&mut self, name: &str) { + for scope in &mut self.scopes { + scope.retain(|owned| owned.name != name); + } + } + + /// Whether the innermost scope drops anything yet. + pub fn scope_owns(&self) -> bool { + self.scopes.last().is_some_and(|scope| !scope.is_empty()) + } + + /// Close the innermost scope, whose statements are `out[start..]`: each variable it + /// drops is dropped in a `finally` around the statements after its declaration, the + /// last declared innermost, or around them all for a parameter. + pub fn close_scope(&mut self, out: &mut Vec, start: usize, span: Span) { + let owned = self.scopes.pop().unwrap_or_default(); + for owned in owned.into_iter().rev() { + let Some(dropped) = self.t.drop_value(Expr::ident(&owned.name), owned.ty, span) else { + continue; + }; + let moved = self.moved.contains(&owned.name); + let mut at = start; + for (i, stmt) in out.iter_mut().enumerate().skip(start) { + if let StmtKind::Decl { + mutable, target, init, .. + } = &mut stmt.kind + && declares(target, &owned.name) + { + *mutable |= moved; + // The `finally` may run before anything assigns it. + init.get_or_insert_with(|| Expr::lit("undefined!")); + at = i + 1; + break; + } + } + // What the declaration moved from is cleared before anything can throw. + while out.get(at).is_some_and(clears) { + at += 1; + } + let body = out.split_off(at); + let dropped = StmtKind::Expr(dropped).at(None); + match body.is_empty() { + true => out.push(dropped), + false => out.push( + StmtKind::Try { + body, + finally: vec![dropped], + } + .at(None), + ), + } + } + } + + /// The value of the place `place`, a `ty` that drops, moved out: the place is left + /// `undefined`, so whatever drops it later drops nothing. + pub fn move_out(&mut self, place: Expr, span: Span, out: &mut Vec) -> Expr { + let base = match &place { + Expr::Ident(name) => name.clone(), + _ => "moved".into(), + }; + match &place { + Expr::Ident(name) => { + self.moved.insert(name.clone()); + } + Expr::Member(..) if place.is_path() => {} + _ => return self.err(span, "a move out of a place that is no variable or field"), + } + let name = self.fresh(&base); + out.push(StmtKind::decl(false, name.clone(), None, Some(place.clone())).at(None)); + self.clear(place, out); + Expr::ident(name) + } + + /// Leave `undefined` in `place`, whose value moved. + pub fn clear(&mut self, place: Expr, out: &mut Vec) { + if let Expr::Ident(name) = &place { + self.moved.insert(name.clone()); + } + out.push(StmtKind::Expr(place.assign(Expr::lit("undefined!"))).at(None)); + } +} + +/// Whether `stmt` leaves a moved-out place `undefined`. +fn clears(stmt: &Stmt) -> bool { + matches!(&stmt.kind, StmtKind::Expr(Expr::Assign(_, value)) if matches!(&**value, Expr::Lit(text) if text == "undefined!")) +} + +/// Whether `target` declares `name`. +fn declares(target: &Target, name: &str) -> bool { + match target { + Target::Name(n) => n == name, + Target::Array(items) => items.iter().flatten().any(|t| declares(t, name)), + } +} diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index bb36e6e82..1e781affb 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -114,6 +114,7 @@ pub fn next<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, iter: Expr) -> Expr /// type without a mapping. pub fn into_iter<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, value: Expr, span: Span) -> Option { let tcx = b.t.tcx; + no_drops(b, ty, span); if b.t.iter_item(ty).is_some() { return Some(match own_next(b, ty) { Some(instance) => { @@ -160,9 +161,20 @@ fn into_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) -> Option Some(Some(Expr::Lambda(vec![param], Box::new(body)))) } +/// Refuse an iterator that owns a `ty` that drops: an `rt.Iter` drops nothing it skips, +/// consumes, or holds when it is dropped. +fn no_drops<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) { + if b.t.drops(ty) { + b.t.err( + span, + format!("an iterator that owns `{ty}`, which drops, which is not supported yet"), + ); + } +} + /// Refuse an `Option` of `ty` that `T | undefined` can't hold, as the type would be. pub fn option_ok<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) { - if !b.t.option_payload_ok(ty) { + if b.t.erases_to(ty).undefined { b.t.err( span, format!("`Option<{ty}>` is not supported: its `None` and `Some` would both be `undefined`"), @@ -209,7 +221,10 @@ pub fn call<'tcx>( _ => {} } match path.as_slice() { - ["iter", "sources", "once", "once"] => return Some(rt_iter("once").call(b.args(args, out))), + ["iter", "sources", "once", "once"] => { + no_drops(b, targ(0)?, span); + return Some(rt_iter("once").call(b.args(args, out))); + } ["iter", "sources", "empty", "empty"] => return Some(rt_iter("empty").call(Vec::new())), ["iter", "sources", "from_fn", "from_fn"] => return Some(rt_iter("fromFn").call(b.args(args, out))), _ => {} @@ -243,6 +258,10 @@ pub fn call<'tcx>( _ => return None, }; let item = b.t.iter_item(self_ty.peel_refs())?; + // The crate's own `next` hands its item over; every other call may leave items behind. + if name != "by_ref" && !(name == "next" && own_next(b, self_ty).is_some()) { + no_drops(b, item, span); + } let mut values = b.args(args, out).into_iter(); let recv = values.next()?; let rest: Vec = values.collect(); diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 8b802d9d2..975d7511a 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -42,6 +42,23 @@ pub enum Std { DebugList, /// What `assert_eq!` and `assert_ne!` compare by: its operator, as a string. AssertKind, + /// `rt.Arc`, counted. + Arc, + /// `rt.Weak`. + Weak, + /// `rt.Mutex`, which is also its own guard. + Mutex, + MutexGuard, + /// What a `Mutex` can't be locked with on one thread: never. + PoisonError, + /// `rt.Waker`. + Waker, + /// `rt.Context`: its waker. + Context, + /// `rt.Poll`: `T` itself, or `rt.Pending`. + Poll, + /// Its pointer. + Pin, /// `rt.Range`, an iterator with `start` and `end` fields. Range, /// `rt.RangeInclusive`. @@ -71,15 +88,33 @@ impl Std { if tcx.is_lang_item(did, rustc_attr_ir::LangItem::OwnedBox) { return Some(Self::Box); } - if tcx.is_lang_item(did, rustc_attr_ir::LangItem::Range) { - return Some(Self::Range); + for (item, std) in [ + (sym::Arc, Self::Arc), + (sym::ArcWeak, Self::Weak), + (sym::Mutex, Self::Mutex), + (sym::MutexGuard, Self::MutexGuard), + (rustc_span::Symbol::intern("Waker"), Self::Waker), + ] { + if tcx.is_diagnostic_item(item, did) { + return Some(std); + } } - if tcx.is_lang_item(did, rustc_attr_ir::LangItem::RangeInclusiveStruct) { - return Some(Self::RangeInclusive); + for (item, std) in [ + (rustc_attr_ir::LangItem::Context, Self::Context), + (rustc_attr_ir::LangItem::ResumeTy, Self::Context), + (rustc_attr_ir::LangItem::Poll, Self::Poll), + (rustc_attr_ir::LangItem::Pin, Self::Pin), + (rustc_attr_ir::LangItem::Range, Self::Range), + (rustc_attr_ir::LangItem::RangeInclusiveStruct, Self::RangeInclusive), + ] { + if tcx.is_lang_item(did, item) { + return Some(std); + } } match tcx.item_name(did).as_str() { "FromUtf8Error" => return Some(Self::FromUtf8Error), "TryFromIntError" => return Some(Self::TryFromIntError), + "PoisonError" => return Some(Self::PoisonError), _ => {} } match std_path(tcx, did) @@ -169,6 +204,45 @@ pub struct Call<'tcx> { pub span: Span, } +/// Whether the mapping of `owner::name` moves each operand it takes by value into its +/// result or a closure, and so drops nothing JS would leave undropped. Any other mapping +/// may discard one, as `unwrap_or` does its fallback. Iterators refuse their own. +fn keeps_operands(owner: &str, name: &str) -> bool { + matches!( + (owner, name), + ("Option", "map" | "unwrap" | "expect") + | ( + "Result", + "map" | "map_err" | "and_then" | "inspect_err" | "unwrap" | "expect" | "unwrap_err" + ) + | ("Vec", "push") + | ("Box", "new") + | ("boxed", "box_new") + | ("mem", "replace" | "take" | "swap") + | ("hint", "must_use") + | ("From", "from") + | ("Into", "into") + ) +} + +/// The first type `call` takes by value that drops. +fn dropping_operand<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>) -> Option> { + let tcx = b.t.tcx; + let sig = tcx.fn_sig(call.def_id).instantiate(tcx, call.args).skip_norm_wip(); + let env = b.t.typing_env_pub(); + // A census's call over type parameters may leave a projection unnormalized. + let inputs: Vec> = tcx + .instantiate_bound_regions_with_erased(sig) + .inputs() + .iter() + .map(|&ty| { + tcx.try_normalize_erasing_regions(env, ty::Unnormalized::new_wip(ty)) + .unwrap_or(ty) + }) + .collect(); + inputs.into_iter().find(|&ty| b.t.drops(ty)) +} + /// `owner::name` for a standard library function. pub fn key(tcx: TyCtxt<'_>, def_id: DefId) -> Option<(String, String)> { let name = tcx.item_name(def_id).to_string(); @@ -211,6 +285,14 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Args<'_>, let (owner, name) = key(tcx, call.def_id)?; let span = call.span; let gargs = call.args; + let args = match super::task::call(b, call, &owner, &name, args, out) { + Ok(e) => return e, + Err(args) => args, + }; + let dropping = match keeps_operands(&owner, &name) { + true => None, + false => dropping_operand(b, call), + }; // Methods on integers. if let Some(int) = int_named(&owner) { @@ -735,6 +817,12 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Args<'_>, } _ => return super::iter::call(b, call, &owner, &name, args, out), }; + if let Some(ty) = dropping { + return Some(b.err( + span, + format!("passing `{ty}`, which drops, to `{owner}::{name}`, which is not supported yet"), + )); + } Some(e) } diff --git a/ts/src/lower/mod.rs b/ts/src/lower/mod.rs index a1c1172a1..05278d22f 100644 --- a/ts/src/lower/mod.rs +++ b/ts/src/lower/mod.rs @@ -7,11 +7,13 @@ mod body; mod consts; +mod drop; mod fmt; mod iter; mod library; mod num; mod pat; +mod task; use std::collections::VecDeque; @@ -160,6 +162,13 @@ pub struct Translator<'a, 'tcx> { env: TypingEnv<'tcx>, /// Refusals collected for a census, rather than reported as errors. refusals: Option>, + /// Whether each type's drop does something JS can see, as [`Self::drops`] found. + drops: rustc_data_structures::fx::FxHashMap, bool>, + /// The types [`Self::drops`] is still deciding, and whether the query open now read one. + drops_open: rustc_data_structures::fx::FxHashSet>, + drops_cycle: bool, + /// The classes given drop glue. + glued: rustc_data_structures::fx::FxHashSet, } /// A local struct or enum, and how it is represented. @@ -189,6 +198,14 @@ pub enum ObjectKind { Closure, } +/// The JS values a type's own can be that `Option` and `Poll` erase to: `None` is +/// `undefined`, and `Pending` is `rt.Pending`. +#[derive(Clone, Copy, Default)] +pub struct Erased { + pub undefined: bool, + pub pending: bool, +} + /// Where a translated function lives. #[derive(Clone, Copy)] pub enum Owner { @@ -225,6 +242,10 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { failed: false, env: TypingEnv::fully_monomorphized(), refusals, + drops: Default::default(), + drops_open: Default::default(), + drops_cycle: false, + glued: Default::default(), }) } @@ -493,7 +514,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { suffix: self.suffix(instance), owner: owner_sym, kind, - order: self.order(def_id), + order: self.order(tcx.def_span(def_id)), // Without trimmed paths, which rustc computes only for a diagnostic. instance: ty::print::with_no_trimmed_paths!(instance.to_string()), name: String::new(), @@ -590,10 +611,10 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { .collect() } - /// Where `def_id` is written, for sorting: where its macro is called, for an item a + /// Where `span` is written, for sorting: where its macro is called, for an item a /// macro makes. - fn order(&self, def_id: DefId) -> u32 { - self.tcx.def_span(def_id).source_callsite().lo().0 + pub fn order(&self, span: Span) -> u32 { + span.source_callsite().lo().0 } fn source(&mut self, file: &rustc_span::SourceFile) -> Option { @@ -642,6 +663,14 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { .instantiate_and_normalize_erasing_regions(args, self.typing_env(), EarlyBinder::bind(self.tcx, value)) } + /// The standard library type `ty` is, if it is one with its own representation. + pub fn std_of(&self, ty: Ty<'tcx>) -> Option { + match ty.kind() { + ty::Adt(adt, _) => library::Std::of(self.tcx, adt.did()), + _ => None, + } + } + pub fn is_copy(&self, ty: Ty<'tcx>) -> bool { self.tcx.type_is_copy_modulo_regions(self.typing_env(), ty) } @@ -653,15 +682,6 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { } let tcx = self.tcx; let did = adt.did(); - if adt.destructor(tcx).is_some() { - self.err( - span, - format!( - "`{}` implements `Drop`, which is not supported yet", - tcx.def_path_str(did) - ), - ); - } let repr = match () { _ if adt.is_struct() => { let variant = adt.non_enum_variant(); @@ -694,7 +714,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { suffix: String::new(), owner: None, kind, - order: self.order(did), + order: self.order(tcx.def_span(did)), instance: String::new(), name: String::new(), }); @@ -783,6 +803,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { } }; self.program.items.push(item); + self.drop_glue(adt, &info, span); info } @@ -800,6 +821,13 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { | library::Std::DebugStruct | library::Std::DebugTuple | library::Std::DebugList + | library::Std::Arc + | library::Std::Weak + | library::Std::Mutex + | library::Std::MutexGuard + | library::Std::Waker + | library::Std::Context + | library::Std::Pin | library::Std::Range | library::Std::RangeInclusive | library::Std::Peekable, @@ -810,6 +838,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { }, // Its captures change through it, as a JS function's do. ty::Closure(..) => Some(ObjectKind::Closure), + ty::Coroutine(..) => Some(ObjectKind::Class), _ if self.deferred(ty) => Some(ObjectKind::Class), _ => None, } @@ -851,6 +880,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { self.fn_ty(&inputs, sig.output(), span) } ty::Adt(adt, args) => self.adt_ty(*adt, args, span), + ty::Coroutine(_, args) => Type::Rt("Future", vec![self.ty(args.as_coroutine().return_ty(), span)]), // `&dyn Debug` and `&dyn Display` are values ready to write. ty::Dynamic(preds, ..) if preds @@ -891,7 +921,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { return match std { library::Std::Option => { let inner = args.type_at(0); - if !self.option_payload_ok(inner) { + if self.erases_to(inner).undefined { self.err( span, format!( @@ -901,6 +931,10 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { } Type::Union(vec![self.ty(inner, span), Type::Builtin("undefined")]) } + // A lock's result, which is never an error: its guard. + library::Std::Result if self.std_of(args.type_at(1)) == Some(library::Std::PoisonError) => { + self.ty(args.type_at(0), span) + } library::Std::Result => Type::Rt( "Result", vec![self.ty(args.type_at(0), span), self.ty(args.type_at(1), span)], @@ -920,6 +954,27 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { library::Std::DebugTuple => Type::Rt("DebugTuple", vec![]), library::Std::DebugList => Type::Rt("DebugList", vec![]), library::Std::AssertKind => Type::Builtin("string"), + library::Std::Arc => Type::Rt("Arc", vec![self.ty(args.type_at(0), span)]), + library::Std::Weak => Type::Rt("Weak", vec![self.ty(args.type_at(0), span)]), + // A guard's first argument is its lifetime. + library::Std::Mutex | library::Std::MutexGuard => { + let inner = args.types().next().expect("a guarded type"); + Type::Rt("Mutex", vec![self.ty(inner, span)]) + } + library::Std::PoisonError => Type::Builtin("never"), + library::Std::Waker => Type::Rt("Waker", vec![]), + library::Std::Context => Type::Rt("Context", vec![]), + library::Std::Poll => { + let inner = args.type_at(0); + if self.erases_to(inner).pending { + self.err( + span, + format!("`Poll<{inner}>` is not supported: it would be pending two ways with one value"), + ); + } + Type::Rt("Poll", vec![self.ty(inner, span)]) + } + library::Std::Pin => self.ty(args.type_at(0), span), library::Std::Range => Type::Rt("Range", vec![self.ty(args.type_at(0), span)]), library::Std::RangeInclusive => Type::Rt("RangeInclusive", vec![self.ty(args.type_at(0), span)]), library::Std::Peekable => { @@ -957,17 +1012,28 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { } } - /// Whether `undefined` is free to mean `None` around a `ty`. - pub fn option_payload_ok(&self, ty: Ty<'tcx>) -> bool { + /// Which of the values `Option` and `Poll` erase to a `ty`'s JS value can already be, + /// seen through the wrappers that erase to their contents. + pub fn erases_to(&self, ty: Ty<'tcx>) -> Erased { match ty.kind() { - ty::Tuple(tys) => !tys.is_empty(), - ty::Ref(_, inner, _) => self.option_payload_ok(*inner), + ty::Tuple(tys) if tys.is_empty() => Erased { + undefined: true, + pending: false, + }, + ty::Ref(_, inner, _) => self.erases_to(*inner), ty::Adt(adt, args) => match library::Std::of(self.tcx, adt.did()) { - Some(library::Std::Option) => false, - Some(library::Std::Box) => self.option_payload_ok(args.type_at(0)), - _ => true, + Some(library::Std::Box | library::Std::Pin) => self.erases_to(args.type_at(0)), + Some(library::Std::Option) => Erased { + undefined: true, + ..self.erases_to(args.type_at(0)) + }, + Some(library::Std::Poll) => Erased { + pending: true, + ..self.erases_to(args.type_at(0)) + }, + _ => Erased::default(), }, - _ => true, + _ => Erased::default(), } } @@ -994,13 +1060,6 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { ); return; }; - if tcx.asyncness(def_id).is_async() { - self.err( - span, - format!("`{}` is async, which is not supported yet", tcx.def_path_str(def_id)), - ); - return; - } let owner = self.owner(instance); let method = matches!(owner, Owner::Class(_)) && self.has_self(def_id); let function = body::function(self, instance, thir, *expr, method); diff --git a/ts/src/lower/pat.rs b/ts/src/lower/pat.rs index 052bbfbcb..08321f80f 100644 --- a/ts/src/lower/pat.rs +++ b/ts/src/lower/pat.rs @@ -42,6 +42,10 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { 0 => scrut.clone().binary("===", Expr::undefined()), _ => scrut.clone().binary("!==", Expr::undefined()), }), + Some(Std::Poll) => tests.push(match variant_index.index() { + 0 => scrut.clone().binary("!==", Expr::Rt("Pending")), + _ => scrut.clone().binary("===", Expr::Rt("Pending")), + }), Some(Std::Result) => tests.push(match variant_index.index() { 0 => scrut.clone().member("ok"), _ => scrut.clone().member("ok").not(), @@ -183,8 +187,10 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { if !by_value { self.ref_binding(mode.0, ty, pat.span); } + // A value that drops moves into its binding, read or not. + let moves = by_value && !self.t.is_copy(ty) && self.t.drops(ty); // Binding has no effect in JS, and TypeScript refuses an unread local. - if subpattern.is_none() && !self.is_read(*var) { + if subpattern.is_none() && !self.is_read(*var) && !moves { return; } let mutable = mode.1 == rustc_ast::Mutability::Mut; @@ -192,9 +198,13 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { true => self.copied(scrut.clone(), ty), false => scrut.clone(), }; - let name = self.bind_var(*var, *name, mutable, ty, pat.span); + let name = self.bind_var(*var, *name, mutable, ty, pat.span, by_value); let annotate = mutable.then(|| self.t.ty(ty, pat.span)); out.push(StmtKind::decl(mutable, name, annotate, Some(value)).at(None)); + // Out of a place, which is left `undefined`. + if moves && !owned { + self.clear(scrut.clone(), out); + } if let Some(sub) = subpattern { self.pat_bind(sub, scrut, owned, out); } @@ -253,6 +263,14 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { ty::Uint(ty::UintTy::U8) => "subarray", _ => "slice", }; + // Its copy of the elements would drop them, and so would the place they stay in. + let rest_ty = self.subst(p.ty); + if !owned && !matches!(rest_ty.kind(), ty::Ref(..)) && self.t.drops(rest_ty) { + self.t.err( + p.span, + "a rest pattern that moves values that drop out of a place, which is not supported yet", + ); + } let rest = scrut.clone().member(method).call(vec![start, end]); self.pat_bind(p, &rest, true, out); } diff --git a/ts/src/lower/task.rs b/ts/src/lower/task.rs new file mode 100644 index 000000000..d11d33a13 --- /dev/null +++ b/ts/src/lower/task.rs @@ -0,0 +1,89 @@ +//! The standard library's ownership and async types, as `rt/task.ts` gives them: `Arc`, +//! `Weak`, `Mutex`, `Waker`, `Context`, `Poll`, and `Pin`. + +use rustc_middle::ty::{self, Ty}; + +use super::body::Body; +use super::library::{Args, Call, Std}; +use crate::ts::{Expr, Stmt}; + +/// A call of a standard library function on these types, or its arguments back if it is +/// none of theirs. `Ok(None)` refuses it. +pub fn call<'tcx, 'x>( + b: &mut Body<'_, '_, 'tcx>, + call: &Call<'tcx>, + owner: &str, + name: &str, + args: Args<'x>, + out: &mut Vec, +) -> Result, Args<'x>> { + let tcx = b.t.tcx; + let std_of = |ty: Ty<'tcx>| match ty.peel_refs().kind() { + ty::Adt(adt, _) => Std::of(tcx, adt.did()), + _ => None, + }; + let self_ty = call.args.types().next(); + let self_std = self_ty.and_then(std_of); + let e = match (owner, name) { + ("Arc", "new") => Expr::New(Box::new(Expr::Rt("Arc")), b.args(args, out)), + ("Arc", "downgrade") => Expr::New(Box::new(Expr::Rt("Weak")), b.args(args, out)), + ("Arc", "strong_count") => first(b, args, out).member("strong"), + ("Arc", "ptr_eq") => { + let [l, r] = two(b, args, out); + l.binary("===", r) + } + ("Weak", "upgrade") => first(b, args, out).member("upgrade").call(vec![]), + ("Weak", "strong_count") => first(b, args, out).member("arc").member("strong"), + ("Weak", "ptr_eq") => { + let [l, r] = two(b, args, out); + l.member("arc").binary("===", r.member("arc")) + } + ("Clone", "clone") if matches!(self_std, Some(Std::Arc | Std::Weak | Std::Waker)) => { + first(b, args, out).member("clone").call(vec![]) + } + ("Deref", "deref") | ("DerefMut", "deref_mut") => match self_std { + Some(Std::Arc | Std::Mutex | Std::MutexGuard) => first(b, args, out).member("value"), + Some(Std::Pin) => first(b, args, out), + _ => return Err(args), + }, + ("Mutex", "new") => Expr::New(Box::new(Expr::Rt("Mutex")), b.args(args, out)), + // The mutex is its own guard, and a lock never fails on one thread. + ("Mutex", "lock") => first(b, args, out), + ("Result", "unwrap" | "expect") if call.args.types().nth(1).and_then(std_of) == Some(Std::PoisonError) => { + first(b, args, out) + } + ("Waker", "wake_by_ref") => first(b, args, out).member("wakeByRef").call(vec![]), + ("Waker", "wake") => first(b, args, out).member("wake").call(vec![]), + ("Waker", "will_wake") => { + let [l, r] = two(b, args, out); + l.member("willWake").call(vec![r]) + } + ("Context", "waker" | "from_waker") + | ("Pin", "new" | "new_unchecked" | "as_mut" | "get_mut" | "get_unchecked_mut") + | ("future", "get_context") + | ("IntoFuture", "into_future") => first(b, args, out), + ("mem", "drop") => { + let ty = call.args.type_at(0); + let value = first(b, args, out); + match b.t.drop_value(value.clone(), ty, call.span) { + Some(dropped) => dropped, + None if value.is_path() => Expr::undefined(), + None => value, + } + } + _ => return Err(args), + }; + Ok(Some(e)) +} + +fn first<'tcx>(b: &mut Body<'_, '_, 'tcx>, args: Args<'_>, out: &mut Vec) -> Expr { + b.args(args, out).into_iter().next().unwrap_or_else(Expr::undefined) +} + +fn two<'tcx>(b: &mut Body<'_, '_, 'tcx>, args: Args<'_>, out: &mut Vec) -> [Expr; 2] { + let mut values = b.args(args, out).into_iter(); + [ + values.next().unwrap_or_else(Expr::undefined), + values.next().unwrap_or_else(Expr::undefined), + ] +} diff --git a/ts/src/print.rs b/ts/src/print.rs index 34224b656..e618c34bf 100644 --- a/ts/src/print.rs +++ b/ts/src/print.rs @@ -272,6 +272,10 @@ impl Refs { self.stmts(program, body); } StmtKind::Block { body, .. } => self.stmts(program, body), + StmtKind::Try { body, finally } => { + self.stmts(program, body); + self.stmts(program, finally); + } StmtKind::Return(None) | StmtKind::Break(_) | StmtKind::Continue(_) => {} } } @@ -279,7 +283,7 @@ impl Refs { fn expr(&mut self, program: &Program, e: &Expr) { match e { - Expr::Ident(_) | Expr::This | Expr::Rt(_) | Expr::Lit(_) | Expr::Str(_) => {} + Expr::Ident(_) | Expr::This | Expr::Rt(_) | Expr::Lit(_) | Expr::Str(_) | Expr::Yield => {} Expr::Sym(id) => self.use_sym(program, *id, true), Expr::Array(es) => es.iter().for_each(|e| self.expr(program, e)), Expr::Object(props) => props.iter().for_each(|(_, e)| self.expr(program, e)), @@ -514,14 +518,19 @@ impl Printer<'_> { self.start(); self.mark(f.loc); let name = self.program.syms[f.sym].name.clone(); + let star = if f.generator { "*" } else { "" }; match cx { - Context::Module => self.write(&format!("export function {name}(")), + Context::Module => self.write(&format!("export function{star} {name}(")), Context::Class if f.method => self.write(&format!("{name}(")), Context::Class => self.write(&format!("static {name}(")), Context::Companion => self.write(&format!("{name}(")), } self.params(&f.params); - let ret = self.ty(&f.ret); + let mut ret = self.ty(&f.ret); + if f.generator { + self.uses_rt = true; + ret = format!("Generator"); + } self.write(&format!("): {ret} {{\n")); self.block_body(&f.body); self.start(); @@ -734,6 +743,15 @@ impl Printer<'_> { self.start(); self.write("}\n"); } + StmtKind::Try { body, finally } => { + self.write("try {\n"); + self.block_body(body); + self.start(); + self.write("} finally {\n"); + self.block_body(finally); + self.start(); + self.write("}\n"); + } StmtKind::Break(label) => match label { Some(label) => self.write(&format!("break {label};\n")), None => self.write("break;\n"), @@ -875,6 +893,7 @@ impl Printer<'_> { self.write("}"); } } + Expr::Yield => self.write("yield"), Expr::Lambda(params, body) => { self.write("("); self.params(params); @@ -1039,7 +1058,7 @@ fn binary_prec(op: &str) -> u8 { fn prec(e: &Expr) -> u8 { match e { - Expr::Assign(..) | Expr::Arrow(..) | Expr::Lambda(..) => 3, + Expr::Assign(..) | Expr::Arrow(..) | Expr::Lambda(..) | Expr::Yield => 3, Expr::Cond(..) => 4, Expr::Binary(_, op, _) => binary_prec(op), Expr::As(..) => 12, @@ -1106,6 +1125,10 @@ fn collect_locals(stmts: &[Stmt], out: &mut Vec) { collect_locals(body, out); } StmtKind::Block { body, .. } => collect_locals(body, out), + StmtKind::Try { body, finally } => { + collect_locals(body, out); + collect_locals(finally, out); + } StmtKind::Return(None) | StmtKind::Break(_) | StmtKind::Continue(_) => {} } } @@ -1144,6 +1167,6 @@ fn collect_expr_locals(e: &Expr, out: &mut Vec) { collect_expr_locals(b, out); collect_expr_locals(c, out); } - Expr::Ident(_) | Expr::This | Expr::Sym(_) | Expr::Rt(_) | Expr::Lit(_) | Expr::Str(_) => {} + Expr::Ident(_) | Expr::This | Expr::Sym(_) | Expr::Rt(_) | Expr::Lit(_) | Expr::Str(_) | Expr::Yield => {} } } diff --git a/ts/src/program.rs b/ts/src/program.rs index ff152fb10..f2572e4da 100644 --- a/ts/src/program.rs +++ b/ts/src/program.rs @@ -11,6 +11,7 @@ use crate::ts::{FieldId, Function, Item, SymId}; pub const RUNTIME: &[(&str, &str)] = &[ ("rt.ts", include_str!("../runtime/rt.ts")), ("rt/iter.ts", include_str!("../runtime/rt/iter.ts")), + ("rt/task.ts", include_str!("../runtime/rt/task.ts")), ]; /// The first line of every generated file, which also marks it as safe to replace. @@ -32,7 +33,7 @@ pub struct Symbol { /// Its source position in rustc's source map, which numbers files in the order it /// loads them. pub order: u32, - /// The function instance it is, printed in full; empty for a type. + /// The function instance it is, printed in full; empty for anything else. pub instance: String, /// The final name, set by [`Program::name`]. pub name: String, diff --git a/ts/src/ts.rs b/ts/src/ts.rs index ed2d6862e..8a12a0ede 100644 --- a/ts/src/ts.rs +++ b/ts/src/ts.rs @@ -50,6 +50,8 @@ pub enum Expr { /// `e as T`: a literal whose type TypeScript would widen, or an integer cast to /// another integer type with the same value. As(Box, Type), + /// `yield`, which a generator resumes from with the next `rt.Context`. + Yield, Located(Loc, Box), } @@ -214,6 +216,11 @@ pub enum StmtKind { }, Break(Option), Continue(Option), + /// `try { body } finally { finally }`: what `finally` drops is dropped on every exit. + Try { + body: Vec, + finally: Vec, + }, } impl StmtKind { @@ -250,7 +257,7 @@ impl Stmt { match &self.kind { StmtKind::Return(_) | StmtKind::Break(_) | StmtKind::Continue(_) => true, StmtKind::If { then, els, .. } => diverges(then) && diverges(els), - StmtKind::Block { label: None, body } => diverges(body), + StmtKind::Block { label: None, body } | StmtKind::Try { body, .. } => diverges(body), StmtKind::While { label, cond: Expr::Lit(cond), @@ -270,6 +277,7 @@ fn breaks_out(stmts: &[Stmt], label: Option<&str>, nested: bool) -> bool { StmtKind::Break(Some(target)) => Some(target.as_str()) == label, StmtKind::If { then, els, .. } => breaks_out(then, label, nested) || breaks_out(els, label, nested), StmtKind::Block { body, .. } => breaks_out(body, label, nested), + StmtKind::Try { body, finally } => breaks_out(body, label, nested) || breaks_out(finally, label, nested), StmtKind::While { body, .. } | StmtKind::ForRange { body, .. } | StmtKind::ForOf { body, .. } => { breaks_out(body, label, true) } @@ -311,6 +319,9 @@ pub struct Function { pub body: Vec, pub loc: Option, pub doc: Option, + /// An async body, run as a generator: `function*`, returning `Generator`. + pub generator: bool, } #[derive(Clone, Debug)] diff --git a/ts/tests/differential.ts b/ts/tests/differential.ts index 4ddfa2107..598cc0379 100644 --- a/ts/tests/differential.ts +++ b/ts/tests/differential.ts @@ -7,8 +7,9 @@ // random bytes rarely make: every pair of values next to a power of two. // // A harness, or a hand-written caller wrapping one, exports `run(input: Uint8Array): -// Vec`; the wasm build exports `input`, `run`, and `output` around the crate's -// entry, which `wasm.ts` calls. A panic traps natively and throws `Panic` in +// Vec`, and may export `cases`, inputs it names, which run with the generated ones. +// The wasm build exports `input`, `run`, and `output` around the crate's entry, which +// `wasm.ts` calls. A panic traps natively and throws `Panic` in // TypeScript, and the two agree only if their messages do. import { readFile } from "node:fs/promises"; @@ -65,6 +66,14 @@ function show(outcome: Outcome): string { return outcome instanceof Uint8Array ? Buffer.from(outcome).toString("hex") : `panic ${JSON.stringify(outcome.panic)}`; } +/** An outcome to read: as text if it is lines of printable ASCII, as a log is. */ +function display(outcome: Outcome): string { + if (outcome instanceof Uint8Array && outcome.every((b) => b === 0x0a || (b >= 0x20 && b < 0x7f))) { + return `\n${Buffer.from(outcome).toString()}`; + } + return show(outcome); +} + const cases: Uint8Array[] = [new Uint8Array(0)]; // Every op with every short tail, so each path's edges are covered exhaustively. for (let op = 0; op < 256; op++) { @@ -127,6 +136,8 @@ if (wideArg === "wide") { } } } +// A harness's own cases, which name what they cover. +if (Array.isArray(harness.cases)) cases.push(...harness.cases); for (let i = 0; i < count; i++) { const len = Number(next() % 48n); const input = new Uint8Array(len + 1); @@ -152,7 +163,7 @@ for (const input of cases) { if (show(want) !== show(got)) { failures++; if (failures <= 20) { - console.error(`input ${show(input)}\nnative ${show(want)}\nts ${show(got)}\n`); + console.error(`input ${show(input)}\nnative ${display(want)}\nts ${display(got)}\n`); } } } diff --git a/ts/tests/fixtures.rs b/ts/tests/fixtures.rs index 2e98ee679..b70252986 100644 --- a/ts/tests/fixtures.rs +++ b/ts/tests/fixtures.rs @@ -265,6 +265,28 @@ fn lang() { differential("lang-fixture", &out, "caller.ts", 50_000, &["wide"]); } +/// Futures and drops: the harness is hand-written TS around the translation, whose FIFO +/// executor logs every poll, wake, and drop as the fixture's native one does. +#[test] +fn task() { + let out = translated("task"); + snapshot("task", &out); + let harness = manifest_dir().join("tests").join("task.ts").display().to_string(); + let text = format!( + r#"import * as rt from "./rt.ts"; +import {{ Log }} from "./log.ts"; +import {{ Channel, Guard, blocks, drain, sum }} from "./model.ts"; +import {{ harness }} from {harness:?}; + +export {{ cases }} from {harness:?}; +export const run = harness({{ Pending: rt.Pending, u64: rt.u64, Log, Channel, Guard, sum, drain, blocks }}); +"# + ); + std::fs::write(out.join("caller.ts"), text).unwrap(); + typecheck(&out); + differential("task-fixture", &out, "caller.ts", 20_000, &[]); +} + /// Each construct outside the subset fails with its span, and nothing is written. #[test] fn reject() { diff --git a/ts/tests/runtime.rs b/ts/tests/runtime.rs new file mode 100644 index 000000000..f9d024358 --- /dev/null +++ b/ts/tests/runtime.rs @@ -0,0 +1,15 @@ +//! The runtime's own behavior, beyond what translated fixtures exercise. + +use std::path::PathBuf; +use std::process::Command; + +/// `rt.spawn` yields to a macrotask once a batch spends its poll budget. +#[test] +fn spawn_budget() { + let script = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests").join("spawn.ts"); + let output = Command::new("node") + .arg(script) + .output() + .expect("running node, which the dev shell provides"); + assert!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr)); +} diff --git a/ts/tests/spawn.ts b/ts/tests/spawn.ts new file mode 100644 index 000000000..8a8cdaeef --- /dev/null +++ b/ts/tests/spawn.ts @@ -0,0 +1,33 @@ +// `rt.spawn` on futures that misbehave: one that keeps waking itself yields to a timer +// queued before it once a batch spends its budget of 128 polls, so it cannot starve +// timers and I/O, and one that panics as it drops rejects its promise. +// +// usage: node spawn.ts + +import * as rt from "../runtime/rt.ts"; + +let polls = 0; +let beforeTimer: number | undefined; +const selfWaking: rt.Future = { + poll(cx: rt.Context): rt.Poll { + if (++polls === 1000) return polls; + cx.wakeByRef(); + return rt.Pending; + }, + [Symbol.dispose](): void {}, +}; +setTimeout(() => { + beforeTimer = polls; +}, 0); +await rt.spawn(selfWaking); +if (beforeTimer !== 128) throw new Error(`${beforeTimer} polls ran before the timer, not 128`); + +const panicsOnDrop: rt.Future = { + poll: () => 1, + [Symbol.dispose]: () => rt.panic("drop failed"), +}; +const settled = await rt.spawn(panicsOnDrop).then( + () => "resolved", + (err: unknown) => (err instanceof rt.Panic ? err.message : `threw ${err}`), +); +if (settled !== "drop failed") throw new Error(`a panic in drop settled the promise as ${settled}`); diff --git a/ts/tests/task.ts b/ts/tests/task.ts new file mode 100644 index 000000000..e335cfe57 --- /dev/null +++ b/ts/tests/task.ts @@ -0,0 +1,251 @@ +// The task fixture's harness on the translation: a deterministic FIFO executor and the +// script runner of `fixtures/task/src/exec.rs` and `harness.rs`, step for step, logging +// the same events through the translated `Log`. `differential.ts` requires the two logs +// to match byte for byte, panics included. + +/** A waker, as the translation calls it. */ +interface Waker extends Disposable { + wake(): void; + wakeByRef(): void; + clone(): Waker; + willWake(other: Waker): boolean; +} + +interface Future extends Disposable { + poll(cx: Waker): T | symbol; +} + +interface Log { + note(event: string): void; + text(): { view(): Uint8Array }; + clone(): Log; +} + +type Consumer = Disposable; +type Guard = Disposable; + +interface Channel extends Disposable { + consumer(): Consumer; + send(value: V): boolean; + close(): boolean; + cloneProducer(): boolean; +} + +/** + * What the harness drives: the translation's roots, and the runtime's `Pending` and + * `u64`, whose values are `V`. + */ +export interface Fixture { + Pending: symbol; + u64: { of(value: number): V }; + Log: { "new"(): Log }; + Channel: { open(): Channel }; + Guard: { "new"(id: number, log: Log): Guard }; + sum(rx: Consumer, guard: Guard): Future; + drain(rx: Consumer, depth: number): Future; + blocks(rx: Consumer, guard: Guard, moves: boolean): Future; +} + +/** What every waker of one task points at. */ +class Wake { + readonly task: number; + readonly log: Log; + readonly queue: number[]; + queued = false; + + constructor(task: number, log: Log, queue: number[]) { + this.task = task; + this.log = log; + this.queue = queue; + } + + schedule(): void { + if (this.queued) return; + this.queued = true; + this.queue.push(this.task); + } +} + +class TaskWaker implements Waker { + readonly target: Wake; + + constructor(target: Wake) { + this.target = target; + } + + wake(): void { + this.target.log.note(`t${this.target.task} wake`); + this.target.schedule(); + } + + wakeByRef(): void { + this.wake(); + } + + clone(): Waker { + this.target.log.note(`t${this.target.task} waker clone`); + return new TaskWaker(this.target); + } + + willWake(other: Waker): boolean { + return other instanceof TaskWaker && other.target === this.target; + } + + [Symbol.dispose](): void { + this.target.log.note(`t${this.target.task} waker drop`); + } +} + +interface Task { + future: Future; + waker: Waker; + wake: Wake; + done: boolean; +} + +/** Runs tasks in the order they were woken. */ +class Executor { + readonly queue: number[] = []; + /** Every task spawned, by id; `undefined` once dropped. */ + readonly tasks: (Task | undefined)[] = []; + readonly log: Log; + readonly pending: symbol; + + constructor(log: Log, pending: symbol) { + this.log = log; + this.pending = pending; + } + + spawn(future: Future): void { + const task = this.tasks.length; + this.log.note(`t${task} spawn`); + const wake = new Wake(task, this.log, this.queue); + wake.schedule(); + this.tasks.push({ future, waker: new TaskWaker(wake), wake, done: false }); + } + + run(): void { + while (this.step()); + } + + step(): boolean { + const task = this.queue.shift(); + if (task === undefined) return false; + const t = this.tasks[task]; + if (t !== undefined && !t.done) this.poll(task); + return true; + } + + poll(task: number): void { + const t = this.tasks[task]; + if (t === undefined) return; + t.wake.queued = false; + this.log.note(`t${task} poll`); + const output = t.future.poll(t.waker); + if (output === this.pending) { + this.log.note(`t${task} pending`); + } else { + t.done = true; + this.log.note(`t${task} ready ${String(output)}`); + } + } + + drop(task: number): void { + const t = this.tasks[task]; + if (t === undefined) return; + this.tasks[task] = undefined; + this.log.note(`t${task} drop`); + t.future[Symbol.dispose](); + t.waker[Symbol.dispose](); + } +} + +/** The script runner, as `harness.rs` runs it natively. */ +export function harness(f: Fixture): (input: Uint8Array) => { view(): Uint8Array } { + return (input) => { + const log = f.Log.new(); + const exec = new Executor(log, f.Pending); + const channels: Channel[] = []; + for (let i = 0; i < input.length; i += 2) { + const op = input[i]; + const arg = input[i + 1] ?? 0; + const pick = (len: number) => (len > 0 ? arg % len : undefined); + const c = pick(channels.length); + switch (op % 11) { + case 0: + log.note(`c${channels.length} open`); + channels.push(f.Channel.open()); + break; + case 1: + if (c !== undefined) exec.spawn(f.sum(channels[c].consumer(), f.Guard.new(exec.tasks.length, log.clone()))); + break; + case 2: + if (c !== undefined) exec.spawn(f.drain(channels[c].consumer(), [1, 4, 16][(arg >> 4) % 3])); + break; + case 3: + if (c !== undefined) { + log.note(`c${c} send ${arg}`); + if (!channels[c].send(f.u64.of(arg))) log.note(`c${c} closed`); + } + break; + case 4: + if (c !== undefined) { + log.note(`c${c} close`); + channels[c].close(); + } + break; + case 5: + if (c !== undefined) { + log.note(`c${c} clone`); + channels[c].cloneProducer(); + } + break; + case 6: + exec.run(); + break; + case 7: + exec.step(); + break; + case 8: { + const t = pick(exec.tasks.length); + if (t !== undefined) exec.poll(t); + break; + } + case 9: { + const t = pick(exec.tasks.length); + if (t !== undefined) exec.drop(t); + break; + } + default: + if (c !== undefined) { + exec.spawn(f.blocks(channels[c].consumer(), f.Guard.new(exec.tasks.length, log.clone()), arg >= 128)); + } + } + } + for (let t = 0; t < exec.tasks.length; t++) exec.drop(t); + channels.forEach((channel, c) => { + log.note(`c${c} drop`); + channel[Symbol.dispose](); + }); + return log.text(); + }; +} + +/** Scripts for the cases async must get right, run before the random ones. */ +export const cases: Uint8Array[] = [ + // A future dropped before its first poll runs nothing, and drops its arguments. + [0, 0, 1, 0, 2, 0, 9, 0, 9, 1], + // One dropped mid-await drops its awaited future, then its locals, then its arguments. + [0, 0, 1, 0, 6, 0, 3, 5, 6, 0, 9, 0], + // Polling a future after it returned `Ready` panics. + [0, 0, 1, 0, 6, 0, 4, 0, 6, 0, 8, 0], + // Every await depth, woken by sends and the channel closing. + [0, 0, 2, 0, 2, 16, 2, 32, 6, 0, 3, 7, 6, 0, 3, 9, 7, 0, 4, 0, 6, 0], + // The channel stays open until its last producer drops. + [0, 0, 1, 0, 5, 0, 4, 0, 6, 0, 3, 2, 4, 0, 6, 0], + // A task woken twice before its poll is queued once. + [0, 0, 0, 1, 1, 0, 1, 1, 6, 0, 3, 1, 5, 0, 3, 2, 6, 0], + // An async block's captures: one borrowed, one moved on one path only, and one moved + // but only borrowed, dropped when its block returns or is cancelled mid-await. + [0, 0, 10, 0, 10, 128, 10, 0, 6, 0, 3, 5, 7, 0, 7, 0, 9, 2, 6, 0], +].map((script) => Uint8Array.from(script));