diff --git a/quest/m2/README.md b/quest/m2/README.md index 073b47f38..77cf7ab9b 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -21,7 +21,8 @@ reports; [m1](/quest/m1/README.md#plan) records the decision. ## Required -- [MIR lowering spike](/quest/m2/ts-mir-spike.md) - the whole lowering ported to MIR on two fixtures decides whether the translator moves +- [MIR's codec as fast as THIR's](/quest/m2/ts-mir-codec.md) - the codec from MIR no slower than from THIR in any engine +- [Lower bodies from MIR](/quest/m2/ts-mir.md) - the MIR spike's prototype becomes the only lowering, and the THIR one goes - [Enum variant field order](/quest/m2/ts-variant-order.md) - a variant's fields evaluate in the order written, not declared - [Stable names](/quest/m2/ts-stable-names.md) - generic instances and trait-impl methods always carry their suffix, so callers' names never shift - [Diverging calls through closures and generics](/quest/m2/ts-never-edges.md) - closures returning `!` and generics at `!` pass `tsc` diff --git a/quest/m2/ts-async.md b/quest/m2/ts-async.md index 0dd1180c1..49bbbaab4 100644 --- a/quest/m2/ts-async.md +++ b/quest/m2/ts-async.md @@ -8,46 +8,35 @@ Differential tests against native Rust prove the same polls, wakes, drops, and r ## Plan -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. +Decided by the MIR spike ([#36](https://github.com/kixelated/rust2/pull/36)): an async +body is the state machine rustc's coroutine transform makes, lowered from MIR. Each +coroutine instance is a class of its state, its captures, and the locals it keeps +across an `.await`; its `poll` is the transform's resume function, a loop over a +`switch` on the next block since it enters loops in the middle, and its +`[Symbol.dispose]` is the transform's drop, which drops what the state it is in holds. +A handoff through 1, 4, and 16 async functions measured 1.5-2.7x faster than #16's +generators in Node, 1.8-5.5x in Bun, and 1.4-3.5x in Chrome, and past depth 1 faster +than moq's hand-written promises. `rt.spawn` stays. + +What moq-net still needs, by its census from MIR at the pinned revision: + +- `Poll`'s helpers (about 340 calls: `is_ready`, `is_pending`, `map`, `map_ok`, + `map_err`), `pin!` (50 with `PinMacroHelper`), `poll_fn` (31), `future::pending` + (5), and `Box::pin` (29, a boxed future that binds to hand-written TS at the + boundary, per the [model](/quest/m2/ts-model.md)). +- `?` inside a function that returns `Poll`, as `ready!(..)?` writes it: 284 sites. +- 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 -THIR, and the [MIR lowering spike](/quest/m2/ts-mir-spike.md) then decides -whether the whole lowering moves to MIR; rewrite this plan to its report too. -Also in scope, found since: - -- `?` inside a function that returns `Poll`, as `ready!(..)?` writes it: about - 224 sites in moq-net. -- A `()` future awaited as a statement fails `tsc`: `Future` against - `Future`. +An async block that captures part of a variable, which THIR refused 47 times, is a +capture like any other in MIR. Public API: the translator's output for async code. ## Required -- [MIR lowering spike](/quest/m2/ts-mir-spike.md) - rewrite to the spike's report before starting +- [Lower bodies from MIR](/quest/m2/ts-mir.md) - async bodies come from the coroutine transform - [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 c501d6658..380257faa 100644 --- a/quest/m2/ts-drop.md +++ b/quest/m2/ts-drop.md @@ -9,56 +9,38 @@ native Rust. ## Plan -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. - -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 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 -THIR's scopes, and the [MIR lowering spike](/quest/m2/ts-mir-spike.md) then -decides whether drops move to MIR's elaboration; rewrite this plan to its report -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. - -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. +Decided by the MIR spike ([#36](https://github.com/kixelated/rust2/pull/36)): drop +points come from MIR's drop elaboration, which puts each drop where Rust runs it, with +a flag where a value moved on some paths only, so the lowering adds nothing of its own: +no `finally` per scope, and no `undefined` left by a move. A panic unwinds through the +cleanup blocks of `-C panic=unwind` MIR, which a function runs from a `catch` (the +task fixture's unwinding test checks them against native unwinding). An enum, tuple, +or `Result` that drops is dropped by glue lowered from rustc's own `drop_in_place` +shim. + +What still fails translation from MIR, by the census of moq-net at the pinned revision +(kio's has 9 in all): + +- An iterator that owns a value that drops (95): an `rt.Iter` drops nothing it skips, + consumes, or still holds when it is dropped. +- Dropping a value whose glue is std's (128): a `Vec` of a value without + `[Symbol.dispose]`, such as an enum, and the instances of generic enums a census + lowers over their type parameters. +- A std mapping that may discard an operand that drops (20): `keeps_operands` in + `library.rs` refuses any operand that drops, even one a mapping always keeps; + it should name the operands each mapping may discard instead. +- A generic type that implements `Drop` (11). + +THIR's refusals that MIR's elaboration closes, for comparison: 657 drops of a value +without glue, 280 temporaries that drop, 60 `for` loops, 112 moves out of other places, +and 75 more in patterns, closures, and struct updates. + +Watch the cost: a function whose cleanup does something JS can see sets a variable +before each call that can panic and wraps its body in a `try`; measure any change on +the [TS benchmark](/ts/README.md#bench). Public API: the translator's output for values that drop. ## Required -- [MIR lowering spike](/quest/m2/ts-mir-spike.md) - rewrite to the spike's report before starting +- [Lower bodies from MIR](/quest/m2/ts-mir.md) - drops come from MIR's elaboration diff --git a/quest/m2/ts-mir-codec.md b/quest/m2/ts-mir-codec.md new file mode 100644 index 000000000..69614bd16 --- /dev/null +++ b/quest/m2/ts-mir-codec.md @@ -0,0 +1,17 @@ +# [M] MIR's codec as fast as THIR's + +## Goal + +Lowering moq-net's codec from MIR runs it no slower than THIR's lowering on +`just ts bench`, in Node, Bun, and Chromium, before MIR becomes the only lowering. + +## Plan + +Decided with the maintainer on 2026-10-24: this blocks making MIR the default. On +[#36](https://github.com/kixelated/rust2/pull/36)'s head, after its review fixes, the +codec's geometric mean from MIR was 1.10x THIR's in Node, 1.02x in Bun, and 1.12x in +Chromium, against A/A controls of 1.00. Group encode was 1.9-2.6x slower in every +engine, frames encode 1.3-1.4x in Chromium, and small varint encodes up to 1.6x in +Node (but 0.7x in Bun). An earlier run, before the review fixes and the bench's +random order, put the codec within 8% apart from group encode, so start by +separating what the review fixes' copies cost from what was always there. diff --git a/quest/m2/ts-mir-spike.md b/quest/m2/ts-mir-spike.md deleted file mode 100644 index bae266c32..000000000 --- a/quest/m2/ts-mir-spike.md +++ /dev/null @@ -1,35 +0,0 @@ -# [L] MIR lowering spike - -## Goal - -A report decides whether [rust2-ts](/ts/README.md) lowers from MIR instead of THIR. -A port of the whole lowering to MIR translates the codec and task fixtures (plain -code, drops, and async state machines), and is measured against the THIR lowering -on bundle size, `just ts bench` speed, exact drop traces (the task fixture's trace -diff), and how many census refusals move. If MIR wins, the translator moves to it; -otherwise the port is deleted. - -## Plan - -Decided with the maintainer on 2026-10-24: merge the async spike -([#16](https://github.com/kixelated/rust2/pull/16)) on THIR, then run this spike -over the whole lowering, on those two fixtures. The priorities that decide it are -the translator's ([design](/ts/README.md#design)): correctness first, speed second, -and bundle size third, with readable output not a goal. So the state machines' -1.4-2.6x speed weighs ahead of their roughly 18% larger bundle; measure the size -as the consumer's final minified JS. - -Why, from #16: - -- Its MIR state machines were 1.4-2.6x faster per handoff than the generators it - landed, and about 18% larger gzipped. -- THIR scope drops drew 10 review rounds of small drop-coverage bugs. MIR's drop - elaboration is exact by construction. - -What #16 found against MIR, to answer rather than assume: its prototype -re-implemented places, rvalues, constants, closures, and `format_args!` at the MIR -level, and under wasm32's `panic=abort` MIR has no cleanup paths, so a JS -exception would skip drops that THIR's `finally` blocks run. That its output read as -basic blocks no longer counts against it. - -Public API: none unless the translator moves; then its whole output. diff --git a/quest/m2/ts-mir.md b/quest/m2/ts-mir.md new file mode 100644 index 000000000..bba9931ab --- /dev/null +++ b/quest/m2/ts-mir.md @@ -0,0 +1,50 @@ +# [L] Lower bodies from MIR + +## Goal + +[rust2-ts](/ts/README.md) lowers every body from MIR, as the +[MIR spike](https://github.com/kixelated/rust2/pull/36) decided: `--lower` goes, +the THIR lowering (`src/lower/body.rs`, `pat.rs`, the drop scopes in `drop.rs`, and +`rt.future`'s generators) is deleted, and every fixture passes its snapshot, +differential, unwinding, and census tests from MIR. + +## Plan + +The spike's prototype (`src/lower/mir/`) already passes the codec, lang, and task +fixtures' differential tests and the task fixture's unwinding test. What the move +still needs: + +- The reject fixture and its census, rewritten to what MIR refuses (`reject_mir` + already checks MIR's errors), and snapshots of MIR's output. Constructs THIR refuses that MIR lowers move into the differential + fixtures, such as a `move` closure taking a reference from a variable assigned again + after (`a closure that copies a mutable variable`), which the prototype snapshots. +- The census to at least THIR's coverage of moq-net and kio. MIR types each + intermediate it holds, so a gap in the library or model shows up once more per + temporary; read the census by construct, not by total. MIR-only refusals to close: + `?` on `Poll>` (284, which THIR refused as 235 too), `ControlFlow` other + than from `?`, switches on and fields of types without a mapping, and a reference + copied out of a field and then passed on. +- `?` on an `Option` returned through a `next` whose item can be `undefined`, and the + other THIR special cases the prototype mirrors (`for` loops by the iterator protocol, + `from_be_bytes` of bytes made in place, ranges stepped in place): keep each only + where `just ts bench` measures it. +- Size: the task fixture's bundle is 35% larger gzipped than THIR's, the codec's 9%. + Likely wins, each to measure: a coroutine resume function structured where the + graph allows (a dispatch at each loop header the resume enters, rather than a + `switch` over every block), temporaries folded through reads of borrowed places, and + `_0 = v; return _0` returned in place. +- Source maps: MIR spans point at calls and asserts; check stack traces read well. +- Panics unwind (`-C panic=unwind`), as decided in + [#36](https://github.com/kixelated/rust2/pull/36) at a measured 4-6% on V8's async + handoff; `RUST2_TS_ABORT`, kept only to measure aborting, goes with `--lower`. + +Watch what the prototype's comments say it relies on: MIR's single-assignment +temporaries, `Box` derefs elaborated into `BoxDerefTransmute`, and drop shims that +take a raw pointer. A nightly bump that changes them should fail loudly, as each does +now. + +Public API: the translator's whole output, and the `--lower` option, which goes. + +## Required + +- [MIR's codec as fast as THIR's](/quest/m2/ts-mir-codec.md) - the codec from MIR no slower than from THIR in any engine diff --git a/quest/m2/ts-shared.md b/quest/m2/ts-shared.md index 4045e3622..31f8f2282 100644 --- a/quest/m2/ts-shared.md +++ b/quest/m2/ts-shared.md @@ -25,6 +25,9 @@ is its own guard. Left: `Rc`, `weak_count` (a dropped `Weak` counts nothing yet) panics. A re-entrant `Mutex` lock on one thread is unspecified in Rust; pick a loud failure (panic) and document it. - Measure the counter overhead on the [TS benchmark](/ts/README.md#bench). +- `rt.copy` copies a `Copy` aggregate deeply, through the shared references in it, + so copying `(&Arc, u8)` copies the `Arc` and its count, or a `Cell` and its + value. Copy only what the type owns. Public API: the translator's output for shared and interior-mutable types. diff --git a/ts/README.md b/ts/README.md index 12b6311b9..28589c2bf 100644 --- a/ts/README.md +++ b/ts/README.md @@ -3,7 +3,7 @@ A rustc driver that translates a subset of Rust to TypeScript with source maps. No WASM: the output is plain ES modules that Node, Bun, and bundlers run as is. ```bash -rust2-ts --config ts.toml (--out dir | --census file.md) [--manifest-path Cargo.toml] [-- cargo args] +rust2-ts --config ts.toml (--out dir | --census file.md) [--manifest-path Cargo.toml] [--lower thir|mir] [-- cargo args] ``` It runs `cargo check` on the package with itself as `RUSTC_WRAPPER`, for @@ -28,6 +28,10 @@ ignore-macros = ["tracing::trace"] moq consumes it as the `ts` package of this repository's flake. In this repository, `just ts run ...` runs it from source on the pinned nightly. +`--lower mir` lowers bodies from MIR instead of THIR: the prototype the MIR spike +built, which the translator is moving to ([quest](/quest/m2/ts-mir.md)). The rest of +this page describes the THIR lowering, the default, except where it says MIR. + ## Census `--census file.md` translates nothing. It treats every item in the library as a root, @@ -201,11 +205,20 @@ 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. 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. +- Bodies lower from MIR ([moving](/quest/m2/ts-mir.md)): drops are where drop + elaboration put them, exact by construction, with no `finally` per scope or + `undefined` left by a move, and a panic runs the cleanup blocks `-C panic=unwind` + MIR has, from a `catch`. Async bodies are the coroutine transform's state machines, + a class each. The MIR spike ([#36](https://github.com/kixelated/rust2/pull/36)) ported + the whole lowering and measured it against THIR's: every fixture's differential test + passes either way, but MIR closes about 1,100 of the 1,400 drop refusals in moq-net's + census, runs an async handoff 1.4-5.5x faster at depths 1 to 16 in Node, Bun, and + Chrome, and runs the codec 2-12% slower than THIR, for 9% more gzip on the codec's + bundle and 35% on the async fixture's. Blocks become nested blocks, loops, and `if`s by Ramsey's + "Beyond Relooper", except a coroutine's resume function, which enters loops in the + middle and becomes a loop over a `switch` on the next block. Temporaries assigned + once and read once fold back into the expression that reads them unless that + would reorder an effect. ## Never @@ -258,9 +271,15 @@ built natively for wasm32 and run beside its translation on the same inputs by - `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. + fixture's native executor does. The logs must match byte for byte. `tests/unwind.ts` + also runs the scripts natively on the host (`examples/unwind.rs`), where a panic + unwinds, catching and logging each step's panic and going on, so what a panic drops + must match too. - `reject` holds constructs outside the subset; its expected errors are `expected.txt`, - and its expected census is `census.md`. + those from MIR `expected-mir.txt`, and its expected census is `census.md`. + +The codec, lang, and task fixtures also run their differential tests from MIR +(`codec_mir`, `lang_mir`, and `task_mir`), without snapshots. `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. diff --git a/ts/fixtures/lang/expected/shapes.ts b/ts/fixtures/lang/expected/shapes.ts index 573c70998..041016e0d 100644 --- a/ts/fixtures/lang/expected/shapes.ts +++ b/ts/fixtures/lang/expected/shapes.ts @@ -100,6 +100,24 @@ export function copies(data: Uint8Array, out: rt.ByteVec): void { a = Point.new(5, 6); const old2 = old; out.extendFromSlice(Uint8Array.of(a.x & 0xff, b.x & 0xff, old2.x & 0xff)); + let n: rt.u32 = data.length as rt.u32; + const both = [n, n] as [rt.u32, rt.u32]; + n = 9; + out.extendFromSlice(rt.u32.toBeBytes(rt.u32.add(rt.u32.add(both[0], both[1]), n))); + let word: Uint8Array = Uint8Array.of(0, 0, 0, data.length & 0xff); + const converted = rt.u32.fromBeBytes(word); + word[0] = 9; + out.extendFromSlice(rt.u32.toBeBytes(rt.u32.add(converted, word[0] as rt.u32))); + let cell: Uint8Array = Uint8Array.of(data.length & 0xff); + out.push(writeThenRead(rt.copy(cell), cell)); + let source: Uint8Array = Uint8Array.of(data.length & 0xff); + const returned = same(rt.copy(source)); + source[0] = 9; + out.extendFromSlice(Uint8Array.of(returned[0], source[0])); + out.push(copyThenWrite([data.length & 0xff, 0])); + const marked = mark(out, 1) === 0; + mark(out, 2); + out.push(marked ? 1 : 0); let opt: Meters.Meters | undefined = { _0: 9 }; const taken = opt; opt = undefined; @@ -117,6 +135,30 @@ export function copies(data: Uint8Array, out: rt.ByteVec): void { out.extendFromSlice(Uint8Array.of(first.x & 0xff, rt.result.unwrap(rt.copy(res), rt.u8.display).x & 0xff, err.y & 0xff, rt.result.unwrapErr(rt.copy(fail), rt.u8.display).y & 0xff)); } +/** `a`'s byte, after writing `b`'s. */ +export function writeThenRead(a: Uint8Array, b: Uint8Array): rt.u8 { + b[0] = 9; + return a[0]; +} + +/** A copy of `a` read after `a` changes in place, which it doesn't see. */ +export function copyThenWrite(a: [rt.u8, rt.u8]): rt.u8 { + const b = rt.copy(a); + a[0] = 9; + return rt.u8.wrappingAdd(b[0], a[0]); +} + +/** `a` itself. */ +export function same(a: Uint8Array): Uint8Array { + return rt.copy(a); +} + +/** Logs `b`, returning how much is logged. */ +export function mark(out: rt.ByteVec, b: rt.u8): rt.usize { + out.push(b); + return out.length; +} + /** Fieldless enums cast to every width, past their discriminant's range and sign. */ export function discriminants(data: Uint8Array, out: rt.ByteVec): void { for (const item of data) { @@ -631,6 +673,10 @@ export function patterns(data: Uint8Array, out: rt.ByteVec): void { area = rt.i64.mul(rt.i32.sub(a2.x, b3.x) as rt.i64, rt.i32.sub(a2.y, b3.y) as rt.i64); } out.extendFromSlice(rt.i64.toBeBytes(area)); + let bytes: Uint8Array = Uint8Array.of(data.length & 0xff, 1, 2); + const tail = bytes.subarray(1, 3); + tail[0] = 9; + out.extendFromSlice(bytes); const total = rt.u64.add(total$Shape([shape_, { tag: "Dot" } as Shape.Shape], out), total$Meters([{ _0: 3 }, { _0: rt.u64.of(18446744073709551615n) }], out)); out.extendFromSlice(rt.u64.toBeBytes(total)); const parsed = rt.Ok(data.length & 0xff); @@ -684,6 +730,9 @@ export function closures(data: Uint8Array, out: rt.ByteVec): void { return rt.u32.add(rt.u32.mul(v2, 3), base); }; out.extendFromSlice(rt.u32.toBeBytes(apply(scale, sum))); + let whole: Uint8Array = Uint8Array.of(data.length & 0xff); + overwrite(whole); + out.push(whole[0]); let parsed: Array = []; for (const item2 of data) { const b2 = item2; @@ -696,6 +745,14 @@ export function closures(data: Uint8Array, out: rt.ByteVec): void { out.extendFromSlice(tags.view()); } +/** Replaces `a` whole from a closure. */ +export function overwrite(a: Uint8Array): void { + let write: () => void = (): void => { + a.set(Uint8Array.of(9)); + }; + write(); +} + export function apply(f: (a0: rt.u32) => rt.u32, v: rt.u32): rt.u32 { return f(v); } diff --git a/ts/fixtures/lang/src/shapes.rs b/ts/fixtures/lang/src/shapes.rs index a9c0a3131..476b7742e 100644 --- a/ts/fixtures/lang/src/shapes.rs +++ b/ts/fixtures/lang/src/shapes.rs @@ -168,6 +168,36 @@ pub fn copies(data: &[u8], out: &mut Vec) { let old = std::mem::replace(&mut a, Point::new(5, 6)); out.extend_from_slice(&[a.x as u8, b.x as u8, old.x as u8]); + // A tuple keeps the values it was made of, though the variable they came from changes. + let mut n = data.len() as u32; + let both = (n, n); + n = 9; + out.extend_from_slice(&(both.0 + both.1 + n).to_be_bytes()); + + // A value computed from a call keeps that call before a later one, though it is read + // after. + // A conversion reads its bytes before they change. + let mut word = [0, 0, 0, data.len() as u8]; + let converted = u32::from_be_bytes(word); + word[0] = 9; + out.extend_from_slice(&(converted + word[0] as u32).to_be_bytes()); + + // An argument copied keeps its value, though the callee changes its source through + // another. + let mut cell = [data.len() as u8]; + out.push(write_then_read(cell, &mut cell)); + + // A result returning an argument is its own copy, though the source changes after. + let mut source = [data.len() as u8]; + let returned = same(source); + source[0] = 9; + out.extend_from_slice(&[returned[0], source[0]]); + out.push(copy_then_write((data.len() as u8, 0))); + + let marked = mark(out, 1) == 0; + mark(out, 2); + out.push(marked as u8); + let mut opt = Some(Meters(9)); let taken = opt.take(); out.push(opt.is_none() as u8); @@ -188,6 +218,33 @@ pub fn copies(data: &[u8], out: &mut Vec) { ]); } +/// `a`'s byte, after writing `b`'s. +#[inline(never)] +fn write_then_read(a: [u8; 1], b: &mut [u8; 1]) -> u8 { + b[0] = 9; + a[0] +} + +/// A copy of `a` read after `a` changes in place, which it doesn't see. +fn copy_then_write(mut a: (u8, u8)) -> u8 { + let b = a; + a.0 = 9; + b.0.wrapping_add(a.0) +} + +/// `a` itself. +#[inline(never)] +fn same(a: [u8; 1]) -> [u8; 1] { + a +} + +/// Logs `b`, returning how much is logged. +#[inline(never)] +fn mark(out: &mut Vec, b: u8) -> usize { + out.push(b); + out.len() +} + /// Fieldless enums cast to every width, past their discriminant's range and sign. pub fn discriminants(data: &[u8], out: &mut Vec) { for &b in data { @@ -586,6 +643,11 @@ pub fn patterns(data: &[u8], out: &mut Vec) { Shape::Line(a, b) => (a.x - b.x) as i64 * (a.y - b.y) as i64, }; out.extend_from_slice(&area.to_be_bytes()); + // A rest pattern borrowing bytes changes them where they are. + let mut bytes = [data.len() as u8, 1, 2]; + let [_, tail @ ..] = &mut bytes; + tail[0] = 9; + out.extend_from_slice(&bytes); let total = total(&[shape, Shape::Dot], out) + total(&[Meters(3), Meters(u64::MAX)], out); out.extend_from_slice(&total.to_be_bytes()); @@ -634,6 +696,11 @@ pub fn closures(data: &[u8], out: &mut Vec) { let scale = move |v: u32| v * 3 + base; out.extend_from_slice(&apply(scale, sum).to_be_bytes()); + // A closure writing a whole array through a reference it captured changes the caller's. + let mut whole = [data.len() as u8]; + overwrite(&mut whole); + out.push(whole[0]); + let mut parsed: Vec> = Vec::new(); for &b in data { parsed.push(dir(b)); @@ -645,6 +712,12 @@ pub fn closures(data: &[u8], out: &mut Vec) { out.extend_from_slice(&tags); } +/// Replaces `a` whole from a closure. +fn overwrite(a: &mut [u8; 1]) { + let mut write = || *a = [9]; + write(); +} + fn apply u32>(f: F, v: u32) -> u32 { f(v) } diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index 5ebf4ff32..ab41823a8 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,7 +1,7 @@ # Census of `reject-fixture` -- Refusals: 75 -- Function instances: 65 +- Refusals: 78 +- Function instances: 68 - Generic functions no instance reached, lowered over their type parameters: 3 ## By construct @@ -10,15 +10,16 @@ | ---: | --- | --- | | 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:218:2 | +| 4 | replacing the value behind a reference to `_` | fixtures/reject/src/lib.rs:161:17 | | 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 | an iterator that owns `_`, which drops, which is not supported yet | fixtures/reject/src/lib.rs:364:4 | | 3 | dropping `_`, which is not supported yet | fixtures/reject/src/lib.rs:59:31 | -| 3 | replacing the value behind a reference to `_` | fixtures/reject/src/lib.rs:161:17 | | 2 | `_`: a JS value of `_` is a copy, so a reference can't change it | fixtures/reject/src/lib.rs:19:13 | | 2 | a `for` loop over values that drop, which is not supported yet | fixtures/reject/src/lib.rs:69:6 | | 2 | a `match` on a temporary that drops, which it doesn't move out of whole | fixtures/reject/src/lib.rs:210:2 | +| 2 | a closure that copies a mutable variable | fixtures/reject/src/lib.rs:142:18 | | 2 | a field of a temporary that drops: bind it to a variable first | fixtures/reject/src/lib.rs:55:2 | | 2 | an iterator of the crate implementing `_`, which is not supported yet | fixtures/reject/src/lib.rs:302:1 | | 2 | overriding `_`, which std's adaptors call in place of `next` or beside it | fixtures/reject/src/lib.rs:282:2 | @@ -34,7 +35,7 @@ | 1 | a cast to `_` | fixtures/reject/src/lib.rs:172:2 | | 1 | a closure of type `_` | fixtures/reject/src/lib.rs:114:10 | | 1 | a closure that captures a value that drops, which is not supported yet | fixtures/reject/src/lib.rs:61:23 | -| 1 | a closure that copies a mutable variable | fixtures/reject/src/lib.rs:142:18 | +| 1 | a mutable borrow of a subslice of `_`, which JS would copy, having no views | fixtures/reject/src/lib.rs:404:13 | | 1 | a mutable reference to `_`, which is no object it can change in place | fixtures/reject/src/lib.rs:137:17 | | 1 | a parameter that drops bound by a pattern, which is not supported yet | fixtures/reject/src/lib.rs:76:21 | | 1 | a precision, as in `{:.2}`, is not supported | fixtures/reject/src/lib.rs:122:3 | @@ -45,7 +46,7 @@ | 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:183: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:200:2 | | 1 | an `if let` on a temporary that drops, which it doesn't move out of whole | fixtures/reject/src/lib.rs:234:9 | -| 1 | an array repeat of a value that drops, which is not supported yet | fixtures/reject/src/lib.rs:409:14 | +| 1 | an array repeat of a value that drops, which is not supported yet | fixtures/reject/src/lib.rs:417:14 | | 1 | an async body returning `_` is not supported: its `Ready` could be pending | fixtures/reject/src/lib.rs:238:54 | | 1 | an async body that borrows `_` mutably, which is no object it can change in place | fixtures/reject/src/lib.rs:106:3 | | 1 | an or-pattern with bindings | fixtures/reject/src/lib.rs:149:3 | @@ -66,8 +67,8 @@ The type or module each refused type or path belongs to. | 6 | `f64` | fixtures/reject/src/lib.rs:6:1 | | 4 | `Option` | fixtures/reject/src/lib.rs:31:15 | | 4 | `std::string::String` | fixtures/reject/src/lib.rs:135:16 | +| 4 | `u32` | fixtures/reject/src/lib.rs:19:13 | | 3 | `Meters` | fixtures/reject/src/lib.rs:161:17 | -| 3 | `u32` | fixtures/reject/src/lib.rs:19:13 | | 2 | `Iterator` | fixtures/reject/src/lib.rs:282:2 | | 2 | `Noisy` | fixtures/reject/src/lib.rs:364:16 | | 2 | `char` | fixtures/reject/src/lib.rs:10:18 | @@ -79,6 +80,7 @@ The type or module each refused type or path belongs to. | 1 | `FusedIterator` | fixtures/reject/src/lib.rs:356:1 | | 1 | `Poll` | fixtures/reject/src/lib.rs:222:20 | | 1 | `Slot` | fixtures/reject/src/lib.rs:86:1 | +| 1 | `[u8; 1]` | fixtures/reject/src/lib.rs:424:45 | | 1 | `dyn std::ops::Fn(u8) -> u8` | fixtures/reject/src/lib.rs:14:16 | | 1 | `std::iter::Chain` | fixtures/reject/src/lib.rs:315:51 | | 1 | `std::ops::Range` | fixtures/reject/src/lib.rs:323:8 | @@ -110,6 +112,7 @@ The type or module each refused type or path belongs to. | 1 | a cast from `_` | `char` | fixtures/reject/src/lib.rs:11:2 | | 1 | a cast to `_` | `f64` | fixtures/reject/src/lib.rs:172:2 | | 1 | a closure of type `_` | `{async closure}` | fixtures/reject/src/lib.rs:114:10 | +| 1 | a mutable borrow of a subslice of `_`, which JS would copy, having no views | `u32` | fixtures/reject/src/lib.rs:404:13 | | 1 | a mutable reference to `_`, which is no object it can change in place | `std::string::String` | fixtures/reject/src/lib.rs:137:17 | | 1 | an async body returning `_` is not supported: its `Ready` could be pending | `std::task::Poll` | fixtures/reject/src/lib.rs:238:54 | | 1 | an async body that borrows `_` mutably, which is no object it can change in place | `u8` | fixtures/reject/src/lib.rs:106:3 | @@ -125,6 +128,7 @@ The type or module each refused type or path belongs to. | 1 | passing `_`, which drops, to `_`, which is not supported yet | `std::option::Option`, `Option::unwrap_or` | fixtures/reject/src/lib.rs:393:4 | | 1 | replacing the `_` behind a reference, whose JS object is shared or private | `std::sync::Arc` | fixtures/reject/src/lib.rs:218:2 | | 1 | replacing the `_` behind a reference, whose JS object is shared or private | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:264:2 | +| 1 | replacing the value behind a reference to `_` | `[u8; 1]` | fixtures/reject/src/lib.rs:424:45 | | 1 | unsupported call to `_` | `::cycle` | fixtures/reject/src/lib.rs:269:11 | | 1 | unsupported call to `_` | `::write_fmt` | fixtures/reject/src/lib.rs:137:10 | | 1 | unsupported call to `_` | `std::ops::Range::contains` | fixtures/reject/src/lib.rs:323:8 | @@ -152,7 +156,16 @@ The type or module each refused type or path belongs to. - fixtures/reject/src/lib.rs:218:2: replacing the `std::sync::Arc` behind a reference, whose JS object is shared or private - fixtures/reject/src/lib.rs:254:2: replacing the `std::pin::Pin>` behind a reference, whose JS object is shared or private - fixtures/reject/src/lib.rs:264:2: replacing the `std::sync::MutexGuard<'_, u8>` behind a reference, whose JS object is shared or private -- fixtures/reject/src/lib.rs:404:2: replacing the `std::pin::Pin<&mut Noisy>` behind a reference, whose JS object is shared or private +- fixtures/reject/src/lib.rs:412:2: replacing the `std::pin::Pin<&mut Noisy>` behind a reference, whose JS object is shared or private + + + +
replacing the value behind a reference to `_` (4) + +- fixtures/reject/src/lib.rs:161:17: replacing the value behind a reference to `Meters` +- fixtures/reject/src/lib.rs:161:20: replacing the value behind a reference to `Meters` +- fixtures/reject/src/lib.rs:165:25: replacing the value behind a reference to `Meters` +- fixtures/reject/src/lib.rs:424:45: replacing the value behind a reference to `[u8; 1]`
@@ -197,14 +210,6 @@ The type or module each refused type or path belongs to. -
replacing the value behind a reference to `_` (3) - -- fixtures/reject/src/lib.rs:161:17: replacing the value behind a reference to `Meters` -- fixtures/reject/src/lib.rs:161:20: replacing the value behind a reference to `Meters` -- fixtures/reject/src/lib.rs:165: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 @@ -226,6 +231,13 @@ The type or module each refused type or path belongs to.
+
a closure that copies a mutable variable (2) + +- fixtures/reject/src/lib.rs:142:18: a closure that copies a mutable variable +- fixtures/reject/src/lib.rs:432:3: a closure that copies a mutable variable + +
+
a field of a temporary that drops: bind it to a variable first (2) - fixtures/reject/src/lib.rs:55:2: a field of a temporary that drops: bind it to a variable first @@ -319,9 +331,9 @@ The type or module each refused type or path belongs to.
-
a closure that copies a mutable variable (1) +
a mutable borrow of a subslice of `_`, which JS would copy, having no views (1) -- fixtures/reject/src/lib.rs:142:18: a closure that copies a mutable variable +- fixtures/reject/src/lib.rs:404:13: a mutable borrow of a subslice of `u32`, which JS would copy, having no views
@@ -387,7 +399,7 @@ The type or module each refused type or path belongs to.
an array repeat of a value that drops, which is not supported yet (1) -- fixtures/reject/src/lib.rs:409:14: an array repeat of a value that drops, which is not supported yet +- fixtures/reject/src/lib.rs:417:14: an array repeat of a value that drops, which is not supported yet
diff --git a/ts/fixtures/reject/expected-mir.txt b/ts/fixtures/reject/expected-mir.txt new file mode 100644 index 000000000..9c273b41c --- /dev/null +++ b/ts/fixtures/reject/expected-mir.txt @@ -0,0 +1,60 @@ +a root must not be generic at fixtures/reject/src/lib.rs:170:1 +no item `missing` in the crate +a printable type must implement `Display` or `Debug` at fixtures/reject/src/lib.rs:168:1 +unsupported type `f64` at fixtures/reject/src/lib.rs:6:1 +a scalar constant of type `f64` at fixtures/reject/src/lib.rs:7:2 +arithmetic on `f64` at fixtures/reject/src/lib.rs:7:2 +unsupported type `char` at fixtures/reject/src/lib.rs:10:1 +a cast from `char` at fixtures/reject/src/lib.rs:11:2 +unsupported type `dyn std::ops::Fn(u8) -> u8` at fixtures/reject/src/lib.rs:14:1 +`&mut u32`: a JS value of `u32` is a copy, so a reference can't change it at fixtures/reject/src/lib.rs:19:1 +an assignment through a reference to `u32` at fixtures/reject/src/lib.rs:20:2 +`&mut u32`: a JS value of `u32` is a copy, so a reference can't change it at fixtures/reject/src/lib.rs:24:1 +`Option>` is not supported: its `None` and `Some` would both be `undefined` at fixtures/reject/src/lib.rs:31:1 +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 +`Option>` is not supported: its `None` and `Some` would both be `undefined` at fixtures/reject/src/lib.rs:39:1 +an iterator that owns `std::vec::Vec`, which drops, which is not supported yet at fixtures/reject/src/lib.rs:69:11 +`&mut Slot`: a JS value of `Slot` is a copy, so a reference can't change it at fixtures/reject/src/lib.rs:88:1 +`Boxed` is generic and implements `Drop`, which is not supported yet at fixtures/reject/src/lib.rs:98:10 +`&mut u8`: a JS value of `u8` is a copy, so a reference can't change it at fixtures/reject/src/lib.rs:105:10 +an assignment through a reference to `u8` at fixtures/reject/src/lib.rs:106:3 +a zero-sized constant of type `{async closure@fixtures/reject/src/lib.rs:114:10: 114:18}` at fixtures/reject/src/lib.rs:115:2 +a precision, as in `{:.2}`, is not supported at fixtures/reject/src/lib.rs:122:3 +the `+` flag of a format spec is not supported at fixtures/reject/src/lib.rs:123:3 +the `-` flag of a format spec is not supported at fixtures/reject/src/lib.rs:124:3 +formatting by `new_binary` is not supported at fixtures/reject/src/lib.rs:125:12 +formatting by `new_octal` is not supported at fixtures/reject/src/lib.rs:126:12 +formatting by `new_lower_exp` is not supported at fixtures/reject/src/lib.rs:127:12 +hex `Debug`, as in `{:x?}`, is not supported at fixtures/reject/src/lib.rs:128:3 +a width or precision from an argument, as in `{:1$}`, is not supported at fixtures/reject/src/lib.rs:129:15 +a width or precision from an argument, as in `{:1$}`, is not supported at fixtures/reject/src/lib.rs:129:3 +formatting by `new_pointer` is not supported at fixtures/reject/src/lib.rs:130:12 +`&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:135:1 +unsupported call to `::write_fmt` at fixtures/reject/src/lib.rs:137:10 +a constant of type `&u8` at fixtures/reject/src/lib.rs:156:2 +replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs:161:2 +replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs:165:2 +dropping `std::sync::Arc<&Noisy>`, which is not supported yet at fixtures/reject/src/lib.rs:179:1 +an assignment through a reference to `std::sync::Arc` at fixtures/reject/src/lib.rs:218:2 +`Poll>>` is not supported: it would be pending two ways with one value at fixtures/reject/src/lib.rs:222:1 +`Option>` is not supported: its `None` and `Some` would both be `undefined` at fixtures/reject/src/lib.rs:227:1 +`Poll>` is not supported: it would be pending two ways with one value at fixtures/reject/src/lib.rs:238:54 +an assignment through a reference to `std::pin::Pin>` at fixtures/reject/src/lib.rs:254:2 +an assignment through a reference to `std::sync::MutexGuard<'_, u8>` at fixtures/reject/src/lib.rs:264:2 +unsupported call to ` as std::iter::Iterator>::cycle` at fixtures/reject/src/lib.rs:269:11 +constructing `std::iter::Map` from another crate at fixtures/reject/src/lib.rs:288:23 +constructing `std::iter::Rev` from another crate at fixtures/reject/src/lib.rs:309:14 +a `zip` of `std::iter::Chain, {closure@fixtures/reject/src/lib.rs:315:15: 315:18}>, std::iter::Copied>>`, whose closures std calls one item further through `next` than through `fold` at fixtures/reject/src/lib.rs:315:51 +unsupported call to `std::ops::Range::::contains::` at fixtures/reject/src/lib.rs:323:8 +an iterator that owns `std::vec::Vec`, which drops, which is not supported yet at fixtures/reject/src/lib.rs:364:4 +an iterator that owns `Noisy`, which drops, which is not supported yet at fixtures/reject/src/lib.rs:369:20 +passing `std::option::Option`, which drops, to `Option::unwrap_or`, which is not supported yet at fixtures/reject/src/lib.rs:393:4 +a mutable borrow of a subslice of `u32`, which JS would copy, having no views at fixtures/reject/src/lib.rs:404:13 +an assignment through a reference to `std::pin::Pin<&mut Noisy>` at fixtures/reject/src/lib.rs:412:2 +replacing the value behind a reference to `[u8; 1]` at fixtures/reject/src/lib.rs:424:22 +a copy of a closure that changes what it captured at fixtures/reject/src/lib.rs:435:14 +overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:282:2 +an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:302:1 +overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:336:2 +an iterator of the crate implementing `FusedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:356:1 diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index 0d3547aff..4070ca5f6 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -67,8 +67,11 @@ an iterator that owns `Noisy`, which drops, which is not supported yet at fixtur a `for` loop over values that drop, which is not supported yet at fixtures/reject/src/lib.rs:385:6 passing `std::option::Option`, which drops, to `Option::unwrap_or`, which is not supported yet at fixtures/reject/src/lib.rs:393:4 a rest pattern that moves values that drop out of a place, which is not supported yet at fixtures/reject/src/lib.rs:398:14 -replacing the `std::pin::Pin<&mut Noisy>` behind a reference, whose JS object is shared or private at fixtures/reject/src/lib.rs:404:2 -an array repeat of a value that drops, which is not supported yet at fixtures/reject/src/lib.rs:409:14 +a mutable borrow of a subslice of `u32`, which JS would copy, having no views at fixtures/reject/src/lib.rs:404:13 +replacing the `std::pin::Pin<&mut Noisy>` behind a reference, whose JS object is shared or private at fixtures/reject/src/lib.rs:412:2 +an array repeat of a value that drops, which is not supported yet at fixtures/reject/src/lib.rs:417:14 +replacing the value behind a reference to `[u8; 1]` at fixtures/reject/src/lib.rs:424:45 +a closure that copies a mutable variable at fixtures/reject/src/lib.rs:432:3 overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:282:2 an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:302:1 overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:336:2 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index 3bf05846a..d290992d8 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -399,6 +399,14 @@ pub fn rest_moved(pair: [Noisy; 2]) -> u8 { first.0 + rest[0].0 } +/// `rest` borrows the tail of `items` mutably, but a JS array has no views. +pub fn rest_mut(items: &mut [u32; 3]) -> u32 { + if let [_, rest @ ..] = items { + rest[0] = 9; + } + items[1] +} + /// 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; @@ -409,3 +417,21 @@ pub fn repeated(n: Noisy) -> usize { let empty = [n; 0]; empty.len() } + +/// A reference copied into a closure holds its referent's object: replacing through it +/// would change the copy, not the referent. +pub fn replace_captured(a: &mut [u8; 1]) -> [u8; 1] { + let mut g = move || std::mem::replace(&mut *a, [9]); + g() +} + +/// Each copy of a closure has its own captures, which copies of an arrow function share. +pub fn copied_closure(n: u8) -> u8 { + let mut a = [n]; + let mut g = move || { + a[0] = a[0].wrapping_add(1); + a[0] + }; + let mut h = g; + g().wrapping_add(h()) +} diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index 21325e271..a660c8cc4 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -51,8 +51,11 @@ roots = [ "looped", "fallback", "rest_moved", + "rest_mut", "replace_pinned_ref", "repeated", + "replace_captured", + "copied_closure", "missing", ] printable = ["Meters"] diff --git a/ts/fixtures/task/examples/unwind.rs b/ts/fixtures/task/examples/unwind.rs new file mode 100644 index 000000000..652f8a491 --- /dev/null +++ b/ts/fixtures/task/examples/unwind.rs @@ -0,0 +1,25 @@ +//! The harness on the host, where a panic unwinds: a step that panics is logged, and the +//! script goes on, so the log shows what unwinding dropped. `tests/unwind.ts` runs the +//! translation the same way and compares the logs. +//! +//! usage: unwind < scripts +//! +//! Each input line is a script in hex; each output line is its log in hex. + +use std::io::{BufRead, Write}; + +fn main() { + // The harness logs each panic; the default hook would print it too. + std::panic::set_hook(Box::new(|_| {})); + let mut out = std::io::stdout().lock(); + for line in std::io::stdin().lock().lines() { + let line = line.expect("reading stdin"); + let input: Vec = (0..line.len()) + .step_by(2) + .map(|i| u8::from_str_radix(&line[i..i + 2], 16).expect("hex")) + .collect(); + let log = task_fixture::harness::run(&input); + let hex: String = log.iter().map(|b| format!("{b:02x}")).collect(); + writeln!(out, "{hex}").expect("writing stdout"); + } +} diff --git a/ts/fixtures/task/expected/model.ts b/ts/fixtures/task/expected/model.ts index 3871bf945..1413f2271 100644 --- a/ts/fixtures/task/expected/model.ts +++ b/ts/fixtures/task/expected/model.ts @@ -11,7 +11,7 @@ import type * as Tally from "./model.Tally.ts"; /** * Every value until the channel closes, summed, holding a guard of its own across - * each await beside the one it was given. + * each await beside the one it was given. It panics on 99. */ export function sum(rx: Consumer.Consumer, guard: Guard.Guard): rt.Future { return rt.future(sumBody, sumDrop, rx, guard); @@ -45,6 +45,9 @@ export function* sumBody(_taskContext: rt.Context, rx: Consumer.Consumer, guard: } if (option !== undefined) { const value = option; + if (value === 99) { + rt.panic(["g", (f: rt.Formatter) => rt.u32.display(guard2.id, f), " poisoned"]); + } Log.note(inner.log, 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 { diff --git a/ts/fixtures/task/src/harness.rs b/ts/fixtures/task/src/harness.rs index ca40a6255..c50b08916 100644 --- a/ts/fixtures/task/src/harness.rs +++ b/ts/fixtures/task/src/harness.rs @@ -4,6 +4,9 @@ //! 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. +//! +//! Where a panic unwinds, as on the host, a step that panics is logged and the script +//! goes on, so what unwinding dropped shows in the log: `examples/unwind.rs`. use crate::exec::Executor; use crate::log::Log; @@ -16,70 +19,88 @@ pub fn run(input: &[u8]) -> Vec { 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()); + #[cfg(panic = "unwind")] + { + let run = std::panic::AssertUnwindSafe(|| run_step(&log, &mut exec, &mut channels, op, arg)); + if let Err(panic) = std::panic::catch_unwind(run) { + let message = match (panic.downcast_ref::(), panic.downcast_ref::<&str>()) { + (Some(s), _) => s.clone(), + (_, Some(s)) => s.to_string(), + _ => "Box".to_string(), + }; + log.note(format!("panic {message}")); } - 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)); - } + } + #[cfg(not(panic = "unwind"))] + run_step(&log, &mut exec, &mut channels, op, arg); + } + 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() +} + +/// Run one step of a script. +fn run_step(log: &Log, exec: &mut Executor, channels: &mut Vec, op: u8, arg: u8) { + 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)); - } + } + 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")); - } + } + 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(); - } + } + 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(); - } + } + 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); - } + } + 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); - } + } + 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)); - } + } + _ => { + 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/model.rs b/ts/fixtures/task/src/model.rs index 4a39ed89c..edbd64016 100644 --- a/ts/fixtures/task/src/model.rs +++ b/ts/fixtures/task/src/model.rs @@ -87,11 +87,13 @@ impl Drop for Guard { } /// Every value until the channel closes, summed, holding a guard of its own across -/// each await beside the one it was given. +/// each await beside the one it was given. It panics on 99. 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 { + // A poisoned value panics with every guard live, which unwinding drops. + assert!(value != 99, "g{} poisoned", guard.id); inner.log.note(format!("g{} got {value}", guard.id)); total += value; } diff --git a/ts/src/census.rs b/ts/src/census.rs index 384b428a1..7232f8ee5 100644 --- a/ts/src/census.rs +++ b/ts/src/census.rs @@ -149,11 +149,15 @@ const VERBATIM: &[&str] = &[ "Option", "Pointer", "Ready", + "Reborrow", "Result", "Some", "UpperExp", + "async fn", + "become", "bool", "break", + "copy_nonoverlapping", "fold", "for", "format_args!", @@ -355,11 +359,22 @@ mod tests { /// stays in the construct. #[test] fn verbatim_covers_messages() { - let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) - .join("src") - .join("lower"); - for entry in std::fs::read_dir(dir).unwrap() { - let path = entry.unwrap().path(); + let mut dirs = vec![ + std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("src") + .join("lower"), + ]; + let mut files = Vec::new(); + while let Some(dir) = dirs.pop() { + for entry in std::fs::read_dir(dir).unwrap() { + let path = entry.unwrap().path(); + match path.is_dir() { + true => dirs.push(path), + false => files.push(path), + } + } + } + for path in files { let text = std::fs::read_to_string(&path).unwrap(); let code = text.lines().filter(|line| !line.trim_start().starts_with("//")); // Every other piece between quotes is a string literal. diff --git a/ts/src/cli.rs b/ts/src/cli.rs index 33e104c24..08df01348 100644 --- a/ts/src/cli.rs +++ b/ts/src/cli.rs @@ -9,14 +9,17 @@ use crate::config::{self, Config}; /// holds exactly, and its `cfg`s are the browser's. const TARGET: &str = "wasm32-unknown-unknown"; -const USAGE: &str = "usage: rust2-ts --config (--out | --census ) [--manifest-path ] [-- ] +const USAGE: &str = "usage: rust2-ts --config (--out | --census ) [--manifest-path ] [--lower thir|mir] [-- ] Translates the library of the package that ts.toml names to TypeScript in . The package's Cargo.toml is beside ts.toml unless --manifest-path says where. Cargo args, such as --features, are passed to `cargo check`. --census writes a Markdown report of every construct translation refuses in the library, -reached from each of its items as if each were a root, instead of translating it."; +reached from each of its items as if each were a root, instead of translating it. + +--lower picks what function bodies lower from: THIR, the default, or MIR, the spike's +prototype."; pub fn run(args: &[String]) -> ExitCode { match try_run(args) { @@ -33,6 +36,7 @@ fn try_run(args: &[String]) -> Result { let mut out = None; let mut census = None; let mut manifest = None; + let mut lower = None; let mut cargo_args: &[String] = &[]; let mut rest = args; while let [arg, tail @ ..] = rest { @@ -51,6 +55,16 @@ fn try_run(args: &[String]) -> Result { }; *slot = Some(absolute(Path::new(value))?); } + "--lower" => { + let [value, tail @ ..] = rest else { + return Err(format!("{arg} needs a value\n\n{USAGE}")); + }; + rest = tail; + if !matches!(value.as_str(), "thir" | "mir") { + return Err(format!("--lower takes thir or mir, not {value}\n\n{USAGE}")); + } + lower = Some(value.clone()); + } "--" => { cargo_args = rest; break; @@ -102,7 +116,11 @@ fn try_run(args: &[String]) -> Result { // Cargo can't tell the TypeScript is stale, so the package always recompiles. run_cargo(cargo("clean").arg("--quiet"))?; - let status = cargo("check") + let mut check = cargo("check"); + if let Some(lower) = &lower { + check.env(config::env::LOWER, lower); + } + let status = check .arg("--lib") .args(cargo_args) .env("RUSTC_WRAPPER", &exe) diff --git a/ts/src/config.rs b/ts/src/config.rs index 1b08f86f9..8b2ef1f1c 100644 --- a/ts/src/config.rs +++ b/ts/src/config.rs @@ -43,4 +43,6 @@ pub mod env { pub const OUT: &str = "RUST2_TS_OUT"; /// Where to write a census instead of translating, absolute. pub const CENSUS: &str = "RUST2_TS_CENSUS"; + /// What bodies lower from: `thir`, the default, or `mir`. + pub const LOWER: &str = "RUST2_TS_LOWER"; } diff --git a/ts/src/driver.rs b/ts/src/driver.rs index 6694e2406..5a41cb54b 100644 --- a/ts/src/driver.rs +++ b/ts/src/driver.rs @@ -25,6 +25,16 @@ pub fn run(mut args: Vec) -> ExitCode { Ok(job) => job, Err(err) => early_dcx.early_fatal(format!("rust2-ts: {err}")), }; + // MIR without the checks of undefined behavior that debug builds add around raw + // pointers, such as a box's: JS has no pointers to check. And with the cleanups a + // panic unwinds through, which wasm32's default of aborting leaves out: a JS caller + // can catch a panic and go on. + if job.as_ref().is_some_and(|job| job.mir) { + args.push("-Zub-checks=no".into()); + if std::env::var_os("RUST2_TS_ABORT").is_none() { + args.push("-Cpanic=unwind".into()); + } + } rustc_driver::catch_with_exit_code(|| match job { Some(mut job) => rustc_driver::compiler_entrypoint(&args, &mut job), None => rustc_driver::compiler_entrypoint(&args, &mut Plain), @@ -40,6 +50,8 @@ impl Callbacks for Plain {} struct Translate { config: Config, output: Output, + /// Whether bodies lower from MIR rather than THIR. + mir: bool, } /// What the job writes. @@ -76,7 +88,12 @@ impl Translate { )); } }; - Ok(Some(Self { config, output })) + let mir = match std::env::var(config::env::LOWER).as_deref() { + Ok("mir") => true, + Ok("thir") | Err(_) => false, + Ok(other) => return Err(format!("{} must be thir or mir, not {other}", config::env::LOWER)), + }; + Ok(Some(Self { config, output, mir })) } } @@ -90,11 +107,11 @@ impl Callbacks for Translate { } // Only a program rustc accepts is translated. let written = match &self.output { - Output::Dir(out) => match lower::translate(tcx, &self.config, &bodies) { + Output::Dir(out) => match lower::translate(tcx, &self.config, &bodies, self.mir) { Some(program) if tcx.dcx().has_errors().is_none() => program.write(out), _ => Ok(()), }, - Output::Census(path) => match lower::census(tcx, &self.config, &bodies) { + Output::Census(path) => match lower::census(tcx, &self.config, &bodies, self.mir) { Some(census) => program::write_file(path, &census.render(&self.config.package)), None => Ok(()), }, diff --git a/ts/src/lower/body.rs b/ts/src/lower/body.rs index 34124937a..d51a38e63 100644 --- a/ts/src/lower/body.rs +++ b/ts/src/lower/body.rs @@ -166,6 +166,29 @@ enum CondPart<'a, 'tcx> { } impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { + /// A body with no THIR of its own: what a MIR lowering calls the library mappings + /// through, passing their arguments already lowered. It names the locals. + pub fn detached(t: &'b mut Translator<'a, 'tcx>, args: GenericArgsRef<'tcx>, ret: Ty<'tcx>) -> Self { + let bodies: &'a super::Bodies<'tcx> = t.bodies; + let thir = &bodies.empty; + Body { + t, + thir, + args, + locals: FxHashMap::default(), + names: FxHashSet::default(), + read: FxHashSet::default(), + breakables: Vec::new(), + ret, + visited: FxHashSet::default(), + scopes: Vec::new(), + moved: FxHashSet::default(), + borrowed: FxHashSet::default(), + yields: 0, + done: false, + } + } + pub fn subst>>(&self, value: T) -> T { self.t.subst(self.args, value) } diff --git a/ts/src/lower/drop.rs b/ts/src/lower/drop.rs index b0bcaf102..3245196dc 100644 --- a/ts/src/lower/drop.rs +++ b/ts/src/lower/drop.rs @@ -145,6 +145,13 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { ty::Coroutine(..) | ty::Param(_) => true, _ => false, }; + // A MIR lowering drops an enum or tuple by the glue rustc builds for it. + if !supported + && self.mir + && let Some(sym) = super::mir::glue::shim(self, ty, span) + { + return Some(super::mir::glue::call(sym, value)); + } if !supported { self.err(span, format!("dropping `{ty}`, which is not supported yet")); return None; diff --git a/ts/src/lower/fmt.rs b/ts/src/lower/fmt.rs index 9226e6883..1cb1e08b3 100644 --- a/ts/src/lower/fmt.rs +++ b/ts/src/lower/fmt.rs @@ -67,7 +67,7 @@ impl Trait { } const UNEXPECTED: &str = "an unexpected `format_args!` lowering"; -const FROM_ARGUMENT: &str = "a width or precision from an argument, as in `{:1$}`,"; +pub const FROM_ARGUMENT: &str = "a width or precision from an argument, as in `{:1$}`,"; /// A formatter's type, for the parameters of the functions written here. fn formatter() -> Type { @@ -421,7 +421,7 @@ pub fn std_impl<'tcx>( } /// One part of a `format_args!` template. -enum Piece { +pub enum Piece { Lit(String), /// A placeholder writing the `index`th argument. Arg { @@ -431,7 +431,7 @@ enum Piece { } /// A placeholder's options, from `FormattingOptions`' flags. -struct Spec { +pub struct Spec { fill: char, align: Option, alternate: bool, @@ -441,7 +441,7 @@ struct Spec { impl Spec { /// The runtime's `Spec` object, or `None` for the defaults. - fn object(&self) -> Option { + pub fn object(&self) -> Option { let mut props = Vec::new(); if self.fill != ' ' { props.push(("fill", Expr::Str(self.fill.to_string()))); @@ -476,7 +476,7 @@ const DEFAULT_FLAGS: u32 = ' ' as u32 | 3 << 29; /// Decode a template as `core::fmt::write` reads it: literal pieces, and placeholders /// with their argument and options. An option outside the subset is an error naming it. -fn decode(template: &[u8]) -> Result, &'static str> { +pub fn decode(template: &[u8]) -> Result, &'static str> { const TRUNCATED: &str = "a truncated template"; let byte = |i: usize| template.get(i).copied().ok_or(TRUNCATED); let u16_at = |i: usize| Ok::<_, &str>(u16::from_le_bytes([byte(i)?, byte(i + 1)?])); diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 975d7511a..bf0fa0058 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -65,6 +65,19 @@ pub enum Std { RangeInclusive, /// `rt.Peekable`, an iterator with `peek`. Peekable, + /// What `?` branches on, as MIR spells it: the value it was applied to. + ControlFlow, + /// `..end`, `start..`, and `..`, as MIR makes them to slice by: `{ end }`, `{ start }`, + /// and `undefined`. + RangeTo, + RangeFrom, + RangeFull, + /// A `format_args!` argument: a function writing its value, `rt.Fmt`. + Argument, + /// Its pointee: what an async body passes its context in. + NonNull, + /// No value: `never`. + Infallible, } impl Std { @@ -106,6 +119,9 @@ impl Std { (rustc_attr_ir::LangItem::Pin, Self::Pin), (rustc_attr_ir::LangItem::Range, Self::Range), (rustc_attr_ir::LangItem::RangeInclusiveStruct, Self::RangeInclusive), + (rustc_attr_ir::LangItem::RangeTo, Self::RangeTo), + (rustc_attr_ir::LangItem::RangeFrom, Self::RangeFrom), + (rustc_attr_ir::LangItem::RangeFull, Self::RangeFull), ] { if tcx.is_lang_item(did, item) { return Some(std); @@ -131,6 +147,10 @@ impl Std { ["fmt", "builders", "DebugList"] => Some(Self::DebugList), ["panicking", "AssertKind"] => Some(Self::AssertKind), ["iter", "adapters", "peekable", "Peekable"] => Some(Self::Peekable), + ["ops", "control_flow", "ControlFlow"] => Some(Self::ControlFlow), + ["fmt", "rt", "Argument"] => Some(Self::Argument), + ["ptr", "non_null", "NonNull"] => Some(Self::NonNull), + ["convert", "Infallible"] => Some(Self::Infallible), _ => None, } } @@ -839,6 +859,11 @@ fn from_be_bytes<'tcx>(b: &mut Body<'_, '_, 'tcx>, int: Int, bytes: ExprId, out: } }; let values = b.operands(fields, out); + be_word(int, values) +} + +/// A number from its big-endian bytes, each a value: shifted and or-ed. +pub fn be_word(int: Int, values: Vec) -> Expr { let last = values.len() - 1; let mut word: Option = None; let mut top = false; @@ -870,7 +895,7 @@ fn from_be_bytes<'tcx>(b: &mut Body<'_, '_, 'tcx>, int: Int, bytes: ExprId, out: } /// The `n`th type among a call's generic arguments, skipping lifetimes. -fn targ<'tcx>(gargs: GenericArgsRef<'tcx>, n: usize) -> Ty<'tcx> { +pub fn targ<'tcx>(gargs: GenericArgsRef<'tcx>, n: usize) -> Ty<'tcx> { gargs.types().nth(n).expect("a type argument") } @@ -1002,7 +1027,7 @@ fn borrowed_place<'tcx>(b: &mut Body<'_, '_, 'tcx>, id: ExprId, out: &mut Vec(b: &mut Body<'_, '_, 'tcx>, place: Expr, value: Expr, base: &str, out: &mut Vec) -> Expr { +pub fn swap_out<'tcx>(b: &mut Body<'_, '_, 'tcx>, place: Expr, value: Expr, base: &str, out: &mut Vec) -> Expr { let name = b.fresh(base); out.push(StmtKind::decl(false, name.clone(), None, Some(place.clone())).at(None)); out.push(StmtKind::Expr(place.assign(value)).at(None)); @@ -1010,7 +1035,7 @@ fn swap_out<'tcx>(b: &mut Body<'_, '_, 'tcx>, place: Expr, value: Expr, base: &s } /// `Default::default()` for a standard type. -fn default_value<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> Option { +pub fn default_value<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> Option { let tcx = b.t.tcx; Some(match ty.kind() { ty::Bool => Expr::lit("false"), @@ -1037,7 +1062,7 @@ fn default_value<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> Option } /// The text of a `fmt::Arguments` with only literal pieces, which writes it as is. -fn literal(parts: &Expr) -> Option { +pub fn literal(parts: &Expr) -> Option { let Expr::Array(parts) = parts else { return None; }; diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs new file mode 100644 index 000000000..53524ccd1 --- /dev/null +++ b/ts/src/lower/mir/call.rs @@ -0,0 +1,641 @@ +//! Calls: to the crate's own functions, to closures, and to std through the library +//! mappings. Some std calls lower here instead, since MIR passes what THIR wrote as an +//! expression through a temporary: a place to replace (`mem::take`), a range to slice +//! by, an array of bytes to read, `format_args!`'s parts, and `?`'s `ControlFlow`. + +use rustc_abi::VariantIdx; +use rustc_middle::mir::{Operand, Place}; +use rustc_middle::ty::{self, GenericArgsRef, Instance, Ty, TyCtxt}; +use rustc_span::{Span, Spanned}; + +use super::{Fx, Slot}; +use crate::lower::fmt::{self, Piece}; +use crate::lower::library::{self, Args, Std}; +use crate::lower::num::Int; +use crate::ts::{Expr, Stmt, StmtKind}; + +impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { + pub(super) fn call( + &mut self, + func: &Operand<'tcx>, + args: &[Spanned>], + dest: Place<'tcx>, + span: Span, + out: &mut Vec, + ) { + let tcx = self.tcx; + let fn_ty = func.ty(&self.mir.local_decls, tcx); + let mut pre = Vec::new(); + let (value, effect) = match *fn_ty.kind() { + // `next` into a local that holds the protocol's item or `rt.DONE`, unconverted. + ty::FnDef(def_id, gargs) + if crate::lower::iter::is_next(tcx, def_id) + && dest.as_local().is_some_and(|l| self.next_results.contains(&l)) => + { + let iter_ty = gargs.skip_binder().type_at(0); + let recv = self.operand_frozen(&args[0].node, span, &mut pre); + if let Some(name) = self.var(dest.local) { + let name = name.to_string(); + self.next_names.insert(name); + } + (crate::lower::iter::next(self.b, iter_ty, recv), true) + } + ty::FnDef(def_id, gargs) => match self.intercept(def_id, gargs.skip_binder(), args, dest, span, &mut pre) { + Some(Some(done)) => done, + // Done: the destination is set. + Some(None) => { + self.emit(pre, None, false, out); + return; + } + None => (self.call_def(def_id, gargs.skip_binder(), args, span, &mut pre), true), + }, + ty::FnPtr(..) => { + let f = self.operand(func, span, &mut pre); + let values = self.operands(args, span, &mut pre); + (f.call(values), true) + } + _ => (self.err(span, format!("a call through `{fn_ty}`")), true), + }; + let value = match self.b.t.loc(span) { + Some(loc) if effect => Expr::Located(loc, Box::new(value)), + _ => value, + }; + if !pre.is_empty() { + self.emit(pre, None, false, out); + } + // What an `Iterator::next` returns, by the protocol, unless a `next` made it already. + let value = match dest.as_local() { + Some(rustc_middle::mir::RETURN_PLACE) + if self.next && !matches!(*fn_ty.kind(), ty::FnDef(d, _) if crate::lower::iter::is_next(tcx, d)) => + { + super::done_of(value) + } + _ => value, + }; + // A result into a local changed in place is its own copy: it may share the callee's. + let value = match dest.as_local() { + Some(local) if self.mutated[local] => { + let ty = self.mir.local_decls[local].ty; + self.b.copied(value, ty) + } + _ => value, + }; + match dest.as_local() { + Some(local) => { + let memory = effect || args.iter().any(|a| super::rvalue::operand_memory(&a.node, self)); + self.set_local(local, value, effect, memory, None, out) + } + None => { + let stmts = self.store(dest, value, span); + self.emit(stmts, None, false, out); + } + } + } + + /// Arguments, uncopied: a callee that changes a parameter in place copies it first. + fn operands(&mut self, args: &[Spanned>], span: Span, out: &mut Vec) -> Vec { + let saved = std::mem::replace(&mut self.into_frozen, true); + let values = args + .iter() + .map(|arg| self.operand_frozen(&arg.node, span, out)) + .collect(); + self.into_frozen = saved; + values + } + + /// An argument, which the callee copies if it changes it in place. + fn operand_frozen(&mut self, op: &Operand<'tcx>, span: Span, out: &mut Vec) -> Expr { + match op { + Operand::Copy(place) | Operand::Move(place) => self.place_expr(*place, span, out), + _ => self.operand(op, span, out), + } + } + + /// A call of `def_id` with `gargs`: the crate's own function, a closure, or std's. + fn call_def( + &mut self, + def_id: rustc_hir::def_id::DefId, + gargs: GenericArgsRef<'tcx>, + args: &[Spanned>], + span: Span, + out: &mut Vec, + ) -> Expr { + let tcx = self.tcx; + // Calling a closure or function value: `FnOnce::call_once(f, (args,))`. + if let Some(trait_id) = tcx.trait_of_assoc(def_id) + && tcx.is_fn_trait(trait_id) + && let [callee, tupled] = args + { + let callee_ty = gargs.type_at(0); + let values = match self.take_parts(&tupled.node) { + Some(parts) => parts, + None => { + let tuple = self.operand_frozen(&tupled.node, span, out); + let tuple = self.path(tuple, "args", out); + let ty::Tuple(tys) = gargs.type_at(1).kind() else { + return self.err(span, "a closure call whose arguments are not a tuple"); + }; + (0..tys.len()) + .map(|i| tuple.clone().index(Expr::lit(i.to_string()))) + .collect() + } + }; + if let ty::FnDef(inner, inner_args) = *callee_ty.peel_refs().kind() { + return self + .b + .call_def(inner, inner_args.skip_binder(), Args::Values(values), span, out); + } + let f = self.operand(&callee.node, span, out); + return f.call(values); + } + // A function of the crate gets what JS passes, so a `&mut` to a number can't reach it. + let local = match Instance::try_resolve(tcx, self.b.t.typing_env_pub(), def_id, gargs) { + Ok(Some(instance)) => matches!(instance.def, ty::InstanceKind::Item(did) if did.is_local()), + _ => false, + }; + if local { + for arg in args { + self.check_escape(&arg.node, span); + } + } + let values = self.operands(args, span, out); + self.b.call_def(def_id, gargs, Args::Values(values), span, out) + } + + /// Refuse a `&mut` to a value JS copies, passed on whole to code that could write + /// through it: it would change a copy. + fn check_escape(&mut self, op: &Operand<'tcx>, span: Span) { + let (Operand::Copy(place) | Operand::Move(place)) = op else { + return; + }; + if !place.projection.is_empty() { + return; + } + let ty = self.mir.local_decls[place.local].ty; + if let ty::Ref(_, inner, rustc_ast::Mutability::Mut) = ty.kind() + && matches!(self.slots[place.local], Slot::Alias(_)) + && self.b.t.object_kind(*inner).is_none() + { + self.err( + span, + format!("a mutable reference to `{inner}` passed on, which is no object it can change in place"), + ); + } + } + + /// A std call MIR needs lowered here: `Some(Some((value, effect)))`, or `Some(None)` + /// once it set the destination itself, or `None` for the library mappings. + fn intercept( + &mut self, + def_id: rustc_hir::def_id::DefId, + gargs: GenericArgsRef<'tcx>, + args: &[Spanned>], + dest: Place<'tcx>, + span: Span, + out: &mut Vec, + ) -> Option> { + let tcx = self.tcx; + if library::std_path(tcx, def_id).is_empty() { + return None; + } + let (owner, name) = library::key(tcx, def_id)?; + let targ = |n: usize| gargs.types().nth(n); + let done = |e: Expr, effect: bool| Some(Some((e, effect))); + match (owner.as_str(), name.as_str()) { + // Replacing what a `&mut` borrows. + ("mem", "take") => { + let place = self.borrowed_place(&args[0].node, span)?; + let default = library::default_value(self.b, targ(0)?)?; + done(self.swap_out(place, default, "taken", out), true) + } + ("mem", "replace") => { + let place = self.borrowed_place(&args[0].node, span)?; + let value = self.operand(&args[1].node, span, out); + done(self.swap_out(place, value, "old", out), true) + } + ("mem", "swap") => { + let a = self.borrowed_place(&args[0].node, span)?; + let c = self.borrowed_place(&args[1].node, span)?; + let old = self.swap_out(a, c.clone(), "swapped", out); + out.push(StmtKind::Expr(c.assign(old)).at(None)); + done(Expr::undefined(), true) + } + // A range of numbers steps in place, with nothing between it and the loop. + ("Iterator", "next") + if let ty::Adt(adt, range_args) = targ(0)?.kind() + && Std::of(tcx, adt.did()) == Some(Std::Range) + && Int::of(range_args.type_at(0)).is_some_and(|i| !i.big()) => + { + let range = self.alias_place(&args[0].node)?; + let start = range.clone().member("start"); + let next = Expr::Cond( + Box::new(start.clone().binary("<", range.member("end"))), + Box::new(Expr::Postfix(Box::new(start), "++")), + Box::new(Expr::undefined()), + ); + done(next, true) + } + ("Option", "take") => { + let place = self.borrowed_place(&args[0].node, span)?; + done(self.swap_out(place, Expr::undefined(), "taken", out), true) + } + // Indexing, whose result a `&mut` reads in place. + ("Index" | "IndexMut", "index" | "index_mut") => self.index(&name, gargs, args, dest, span, out), + ("slice", "copy_within") => { + let s = self.operand(&args[0].node, span, out); + let s = self.path(s, "slice", out); + let (start, end) = self.range_bounds(&args[1].node, targ(1)?, span, out)?; + let end = end.unwrap_or_else(|| s.clone().member("length")); + let dest = self.operand(&args[2].node, span, out); + done( + Expr::Rt("slice").member("copyWithin").call(vec![s, start, end, dest]), + true, + ) + } + // An integer from an array of bytes made in place: its bytes shifted, no array made. + (int, "from_be_bytes") if let Some(int) = int_named(int) => { + let parts = match int.big() { + true => None, + false => self.take_parts(&args[0].node), + }; + let value = match parts { + Some(parts) => library::be_word(int, parts), + None => { + let bytes = self.operand_frozen(&args[0].node, span, out); + int.method("fromBeBytes", vec![bytes]) + } + }; + done(value, false) + } + // `format_args!`, as MIR builds it: each argument a function writing its value, + // and a template of literal pieces and placeholders. + ("Argument", name) if name.starts_with("new_") || name == "from_usize" => { + let tr = match name { + "new_display" => fmt::Trait::Display, + "new_debug" => fmt::Trait::Debug, + "new_lower_hex" => fmt::Trait::LowerHex, + "new_upper_hex" => fmt::Trait::UpperHex, + "from_usize" => { + self.err(span, format!("{} is not supported", fmt::FROM_ARGUMENT)); + return done(Expr::undefined(), false); + } + other => { + self.err(span, format!("formatting by `{other}` is not supported")); + return done(Expr::undefined(), false); + } + }; + let ty = targ(0)?; + let value = self.operand(&args[0].node, span, out); + let value = self.path(value, "arg", out); + done(self.b.fmt_value(tr, ty, value, span, out), false) + } + ("Arguments", "from_str" | "from_str_nonconst") => { + let s = self.operand(&args[0].node, span, out); + done(Expr::Array(vec![s]), false) + } + ("Arguments", "new") => { + let Some(template) = self.const_bytes(&args[0].node) else { + self.err( + span, + format!("an unexpected `format_args!` lowering: {:?}", args[0].node), + ); + return done(Expr::undefined(), false); + }; + let values = self.operand(&args[1].node, span, out); + // The arguments array made once in place: each piece its function. + let made: Option> = match &values { + Expr::Ident(name) => self.local_named(name).and_then(|l| self.var_parts.get(&l).cloned()), + _ => None, + }; + let values = self.path(values, "args", out); + let pieces = match fmt::decode(&template) { + Ok(pieces) => pieces, + Err(msg) => { + self.err(span, format!("{msg} is not supported")); + return done(Expr::undefined(), false); + } + }; + let mut parts = Vec::new(); + for piece in pieces { + match piece { + Piece::Lit(s) => parts.push(Expr::Str(s)), + Piece::Arg { index, spec } => { + let value = match made.as_ref().and_then(|p| p.get(index)) { + Some(part) => part.clone(), + None => values.clone().index(Expr::lit(index.to_string())), + }; + parts.push(match spec.object() { + Some(spec) => Expr::Rt("fmt").member("spec").call(vec![spec, value]), + None => value, + }); + } + } + } + done(Expr::Array(parts), false) + } + // `?`: the value it was applied to is the `ControlFlow` MIR branches on. + // The crate's own iterator is its own `IntoIterator`; std's adaptors convert it + // where they take it. + ("IntoIterator", "into_iter") + if matches!(targ(0)?.kind(), ty::Adt(adt, _) if adt.did().is_local()) + && self.b.t.iter_item(targ(0)?).is_some() => + { + let value = self.operand(&args[0].node, span, out); + done(value, false) + } + ("Try", "branch") => { + let value = self.operand(&args[0].node, span, out); + // A `next`'s item or `rt.DONE` branches as itself. + if let Some(local) = dest.as_local() + && self.next_results.contains(&local) + && let Some(name) = self.var(local) + { + let name = name.to_string(); + self.next_names.insert(name); + } + done(value, false) + } + ("FromResidual", "from_residual") => { + let to = targ(0)?; + let residual = targ(1)?; + let value = self.operand(&args[0].node, span, out); + match (self.b.t.std_of(to), to.kind(), residual.kind()) { + (Some(Std::Option), ..) => done(Expr::undefined(), false), + (Some(Std::Result), ty::Adt(_, to_args), ty::Adt(_, from_args)) => { + let (from, to) = (from_args.type_at(1), to_args.type_at(1)); + if from == to { + return done(value, false); + } + let value = self.path(value, "residual", out); + // The residual is an `Err`. + let err = crate::ts::Type::Object(vec![ + ("ok".into(), crate::ts::Type::Builtin("false")), + ("error".into(), self.b.t.ty(from, span)), + ]); + let error = Expr::As(Box::new(value), err).member("error"); + let converted = self.b.convert(error, from, to, span, out); + done(Expr::rt("Err", vec![converted]), true) + } + _ => { + self.err(span, format!("`?` from `{residual}` to `{to}`")); + done(Expr::undefined(), false) + } + } + } + ("DebugList", "entries") => { + let list = self.operand(&args[0].node, span, out); + let iter_ty = targ(1)?; + let items = self.operand(&args[1].node, span, out); + let items = self.items(items, iter_ty, span)?; + let debug = self.b.fmt_fn(fmt::Trait::Debug, targ(0)?, span); + done(list.member("entries").call(vec![items, debug]), true) + } + _ => None, + } + } + + /// The place a `&mut` operand borrows, as an assignable path: an alias of a borrowed + /// place, not a copy of a reference, which holds the object it points at. + fn borrowed_place(&mut self, op: &Operand<'tcx>, span: Span) -> Option { + let (Operand::Copy(place) | Operand::Move(place)) = op else { + return None; + }; + if !place.projection.is_empty() { + return None; + } + match &self.slots[place.local] { + Slot::Alias(Some(target)) + if (target.is_path() || matches!(target, Expr::Index(..))) && self.alias_of_place(place.local) => + { + Some(target.clone()) + } + _ => { + let pointee = self.mir.local_decls[place.local].ty.peel_refs(); + Some(self.err(span, format!("replacing the value behind a reference to `{pointee}`"))) + } + } + } + + /// The place a reference operand reads in place, if it is one. + fn alias_place(&self, op: &Operand<'tcx>) -> Option { + let (Operand::Copy(place) | Operand::Move(place)) = op else { + return None; + }; + match (&self.slots[place.as_local()?], place.projection.is_empty()) { + (Slot::Alias(Some(target)), true) if target.is_path() => Some(target.clone()), + _ => None, + } + } + + /// The value at `place`, after setting `place` to `value`. + fn swap_out(&mut self, place: Expr, value: Expr, base: &str, out: &mut Vec) -> Expr { + let name = self.b.fresh(base); + out.push(StmtKind::decl(false, name.clone(), None, Some(place.clone())).at(None)); + out.push(StmtKind::Expr(place.assign(value)).at(None)); + Expr::ident(name) + } + + /// `container[index]`, or a slice of it by a range, which a reference reads in place. + fn index( + &mut self, + name: &str, + gargs: GenericArgsRef<'tcx>, + args: &[Spanned>], + dest: Place<'tcx>, + span: Span, + out: &mut Vec, + ) -> Option> { + let container = gargs.type_at(0); + let index_ty = gargs.type_at(1); + let elem_ty = elem(self.tcx, container)?; + let base = self.operand(&args[0].node, span, out); + let base = match container.kind() { + ty::Adt(..) => library::vec_view(base, container), + _ => base, + }; + match Int::of(index_ty) { + Some(_) => { + let base = self.path(base, "slice", out); + let i = self.operand(&args[1].node, span, out); + let i = self.path(i, "index", out); + let checked = Expr::rt("idx", vec![base.clone(), i]); + let checked = self.path(checked, "at", out); + let place = base.index(checked); + // The reference reads and writes the element where it is. + if let Some(local) = dest.as_local() + && self.defs[local] == 1 + && matches!(self.slots[local], Slot::Var(_)) + { + self.slots[local] = Slot::Alias(Some(place)); + return Some(None); + } + Some(Some((place, true))) + } + None => { + if name == "index_mut" && !matches!(elem_ty.kind(), ty::Uint(ty::UintTy::U8)) { + // A JS array has no views, so a mutable subslice would be a copy. + return None; + } + // The range's bounds are taken apart before the slice is read, while they are + // still held back. + let (start, end) = self.range_bounds(&args[1].node, index_ty, span, out)?; + let base = self.path(base, "slice", out); + let mut values = vec![base, start]; + values.extend(end); + Some(Some((Expr::Rt("slice").member("range").call(values), true))) + } + } + } + + /// The start and optional end of a range: `a..b`, `..b`, `a..`, or `..`. + fn range_bounds( + &mut self, + op: &Operand<'tcx>, + ty: Ty<'tcx>, + span: Span, + out: &mut Vec, + ) -> Option<(Expr, Option)> { + let tcx = self.tcx; + let ty::Adt(adt, _) = ty.kind() else { + return None; + }; + let name = tcx.item_name(adt.did()).to_string(); + match name.as_str() { + "RangeFull" => return Some((Expr::lit("0"), None)), + "Range" | "RangeTo" | "RangeFrom" => {} + _ => return None, + } + let fields: Vec = match self.take_parts(op) { + Some(parts) => parts, + None => { + let range = self.operand(op, span, out); + let range = self.path(range, "range", out); + adt.non_enum_variant() + .fields + .iter() + .map(|f| range.clone().member(f.name.as_str())) + .collect() + } + }; + let mut fields = fields.into_iter(); + match name.as_str() { + "Range" => Some((fields.next()?, Some(fields.next()?))), + "RangeTo" => Some((Expr::lit("0"), fields.next())), + _ => Some((fields.next()?, None)), + } + } + + /// What a `DebugList` iterates, as an iterable JS value. + fn items(&mut self, value: Expr, ty: Ty<'tcx>, span: Span) -> Option { + let tcx = self.tcx; + match ty.peel_refs().kind() { + ty::Slice(_) | ty::Array(..) => Some(value), + ty::Adt(adt, _) if Std::of(tcx, adt.did()) == Some(Std::Vec) => Some(library::vec_view(value, ty)), + _ => crate::lower::iter::into_iter(self.b, ty, value, span), + } + } +} + +/// The element type of a slice, array, or `Vec`. +fn elem<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option> { + match ty.peel_refs().kind() { + ty::Slice(e) | ty::Array(e, _) => Some(*e), + ty::Adt(adt, args) if Std::of(tcx, adt.did()) == Some(Std::Vec) => Some(args.type_at(0)), + _ => None, + } +} + +fn int_named(name: &str) -> Option { + let (signed, bits, pointer) = match name { + "u8" => (false, 8, false), + "u16" => (false, 16, false), + "u32" => (false, 32, false), + "u64" => (false, 64, false), + "u128" => (false, 128, false), + "usize" => (false, 32, true), + "i8" => (true, 8, false), + "i16" => (true, 16, false), + "i32" => (true, 32, false), + "i64" => (true, 64, false), + "i128" => (true, 128, false), + "isize" => (true, 32, true), + _ => return None, + }; + Some(Int { bits, signed, pointer }) +} + +/// The residual a `ControlFlow` breaks with: what `?` was applied to. +fn residual<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option { + let ty::Adt(_, args) = ty.kind() else { + return None; + }; + match args.type_at(0).kind() { + ty::Adt(adt, _) => Std::of(tcx, adt.did()), + _ => None, + } +} + +/// The test that `scrut`, a `ControlFlow`, is `Continue` (variant 0) or `Break`. +pub fn control_flow_test<'tcx>( + fx: &mut Fx<'_, '_, '_, 'tcx>, + scrut: &Expr, + ty: Ty<'tcx>, + variant: VariantIdx, + span: Span, +) -> Expr { + let continues = variant.index() == 0; + if matches!(scrut, Expr::Ident(n) if fx.next_names.contains(n)) { + return match continues { + true => scrut.clone().binary("!==", Expr::Rt("DONE")), + false => scrut.clone().binary("===", Expr::Rt("DONE")), + }; + } + match residual(fx.tcx, ty) { + Some(Std::Result) => match continues { + true => scrut.clone().member("ok"), + false => scrut.clone().member("ok").not(), + }, + Some(Std::Option) => match continues { + true => scrut.clone().binary("!==", Expr::undefined()), + false => scrut.clone().binary("===", Expr::undefined()), + }, + _ => fx.err(span, format!("`{ty}` other than from `?`")), + } +} + +/// A field of a `ControlFlow`: `Continue`'s value, or `Break`'s residual, which is the +/// value `?` was applied to. +pub fn control_flow_field<'tcx>( + fx: &mut Fx<'_, '_, '_, 'tcx>, + base: Expr, + ty: Ty<'tcx>, + variant: Option, + span: Span, +) -> Expr { + let continues = variant.is_none_or(|v| v.index() == 0); + match (residual(fx.tcx, ty), continues) { + (Some(Std::Result), true) => base.member("value"), + (Some(Std::Result), false) => base, + (Some(Std::Option), true) => base, + (Some(Std::Option), false) => Expr::undefined(), + _ => fx.err(span, format!("`{ty}` other than from `?`")), + } +} + +/// A `ControlFlow` made in place. +pub fn control_flow<'tcx>( + fx: &mut Fx<'_, '_, '_, 'tcx>, + ty: Ty<'tcx>, + variant: VariantIdx, + mut values: Vec, + span: Span, +) -> Expr { + let value = values.pop().unwrap_or_else(Expr::undefined); + match (residual(fx.tcx, ty), variant.index()) { + (Some(Std::Result), 0) => Expr::rt("Ok", vec![value]), + (Some(Std::Result), _) => value, + (Some(Std::Option), 0) => value, + (Some(Std::Option), _) => Expr::undefined(), + _ => fx.err(span, format!("`{ty}` other than from `?`")), + } +} diff --git a/ts/src/lower/mir/cfg.rs b/ts/src/lower/mir/cfg.rs new file mode 100644 index 000000000..8ccd718b9 --- /dev/null +++ b/ts/src/lower/mir/cfg.rs @@ -0,0 +1,508 @@ +//! Structured control flow from a control-flow graph, as JS needs it. +//! +//! A reducible graph, which every function without `.await` has, becomes nested blocks, +//! loops, and `if`s, left by labeled `break` and `continue`: Ramsey's "Beyond Relooper" +//! (ICFP 2022), which places each node under its immediate dominator. An irreducible +//! graph, such as a coroutine's resume function, which enters loops in the middle, +//! becomes a loop over a `switch` on the next node, with each chain of nodes that have +//! one predecessor in one case. + +use crate::ts::{Expr, Stmt, StmtKind}; + +/// How a node leaves. +pub enum Exit { + /// To one successor. + Goto(usize), + /// To the first target whose condition holds, else to the last. + Branch(Vec<(Expr, usize)>, usize), + /// Nowhere: the node returned, or can't finish. + End, +} + +/// What emits each node. +pub trait Lower { + /// Emit `node`'s statements into `out`, and say how it leaves. A branch is decided + /// from what `out` already holds. + fn node(&mut self, node: usize, out: &mut Vec) -> Exit; + /// Control is about to leave the statements emitted so far for code emitted + /// elsewhere: emit whatever the node lowering held back. + fn flush(&mut self, out: &mut Vec); +} + +/// A control-flow graph, entered at node 0. +pub struct Graph { + pub succs: Vec>, +} + +/// The graph's nodes in order, its dominators, and its shape. +struct Shape { + /// Each node's place in reverse postorder, or `usize::MAX` if unreachable. + rpo: Vec, + /// The reachable nodes in reverse postorder. + order: Vec, + idom: Vec, + /// Each node's dominator-tree children, in reverse postorder. + children: Vec>, + /// How many reachable predecessors reach each node by a forward edge. + forward: Vec, + /// Whether a back edge targets each node. + header: Vec, + /// Whether every back edge targets a node that dominates its source. + reducible: bool, +} + +impl Shape { + fn new(g: &Graph) -> Self { + let n = g.succs.len(); + // Postorder, iteratively. + let mut post = Vec::with_capacity(n); + let mut seen = vec![false; n]; + let mut stack = vec![(0usize, 0usize)]; + seen[0] = true; + while let Some((node, i)) = stack.pop() { + match g.succs[node].get(i) { + Some(&next) => { + stack.push((node, i + 1)); + if !seen[next] { + seen[next] = true; + stack.push((next, 0)); + } + } + None => post.push(node), + } + } + let order: Vec = post.into_iter().rev().collect(); + let mut rpo = vec![usize::MAX; n]; + for (i, &node) in order.iter().enumerate() { + rpo[node] = i; + } + let mut preds = vec![Vec::new(); n]; + for &node in &order { + for &next in &g.succs[node] { + preds[next].push(node); + } + } + // Cooper, Harvey, and Kennedy's iterative dominators. + let mut idom = vec![usize::MAX; n]; + idom[0] = 0; + let intersect = |idom: &[usize], mut a: usize, mut b: usize| { + while a != b { + while rpo[a] > rpo[b] { + a = idom[a]; + } + while rpo[b] > rpo[a] { + b = idom[b]; + } + } + a + }; + let mut changed = true; + while changed { + changed = false; + for &node in order.iter().skip(1) { + let mut new = usize::MAX; + for &p in &preds[node] { + if idom[p] == usize::MAX { + continue; + } + new = match new { + usize::MAX => p, + cur => intersect(&idom, p, cur), + }; + } + if new != idom[node] { + idom[node] = new; + changed = true; + } + } + } + let mut children = vec![Vec::new(); n]; + for &node in order.iter().skip(1) { + children[idom[node]].push(node); + } + let dominates = |a: usize, mut b: usize| loop { + if a == b { + return true; + } + if b == 0 { + return false; + } + b = idom[b]; + }; + let mut forward = vec![0; n]; + let mut header = vec![false; n]; + let mut reducible = true; + for &node in &order { + for &next in &g.succs[node] { + if rpo[next] > rpo[node] { + forward[next] += 1; + } else { + header[next] = true; + reducible &= dominates(next, node); + } + } + } + Self { + rpo, + order, + idom, + children, + forward, + header, + reducible, + } + } + + fn merge(&self, node: usize) -> bool { + self.forward[node] > 1 + } +} + +/// The structured statements of `g`, each node emitted by `lower`. +pub fn structure(g: &Graph, lower: &mut impl Lower, labels: &mut u32) -> Vec { + let shape = Shape::new(g); + let mut out = Vec::new(); + match shape.reducible { + true => { + let mut s = Ramsey { + shape: &shape, + lower, + labels: vec![None; g.succs.len()], + next: labels, + }; + s.tree(0, &mut out); + } + false => dispatch(g, &shape, lower, labels, &mut out), + } + // Dropping a jump can leave a branch empty, which the next pass inverts. + simplify(&mut out); + simplify(&mut out); + out +} + +struct Ramsey<'s, L> { + shape: &'s Shape, + lower: &'s mut L, + /// The label of each node's loop or of the block it follows. + labels: Vec>, + next: &'s mut u32, +} + +impl Ramsey<'_, L> { + fn label(&mut self, node: usize, prefix: &str) -> String { + if let Some(label) = &self.labels[node] { + return label.clone(); + } + let label = format!("{prefix}{}", *self.next); + *self.next += 1; + self.labels[node] = Some(label.clone()); + label + } + + /// `x` and what it dominates. + fn tree(&mut self, x: usize, out: &mut Vec) { + // The merge nodes it dominates, the last in reverse postorder outermost. + let mut merges: Vec = self.shape.children[x] + .iter() + .copied() + .filter(|&c| self.shape.merge(c)) + .collect(); + merges.sort_by_key(|&c| std::cmp::Reverse(self.shape.rpo[c])); + match self.shape.header[x] { + true => { + let label = self.label(x, "l"); + let mut body = Vec::new(); + self.within(x, &merges, &mut body); + out.push( + StmtKind::While { + label: Some(label), + cond: Expr::lit("true"), + body, + } + .at(None), + ); + } + false => self.within(x, &merges, out), + } + } + + fn within(&mut self, x: usize, merges: &[usize], out: &mut Vec) { + if let [y, rest @ ..] = merges { + let label = self.label(*y, "b"); + let mut body = Vec::new(); + self.within(x, rest, &mut body); + out.push( + StmtKind::Block { + label: Some(label), + body, + } + .at(None), + ); + self.tree(*y, out); + return; + } + match self.lower.node(x, out) { + Exit::Goto(target) => self.branch(x, target, out), + Exit::Branch(arms, default) => { + let mut els = Vec::new(); + self.branch(x, default, &mut els); + for (cond, target) in arms.into_iter().rev() { + let mut then = Vec::new(); + self.branch(x, target, &mut then); + els = vec![StmtKind::If { cond, then, els }.at(None)]; + } + out.extend(els); + } + Exit::End => self.lower.flush(out), + } + } + + fn branch(&mut self, from: usize, to: usize, out: &mut Vec) { + if self.shape.rpo[to] <= self.shape.rpo[from] { + self.lower.flush(out); + let label = self.label(to, "l"); + out.push(StmtKind::Continue(Some(label)).at(None)); + } else if self.shape.merge(to) { + self.lower.flush(out); + let label = self.label(to, "b"); + out.push(StmtKind::Break(Some(label)).at(None)); + } else { + debug_assert_eq!(self.shape.idom[to], from); + self.tree(to, out); + } + } +} + +/// `let n = 0; for (;;) switch (n) { ... }`: each node with other than one predecessor is a +/// case, which the chain of nodes after it with one predecessor follows in place. +fn dispatch(g: &Graph, shape: &Shape, lower: &mut impl Lower, labels: &mut u32, out: &mut Vec) { + let label = format!("l{labels}"); + let var = format!("bb{labels}"); + *labels += 1; + let n = shape.rpo.len(); + let mut preds = vec![0usize; n]; + let mut case = vec![false; n]; + case[0] = true; + for &node in &shape.order { + for &next in &g.succs[node] { + preds[next] += 1; + } + } + for &node in &shape.order { + if preds[node] != 1 { + case[node] = true; + } + } + let mut d = Dispatch { + lower, + case: &case, + label: &label, + var: &var, + }; + let mut cases = Vec::new(); + for &node in &shape.order { + if !case[node] { + continue; + } + let mut body = Vec::new(); + d.chain(node, &mut body); + cases.push((Expr::lit(node.to_string()), body)); + } + out.push( + StmtKind::decl( + true, + var.clone(), + Some(crate::ts::Type::Builtin("number")), + Some(Expr::lit("0")), + ) + .at(None), + ); + out.push( + StmtKind::While { + label: Some(label.clone()), + cond: Expr::lit("true"), + body: vec![ + StmtKind::Switch { + label: None, + disc: Expr::ident(&var), + cases, + default: Vec::new(), + } + .at(None), + ], + } + .at(None), + ); +} + +struct Dispatch<'d, L> { + lower: &'d mut L, + case: &'d [bool], + label: &'d str, + var: &'d str, +} + +impl Dispatch<'_, L> { + fn chain(&mut self, node: usize, out: &mut Vec) { + match self.lower.node(node, out) { + Exit::Goto(target) => self.jump(target, out), + Exit::Branch(arms, default) => { + let mut els = Vec::new(); + self.jump(default, &mut els); + for (cond, target) in arms.into_iter().rev() { + let mut then = Vec::new(); + self.jump(target, &mut then); + els = vec![StmtKind::If { cond, then, els }.at(None)]; + } + out.extend(els); + } + Exit::End => self.lower.flush(out), + } + } + + fn jump(&mut self, to: usize, out: &mut Vec) { + if !self.case[to] { + self.chain(to, out); + return; + } + self.lower.flush(out); + out.push(StmtKind::Expr(Expr::ident(self.var).assign(Expr::lit(to.to_string()))).at(None)); + out.push(StmtKind::Continue(Some(self.label.to_string())).at(None)); + } +} + +/// Drop the jumps that only fall through, then the labels nothing targets. +pub fn simplify(stmts: &mut Vec) { + for stmt in stmts.iter_mut() { + match &mut stmt.kind { + StmtKind::Block { label, body } => { + simplify(body); + if let Some(l) = label { + strip_tail(body, &StmtKind::Break(Some(l.clone()))); + } + } + StmtKind::While { label, body, .. } => { + simplify(body); + if let Some(l) = label { + strip_tail(body, &StmtKind::Continue(Some(l.clone()))); + } + } + StmtKind::If { then, els, .. } => { + simplify(then); + simplify(els); + } + StmtKind::Switch { cases, default, .. } => { + cases.iter_mut().for_each(|(_, body)| simplify(body)); + simplify(default); + } + StmtKind::Try { body, finally } => { + simplify(body); + simplify(finally); + } + StmtKind::Catch { body, handler, .. } => { + simplify(body); + simplify(handler); + } + _ => {} + } + } + // Unused labels, and blocks left without one. + let mut i = 0; + while i < stmts.len() { + let unused = match &stmts[i].kind { + StmtKind::Block { label: Some(l), body } + | StmtKind::While { + label: Some(l), body, .. + } => !targets(body, l), + _ => false, + }; + if unused { + match &mut stmts[i].kind { + StmtKind::Block { label, .. } | StmtKind::While { label, .. } => *label = None, + _ => unreachable!(), + } + } + if let StmtKind::Block { label: None, .. } = &stmts[i].kind { + let StmtKind::Block { body, .. } = std::mem::replace(&mut stmts[i].kind, StmtKind::Break(None)) else { + unreachable!() + }; + let len = body.len(); + stmts.splice(i..=i, body); + i += len; + continue; + } + // Nothing runs after a jump. + if stmts[i].diverges() { + stmts.truncate(i + 1); + break; + } + i += 1; + } + // `if (c) {} else { x }` is `if (!c) { x }`. + for stmt in stmts.iter_mut() { + if let StmtKind::If { cond, then, els } = &mut stmt.kind + && then.is_empty() + && !els.is_empty() + { + *cond = std::mem::replace(cond, Expr::undefined()).not(); + std::mem::swap(then, els); + } + } + stmts.retain( + |s| !matches!(&s.kind, StmtKind::If { then, els, cond } if then.is_empty() && els.is_empty() && pure(cond)), + ); +} + +/// Whether evaluating `e` does nothing but read. +pub fn pure(e: &Expr) -> bool { + match e { + Expr::Ident(_) | Expr::This | Expr::Sym(_) | Expr::Rt(_) | Expr::Lit(_) | Expr::Str(_) => true, + Expr::Member(base, _) | Expr::Unary(_, base) | Expr::Located(_, base) | Expr::As(base, _) => pure(base), + Expr::Binary(l, _, r) => pure(l) && pure(r), + Expr::Cond(c, a, b) => pure(c) && pure(a) && pure(b), + _ => false, + } +} + +/// Remove `jump` wherever it is the last thing `stmts` runs before falling off its end. +fn strip_tail(stmts: &mut Vec, jump: &StmtKind) { + let Some(last) = stmts.last_mut() else { + return; + }; + match &mut last.kind { + k if same_jump(k, jump) => { + stmts.pop(); + } + StmtKind::If { then, els, .. } => { + strip_tail(then, jump); + strip_tail(els, jump); + } + StmtKind::Block { body, .. } => strip_tail(body, jump), + _ => {} + } +} + +fn same_jump(a: &StmtKind, b: &StmtKind) -> bool { + match (a, b) { + (StmtKind::Break(x), StmtKind::Break(y)) | (StmtKind::Continue(x), StmtKind::Continue(y)) => x == y, + _ => false, + } +} + +/// Whether a `break` or `continue` in `stmts` names `label`. +fn targets(stmts: &[Stmt], label: &str) -> bool { + stmts.iter().any(|stmt| match &stmt.kind { + StmtKind::Break(Some(l)) | StmtKind::Continue(Some(l)) => l == label, + StmtKind::If { then, els, .. } => targets(then, label) || targets(els, label), + StmtKind::Block { body, .. } + | StmtKind::While { body, .. } + | StmtKind::ForRange { body, .. } + | StmtKind::ForOf { body, .. } => targets(body, label), + StmtKind::Try { body, finally } => targets(body, label) || targets(finally, label), + StmtKind::Catch { body, handler, .. } => targets(body, label) || targets(handler, label), + StmtKind::Switch { cases, default, .. } => { + cases.iter().any(|(_, body)| targets(body, label)) || targets(default, label) + } + _ => false, + }) +} diff --git a/ts/src/lower/mir/coroutine.rs b/ts/src/lower/mir/coroutine.rs new file mode 100644 index 000000000..b8e4cb081 --- /dev/null +++ b/ts/src/lower/mir/coroutine.rs @@ -0,0 +1,218 @@ +//! Async bodies, after rustc's state transform: each coroutine instance is a class of +//! its state, its captures, and the locals it keeps across an `.await`. Its `poll` is +//! the resume function the transform made, and its `[Symbol.dispose]` the drop the +//! transform made, which drops what the state it is in holds. + +use rustc_abi::{FieldIdx, VariantIdx}; +use rustc_hir::def_id::DefId; +use rustc_index::IndexVec; +use rustc_middle::mir::{CoroutineSavedLocal, Operand}; +use rustc_middle::ty::{self, EarlyBinder, GenericArgsRef, Instance}; +use rustc_span::Span; + +use super::{Env, Fx}; +use crate::lower::Translator; +use crate::lower::body::Body; +use crate::program::{SymKind, Symbol}; +use crate::ts::{self, Expr, FieldId, FieldInit, Item, Param, Stmt, SymId, Type}; + +/// A coroutine instance's class. +#[derive(Clone)] +pub struct Info<'tcx> { + pub sym: SymId, + /// Which state it is in: rustc's discriminant. + pub state: FieldId, + pub upvars: Vec, + pub saved: IndexVec, + variant_fields: IndexVec>, + _tcx: std::marker::PhantomData>, +} + +impl Info<'_> { + /// The saved local a variant's field is. + pub fn layout_variant_field(&self, variant: VariantIdx, field: FieldIdx) -> CoroutineSavedLocal { + self.variant_fields[variant][field] + } +} + +/// The class of the coroutine `def_id` with `args`, made the first time. +pub fn info<'tcx>(t: &mut Translator<'_, 'tcx>, def_id: DefId, args: GenericArgsRef<'tcx>, span: Span) -> Info<'tcx> { + let instance = Instance::new_raw(def_id, args); + if let Some(info) = t.coroutines.get(&instance) { + return info.clone(); + } + let tcx = t.tcx; + let parent = tcx.typeck_root_def_id(def_id); + let base = format!("{}Future", crate::lower::camel(tcx.item_name(parent).as_str())); + let sym = t.program.sym(Symbol { + module: t.module_of(parent), + base, + suffix: String::new(), + owner: None, + kind: SymKind::Struct, + order: t.order(tcx.def_span(def_id)), + instance: String::new(), + name: String::new(), + alias: None, + }); + let mut fields = Vec::new(); + let state = t.program.field(sym, "state".into()); + fields.push(ts::Field { + id: state, + ty: Type::Builtin("number"), + doc: None, + init: FieldInit::Value(Expr::lit("0")), + }); + let coroutine = args.as_coroutine(); + let names: Vec = match def_id.as_local() { + Some(local) => tcx + .closure_captures(local) + .iter() + .map(|c| crate::lower::camel(c.to_symbol().as_str())) + .collect(), + None => Vec::new(), + }; + let mut upvars = Vec::new(); + for (i, ty) in coroutine.upvar_tys().iter().enumerate() { + let name = names.get(i).cloned().unwrap_or_else(|| format!("u{i}")); + let id = t.program.field(sym, name); + upvars.push(id); + let ty = t.ty(ty, span); + fields.push(ts::Field { + id, + ty, + doc: None, + init: FieldInit::Ctor, + }); + } + let layout = match tcx.coroutine_layout(def_id, args) { + Ok(layout) => layout, + Err(_) => { + t.err(span, "a coroutine without a layout"); + return Info { + sym, + state, + upvars, + saved: IndexVec::new(), + variant_fields: IndexVec::new(), + _tcx: std::marker::PhantomData, + }; + } + }; + let mut saved = IndexVec::new(); + for field in layout.field_tys.iter() { + let name = match field.debuginfo_name { + Some(name) => format!("{}_", crate::lower::camel(name.as_str())), + None => "saved".into(), + }; + let id = t.program.field(sym, name); + saved.push(id); + let ty = t.subst(args, field.ty); + let ty = t.ty(ty, span); + fields.push(ts::Field { + id, + ty, + doc: None, + init: FieldInit::Later, + }); + } + let info = Info { + sym, + state, + upvars, + saved, + variant_fields: layout.variant_fields.clone(), + _tcx: std::marker::PhantomData, + }; + t.coroutines.insert(instance, info.clone()); + t.program.items.push(Item::Struct { + sym, + params: Vec::new(), + fields, + ctor: match info.upvars.is_empty() { + true => ts::Ctor::Unit, + false => ts::Ctor::Positional, + }, + doc: None, + }); + // `poll`: the resume function. + let poll = t.program.sym(Symbol { + module: t.module_of(parent), + base: "poll".into(), + suffix: String::new(), + owner: Some(sym), + kind: SymKind::Method, + order: 0, + instance: String::new(), + name: String::new(), + alias: None, + }); + if let Some(mir) = super::instance_mir(t, instance, span) { + let ret = mir.return_ty(); + let mut b = Body::detached(t, args, ret); + let mut fx = Fx::new(&mut b, &mir, Env::Coroutine, span); + let params = fx.params(); + let body = fx.lower(); + let ret = b.t.ret_ty(ret, span); + let loc = t.loc(span); + t.program.methods.entry(sym).or_default().push(ts::Function { + sym: poll, + params, + ret, + body, + loc, + doc: None, + generator: false, + }); + } + // `[Symbol.dispose]`: the drop, by the state it is in. + let dispose = t.program.sym(Symbol { + module: t.module_of(parent), + base: "[Symbol.dispose]".into(), + suffix: String::new(), + owner: Some(sym), + kind: SymKind::Method, + order: 0, + instance: String::new(), + name: String::new(), + alias: None, + }); + let drop = tcx.optimized_mir(def_id).coroutine_drop().cloned(); + let body: Vec = match drop { + Some(drop) => { + let mir = instance.instantiate_mir_and_normalize_erasing_regions( + tcx, + t.typing_env_pub(), + EarlyBinder::bind(tcx, drop), + ); + let mut b = Body::detached(t, args, tcx.types.unit); + let mut fx = Fx::new(&mut b, &mir, Env::Coroutine, span); + fx.lower() + } + None => Vec::new(), + }; + t.program.methods.entry(sym).or_default().push(ts::Function { + sym: dispose, + params: Vec::::new(), + ret: Type::Builtin("void"), + body, + loc: None, + doc: None, + generator: false, + }); + info +} + +/// A coroutine made in place: `new Class(...captures)`. +pub fn construct<'tcx>( + fx: &mut Fx<'_, '_, '_, 'tcx>, + def_id: DefId, + args: GenericArgsRef<'tcx>, + ops: &IndexVec>, + span: Span, + out: &mut Vec, +) -> Expr { + let info = info(fx.b.t, def_id, args, span); + let values = ops.iter().map(|op| fx.operand(op, span, out)).collect(); + Expr::New(Box::new(Expr::Sym(info.sym)), values) +} diff --git a/ts/src/lower/mir/glue.rs b/ts/src/lower/mir/glue.rs new file mode 100644 index 000000000..3d45b028f --- /dev/null +++ b/ts/src/lower/mir/glue.rs @@ -0,0 +1,75 @@ +//! Drop glue rustc builds, for a value whose JS form carries no `[Symbol.dispose]` of its +//! own: a `Result`, an enum, or a tuple holding something that drops. The glue switches on +//! the variant and drops each field, as rustc's `drop_in_place` shim does. + +use rustc_middle::ty::{self, EarlyBinder, Instance, Ty, TypeVisitableExt}; +use rustc_span::Span; + +use super::{Env, Fx}; +use crate::lower::Translator; +use crate::lower::body::Body; +use crate::program::{SymKind, Symbol}; +use crate::ts::{self, Expr, Param, SymId, Type}; + +/// The function dropping a `ty`, made from its drop shim the first time. +pub fn shim<'tcx>(t: &mut Translator<'_, 'tcx>, ty: Ty<'tcx>, span: Span) -> Option { + if let Some(&sym) = t.drop_shims.get(&ty) { + return Some(sym); + } + // Only an instance can say what a parameter's glue is. + if ty.has_param() || ty.has_aliases() { + return None; + } + let structural = match ty.kind() { + ty::Tuple(_) | ty::Closure(..) => true, + ty::Adt(adt, _) => adt.is_enum(), + _ => false, + }; + if !structural { + return None; + } + let tcx = t.tcx; + let instance = Instance::resolve_drop_glue(tcx, ty); + let sym = t.program.sym(Symbol { + module: Vec::new(), + base: "drop".into(), + suffix: crate::lower::short_name(tcx, ty), + owner: None, + kind: SymKind::Function, + order: 0, + instance: ty::print::with_no_trimmed_paths!(ty.to_string()), + name: String::new(), + alias: None, + }); + t.drop_shims.insert(ty, sym); + let body = tcx.instance_mir(instance.def).clone(); + let mir = + instance.instantiate_mir_and_normalize_erasing_regions(tcx, t.typing_env_pub(), EarlyBinder::bind(tcx, body)); + let mut b = Body::detached(t, instance.args, tcx.types.unit); + let mut fx = Fx::new(&mut b, &mir, Env::Shim, span); + let params = fx.params(); + let body = fx.lower(); + // The shim takes a pointer, which is the value in JS. + let params = params + .into_iter() + .map(|p| Param { + ty: Some(b.t.ty(ty, span)), + ..p + }) + .collect(); + t.program.items.push(ts::Item::Function(ts::Function { + sym, + params, + ret: Type::Builtin("void"), + body, + loc: None, + doc: None, + generator: false, + })); + Some(sym) +} + +/// `value` dropped by its glue. +pub fn call(sym: SymId, value: Expr) -> Expr { + Expr::Sym(sym).call(vec![value]) +} diff --git a/ts/src/lower/mir/mod.rs b/ts/src/lower/mir/mod.rs new file mode 100644 index 000000000..0ca86c085 --- /dev/null +++ b/ts/src/lower/mir/mod.rs @@ -0,0 +1,2123 @@ +//! Function bodies from MIR: the spike's prototype, beside the THIR lowering in `body`. +//! +//! MIR is each body's control-flow graph after borrow checking. Every drop is a +//! terminator where drop elaboration put it, with a flag wherever a value is moved on +//! some paths only, and an async body is already a state machine, its resume function +//! and its drop. Blocks become structured JS (`cfg`). Statements become JS statements, +//! with each temporary that is assigned once and read once folded back into the +//! expression that reads it, unless that would reorder an effect. +//! +//! Library calls go through the THIR lowering's mappings, which take their arguments +//! lowered: a detached `Body` holds the names, and its translator everything else. + +mod call; +mod cfg; +pub mod coroutine; +pub mod glue; +mod place; +mod rvalue; +mod unwind; + +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; +use rustc_index::IndexVec; +use rustc_middle::mir::{self, BasicBlock, Local, Operand, Place, Rvalue, StatementKind, TerminatorKind}; +use rustc_middle::ty::{self, EarlyBinder, Instance, Ty, TyCtxt}; +use rustc_span::Span; + +use self::cfg::Exit; +use super::Translator; +use super::body::Body; + +use crate::ts::{self, Expr, Param, Stmt, StmtKind, Type}; + +/// Translate one function instance's body from its MIR. +pub fn function<'a, 'tcx>(t: &mut Translator<'a, 'tcx>, instance: Instance<'tcx>) -> Option { + let tcx = t.tcx; + let def_id = instance.def_id(); + let span = tcx.def_span(def_id); + let mir = instance_mir(t, instance, span)?; + let next = super::iter::is_next(tcx, def_id); + if next { + super::iter::check_impl(t, def_id); + } + let ret_ty = mir.return_ty(); + let mut b = Body::detached(t, instance.args, ret_ty); + let mut fx = Fx::new(&mut b, &mir, Env::Plain, span); + let params = fx.params(); + let body = fx.lower(); + let ret = match (next, ret_ty.kind()) { + (true, ty::Adt(_, args)) => Type::Union(vec![fx.b.t.ty(args.type_at(0), span), Type::Rt("Done", Vec::new())]), + _ => fx.b.t.ret_ty(ret_ty, span), + }; + Some(ts::Function { + sym: 0, + params, + ret, + body, + loc: None, + doc: None, + generator: false, + }) +} + +/// The MIR of `instance`, with its generic arguments in place. +fn instance_mir<'tcx>(t: &mut Translator<'_, 'tcx>, instance: Instance<'tcx>, span: Span) -> Option> { + let tcx = t.tcx; + let def_id = instance.def_id(); + if !tcx.is_mir_available(def_id) { + t.err(span, format!("no MIR for `{}`", tcx.def_path_str(def_id))); + return None; + } + let body = tcx.optimized_mir(def_id).clone(); + Some(instance.instantiate_mir_and_normalize_erasing_regions(tcx, t.typing_env_pub(), EarlyBinder::bind(tcx, body))) +} + +/// What `_1` is. +#[derive(Clone)] +pub enum Env { + /// A parameter like any other. + Plain, + /// A closure's environment, whose fields are the expressions it captured. + Closure(Vec), + /// A coroutine's state, behind `Pin<&mut Self>` or `&mut Self`: `this` of its class. + Coroutine, + /// Drop glue, whose `_1` is a pointer to the value: the value in JS. + Shim, +} + +/// How a MIR local lowers. +#[derive(Clone, Debug)] +enum Slot<'tcx> { + /// No JS value: `()`, a function item, or another zero-sized value. + Unit, + /// A JS variable. + Var(String), + /// A reference assigned once, read as the place it borrows, which is set once its + /// assignment is lowered. + Alias(Option), + /// A discriminant only a `switchInt` reads: tested as the place's variant. + Discr(Option<(Expr, Ty<'tcx>)>), +} + +/// A temporary's value, held back to fold into the one expression that reads it. +struct Pending { + name: String, + value: Expr, + /// In the order assignments ran. + order: u32, + /// Whether evaluating it can do or throw anything. + effect: bool, + /// Whether it reads memory something else could change. + memory: bool, + /// The aggregate's operands, for a call that takes them apart. + parts: Option>, + /// The names it reads, which an assignment to must not pass it. + reads: Vec, + /// Whether it is a `Copy` aggregate read from where it could change, which only an + /// argument may share: emitted on its own, it is copied. + copy: bool, + /// Whether it is an aggregate's part, with no variable of its own until emitted. + decl: bool, +} + +pub struct Fx<'m, 'b, 'a, 'tcx> { + pub b: &'m mut Body<'b, 'a, 'tcx>, + pub mir: &'m mir::Body<'tcx>, + pub tcx: TyCtxt<'tcx>, + env: Env, + slots: IndexVec>, + /// Where each local assigned once is assigned, by a statement. + def_at: IndexVec>, + /// How many times each local is read, and assigned whole. + uses: IndexVec, + defs: IndexVec, + /// Locals something borrows, which a call or closure can change behind a read. + borrowed: IndexVec, + /// Locals changed in place: assigned through a projection, or borrowed mutably. + mutated: IndexVec, + /// The names of borrowed locals. + shared: FxHashSet, + /// `(value, overflowed)` pairs of checked arithmetic, whose `.1` is false once the value + /// is made, which panics where the assert would. + checked: FxHashSet, + /// The temporaries held back, in order. + pending: Vec, + order: u32, + /// Locals some statement assigns, which need a declaration. + assigned: IndexVec, + span: Span, + pub labels: u32, + /// Whether this is an `Iterator::next`, which returns an item or `rt.DONE`. + next: bool, + /// Blocks whose only statement is `unreachable`. + unreachable: FxHashSet, + /// The operands of the aggregate lowered last. + parts: Option>, + /// Blocks that start an ignored macro's code, and the block after it. + redirect: FxHashMap, + /// Whether the operands lowered now go to a local nothing changes in place. + into_frozen: bool, + /// Whether the value held back next is copied unless it folds into an argument. + copy_on_flush: bool, + /// Whether a copy lowered now shares what it copies, whatever it reads from. + force_share: bool, + /// Locals whose one read is as a call's argument, which may share what it copies. + arg_use: IndexVec, + /// Locals whose one read is as an argument to a call given nothing that changes memory. + arg_alone: IndexVec, + /// Locals whose one read is as an argument to a call whose result could hold it. + arg_kept: IndexVec, + /// The cleanups a panic runs, if any does something JS can see. + unwind: Option, + /// While the cleanups are lowered, the caught exception's name. + in_cleanup: Option, + /// Where each left-out macro invocation in the body is written. + ignored_sites: Vec, + /// The operands of aggregates assigned once to locals nothing changes. + var_parts: FxHashMap>, + /// Locals holding what `Iterator::next` returns by the protocol: an item or `rt.DONE`. + next_results: FxHashSet, + /// Their names, which a switch on their variant tests against `rt.DONE`. + next_names: FxHashSet, +} + +impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { + pub fn new(b: &'m mut Body<'b, 'a, 'tcx>, mir: &'m mir::Body<'tcx>, env: Env, span: Span) -> Self { + let tcx = b.t.tcx; + let n = mir.local_decls.len(); + let mut fx = Fx { + b, + mir, + tcx, + env, + slots: IndexVec::from_elem_n(Slot::Unit, n), + def_at: IndexVec::from_elem_n(None, n), + uses: IndexVec::from_elem_n(0, n), + defs: IndexVec::from_elem_n(0, n), + borrowed: IndexVec::from_elem_n(false, n), + mutated: IndexVec::from_elem_n(false, n), + shared: FxHashSet::default(), + checked: FxHashSet::default(), + pending: Vec::new(), + order: 0, + assigned: IndexVec::from_elem_n(false, n), + span, + labels: 0, + next: crate::lower::iter::is_next(tcx, mir.source.def_id()), + unreachable: FxHashSet::default(), + parts: None, + redirect: FxHashMap::default(), + into_frozen: false, + copy_on_flush: false, + force_share: false, + arg_use: IndexVec::from_elem_n(false, n), + arg_alone: IndexVec::from_elem_n(false, n), + arg_kept: IndexVec::from_elem_n(false, n), + unwind: None, + in_cleanup: None, + ignored_sites: Vec::new(), + var_parts: FxHashMap::default(), + next_results: FxHashSet::default(), + next_names: FxHashSet::default(), + }; + fx.analyze(); + fx + } + + pub fn err(&mut self, span: Span, msg: impl Into) -> Expr { + self.b.t.err(span, msg); + Expr::undefined() + } + + /// Count each local's reads and assignments, and decide how each lowers. + fn analyze(&mut self) { + let mir = self.mir; + for (bb, data) in mir.basic_blocks.iter_enumerated() { + if data.statements.iter().all(|s| { + matches!( + s.kind, + StatementKind::StorageDead(_) | StatementKind::StorageLive(_) | StatementKind::Nop + ) + }) && matches!(data.terminator().kind, TerminatorKind::Unreachable) + { + self.unreachable.insert(bb); + } + for (i, stmt) in data.statements.iter().enumerate() { + match &stmt.kind { + StatementKind::Assign(assign) => { + let (place, rvalue) = &**assign; + if let Some(local) = place.as_local() { + self.def_at[local] = Some((bb, i)); + } + self.def(*place); + self.rvalue_uses(rvalue); + if let Rvalue::BinaryOp(op, _) = rvalue + && op.overflowing_to_wrapping().is_some() + && let Some(local) = place.as_local() + { + self.checked.insert(local); + } + } + StatementKind::SetDiscriminant { place, .. } => { + self.place_use(**place); + self.mutated[place.local] = true; + } + _ => {} + } + } + let term = data.terminator(); + match &term.kind { + TerminatorKind::SwitchInt { discr, .. } => self.operand_use(discr), + TerminatorKind::Return => self.uses[mir::RETURN_PLACE] += 1, + TerminatorKind::Drop { place, .. } => self.place_use(*place), + TerminatorKind::Call { + func, + args, + destination, + .. + } => { + self.operand_use(func); + // A callee given nothing that changes memory can't change an argument's + // source while it runs. + let alone = !std::iter::once(func) + .chain(args.iter().map(|a| &a.node)) + .any(|op| self.reaches_mutable(op.ty(&mir.local_decls, self.tcx))); + let result = destination.ty(&mir.local_decls, self.tcx).ty; + for arg in args.iter() { + self.operand_use(&arg.node); + if let Operand::Copy(p) | Operand::Move(p) = &arg.node + && let Some(local) = p.as_local() + { + self.arg_use[local] = true; + self.arg_alone[local] = alone; + self.arg_kept[local] = holds(result, mir.local_decls[local].ty); + } + } + self.def(*destination); + } + TerminatorKind::Assert { cond, msg, .. } => { + // The assert after checked arithmetic reads `.1`, which lowering folds away. + let folded = matches!(cond, Operand::Move(p) | Operand::Copy(p) + if p.projection.len() == 1 && self.checked.contains(&p.local)); + if !folded && !redundant(msg) { + self.operand_use(cond); + // Only a bounds check's message shows its operands. + if let mir::AssertKind::BoundsCheck { len, index } = &**msg { + self.operand_use(len); + self.operand_use(index); + } + } + } + _ => {} + } + } + self.find_next_results(); + for local in mir.local_decls.indices() { + let ty = mir.local_decls[local].ty; + let slot = if self.zero_sized(ty) { + Slot::Unit + } else if let Some(value) = self.unit_struct(local, ty) { + Slot::Alias(Some(value)) + } else if self.alias(local) { + Slot::Alias(None) + } else if self.discr_only(local) { + Slot::Discr(None) + } else { + Slot::Var(String::new()) + }; + self.slots[local] = slot; + } + // Names: what debug info calls a local, else its number. + let mut named: FxHashMap = FxHashMap::default(); + for info in &mir.var_debug_info { + if let mir::VarDebugInfoContents::Place(place) = &info.value + && let Some(local) = place.as_local() + && info.composite.is_none() + { + named.entry(local).or_insert(info.name); + } + } + for local in mir.local_decls.indices() { + if let Slot::Var(_) = self.slots[local] { + let base = match named.get(&local) { + Some(name) => super::camel(name.as_str()), + None => format!("_{}", local.as_u32()), + }; + let base = match self.uses[local] == 0 && local.as_usize() <= mir.arg_count { + // A parameter nothing reads, which `noUnusedParameters` wants marked. + true if !base.starts_with('_') => format!("_{base}"), + _ => base, + }; + let name = self.b.fresh(&base); + if self.borrowed[local] { + self.shared.insert(name.clone()); + } + self.slots[local] = Slot::Var(name); + } + } + } + + fn def(&mut self, place: Place<'tcx>) { + match place.as_local() { + Some(local) => self.defs[local] += 1, + // Assigning a projection reads the local it starts from, and changes it in place + // unless it goes through a reference. + None => { + self.place_use(place); + if own(place) { + self.mutated[place.local] = true; + } + } + } + } + + fn place_use(&mut self, place: Place<'tcx>) { + self.uses[place.local] += 1; + for elem in place.projection { + if let mir::ProjectionElem::Index(index) = elem { + self.uses[index] += 1; + } + } + } + + fn operand_use(&mut self, op: &Operand<'tcx>) { + if let Operand::Copy(place) | Operand::Move(place) = op { + self.place_use(*place); + } + } + + fn rvalue_uses(&mut self, rvalue: &Rvalue<'tcx>) { + match rvalue { + Rvalue::Use(op, _) | Rvalue::Repeat(op, _) | Rvalue::Cast(_, op, _) | Rvalue::UnaryOp(_, op) => { + self.operand_use(op) + } + Rvalue::BinaryOp(_, ops) => { + self.operand_use(&ops.0); + self.operand_use(&ops.1); + } + // A borrow through a reference borrows what it points at, not the local. + Rvalue::Ref(_, kind, place) => { + self.place_use(*place); + if own(*place) { + self.borrowed[place.local] = true; + if let mir::BorrowKind::Mut { .. } = kind { + self.mutated[place.local] = true; + } + } + } + Rvalue::RawPtr(_, place) => { + self.place_use(*place); + if own(*place) { + self.borrowed[place.local] = true; + self.mutated[place.local] = true; + } + } + Rvalue::Discriminant(place) | Rvalue::CopyForDeref(place) => self.place_use(*place), + Rvalue::Reborrow(_, _, place) => { + self.place_use(*place); + self.borrowed[place.local] = true; + } + Rvalue::Aggregate(_, ops) => ops.iter().for_each(|op| self.operand_use(op)), + Rvalue::ThreadLocalRef(_) | Rvalue::WrapUnsafeBinder(..) => {} + } + } + + /// Whether a `ty` has no JS value: `()`, a function item, or a struct of no fields. + fn zero_sized(&self, ty: Ty<'tcx>) -> bool { + match ty.kind() { + ty::Tuple(tys) => tys.is_empty(), + ty::FnDef(..) | ty::Never => true, + _ => false, + } + } + + /// A struct of no fields that nothing assigns, which MIR leaves unassigned since it + /// holds nothing: JS needs it made. + fn unit_struct(&mut self, local: Local, ty: Ty<'tcx>) -> Option { + if self.defs[local] != 0 || self.is_arg(local) || local == mir::RETURN_PLACE { + return None; + } + let ty::Adt(adt, _) = ty.kind() else { + return None; + }; + if !adt.is_struct() || !adt.did().is_local() || adt.all_fields().next().is_some() { + return None; + } + let info = self.b.t.adt(*adt, self.span); + Some(self.b.t.construct(info.sym, &ts::Ctor::Unit, &[], Vec::new())) + } + + /// Whether `local` is a box's pointer, as a box's dereference reads it. + fn box_pointer(&self, local: Local) -> bool { + matches!( + self.def_rvalue(local), + Some(Rvalue::Cast(mir::CastKind::BoxDerefTransmute, ..)) + ) + } + + /// Whether `local` is a parameter. + fn is_arg(&self, local: Local) -> bool { + (1..=self.mir.arg_count).contains(&local.as_usize()) + } + + /// Whether `local` is a reference assigned once from a place that stays put, which + /// every read can read in its place, or a box's pointer, which is the box in JS. + fn alias(&self, local: Local) -> bool { + let mir = self.mir; + if self.is_arg(local) || self.defs[local] != 1 || self.borrowed[local] { + return false; + } + let Some(rvalue) = self.def_rvalue(local) else { + return false; + }; + match (mir.local_decls[local].ty.kind(), rvalue) { + // A borrow: borrow checking keeps the place as it is but through the borrow, + // unless the path to it goes through a reference held in a field or element, + // which can be replaced while the borrow lives. + (ty::Ref(..), Rvalue::Ref(_, _, place)) => { + self.stable(*place) + && place + .projection + .iter() + .skip(1) + .all(|e| !matches!(e, mir::ProjectionElem::Deref)) + } + // A copy of a reference, or a box's pointer: the place it is read from must not + // change at all. + ( + ty::Ref(..), + Rvalue::Use(Operand::Copy(place) | Operand::Move(place), _) | Rvalue::CopyForDeref(place), + ) + | ( + ty::RawPtr(..), + Rvalue::Cast(mir::CastKind::BoxDerefTransmute, Operand::Copy(place) | Operand::Move(place), _), + ) if self.env_field(*place) => true, + ( + ty::Ref(..), + Rvalue::Use(Operand::Copy(place) | Operand::Move(place), _) | Rvalue::CopyForDeref(place), + ) + | ( + ty::RawPtr(..), + Rvalue::Cast(mir::CastKind::BoxDerefTransmute, Operand::Copy(place) | Operand::Move(place), _), + ) => { + self.stable(*place) + && !self.mutated[place.local] + && place + .projection + .iter() + .all(|e| !matches!(e, mir::ProjectionElem::Deref)) + } + _ => false, + } + } + + /// Whether `place` is a field of a closure's environment, which stays put for the call. + fn env_field(&self, place: Place<'tcx>) -> bool { + matches!(self.env, Env::Closure(_)) + && place.local.as_u32() == 1 + && matches!( + place.projection.as_slice(), + [mir::ProjectionElem::Field(..)] | [mir::ProjectionElem::Deref, mir::ProjectionElem::Field(..)] + ) + } + + /// Locals an `Iterator::next` assigns that only a switch on their variant and a read + /// of `Some`'s value read: each holds the item or `rt.DONE`, as the protocol returns + /// it, so an iterator of `Option` keeps each `None` apart from its end. + fn find_next_results(&mut self) { + use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor}; + struct Uses<'tcx> { + local: Local, + /// The variants whose value may be read. + variants: &'static [u32], + /// Where a move of the whole into `?`'s branch may be. + branch: Option, + ok: bool, + _tcx: std::marker::PhantomData>, + } + impl<'tcx> Visitor<'tcx> for Uses<'tcx> { + fn visit_place(&mut self, place: &Place<'tcx>, cx: PlaceContext, at: mir::Location) { + if place.local != self.local { + return; + } + self.ok &= match place.projection.as_slice() { + [] => { + matches!( + cx, + PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect) + | PlaceContext::MutatingUse(MutatingUseContext::Call) + ) || (matches!(cx, PlaceContext::NonMutatingUse(NonMutatingUseContext::Move)) + && Some(at) == self.branch) + } + [ + mir::ProjectionElem::Downcast(_, v), + mir::ProjectionElem::Field(f, _), + .., + ] => self.variants.contains(&v.as_u32()) && f.as_u32() == 0, + _ => false, + }; + } + } + let mir = self.mir; + let tcx = self.tcx; + // `?` on a local: where, and what it branches to. + let mut branches: FxHashMap = FxHashMap::default(); + let mut nexts = Vec::new(); + for (bb, data) in mir.basic_blocks.iter_enumerated() { + let TerminatorKind::Call { + func, + args, + destination, + .. + } = &data.terminator().kind + else { + continue; + }; + let ty::FnDef(def_id, _) = *func.ty(&mir.local_decls, tcx).kind() else { + continue; + }; + let Some(local) = destination.as_local() else { + continue; + }; + if crate::lower::iter::is_next(tcx, def_id) && self.defs[local] == 1 { + nexts.push(local); + } + if crate::lower::library::key(tcx, def_id).is_some_and(|(o, n)| o == "Try" && n == "branch") + && let [arg] = &**args + && let Operand::Move(p) = &arg.node + && let Some(from) = p.as_local() + { + let at = mir::Location { + block: bb, + statement_index: data.statements.len(), + }; + branches.insert(from, (at, local)); + } + } + for local in nexts { + let branch = branches.get(&local).copied(); + let mut uses = Uses { + local, + variants: &[1], + branch: branch.map(|(at, _)| at), + ok: true, + _tcx: std::marker::PhantomData, + }; + uses.visit_body(mir); + if !uses.ok { + continue; + } + // What `?` branches to holds the same: the item, or `rt.DONE` to return early. + if let Some((_, to)) = branch { + let mut uses = Uses { + local: to, + variants: &[0, 1], + branch: None, + ok: self.defs[to] == 1, + _tcx: std::marker::PhantomData, + }; + uses.visit_body(mir); + if !uses.ok { + continue; + } + self.next_results.insert(to); + } + self.next_results.insert(local); + } + // An `Iterator::next` returns the item or `rt.DONE` too. + if self.next { + self.next_results.insert(mir::RETURN_PLACE); + } + } + + /// The rvalue a local's one assignment by a statement assigns. + fn def_rvalue(&self, local: Local) -> Option<&'m Rvalue<'tcx>> { + let (bb, i) = self.def_at[local]?; + let mir: &'m mir::Body<'tcx> = self.mir; + match &mir.basic_blocks[bb].statements[i].kind { + StatementKind::Assign(assign) => Some(&assign.1), + _ => None, + } + } + + /// Whether `place` names the same memory wherever it is read: every local in it is + /// assigned at most once. + fn stable(&self, place: Place<'tcx>) -> bool { + let fixed = |local: Local| self.defs[local] <= 1 && (!self.is_arg(local) || self.defs[local] == 0); + fixed(place.local) + && place.projection.iter().all(|elem| match elem { + mir::ProjectionElem::Index(index) => fixed(index), + _ => true, + }) + } + + /// Whether every read of `local` is a `switchInt` on it, after its one assignment of a + /// discriminant. + fn discr_only(&self, local: Local) -> bool { + if self.defs[local] != 1 || self.uses[local] != 1 { + return false; + } + let assigned = matches!(self.def_rvalue(local), Some(Rvalue::Discriminant(_))); + let mut switched = false; + for data in self.mir.basic_blocks.iter() { + if let TerminatorKind::SwitchInt { + discr: Operand::Move(p) | Operand::Copy(p), + .. + } = &data.terminator().kind + && p.as_local() == Some(local) + { + switched = true; + } + } + assigned && switched + } + + /// The parameters: `_1` through the argument count, but a closure's environment or a + /// coroutine's `this`. + pub fn params(&mut self) -> Vec { + let mut params = Vec::new(); + let skip = match self.env { + Env::Plain | Env::Shim => 0, + _ => 1, + }; + for local in self.mir.args_iter().skip(skip) { + let ty = match (&self.env, self.mir.local_decls[local].ty.kind()) { + (Env::Shim, ty::RawPtr(pointee, _)) => *pointee, + (_, _) => self.mir.local_decls[local].ty, + }; + let name = match &self.slots[local] { + Slot::Var(name) => name.clone(), + _ => self.b.fresh("_"), + }; + let ty = self.b.t.ty(ty, self.span); + params.push(Param { name, ty: Some(ty) }); + } + params + } + + /// The body: its locals' declarations, then its blocks as structured statements. + pub fn lower(&mut self) -> Vec { + // A parameter changed in place gets its own copy, since the caller's may be shared. + let mut entry = Vec::new(); + for local in self.mir.args_iter() { + let ty = self.mir.local_decls[local].ty; + if self.mutated[local] + && self.b.needs_copy(ty) + && let Slot::Var(name) = &self.slots[local] + { + let name = name.clone(); + entry.push( + StmtKind::Expr(Expr::ident(&name).assign(Expr::rt("copy", vec![Expr::ident(&name)]))).at(None), + ); + } + } + // A closure that captures nothing holds nothing, so MIR never assigns it: JS needs + // its function. + for local in self.mir.local_decls.indices() { + if let ty::Closure(def_id, args) = *self.mir.local_decls[local].ty.kind() + && self.defs[local] == 0 + && !self.is_arg(local) + && self.uses[local] > 0 + && let Slot::Var(name) = self.slots[local].clone() + { + let f = self.bare_closure(def_id, args, self.span); + self.assigned[local] = true; + entry.push(StmtKind::Expr(Expr::ident(&name).assign(f)).at(None)); + } + } + let n = self.mir.basic_blocks.len(); + self.skip_ignored_macros(); + self.plan_unwind(); + let mut succs = vec![Vec::new(); n]; + for (bb, data) in self.mir.basic_blocks.iter_enumerated() { + if data.is_cleanup { + continue; + } + succs[bb.as_usize()] = match self.redirect.get(&bb) { + Some(&to) => vec![to.as_usize()], + None => self + .successors(data.terminator()) + .into_iter() + .map(BasicBlock::as_usize) + .collect(), + }; + } + let graph = cfg::Graph { succs }; + let mut labels = self.labels; + let body = cfg::structure(&graph, self, &mut labels); + self.labels = labels; + let body = self.catch(body); + let mut out = Vec::new(); + for local in self.mir.local_decls.indices() { + // Parameters are declared by the signature. + if self.is_arg(local) || !self.assigned[local] { + continue; + } + if let Slot::Var(name) = self.slots[local].clone() { + let ty = self.decl_ty(local); + out.push(StmtKind::Declare { name, ty }.at(None)); + } + } + out.extend(entry); + out.extend(body); + drop_self_assignments(&mut out); + prune_unread(&mut out); + out + } + + /// Send each block whose terminator an ignored macro made to the one block its code + /// leaves to. Such a macro evaluates to `()`, so its code has one way out. + fn skip_ignored_macros(&mut self) { + let mir = self.mir; + // Every left-out invocation's span, which its arguments' code is within too. + let mut sites = Vec::new(); + for data in mir.basic_blocks.iter() { + let spans = data.statements.iter().map(|s| s.source_info.span); + for span in spans.chain([data.terminator().source_info.span]) { + if let Some(site) = self.b.t.ignored_site(span) + && !sites.contains(&site) + { + sites.push(site); + } + } + } + self.ignored_sites = sites; + let flags: IndexVec = mir + .basic_blocks + .iter() + .map(|data| !data.is_cleanup && self.in_ignored(data.terminator().source_info.span)) + .collect(); + let ignored = |bb: BasicBlock| flags[bb]; + for bb in mir.basic_blocks.indices() { + if !ignored(bb) { + continue; + } + let mut seen = FxHashSet::default(); + let mut stack = vec![bb]; + let mut exits = FxHashSet::default(); + while let Some(next) = stack.pop() { + if !seen.insert(next) { + continue; + } + for succ in self.successors(mir.basic_blocks[next].terminator()) { + match ignored(succ) { + true => stack.push(succ), + false => { + exits.insert(succ); + } + } + } + } + match exits.len() { + 1 => { + self.redirect.insert(bb, *exits.iter().next().unwrap()); + } + _ => { + let span = mir.basic_blocks[bb].terminator().source_info.span; + self.err(span, "an ignored macro whose code has other than one way out"); + } + } + } + } + + /// Whether `span` is within a left-out macro's invocation. + fn in_ignored(&self, span: Span) -> bool { + let span = span.source_callsite(); + self.ignored_sites.iter().any(|site| site.contains(span)) + } + + /// A local's declared type: the value's, for a checked pair. + fn decl_ty(&mut self, local: Local) -> Type { + let ty = self.mir.local_decls[local].ty; + if self.next_results.contains(&local) + && let ty::Adt(adt, args) = ty.kind() + { + // `Option`, or `?`'s `ControlFlow<_, Item>`. + let item = match crate::lower::library::Std::of(self.tcx, adt.did()) { + Some(crate::lower::library::Std::ControlFlow) => args.type_at(1), + _ => args.type_at(0), + }; + let item = self.b.t.ty(item, self.span); + return Type::Union(vec![item, Type::Rt("Done", Vec::new())]); + } + let ty = match (self.checked.contains(&local), ty.kind()) { + (true, ty::Tuple(tys)) => tys[0], + _ => ty, + }; + self.b.t.ty(ty, self.span) + } + + /// Where a terminator can go, without its unwinding. + fn successors(&self, term: &mir::Terminator<'tcx>) -> Vec { + match &term.kind { + TerminatorKind::Goto { target, .. } => vec![*target], + // An unreachable default is no edge: the last value takes its place. + TerminatorKind::SwitchInt { targets, .. } => { + let mut all = targets.all_targets().to_vec(); + if self.unreachable.contains(&targets.otherwise()) && all.len() > 1 { + all.pop(); + } + all + } + TerminatorKind::Assert { .. } if fails(term) => Vec::new(), + TerminatorKind::Drop { target, .. } | TerminatorKind::Assert { target, .. } => vec![*target], + TerminatorKind::Call { target, .. } => target.iter().copied().collect(), + TerminatorKind::FalseEdge { real_target, .. } | TerminatorKind::FalseUnwind { real_target, .. } => { + vec![*real_target] + } + TerminatorKind::Yield { resume, .. } => vec![*resume], + TerminatorKind::InlineAsm { targets, .. } => targets.to_vec(), + _ => Vec::new(), + } + } + + // Locals + + /// The JS name of a local held in a variable. + fn var(&self, local: Local) -> Option<&str> { + match &self.slots[local] { + Slot::Var(name) => Some(name), + _ => None, + } + } + + /// Whether `local` can be held back as a pending temporary: one assignment, one read. + fn foldable(&self, local: Local) -> bool { + matches!(self.slots[local], Slot::Var(_)) + && self.defs[local] == 1 + && self.uses[local] == 1 + && !self.borrowed[local] + && !self.is_arg(local) + } + + // Emission + + /// Emit the statements `stmts`, which together are one MIR statement, folding in the + /// pending temporaries they read once where that keeps every effect in order. + /// `writes` is the local the statement assigns whole. If `hold`, nothing runs now: + /// the statements are a value being held back itself. + fn emit(&mut self, stmts: Vec, writes: Option<&str>, hold: bool, out: &mut Vec) { + if self.pending.is_empty() { + out.extend(stmts); + return; + } + // Where each pending temporary is read, in evaluation order, and what else is read + // before it. + let mut reads = Vec::new(); + for stmt in &stmts { + scan_stmt(stmt, &mut reads, false); + } + let names: FxHashMap<&str, usize> = self + .pending + .iter() + .enumerate() + .map(|(i, p)| (p.name.as_str(), i)) + .collect(); + let mut count = vec![0u32; self.pending.len()]; + let mut deferred = vec![false; self.pending.len()]; + for read in &reads { + if let Read::Ident(name, lazy) = read + && let Some(&i) = names.get(name.as_str()) + { + count[i] += 1; + deferred[i] |= *lazy; + } + } + let fold: Vec = (0..self.pending.len()).map(|i| count[i] == 1 && !deferred[i]).collect(); + // Check the folded ones run in their order, after nothing they could change or be + // changed by. + let mut ok = true; + let mut last: Option = None; + let mut seen_memory = false; + let mut seen_effect = false; + for read in &reads { + match read { + Read::Ident(name, _) => match names.get(name.as_str()) { + Some(&i) if fold[i] => { + let p = &self.pending[i]; + if last.is_some_and(|l| l >= p.order) + || (p.effect && (seen_memory || seen_effect)) + || (p.memory && seen_effect) + { + ok = false; + } + last = Some(p.order); + } + Some(_) => {} + None => seen_memory |= self.shared.contains(name.as_str()), + }, + Read::Memory => seen_memory = true, + Read::Effect => seen_effect = true, + } + } + // What stays pending past this statement, and what must run before it. + let first_folded = (0..self.pending.len()) + .filter(|&i| fold[i]) + .map(|i| self.pending[i].order) + .min(); + let mut flush = Vec::new(); + for (i, p) in self.pending.iter().enumerate() { + if fold[i] { + continue; + } + let read = count[i] > 0; + let keep = match hold { + true => !read, + false => { + let overwritten = writes.is_some_and(|w| w == p.name || p.reads.iter().any(|r| r == w)); + !read && !p.effect && !p.memory && !overwritten + } + }; + let after = first_folded.is_some_and(|f| p.order > f); + if !keep { + ok &= !after; + flush.push(i); + } else if hold && after && (p.effect || p.memory) { + // It ran after something folded into a value held back past it. + ok = false; + } + } + if !ok { + self.flush(out); + out.extend(stmts); + return; + } + let pending = std::mem::take(&mut self.pending); + let mut stmts = stmts; + let mut kept = Vec::new(); + for (i, p) in pending.into_iter().enumerate() { + if fold[i] { + for stmt in &mut stmts { + if subst_stmt(stmt, &p.name, &p.value) { + break; + } + } + } else if flush.contains(&i) { + self.flush_one(p, out); + } else { + kept.push(p); + } + } + self.pending = kept; + out.extend(stmts); + } + + fn flush_one(&mut self, p: Pending, out: &mut Vec) { + if let Some(local) = self.local_named(&p.name) { + self.assigned[local] = true; + } + let value = match p.copy { + true => Expr::rt("copy", vec![p.value]), + false => p.value, + }; + out.push(match p.decl { + true => StmtKind::decl(false, p.name, None, Some(value)).at(None), + false => StmtKind::Expr(Expr::ident(&p.name).assign(value)).at(None), + }); + } + + /// Emit the pending temporaries `e` reads, and those held back before them, so that + /// `e` can be read more than once, or later. + pub(super) fn materialize(&mut self, e: &Expr, out: &mut Vec) { + let mut names = Vec::new(); + reads_of(e, &mut names); + let Some(last) = self + .pending + .iter() + .filter(|p| names.contains(&p.name)) + .map(|p| p.order) + .max() + else { + return; + }; + let pending = std::mem::take(&mut self.pending); + let (flush, keep): (Vec, Vec) = pending.into_iter().partition(|p| p.order <= last); + self.pending = keep; + for p in flush { + self.flush_one(p, out); + } + } + + fn local_named(&self, name: &str) -> Option { + self.slots + .iter_enumerated() + .find(|(_, slot)| matches!(slot, Slot::Var(n) if n == name)) + .map(|(local, _)| local) + } + + /// `exprs`, after the statements `pre`, with what they read that is held back folded + /// in where that keeps every effect in order; what can't fold is emitted to `out`. + fn fold_exprs(&mut self, pre: Vec, exprs: Vec, hold: bool, out: &mut Vec) -> Vec { + let n = exprs.len(); + let mut stmts = pre; + stmts.extend(exprs.into_iter().map(|e| StmtKind::Expr(e).at(None))); + let mut emitted = Vec::new(); + self.emit(stmts, None, hold, &mut emitted); + let folded = emitted + .drain(emitted.len() - n..) + .map(|s| match s.kind { + StmtKind::Expr(e) => e, + _ => unreachable!("an expression statement"), + }) + .collect(); + out.extend(emitted); + folded + } + + /// Hold back `value`, the assignment of `local`, to fold into what reads it. + fn hold(&mut self, local: Local, value: Expr, effect: bool, memory: bool, parts: Option>) { + let name = self.var(local).expect("a pending local is a variable").to_string(); + self.order += 1; + let mut reads = Vec::new(); + reads_of(&value, &mut reads); + let copy = std::mem::take(&mut self.copy_on_flush); + self.pending.push(Pending { + name, + value, + order: self.order, + effect, + memory, + parts, + reads, + copy, + decl: false, + }); + } + + /// The parts of the pending aggregate `local`, each held back in its place, so the + /// expression that reads them keeps what they do and read in order. + fn take_parts(&mut self, op: &Operand<'tcx>) -> Option> { + let Operand::Move(place) = op else { + return None; + }; + let local = place.as_local()?; + let name = self.var(local)?; + let i = self.pending.iter().position(|p| p.name == name && p.parts.is_some())?; + let p = self.pending.remove(i); + let parts = p.parts.expect("checked above"); + if !p.effect && !p.memory { + return Some(parts); + } + let n = parts.len() as u32; + for q in &mut self.pending { + if q.order > p.order { + q.order += n; + } + } + self.order += n; + let mut names = Vec::new(); + let mut held = 0; + for value in parts { + let mut events = Vec::new(); + scan(&value, &mut events, false); + let effect = events.iter().any(|r| matches!(r, Read::Effect)); + let memory = events.iter().any(|r| match r { + Read::Memory => true, + Read::Ident(name, _) => self.shared.contains(name.as_str()), + Read::Effect => false, + }); + if !effect && !memory { + names.push(value); + continue; + } + let mut reads = Vec::new(); + reads_of(&value, &mut reads); + let name = self.b.fresh("part"); + names.push(Expr::ident(&name)); + self.pending.insert( + i + held, + Pending { + name, + value, + order: p.order + 1 + held as u32, + effect, + memory, + parts: None, + reads, + copy: false, + decl: true, + }, + ); + held += 1; + } + Some(names) + } + + /// Assign `value` to the local `local` now, or hold it back. + fn set_local( + &mut self, + local: Local, + value: Expr, + effect: bool, + memory: bool, + parts: Option>, + out: &mut Vec, + ) { + match self.slots[local].clone() { + Slot::Var(name) => { + if self.uses[local] == 0 { + // Nothing reads it: only its effects run. + if effect { + self.emit(vec![StmtKind::Expr(value).at(None)], None, false, out); + } + return; + } + if self.foldable(local) { + // What it reads that is held back folds into it first, so nothing held + // back reads another, and its effects and reads go with it. + let mut reads = Vec::new(); + reads_of(&value, &mut reads); + let (effect, memory) = self + .pending + .iter() + .filter(|p| reads.contains(&p.name)) + .fold((effect, memory), |(e, m), p| (e || p.effect, m || p.memory)); + let mut folded = self.fold_exprs(Vec::new(), vec![value], true, out); + let value = folded.pop().unwrap(); + let parts = parts.and_then(|_| parts_of(&value)); + self.hold(local, value, effect, memory, parts); + return; + } + self.assigned[local] = true; + let stmt = StmtKind::Expr(Expr::ident(&name).assign(value)).at(None); + self.emit(vec![stmt], Some(&name), false, out); + // An aggregate assigned once and never changed: its operands, as emitted, + // read in place of its fields. + if parts.is_some() + && self.defs[local] == 1 + && !self.mutated[local] + && let Some(Stmt { + kind: StmtKind::Expr(Expr::Assign(_, value)), + .. + }) = out.last() + && let Some(parts) = parts_of(value) + { + self.var_parts.insert(local, parts); + } + } + Slot::Unit => { + if effect { + self.emit(vec![StmtKind::Expr(value).at(None)], None, false, out); + } + } + Slot::Alias(_) | Slot::Discr(_) => unreachable!("set by their own assignment"), + } + } + + // Blocks + + fn block(&mut self, bb: BasicBlock, out: &mut Vec) -> Exit { + let mir = self.mir; + let data = &mir.basic_blocks[bb]; + self.set_unwind(data.terminator(), out); + for stmt in &data.statements { + if !self.in_ignored(stmt.source_info.span) { + self.statement(stmt, out); + } + } + if let Some(&to) = self.redirect.get(&bb) { + return Exit::Goto(to.as_usize()); + } + self.terminator(data.terminator(), out) + } + + fn statement(&mut self, stmt: &mir::Statement<'tcx>, out: &mut Vec) { + let span = stmt.source_info.span; + match &stmt.kind { + StatementKind::Assign(assign) => { + let (place, rvalue) = &**assign; + self.assign(*place, rvalue, span, out); + } + StatementKind::SetDiscriminant { place, variant_index } => { + let target = self.set_discriminant(**place, *variant_index, span); + if let Some(stmt) = target { + self.emit(vec![stmt], None, false, out); + } + } + StatementKind::Intrinsic(intrinsic) => match &**intrinsic { + mir::NonDivergingIntrinsic::Assume(_) => {} + mir::NonDivergingIntrinsic::CopyNonOverlapping(_) => { + self.err(span, "`copy_nonoverlapping` is not supported"); + } + }, + StatementKind::FakeRead(..) + | StatementKind::StorageLive(_) + | StatementKind::StorageDead(_) + | StatementKind::PlaceMention(_) + | StatementKind::AscribeUserType(..) + | StatementKind::Coverage(_) + | StatementKind::ConstEvalCounter + | StatementKind::Nop + | StatementKind::BackwardIncompatibleDropHint { .. } => {} + } + } + + fn assign(&mut self, place: Place<'tcx>, rvalue: &Rvalue<'tcx>, span: Span, out: &mut Vec) { + if let Some(local) = place.as_local() { + match self.slots[local] { + Slot::Alias(_) => { + let target = match rvalue { + Rvalue::Ref(_, kind, p) => match self.mut_subslice(*kind, *p, span) { + Some(e) => e, + None => self.place_expr(*p, span, out), + }, + Rvalue::CopyForDeref(p) => self.place_expr(*p, span, out), + Rvalue::Use(Operand::Copy(p) | Operand::Move(p), _) => self.place_expr(*p, span, out), + Rvalue::Cast(_, Operand::Copy(p) | Operand::Move(p), _) => self.place_expr(*p, span, out), + _ => unreachable!("an alias is a borrow or a copy of one"), + }; + self.materialize(&target, out); + self.slots[local] = Slot::Alias(Some(target)); + return; + } + Slot::Discr(_) => { + let Rvalue::Discriminant(p) = rvalue else { + unreachable!("checked by discr_only") + }; + let ty = p.ty(&self.mir.local_decls, self.tcx).ty; + let target = self.place_expr(*p, span, out); + let target = self.path(target, "scrutinee", out); + self.slots[local] = Slot::Discr(Some((target, ty))); + return; + } + _ => {} + } + } + let mut pre = Vec::new(); + // A copy into a local nothing changes in place can share what it copies, if that + // doesn't change either. One into a temporary only an argument reads shares it as + // long as it folds into the argument, before anything could change it, if the callee + // can't change it either (a local's own value nothing borrows, or a callee given + // nothing that changes memory) nor return it. + self.into_frozen = place.as_local().is_some_and(|l| !self.mutated[l]); + let shares = place.as_local().is_some_and(|l| { + self.arg_use[l] + && !self.arg_kept[l] + && self.foldable(l) + && matches!(rvalue, Rvalue::Use(Operand::Copy(p), _) + if !self.frozen(*p) + && (own(*p) && !self.borrowed[p.local] || self.arg_alone[l]) + && self.b.needs_copy(p.ty(&self.mir.local_decls, self.tcx).ty)) + }); + if shares { + self.into_frozen = true; + self.force_share = true; + } + let value = self.rvalue(rvalue, place, span, &mut pre); + self.into_frozen = false; + self.force_share = false; + self.copy_on_flush = shares; + let Some(value) = value else { + out.extend(pre); + return; + }; + // What an `Iterator::next` returns, by the protocol. + let value = match ( + self.next && place.local == mir::RETURN_PLACE && place.projection.is_empty(), + rvalue, + ) { + (false, _) => value, + (true, Rvalue::Aggregate(kind, _)) if matches!(**kind, mir::AggregateKind::Adt(_, v, ..) if v.as_u32() == 0) => { + Expr::Rt("DONE") + } + (true, Rvalue::Aggregate(..)) => value, + (true, Rvalue::Use(Operand::Move(p) | Operand::Copy(p), _)) + if p.as_local().is_some_and(|l| self.next_results.contains(&l)) => + { + value + } + (true, _) => done_of(value), + }; + let effect = rvalue::effect(rvalue, self); + // A copy shared until it folds into its argument reads its source there. + let memory = rvalue::memory(rvalue, self) || shares; + let parts = self.parts.take(); + if let Some(local) = place.as_local() { + let parts = match rvalue { + Rvalue::Aggregate(..) => parts, + _ => None, + }; + if !pre.is_empty() { + self.emit(pre, None, false, out); + } + self.set_local(local, value, effect, memory, parts, out); + return; + } + if !pre.is_empty() { + self.emit(pre, None, false, out); + } + let stmts = self.store(place, value, span); + self.emit(stmts, None, false, out); + } + + /// Hold `value` in a path that reads it again for free. + pub fn path(&mut self, value: Expr, base: &str, out: &mut Vec) -> Expr { + if value.is_path() { + self.materialize(&value, out); + return value; + } + let name = self.b.fresh(base); + let stmt = StmtKind::decl(false, name.clone(), None, Some(value)).at(None); + self.emit(vec![stmt], None, false, out); + Expr::ident(name) + } + + fn terminator(&mut self, term: &mir::Terminator<'tcx>, out: &mut Vec) -> Exit { + let span = term.source_info.span; + match &term.kind { + TerminatorKind::Goto { target, .. } => Exit::Goto(target.as_usize()), + TerminatorKind::FalseEdge { real_target, .. } | TerminatorKind::FalseUnwind { real_target, .. } => { + Exit::Goto(real_target.as_usize()) + } + TerminatorKind::Return => { + let ret = self.mir.local_decls[mir::RETURN_PLACE].ty; + let value = match self.zero_sized(ret) { + true => None, + false => { + let mut pre = Vec::new(); + // Uncopied: a caller that changes the result in place copies it. + let value = self.place_expr(Place::return_place(), span, &mut pre); + out.extend(pre); + Some(value) + } + }; + let loc = self.b.t.loc(span); + self.emit(vec![StmtKind::Return(value).at(loc)], None, false, out); + self.flush(out); + Exit::End + } + TerminatorKind::Unreachable => { + self.emit( + vec![StmtKind::Expr(Expr::rt("unreachable", vec![Expr::undefined()])).at(None)], + None, + false, + out, + ); + Exit::End + } + TerminatorKind::SwitchInt { discr, targets } => self.switch(discr, targets, span, out), + TerminatorKind::Drop { place, target, .. } => { + let ty = place.ty(&self.mir.local_decls, self.tcx).ty; + if self.b.t.drops(ty) { + let mut pre = Vec::new(); + let value = self.place_expr(*place, span, &mut pre); + if let Some(dropped) = self.b.t.drop_value(value, ty, span) { + pre.push(StmtKind::Expr(dropped).at(None)); + } + self.emit(pre, None, false, out); + } + Exit::Goto(target.as_usize()) + } + TerminatorKind::Call { + func, + args, + destination, + target, + fn_span, + .. + } => { + self.call(func, args, *destination, *fn_span, out); + match target { + Some(target) => Exit::Goto(target.as_usize()), + None => { + self.flush(out); + Exit::End + } + } + } + TerminatorKind::Assert { + cond, + expected, + msg, + target, + .. + } => { + self.assert(cond, *expected, msg, span, out); + match fails(term) { + true => { + self.flush(out); + Exit::End + } + false => Exit::Goto(target.as_usize()), + } + } + TerminatorKind::TailCall { .. } => { + self.err(span, "`become` is not supported"); + Exit::End + } + TerminatorKind::InlineAsm { .. } => { + self.err(span, "inline assembly is not supported"); + Exit::End + } + TerminatorKind::Yield { .. } | TerminatorKind::CoroutineDrop => { + self.err(span, "a coroutine before its state transform"); + Exit::End + } + TerminatorKind::UnwindResume | TerminatorKind::UnwindTerminate(_) => match self.in_cleanup.clone() { + Some(caught) => { + self.flush(out); + out.push(StmtKind::Throw(Expr::ident(caught)).at(None)); + Exit::End + } + None => { + self.err(span, "unwinding outside cleanup"); + Exit::End + } + }, + } + } + + fn switch(&mut self, discr: &Operand<'tcx>, targets: &mir::SwitchTargets, span: Span, out: &mut Vec) -> Exit { + let mut arms: Vec<(u128, BasicBlock)> = targets.iter().collect(); + let mut default = targets.otherwise(); + // An unreachable default leaves the last value as the default. + if self.unreachable.contains(&default) + && let Some((_, last)) = arms.pop() + { + default = last; + } + let discr_local = match discr { + Operand::Copy(p) | Operand::Move(p) => p.as_local(), + _ => None, + }; + let tests: Vec<(Expr, BasicBlock)> = match discr_local.map(|l| self.slots[l].clone()) { + Some(Slot::Discr(Some((scrut, ty)))) => arms + .iter() + .map(|&(value, target)| (self.variant_test(&scrut, ty, value, span), target)) + .collect(), + _ => { + let ty = discr.ty(&self.mir.local_decls, self.tcx); + let mut pre = Vec::new(); + let value = self.operand(discr, span, &mut pre); + let value = match arms.len() { + 0 | 1 => value, + _ => match value.is_path() { + true => value, + false => { + let name = self.b.fresh("value"); + pre.push(StmtKind::decl(false, name.clone(), None, Some(value)).at(None)); + Expr::ident(name) + } + }, + }; + let mut tests = Vec::new(); + for &(v, target) in &arms { + let test = match ty.kind() { + ty::Bool => match v { + 0 => value.clone().not(), + _ => value.clone(), + }, + _ => match super::num::Int::of(ty) { + Some(int) => { + let lit = int.lit(int.fold(v as i128)); + int.compare("===", value.clone(), lit) + } + None => self.err(span, format!("a switch on `{ty}`")), + }, + }; + tests.push((test, target)); + } + // What the tests read that is held back folds into them. + let targets: Vec = tests.iter().map(|(_, t)| *t).collect(); + let conds = self.fold_exprs(pre, tests.into_iter().map(|(c, _)| c).collect(), false, out); + conds.into_iter().zip(targets).collect() + } + }; + self.flush(out); + // Arms to one target are one test. + let mut grouped: Vec<(Expr, BasicBlock)> = Vec::new(); + for (cond, target) in tests { + match grouped.iter_mut().find(|(_, t)| *t == target) { + Some((c, _)) => *c = std::mem::replace(c, Expr::undefined()).binary("||", cond), + None => grouped.push((cond, target)), + } + } + let tests: Vec<(Expr, usize)> = grouped.into_iter().map(|(c, t)| (fold(c), t.as_usize())).collect(); + match tests.is_empty() { + true => Exit::Goto(default.as_usize()), + false => Exit::Branch(tests, default.as_usize()), + } + } + + fn assert( + &mut self, + cond: &Operand<'tcx>, + expected: bool, + msg: &mir::AssertMessage<'tcx>, + span: Span, + out: &mut Vec, + ) { + // Checked arithmetic already panicked with this message as it made its value. + if let Operand::Move(p) | Operand::Copy(p) = cond + && p.projection.len() == 1 + && self.checked.contains(&p.local) + { + return; + } + if redundant(msg) { + return; + } + let mut pre = Vec::new(); + let value = self.operand(cond, span, &mut pre); + let mut exprs = vec![value]; + if let mir::AssertKind::BoundsCheck { len, index } = msg { + exprs.push(self.operand(len, span, &mut pre)); + exprs.push(self.operand(index, span, &mut pre)); + } + let mut folded = self.fold_exprs(pre, exprs, false, out).into_iter(); + let value = fold(folded.next().unwrap()); + let operands: Vec = folded.collect(); + let mut pre = Vec::new(); + let failed = fold(match expected { + true => value.not(), + false => value, + }); + // An assert of a constant that holds, such as of a shift by a literal. + if matches!(&failed, Expr::Lit(t) if t == "false") { + return; + } + let message = match self.assert_message(msg, operands, span) { + Some(message) => message, + None => return, + }; + // An assert that always fails, such as a coroutine's after it returned. + if matches!(&failed, Expr::Lit(t) if t == "true") { + pre.push(StmtKind::Expr(Expr::rt("panic", vec![message])).at(self.b.t.loc(span))); + self.emit(pre, None, false, out); + return; + } + pre.push( + StmtKind::If { + cond: failed, + then: vec![StmtKind::Expr(Expr::rt("panic", vec![message])).at(None)], + els: Vec::new(), + } + .at(self.b.t.loc(span)), + ); + self.emit(pre, None, false, out); + } + + /// The message an assert panics with, as the runtime spells it. + fn assert_message(&mut self, msg: &mir::AssertMessage<'tcx>, operands: Vec, span: Span) -> Option { + use mir::AssertKind; + use rustc_middle::mir::BinOp; + let text = |s: &str| Some(Expr::Str(s.to_string())); + match msg { + AssertKind::BoundsCheck { .. } => { + let [len, index] = <[Expr; 2]>::try_from(operands).ok()?; + Some( + Expr::Str("index out of bounds: the len is ".into()) + .binary("+", len) + .binary("+", Expr::Str(" but the index is ".into())) + .binary("+", index), + ) + } + AssertKind::Overflow(op, ..) => match op { + BinOp::Add => text("attempt to add with overflow"), + BinOp::Sub => text("attempt to subtract with overflow"), + BinOp::Mul => text("attempt to multiply with overflow"), + BinOp::Div => text("attempt to divide with overflow"), + BinOp::Rem => text("attempt to calculate the remainder with overflow"), + BinOp::Shl => text("attempt to shift left with overflow"), + BinOp::Shr => text("attempt to shift right with overflow"), + _ => { + self.err(span, format!("an overflow check of `{op:?}`")); + None + } + }, + AssertKind::OverflowNeg(_) => text("attempt to negate with overflow"), + AssertKind::DivisionByZero(_) => text("attempt to divide by zero"), + AssertKind::RemainderByZero(_) => text("attempt to calculate the remainder with a divisor of zero"), + AssertKind::ResumedAfterReturn(_) => text("`async fn` resumed after completion"), + AssertKind::ResumedAfterPanic(_) => text("`async fn` resumed after panicking"), + _ => { + self.err(span, format!("an assert of `{msg:?}`")); + None + } + } + } + + /// Emit every pending temporary, in order. + fn flush_pending(&mut self, out: &mut Vec) { + for p in std::mem::take(&mut self.pending) { + self.flush_one(p, out); + } + } +} + +impl cfg::Lower for Fx<'_, '_, '_, '_> { + fn node(&mut self, node: usize, out: &mut Vec) -> Exit { + self.block(BasicBlock::from_usize(node), out) + } + + fn flush(&mut self, out: &mut Vec) { + self.flush_pending(out); + // Control goes elsewhere, which other code may reach with another cleanup. + if let Some(unwind) = &mut self.unwind { + unwind.known = None; + } + } +} + +impl Fx<'_, '_, '_, '_> { + fn flush(&mut self, out: &mut Vec) { + cfg::Lower::flush(self, out); + } + + /// A block's statements and how it leaves, for the cleanups' lowering. + fn block_mapped(&mut self, bb: BasicBlock, out: &mut Vec) -> Exit { + self.block(bb, out) + } + + /// Emit a statement every held-back value that can do anything runs before. + fn emit_barrier(&mut self, stmt: Stmt, out: &mut Vec) { + self.emit(vec![stmt], None, false, out); + } +} + +impl<'tcx> Fx<'_, '_, '_, 'tcx> { + /// Whether a value of `ty` can change memory: it holds a mutable reference, a raw + /// pointer, or something with interior mutability. + fn reaches_mutable(&self, ty: Ty<'tcx>) -> bool { + use rustc_middle::ty::TypeVisitableExt; + let env = self.b.t.typing_env_pub(); + ty.walk().any(|arg| { + arg.as_type().is_some_and(|t| { + matches!(t.kind(), ty::Ref(_, _, rustc_ast::Mutability::Mut) | ty::RawPtr(..)) + // Inside a binder, as a function pointer's signature, only its kind tells. + || !t.has_escaping_bound_vars() && !t.is_freeze(self.tcx, env) + }) + }) + } +} + +/// Whether a value of `ty` could hold a `part`: contains its type, or hides what it holds. +fn holds<'tcx>(ty: Ty<'tcx>, part: Ty<'tcx>) -> bool { + ty.walk().any(|arg| { + arg.as_type().is_some_and(|t| { + t == part + || matches!( + t.kind(), + ty::Dynamic(..) | ty::Alias(..) | ty::Param(_) | ty::Closure(..) | ty::Coroutine(..) + ) + }) + }) +} + +/// Whether `place` is part of its local's own value, rather than reached through a +/// reference or pointer. +fn own(place: Place<'_>) -> bool { + place + .projection + .iter() + .all(|e| !matches!(e, mir::ProjectionElem::Deref)) +} + +/// What an expression reads, in evaluation order. +enum Read { + /// A name, and whether it is read later or maybe not at all: in a function, or in a + /// branch that may not run. + Ident(String, bool), + /// Memory: a member or an element. + Memory, + /// A call, which may do anything. + Effect, +} + +fn scan_stmt(stmt: &Stmt, reads: &mut Vec, lazy: bool) { + match &stmt.kind { + StmtKind::Expr(e) | StmtKind::Return(Some(e)) | StmtKind::Throw(e) => scan(e, reads, lazy), + StmtKind::Decl { init: Some(e), .. } => scan(e, reads, lazy), + StmtKind::If { cond, then, els } => { + scan(cond, reads, lazy); + then.iter().chain(els).for_each(|s| scan_stmt(s, reads, true)); + } + StmtKind::While { cond, body, .. } => { + scan(cond, reads, true); + body.iter().for_each(|s| scan_stmt(s, reads, true)); + } + StmtKind::Block { body, .. } => body.iter().for_each(|s| scan_stmt(s, reads, lazy)), + _ => {} + } +} + +fn scan(e: &Expr, reads: &mut Vec, lazy: bool) { + match e { + Expr::Ident(name) => reads.push(Read::Ident(name.clone(), lazy)), + Expr::This | Expr::Sym(_) | Expr::Rt(_) | Expr::Lit(_) | Expr::Str(_) | Expr::Yield => {} + Expr::Array(items) => items.iter().for_each(|e| scan(e, reads, lazy)), + Expr::Object(props) => props.iter().for_each(|(_, e)| scan(e, reads, lazy)), + // The runtime's and the program's own items never change. + Expr::Member(base, _) if matches!(&**base, Expr::Rt(_) | Expr::Sym(_)) => {} + Expr::Member(base, _) => { + scan(base, reads, lazy); + reads.push(Read::Memory); + } + Expr::Index(base, index) => { + scan(base, reads, lazy); + scan(index, reads, lazy); + reads.push(Read::Memory); + } + Expr::Call(f, args) | Expr::New(f, args) => { + // A method's lookup reads no data the arguments could change: only its receiver. + match &**f { + Expr::Member(recv, _) => scan(recv, reads, lazy), + f => scan(f, reads, lazy), + } + args.iter().for_each(|e| scan(e, reads, lazy)); + reads.push(Read::Effect); + } + Expr::Unary(_, e) | Expr::Located(_, e) | Expr::As(e, _) => scan(e, reads, lazy), + Expr::Postfix(e, _) => { + scan(e, reads, lazy); + reads.push(Read::Effect); + } + Expr::Binary(l, op, r) => { + scan(l, reads, lazy); + let lazy_r = lazy || matches!(*op, "&&" | "||" | "??"); + scan(r, reads, lazy_r); + } + Expr::Cond(c, a, b) => { + scan(c, reads, lazy); + scan(a, reads, true); + scan(b, reads, true); + } + Expr::Assign(target, value) => { + // A variable assigned is not read; a place's base and index are, before the value. + if !matches!(**target, Expr::Ident(_)) { + scan(target, reads, lazy); + } + scan(value, reads, lazy); + reads.push(Read::Effect); + } + Expr::Arrow(_, _, body) => body.iter().for_each(|s| scan_stmt(s, reads, true)), + Expr::Lambda(_, body) => scan(body, reads, true), + } +} + +/// Replace the one read of `name` in `stmt` with `value`; whether it was there. +fn subst_stmt(stmt: &mut Stmt, name: &str, value: &Expr) -> bool { + match &mut stmt.kind { + StmtKind::Expr(e) | StmtKind::Return(Some(e)) | StmtKind::Throw(e) => subst(e, name, value), + StmtKind::Decl { init: Some(e), .. } => subst(e, name, value), + StmtKind::If { cond, .. } => subst(cond, name, value), + _ => false, + } +} + +fn subst(e: &mut Expr, name: &str, value: &Expr) -> bool { + match e { + Expr::Ident(n) if n == name => { + *e = value.clone(); + true + } + Expr::Ident(_) | Expr::This | Expr::Sym(_) | Expr::Rt(_) | Expr::Lit(_) | Expr::Str(_) | Expr::Yield => false, + Expr::Array(items) => items.iter_mut().any(|e| subst(e, name, value)), + Expr::Object(props) => props.iter_mut().any(|(_, e)| subst(e, name, value)), + Expr::Member(base, _) => subst(base, name, value), + Expr::Index(base, index) => subst(base, name, value) || subst(index, name, value), + Expr::Call(f, args) | Expr::New(f, args) => { + subst(f, name, value) || args.iter_mut().any(|e| subst(e, name, value)) + } + Expr::Unary(_, e) | Expr::Located(_, e) | Expr::As(e, _) | Expr::Postfix(e, _) => subst(e, name, value), + Expr::Binary(l, _, r) | Expr::Assign(l, r) => subst(l, name, value) || subst(r, name, value), + Expr::Cond(c, a, b) => subst(c, name, value) || subst(a, name, value) || subst(b, name, value), + Expr::Arrow(..) | Expr::Lambda(..) => false, + } +} + +/// Drop the declarations of variables nothing reads, keeping the effects of what was +/// assigned to them. +fn prune_unread(stmts: &mut Vec) { + let mut declared = Vec::new(); + for stmt in stmts.iter() { + if let StmtKind::Declare { name, .. } = &stmt.kind { + declared.push(name.clone()); + } + } + let mut reads = Vec::new(); + for stmt in stmts.iter() { + scan_reads(stmt, &mut reads); + } + let read: FxHashSet<&str> = reads.iter().map(String::as_str).collect(); + let unread: FxHashSet = declared.into_iter().filter(|n| !read.contains(n.as_str())).collect(); + if unread.is_empty() { + return; + } + strip_assignments(stmts, &unread); +} + +/// Every name `stmt` reads, but not as the target of an assignment to it. +fn scan_reads(stmt: &Stmt, out: &mut Vec) { + let mut visit = |e: &Expr| reads_of(e, out); + match &stmt.kind { + StmtKind::Expr(e) | StmtKind::Return(Some(e)) | StmtKind::Throw(e) => visit(e), + StmtKind::Decl { init: Some(e), .. } => visit(e), + StmtKind::If { cond, then, els } => { + visit(cond); + then.iter().chain(els).for_each(|s| scan_reads(s, out)); + } + StmtKind::While { cond, body, .. } => { + reads_of(cond, out); + body.iter().for_each(|s| scan_reads(s, out)); + } + StmtKind::Block { body, .. } => body.iter().for_each(|s| scan_reads(s, out)), + StmtKind::Switch { + disc, cases, default, .. + } => { + reads_of(disc, out); + for (v, body) in cases { + reads_of(v, out); + body.iter().for_each(|s| scan_reads(s, out)); + } + default.iter().for_each(|s| scan_reads(s, out)); + } + StmtKind::Try { body, finally } => body.iter().chain(finally).for_each(|s| scan_reads(s, out)), + StmtKind::Catch { body, handler, .. } => body.iter().chain(handler).for_each(|s| scan_reads(s, out)), + StmtKind::ForRange { start, end, body, .. } => { + reads_of(start, out); + reads_of(end, out); + body.iter().for_each(|s| scan_reads(s, out)); + } + StmtKind::ForOf { iter, body, .. } => { + reads_of(iter, out); + body.iter().for_each(|s| scan_reads(s, out)); + } + _ => {} + } +} + +fn reads_of(e: &Expr, out: &mut Vec) { + match e { + Expr::Ident(name) => out.push(name.clone()), + Expr::This | Expr::Sym(_) | Expr::Rt(_) | Expr::Lit(_) | Expr::Str(_) | Expr::Yield => {} + Expr::Array(items) => items.iter().for_each(|e| reads_of(e, out)), + Expr::Object(props) => props.iter().for_each(|(_, e)| reads_of(e, out)), + Expr::Member(base, _) => reads_of(base, out), + Expr::Index(base, index) => { + reads_of(base, out); + reads_of(index, out); + } + Expr::Call(f, args) | Expr::New(f, args) => { + reads_of(f, out); + args.iter().for_each(|e| reads_of(e, out)); + } + Expr::Unary(_, e) | Expr::Located(_, e) | Expr::As(e, _) | Expr::Lambda(_, e) | Expr::Postfix(e, _) => { + reads_of(e, out) + } + Expr::Binary(l, _, r) => { + reads_of(l, out); + reads_of(r, out); + } + Expr::Cond(c, a, b) => { + reads_of(c, out); + reads_of(a, out); + reads_of(b, out); + } + // Assigning a name whole doesn't read it. + Expr::Assign(target, value) => { + if !matches!(&**target, Expr::Ident(_)) { + reads_of(target, out); + } + reads_of(value, out); + } + Expr::Arrow(_, _, body) => body.iter().for_each(|s| scan_reads(s, out)), + } +} + +fn strip_assignments(stmts: &mut Vec, unread: &FxHashSet) { + stmts.retain(|s| !matches!(&s.kind, StmtKind::Declare { name, .. } if unread.contains(name))); + for stmt in stmts.iter_mut() { + match &mut stmt.kind { + StmtKind::Expr(e) => { + if let Expr::Assign(target, value) = e + && let Expr::Ident(name) = &**target + && unread.contains(name) + { + *e = std::mem::replace(&mut **value, Expr::undefined()); + } + } + StmtKind::If { then, els, .. } => { + strip_assignments(then, unread); + strip_assignments(els, unread); + } + StmtKind::While { body, .. } | StmtKind::Block { body, .. } => strip_assignments(body, unread), + StmtKind::Switch { cases, default, .. } => { + cases.iter_mut().for_each(|(_, body)| strip_assignments(body, unread)); + strip_assignments(default, unread); + } + StmtKind::Try { body, finally } => { + strip_assignments(body, unread); + strip_assignments(finally, unread); + } + StmtKind::Catch { body, handler, .. } => { + strip_assignments(body, unread); + strip_assignments(handler, unread); + } + _ => {} + } + } + // What is left of an assignment and does nothing. + stmts.retain(|s| !matches!(&s.kind, StmtKind::Expr(e) if inert(e))); +} + +/// The operands of an aggregate, from its expression. +fn parts_of(value: &Expr) -> Option> { + match value { + Expr::Array(items) => Some(items.clone()), + Expr::As(inner, _) => parts_of(inner), + Expr::Call(f, items) if matches!(&**f, Expr::Member(base, crate::ts::Key::Name(of)) if of == "of" && matches!(&**base, Expr::Ident(u) if u == "Uint8Array")) => { + Some(items.clone()) + } + Expr::Object(props) => Some(props.iter().map(|(_, v)| v.clone()).collect()), + Expr::New(f, items) if matches!(&**f, Expr::Rt("Range")) => Some(items.clone()), + _ => None, + } +} + +/// `e` with comparisons of two number literals, and their negations, worked out. +pub fn fold(e: Expr) -> Expr { + match e { + Expr::Binary(l, op, r) => { + let (l, r) = (fold(*l), fold(*r)); + let num = |e: &Expr| match e { + Expr::Lit(t) => t.parse::().ok(), + _ => None, + }; + if let (Some(a), Some(b)) = (num(&l), num(&r)) { + // Integer arithmetic JS does exactly, in 32 bits for the bitwise operators. + let small = |v: f64| v.fract() == 0.0 && v.abs() < 2f64.powi(31); + if small(a) && small(b) { + let (x, y) = (a as i64, b as i64); + let int = match op { + "<<" if (0..31).contains(&y) => Some((x as i32).wrapping_shl(y as u32) as i64), + ">>" if (0..32).contains(&y) => Some(((x as i32) >> y) as i64), + "|" => Some((x as i32 | y as i32) as i64), + "&" => Some((x as i32 & y as i32) as i64), + "^" => Some((x as i32 ^ y as i32) as i64), + "+" => Some(x + y), + "-" => Some(x - y), + _ => None, + }; + if let Some(v) = int + && (x >= 0 && y >= 0 || !matches!(op, "<<" | ">>" | "|" | "&" | "^")) + { + return Expr::lit(v.to_string()); + } + } + let value = match op { + "<" => Some(a < b), + "<=" => Some(a <= b), + ">" => Some(a > b), + ">=" => Some(a >= b), + "===" => Some(a == b), + "!==" => Some(a != b), + _ => None, + }; + if let Some(value) = value { + return Expr::lit(value.to_string()); + } + } + Expr::Binary(Box::new(l), op, Box::new(r)) + } + // An element of an array made in place, as a format argument's tuple is read. + Expr::Index(base, index) => { + let (base, index) = (fold(*base), fold(*index)); + let items = match &base { + Expr::Array(items) => Some(items), + Expr::As(inner, _) => match &**inner { + Expr::Array(items) => Some(items), + _ => None, + }, + _ => None, + }; + if let (Some(items), Expr::Lit(i)) = (items, &index) + && let Ok(i) = i.parse::() + && i < items.len() + && items.iter().all(Expr::is_path) + { + return items[i].clone(); + } + Expr::Index(Box::new(base), Box::new(index)) + } + Expr::Unary("!", inner) => match fold(*inner) { + Expr::Lit(t) if t == "true" => Expr::lit("false"), + Expr::Lit(t) if t == "false" => Expr::lit("true"), + inner => Expr::Unary("!", Box::new(inner)), + }, + e => e, + } +} + +/// Whether an assert checks what the runtime's division, remainder, negation, or shift +/// checks as the operation it guards runs next, which panics with the same message. +fn redundant(msg: &mir::AssertMessage<'_>) -> bool { + use mir::{AssertKind, BinOp}; + matches!( + msg, + AssertKind::OverflowNeg(_) + | AssertKind::DivisionByZero(_) + | AssertKind::RemainderByZero(_) + | AssertKind::Overflow(BinOp::Div | BinOp::Rem | BinOp::Shl | BinOp::Shr, ..) + ) +} + +/// An `Option` as `Iterator::next` returns it: the item, or `rt.DONE` for `None`. +pub fn done_of(option: Expr) -> Expr { + match option { + Expr::Lit(t) if t == "undefined" => Expr::Rt("DONE"), + option => Expr::Rt("iter").member("fromOption").call(vec![option]), + } +} + +/// Whether `term` is an assert of a constant that fails. +fn fails(term: &mir::Terminator<'_>) -> bool { + let TerminatorKind::Assert { + cond: Operand::Constant(c), + expected, + .. + } = &term.kind + else { + return false; + }; + c.const_.try_to_bool().is_some_and(|b| b != *expected) +} + +/// Drop the assignments of a variable to itself, which ferrying a value through +/// temporaries leaves. +fn drop_self_assignments(stmts: &mut Vec) { + stmts.retain(|s| !matches!(&s.kind, StmtKind::Expr(Expr::Assign(t, v)) if matches!((&**t, &**v), (Expr::Ident(a), Expr::Ident(b)) if a == b))); + for stmt in stmts.iter_mut() { + match &mut stmt.kind { + StmtKind::If { then, els, .. } => { + drop_self_assignments(then); + drop_self_assignments(els); + } + StmtKind::While { body, .. } | StmtKind::Block { body, .. } => drop_self_assignments(body), + StmtKind::Switch { cases, default, .. } => { + cases.iter_mut().for_each(|(_, body)| drop_self_assignments(body)); + drop_self_assignments(default); + } + StmtKind::Try { body, finally } => { + drop_self_assignments(body); + drop_self_assignments(finally); + } + StmtKind::Catch { body, handler, .. } => { + drop_self_assignments(body); + drop_self_assignments(handler); + } + _ => {} + } + } +} + +/// Whether evaluating `e` does nothing at all: a path, or a literal made of them. +fn inert(e: &Expr) -> bool { + match e { + Expr::Array(items) => items.iter().all(inert), + Expr::Object(props) => props.iter().all(|(_, v)| inert(v)), + Expr::As(e, _) | Expr::Located(_, e) => inert(e), + Expr::Lambda(..) | Expr::Arrow(..) => true, + e => e.is_path(), + } +} diff --git a/ts/src/lower/mir/place.rs b/ts/src/lower/mir/place.rs new file mode 100644 index 000000000..59de279bd --- /dev/null +++ b/ts/src/lower/mir/place.rs @@ -0,0 +1,628 @@ +//! Places, operands, and constants. +//! +//! A reference is its referent in JS, so `Deref` reads nothing and a field of a borrowed +//! struct is a member of the same object. A reference assigned once from a place that +//! stays put reads as that place, which is how a `&mut` to a number changes the number. + +use rustc_abi::{FieldIdx, VariantIdx}; +use rustc_middle::mir::interpret::{GlobalId, Scalar}; +use rustc_middle::mir::{self, ConstValue, Local, Operand, Place, PlaceTy, ProjectionElem}; +use rustc_middle::ty::{self, Instance, Ty}; +use rustc_span::Span; + +use super::{Env, Fx, Slot}; +use crate::lower::library::Std; +use crate::lower::num::Int; +use crate::lower::{AdtRepr, ObjectKind}; +use crate::ts::{Expr, Stmt, StmtKind}; + +impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { + /// A local read whole. + pub(super) fn local_expr(&mut self, local: Local, span: Span) -> Expr { + if local.as_u32() == 1 { + match &self.env { + Env::Coroutine => return Expr::This, + Env::Closure(_) => return self.err(span, "a closure's environment read whole"), + Env::Plain | Env::Shim => {} + } + } + match &self.slots[local] { + Slot::Var(name) => Expr::ident(name), + Slot::Unit => Expr::undefined(), + Slot::Alias(Some(target)) => target.clone(), + Slot::Alias(None) => self.err(span, "a reference read before its borrow"), + Slot::Discr(_) => self.err(span, "a discriminant read other than by a switch"), + } + } + + /// The JS expression of `place`, read without a copy. + pub fn place_expr(&mut self, place: Place<'tcx>, span: Span, out: &mut Vec) -> Expr { + let tcx = self.tcx; + let decls = &self.mir.local_decls; + let mut pty = PlaceTy::from_ty(decls[place.local].ty); + let mut projection = place.projection.iter().peekable(); + // A closure's captures, and a coroutine's state, are `_1`'s fields. + let mut e = match (&self.env, place.local.as_u32()) { + (Env::Closure(captures), 1) => { + let captures = captures.clone(); + if matches!(projection.peek(), Some(ProjectionElem::Deref)) { + pty = pty.projection_ty(tcx, ProjectionElem::>::Deref); + projection.next(); + } + match projection.next() { + Some(elem @ ProjectionElem::Field(field, _)) => { + pty = pty.projection_ty(tcx, elem); + match captures.get(field.as_usize()) { + Some(capture) => capture.clone(), + None => return self.err(span, "a capture the closure doesn't have"), + } + } + _ => return self.err(span, "a closure's environment read whole"), + } + } + // A field of an aggregate made once and never changed is its operand: a + // reference, which borrow checking keeps as it was, or a path that stays put. + (_, _) + if let Some(parts) = self.var_parts.get(&place.local) + && let Some(ProjectionElem::Field(field, field_ty)) = projection.peek() + && let Some(part) = parts.get(field.as_usize()) + && (matches!(field_ty.kind(), ty::Ref(..)) || part.is_constant() || self.stable_name(part)) => + { + let part = part.clone(); + let elem = *projection.peek().unwrap(); + pty = pty.projection_ty(tcx, elem); + projection.next(); + part + } + _ => self.local_expr(place.local, span), + }; + // Checked arithmetic's pair is its value, and `.1` is whether it overflowed, which + // it didn't: it would have panicked. + if self.checked.contains(&place.local) + && let Some(ProjectionElem::Field(field, _)) = projection.peek() + { + let field = *field; + projection.next(); + if field.as_u32() == 1 { + return Expr::lit("false"); + } + pty = PlaceTy::from_ty(match decls[place.local].ty.kind() { + ty::Tuple(tys) => tys[0], + _ => unreachable!("a checked pair is a tuple"), + }); + } + for elem in projection { + let ty = pty.ty; + match elem { + ProjectionElem::Deref => { + // A box's pointer is the box in JS: one to an object shares it. + if let ty::RawPtr(pointee, _) = ty.kind() + && !matches!(self.slots[place.local], Slot::Alias(_)) + && !(matches!(self.env, Env::Shim) && place.local.as_u32() == 1) + && !(self.box_pointer(place.local) && self.b.t.object_kind(*pointee).is_some()) + { + return self.err(span, "a raw pointer dereferenced"); + } + } + ProjectionElem::Field(field, _) => { + e = self.field(e, ty, pty.variant_index, field, span); + } + ProjectionElem::Downcast(..) => {} + ProjectionElem::Index(index) => { + let i = self.local_expr(index, span); + e = e.index(i); + } + ProjectionElem::ConstantIndex { + offset, + min_length: _, + from_end, + } => { + e = match from_end { + false => e.index(Expr::lit(offset.to_string())), + true => { + let base = self.path(e, "slice", out); + let len = base.clone().member("length"); + base.index(len.binary("-", Expr::lit(offset.to_string()))) + } + }; + } + ProjectionElem::Subslice { from, to, from_end } => { + let elem_ty = match ty.kind() { + ty::Array(elem, _) | ty::Slice(elem) => *elem, + _ => return self.err(span, format!("a subslice of `{ty}`")), + }; + let method = match elem_ty.kind() { + ty::Uint(ty::UintTy::U8) => "subarray", + _ => "slice", + }; + let base = self.path(e, "slice", out); + let end = match (from_end, ty.kind()) { + (false, _) => Expr::lit(to.to_string()), + (true, ty::Array(_, len)) if let Some(len) = len.try_to_target_usize(tcx) => { + Expr::lit((len - to).to_string()) + } + (true, _) => base.clone().member("length").binary("-", Expr::lit(to.to_string())), + }; + e = base.member(method).call(vec![Expr::lit(from.to_string()), end]); + } + ProjectionElem::OpaqueCast(_) + | ProjectionElem::UnwrapUnsafeBinder(_) + | ProjectionElem::PhantomDeref => {} + } + pty = pty.projection_ty(tcx, elem); + } + e + } + + /// An error if `kind` borrows a subslice of other than bytes mutably: a JS array has + /// no views, so it would change a copy. + pub(super) fn mut_subslice(&mut self, kind: mir::BorrowKind, place: Place<'tcx>, span: Span) -> Option { + if !matches!(kind, mir::BorrowKind::Mut { .. }) { + return None; + } + let elem = place.iter_projections().find_map(|(base, elem)| match elem { + ProjectionElem::Subslice { .. } => match base.ty(&self.mir.local_decls, self.tcx).ty.kind() { + ty::Array(elem, _) | ty::Slice(elem) if !matches!(elem.kind(), ty::Uint(ty::UintTy::U8)) => Some(*elem), + _ => None, + }, + _ => None, + })?; + Some(self.err( + span, + format!("a mutable borrow of a subslice of `{elem}`, which JS would copy, having no views"), + )) + } + + /// A field of `base`, a `ty`, in `variant` if it was downcast to one. + fn field(&mut self, base: Expr, ty: Ty<'tcx>, variant: Option, field: FieldIdx, span: Span) -> Expr { + let tcx = self.tcx; + match ty.kind() { + ty::Tuple(_) => base.index(Expr::lit(field.index().to_string())), + ty::Coroutine(def_id, args) => { + let info = super::coroutine::info(self.b.t, *def_id, args, span); + match variant { + None => base.field(info.upvars[field.index()]), + Some(variant) => { + let saved = info.layout_variant_field(variant, field); + base.field(info.saved[saved]) + } + } + } + ty::Adt(adt, _) => match Std::of(tcx, adt.did()) { + Some(Std::Option | Std::Poll | Std::Box | Std::Pin) => base, + // A variant MIR reads without a test, since the other can't be, is cast to it. + Some(Std::Result) => { + let ok = variant.map_or(0, VariantIdx::index) == 0; + let ty::Adt(_, args) = ty.kind() else { unreachable!() }; + let (this, other) = match ok { + true => (args.type_at(0), args.type_at(1)), + false => (args.type_at(1), args.type_at(0)), + }; + let uninhabited = other.is_never() + || matches!(other.kind(), ty::Adt(adt, _) if adt.is_enum() && adt.variants().is_empty()); + let (flag, name) = match ok { + true => ("true", "value"), + false => ("false", "error"), + }; + let base = match uninhabited { + true => { + let variant = crate::ts::Type::Object(vec![ + ("ok".into(), crate::ts::Type::Builtin(flag)), + (name.into(), self.b.t.ty(this, span)), + ]); + Expr::As(Box::new(base), variant) + } + false => base, + }; + base.member(name) + } + Some(Std::Range | Std::RangeInclusive) => { + base.member(adt.non_enum_variant().fields[field].name.as_str()) + } + Some(Std::Context | Std::NonNull) => base, + Some(Std::ControlFlow) => super::call::control_flow_field(self, base, ty, variant, span), + _ if transparent(self.b.t, ty) => base, + None if adt.did().is_local() => { + let info = self.b.t.adt(*adt, span); + match info.repr { + AdtRepr::Struct { fields, .. } => base.field(fields[field.index()]), + AdtRepr::Union | AdtRepr::Tags => { + let variant = variant.unwrap_or(rustc_abi::FIRST_VARIANT); + let def = &adt.variant(variant).fields[field]; + base.member(crate::lower::field_name(def.name.as_str(), field.index())) + } + } + } + _ => self.err(span, format!("a field of `{ty}`")), + }, + _ => self.err(span, format!("a field of `{ty}`")), + } + } + + /// An operand's value, copied if it is a `Copy` aggregate read from a place that keeps it. + pub fn operand(&mut self, op: &Operand<'tcx>, span: Span, out: &mut Vec) -> Expr { + match op { + // JS shares objects where Rust copies, so a `Copy` aggregate is copied where + // either side could change in place: into a local changed in place, or out of a + // place that could change. + Operand::Copy(place) => { + let ty = place.ty(&self.mir.local_decls, self.tcx).ty; + if changes_captures(ty) { + return self.err(span, "a copy of a closure that changes what it captured"); + } + let value = self.place_expr(*place, span, out); + match self.force_share || self.into_frozen && self.frozen(*place) { + true => value, + false => self.b.copied(value, ty), + } + } + Operand::Move(place) => { + let ty = place.ty(&self.mir.local_decls, self.tcx).ty; + let value = self.place_expr(*place, span, out); + match self.into_frozen { + true => value, + false => self.b.copied(value, ty), + } + } + Operand::Constant(c) => self.constant(c, span), + Operand::RuntimeChecks(_) => Expr::lit("false"), + } + } + + /// Whether `e` names a local nothing assigns again or changes in place, so reading it + /// later reads what it held earlier. + pub(super) fn stable_name(&self, e: &Expr) -> bool { + let Expr::Ident(name) = e else { + return false; + }; + self.local_named(name).is_some_and(|local| { + let assigned = self.defs[local] + u32::from(self.is_arg(local)); + assigned <= 1 && !self.mutated[local] + }) + } + + /// Whether `place` is part of a local's own value that nothing changes in place. + pub(super) fn frozen(&self, place: Place<'tcx>) -> bool { + !self.mutated[place.local] && place.projection.iter().all(|e| !matches!(e, ProjectionElem::Deref)) + } + + /// The statements that assign `value` to `place`, which has projections. + pub(super) fn store(&mut self, place: Place<'tcx>, value: Expr, span: Span) -> Vec { + let mut out = Vec::new(); + // `*r = v` replaces the object `r` points at in place, which other references and + // captures share. An alias of a borrowed place, or of a reference to no object, + // assigns the place. + if let [ProjectionElem::Deref] = place.projection.as_slice() + && !self.alias_of_place(place.local) + && !(place.local.as_u32() == 1 && matches!(self.env, Env::Closure(_))) + { + let pointee = place.ty(&self.mir.local_decls, self.tcx).ty; + let alias = matches!(self.slots[place.local], Slot::Alias(_)); + let target = self.local_expr(place.local, span); + let e = match self.b.t.object_kind(pointee) { + Some(ObjectKind::Class) if !shared(self, pointee) => { + Some(Expr::ident("Object").member("assign").call(vec![target, value.clone()])) + } + Some(ObjectKind::Bytes) => Some(target.member("set").call(vec![value.clone()])), + _ => None, + }; + match (e, alias) { + (Some(e), _) => { + out.push(StmtKind::Expr(e).at(None)); + return out; + } + (None, true) => {} + (None, false) => { + return vec![ + StmtKind::Expr(self.err(span, format!("an assignment through a reference to `{pointee}`"))) + .at(None), + ]; + } + } + } + let target = self.place_expr(place, span, &mut out); + out.push(StmtKind::Expr(target.assign(value)).at(self.b.t.loc(span))); + out + } + + /// Whether `local` is an alias of a borrowed place, as `&mut x` or `&mut v[i]`, rather + /// than a copy of a reference or a reborrow of what one points at. + pub(super) fn alias_of_place(&self, local: Local) -> bool { + matches!(self.slots[local], Slot::Alias(_)) + && match self.def_rvalue(local) { + Some(mir::Rvalue::Ref(_, _, p)) => !matches!(p.projection.last(), Some(ProjectionElem::Deref)), + Some(_) => false, + // An element `index_mut` borrows. + None => true, + } + } + + /// `SetDiscriminant`: a coroutine's state. + pub(super) fn set_discriminant(&mut self, place: Place<'tcx>, variant: VariantIdx, span: Span) -> Option { + let ty = place.ty(&self.mir.local_decls, self.tcx).ty; + let ty::Coroutine(def_id, args) = ty.kind() else { + self.err(span, format!("setting the discriminant of `{ty}`")); + return None; + }; + let info = super::coroutine::info(self.b.t, *def_id, args, span); + let mut out = Vec::new(); + let target = self.place_expr(place, span, &mut out); + Some(StmtKind::Expr(target.field(info.state).assign(Expr::lit(variant.as_u32().to_string()))).at(None)) + } + + /// The test that `scrut`, a `ty`, has the variant whose discriminant is `value`. + pub(super) fn variant_test(&mut self, scrut: &Expr, ty: Ty<'tcx>, value: u128, span: Span) -> Expr { + let tcx = self.tcx; + match ty.kind() { + ty::Coroutine(def_id, args) => { + let info = super::coroutine::info(self.b.t, *def_id, args, span); + scrut + .clone() + .field(info.state) + .binary("===", Expr::lit(value.to_string())) + } + ty::Adt(adt, _) if adt.is_enum() => { + let Some((index, _)) = adt.discriminants(tcx).find(|(_, d)| d.val == value) else { + return self.err(span, format!("no variant of `{ty}` has discriminant {value}")); + }; + match Std::of(tcx, adt.did()) { + Some(Std::Option) if matches!(scrut, Expr::Ident(n) if self.next_names.contains(n)) => { + match index.index() { + 0 => scrut.clone().binary("===", Expr::Rt("DONE")), + _ => scrut.clone().binary("!==", Expr::Rt("DONE")), + } + } + Some(Std::Option) => match index.index() { + 0 => scrut.clone().binary("===", Expr::undefined()), + _ => scrut.clone().binary("!==", Expr::undefined()), + }, + Some(Std::Poll) => match index.index() { + 0 => scrut.clone().binary("!==", Expr::Rt("Pending")), + _ => scrut.clone().binary("===", Expr::Rt("Pending")), + }, + Some(Std::Result) => match index.index() { + 0 => scrut.clone().member("ok"), + _ => scrut.clone().member("ok").not(), + }, + None if adt.did().is_local() => { + let info = self.b.t.adt(*adt, span); + let name = Expr::Str(adt.variant(index).name.to_string()); + match info.repr { + AdtRepr::Tags => scrut.clone().binary("===", name), + AdtRepr::Union => scrut.clone().member("tag").binary("===", name), + AdtRepr::Struct { .. } => self.err(span, format!("a switch on `{ty}`")), + } + } + Some(Std::ControlFlow) => super::call::control_flow_test(self, scrut, ty, index, span), + _ => self.err(span, format!("a switch on `{ty}`")), + } + } + _ => self.err(span, format!("a switch on the discriminant of `{ty}`")), + } + } + + /// A constant operand's value. + pub fn constant(&mut self, c: &mir::ConstOperand<'tcx>, span: Span) -> Expr { + let tcx = self.tcx; + let env = self.b.t.typing_env_pub(); + match c.const_ { + mir::Const::Ty(ty, ct) => match ct.kind() { + ty::ConstKind::Value(v) => self.b.value(v.ty, v.valtree, span), + _ => self.err(span, format!("a constant of type `{ty}` that is no value")), + }, + mir::Const::Unevaluated(uv, ty) => { + let instance = match Instance::try_resolve(tcx, env, uv.def, uv.args) { + Ok(Some(instance)) => instance, + _ => return self.err(span, "a constant that does not resolve"), + }; + let cid = GlobalId { + instance, + promoted: uv.promoted, + }; + match tcx.const_eval_global_id_for_typeck(env, cid, span) { + Ok(Ok(valtree)) => self.b.value(ty, valtree, span), + Ok(Err(_)) => self.err(span, format!("a constant of type `{ty}`, which has no value tree")), + Err(_) => self.err(span, "a constant that failed to evaluate"), + } + } + mir::Const::Val(val, ty) => self.const_value(val, ty, span), + } + } + + fn const_value(&mut self, val: ConstValue, ty: Ty<'tcx>, span: Span) -> Expr { + let tcx = self.tcx; + // An aggregate, field by field. + if let ty::Adt(..) | ty::Tuple(_) | ty::Array(..) = ty.kind() + && !matches!(ty.kind(), ty::Tuple(tys) if tys.is_empty()) + && !matches!(ty.kind(), ty::Adt(adt, _) if adt.is_struct() && adt.did().is_local() && adt.all_fields().next().is_none()) + { + let Some(d) = tcx.try_destructure_mir_constant_for_user_output(val, ty) else { + return self.err(span, format!("a constant of type `{ty}` that can't be taken apart")); + }; + let values: Vec = d.fields.iter().map(|(v, t)| self.const_value(*v, *t, span)).collect(); + return match ty.kind() { + ty::Tuple(_) => { + let ts_ty = self.b.t.ty(ty, span); + Expr::As(Box::new(Expr::Array(values)), ts_ty) + } + ty::Array(elem, _) => match elem.kind() { + ty::Uint(ty::UintTy::U8) => Expr::ident("Uint8Array").member("of").call(values), + _ => Expr::Array(values), + }, + ty::Adt(adt, args) => self.adt( + adt.did(), + d.variant.unwrap_or(rustc_abi::FIRST_VARIANT), + args, + values, + ty, + span, + ), + _ => unreachable!(), + }; + } + match val { + ConstValue::ZeroSized => match *ty.kind() { + ty::FnDef(def_id, args) => self.b.fn_value(def_id, args.skip_binder(), span), + ty::Tuple(tys) if tys.is_empty() => Expr::undefined(), + ty::Adt(adt, _) if adt.is_struct() && adt.did().is_local() => { + let info = self.b.t.adt(adt, span); + self.b.t.construct(info.sym, &crate::ts::Ctor::Unit, &[], Vec::new()) + } + ty::Array(..) => Expr::Array(Vec::new()), + ty::Closure(def_id, args) => self.bare_closure(def_id, args, span), + ty::Adt(adt, _) if Std::of(tcx, adt.did()) == Some(Std::RangeFull) => Expr::undefined(), + _ => self.err(span, format!("a zero-sized constant of type `{ty}`")), + }, + ConstValue::Scalar(Scalar::Int(int)) => match ty.kind() { + ty::Bool => Expr::lit((int.to_bits_unchecked() != 0).to_string()), + _ => match Int::of(ty) { + Some(i) => { + let size = rustc_abi::Size::from_bits(i.bits); + let value = match i.signed { + true => int.to_int(size), + false => int.to_uint(size) as i128, + }; + i.lit(value) + } + None => self.err(span, format!("a scalar constant of type `{ty}`")), + }, + }, + ConstValue::Scalar(Scalar::Ptr(ptr, _)) => { + // `&[u8; N]`, such as a byte string. + let ty::Ref(_, inner, _) = ty.kind() else { + return self.err(span, format!("a pointer constant of type `{ty}`")); + }; + let ty::Array(elem, len) = inner.kind() else { + return self.err(span, format!("a constant of type `{ty}`")); + }; + let (Some(len), ty::Uint(ty::UintTy::U8)) = (len.try_to_target_usize(tcx), elem.kind()) else { + return self.err(span, format!("a constant of type `{ty}`")); + }; + let (prov, offset) = ptr.prov_and_relative_offset(); + let alloc = tcx.global_alloc(prov.alloc_id()).unwrap_memory(); + let start = offset.bytes() as usize; + let bytes = alloc + .inner() + .inspect_with_uninit_and_ptr_outside_interpreter(start..start + len as usize); + let items = bytes.iter().map(|b| Expr::lit(b.to_string())).collect(); + Expr::ident("Uint8Array").member("of").call(items) + } + ConstValue::Slice { .. } => { + let Some(bytes) = val.try_get_slice_bytes_for_diagnostics(tcx) else { + return self.err(span, "a slice constant out of bounds"); + }; + match ty.peel_refs().kind() { + ty::Str => match std::str::from_utf8(bytes) { + Ok(s) => Expr::Str(s.to_string()), + Err(_) => self.err(span, "a string constant that is not UTF-8"), + }, + ty::Slice(elem) if matches!(elem.kind(), ty::Uint(ty::UintTy::U8)) => { + let items = bytes.iter().map(|b| Expr::lit(b.to_string())).collect(); + Expr::ident("Uint8Array").member("of").call(items) + } + _ => self.err(span, format!("a slice constant of type `{ty}`")), + } + } + ConstValue::Indirect { .. } => self.err(span, format!("a constant of type `{ty}` held in memory")), + } + } + + /// The constant an operand is, through a temporary that holds only it, which is + /// then no longer pending. + pub(super) fn const_operand(&mut self, op: &Operand<'tcx>) -> Option>> { + match op { + Operand::Constant(c) => Some(c.clone()), + Operand::Copy(place) | Operand::Move(place) => { + let local = place.as_local()?; + if self.defs[local] != 1 { + return None; + } + let Some(mir::Rvalue::Use(Operand::Constant(c), _)) = self.def_rvalue(local) else { + return None; + }; + let c = c.clone(); + if let Some(name) = self.var(local) { + let name = name.to_string(); + self.pending.retain(|p| p.name != name); + } + Some(c) + } + _ => None, + } + } + + /// The bytes of a byte string constant, `&[u8; N]`. + pub(super) fn const_bytes(&mut self, op: &Operand<'tcx>) -> Option> { + let tcx = self.tcx; + let c = self.const_operand(op)?; + let mir::Const::Val(ConstValue::Scalar(Scalar::Ptr(ptr, _)), ty) = c.const_ else { + return None; + }; + let ty::Ref(_, inner, _) = ty.kind() else { + return None; + }; + let ty::Array(_, len) = inner.kind() else { + return None; + }; + let len = len.try_to_target_usize(tcx)? as usize; + let (prov, offset) = ptr.prov_and_relative_offset(); + let alloc = tcx.global_alloc(prov.alloc_id()).unwrap_memory(); + let start = offset.bytes() as usize; + Some( + alloc + .inner() + .inspect_with_uninit_and_ptr_outside_interpreter(start..start + len) + .to_vec(), + ) + } +} + +/// Whether `ty` is a struct of one field that JS represents as that field: a wrapper +/// around a pointer, such as the `ResumeTy` and `NonNull` an async body passes its +/// context in. +pub fn transparent<'tcx>(t: &crate::lower::Translator<'_, 'tcx>, ty: Ty<'tcx>) -> bool { + let ty::Adt(adt, _) = ty.kind() else { + return false; + }; + let path = crate::lower::library::std_path(t.tcx, adt.did()); + let path: Vec<&str> = path.iter().map(String::as_str).collect(); + matches!( + path.as_slice(), + ["future", "ResumeTy"] | ["ptr", "non_null", "NonNull"] | ["future", _, "ResumeTy"] + ) +} + +/// Whether `ty` holds an `FnMut` closure by value: a copy of its arrow function shares +/// the variables it captured, which in Rust each copy has its own of. +fn changes_captures(ty: Ty<'_>) -> bool { + match ty.kind() { + ty::Closure(_, args) => { + let closure = args.as_closure(); + closure.kind() == ty::ClosureKind::FnMut || closure.upvar_tys().iter().any(changes_captures) + } + ty::Tuple(tys) => tys.iter().any(changes_captures), + ty::Array(t, _) => changes_captures(*t), + ty::Adt(_, args) => args.types().any(changes_captures), + _ => false, + } +} + +/// Whether the JS object of a `ty` is shared or private, so that copying fields into it +/// would change other handles, or nothing. +fn shared<'tcx>(fx: &Fx<'_, '_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { + let tcx = fx.tcx; + let mut erased = ty; + while let ty::Adt(adt, args) = erased.kind() + && matches!(Std::of(tcx, adt.did()), Some(Std::Pin | Std::Box)) + { + erased = args.type_at(0); + } + match erased.kind() { + ty::Adt(adt, _) => matches!( + Std::of(tcx, adt.did()), + Some(Std::Arc | Std::Waker | Std::Context | Std::MutexGuard) + ), + ty::Ref(..) | ty::Coroutine(..) => true, + _ => false, + } +} diff --git a/ts/src/lower/mir/rvalue.rs b/ts/src/lower/mir/rvalue.rs new file mode 100644 index 000000000..2a9901512 --- /dev/null +++ b/ts/src/lower/mir/rvalue.rs @@ -0,0 +1,493 @@ +//! Rvalues: arithmetic, casts, aggregates, and closures. + +use rustc_abi::{FieldIdx, VariantIdx}; +use rustc_index::IndexVec; +use rustc_middle::mir::{self, AggregateKind, BinOp, CastKind, Operand, Place, Rvalue, UnOp}; +use rustc_middle::ty::adjustment::PointerCoercion; +use rustc_middle::ty::{self, Instance, Ty}; +use rustc_span::Span; + +use super::{Env, Fx, Slot}; +use crate::lower::library::Std; +use crate::lower::num::{self, Int}; +use crate::lower::{AdtRepr, field_name}; +use crate::ts::{Expr, Key, Stmt, StmtKind}; + +impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { + /// The value of `rvalue`, assigned to `dest`, or `None` if it has none to assign. + pub(super) fn rvalue( + &mut self, + rvalue: &Rvalue<'tcx>, + dest: Place<'tcx>, + span: Span, + out: &mut Vec, + ) -> Option { + let tcx = self.tcx; + let ty = rvalue.ty(&self.mir.local_decls, tcx); + Some(match rvalue { + Rvalue::Use(op, _) => self.operand(op, span, out), + Rvalue::CopyForDeref(place) => self.place_expr(*place, span, out), + Rvalue::Repeat(op, count) => self.repeat(op, *count, span, out), + Rvalue::Ref(_, kind, place) => { + if let Some(e) = self.mut_subslice(*kind, *place, span) { + return Some(e); + } + if let mir::BorrowKind::Mut { .. } = kind { + let pointee = place.ty(&self.mir.local_decls, tcx).ty; + if self.b.t.object_kind(pointee).is_none() + && !matches!(pointee.kind(), ty::Ref(_, _, rustc_ast::Mutability::Mut)) + { + return Some(self.err( + span, + format!("a mutable reference to `{pointee}`, which is no object it can change in place"), + )); + } + } + self.place_expr(*place, span, out) + } + Rvalue::RawPtr(..) => self.err(span, "raw pointers are not supported"), + Rvalue::Reborrow(..) => self.err(span, "reborrowing a type by its `Reborrow` impl is not supported"), + Rvalue::ThreadLocalRef(_) => self.err(span, "thread locals are not supported"), + Rvalue::WrapUnsafeBinder(..) => self.err(span, "unsafe binders are not supported"), + Rvalue::Cast(kind, op, to) => self.cast(*kind, op, *to, span, out), + Rvalue::BinaryOp(op, ops) => { + let (l, r) = &**ops; + let lty = l.ty(&self.mir.local_decls, tcx); + let rty = r.ty(&self.mir.local_decls, tcx); + let op = op.overflowing_to_wrapping().unwrap_or(*op); + let op = match op { + BinOp::AddUnchecked => BinOp::Add, + BinOp::SubUnchecked => BinOp::Sub, + BinOp::MulUnchecked => BinOp::Mul, + BinOp::ShlUnchecked => BinOp::Shl, + BinOp::ShrUnchecked => BinOp::Shr, + op => op, + }; + if matches!(op, BinOp::Cmp | BinOp::Offset) { + return Some(self.err(span, format!("`{op:?}` is not supported"))); + } + let lv = self.operand(l, span, out); + let rv = self.operand(r, span, out); + let l = num::Operand { + expr: lv, + value: self.const_int(l), + }; + let r = num::Operand { + expr: rv, + value: self.const_int(r), + }; + num::binary(self.b, op, l, r, lty, rty, span) + } + Rvalue::UnaryOp(op, operand) => { + let aty = operand.ty(&self.mir.local_decls, tcx); + let value = self.operand(operand, span, out); + match op { + UnOp::Not | UnOp::Neg => num::unary(self.b, *op, value, aty, span), + UnOp::PtrMetadata => match aty.builtin_deref(true).map(|t| t.kind()) { + Some(ty::Str) => Expr::Rt("string").member("len").call(vec![value]), + Some(ty::Slice(_)) => value.member("length"), + _ => self.err(span, format!("the metadata of `{aty}`")), + }, + } + } + Rvalue::Discriminant(place) => { + let pty = place.ty(&self.mir.local_decls, tcx).ty; + let scrut = self.place_expr(*place, span, out); + self.discriminant(scrut, pty, span, out) + } + Rvalue::Aggregate(kind, ops) => self.aggregate(kind, ops, ty, dest, span, out)?, + }) + } + + /// The integer an operand has, if it is a constant one. + pub(super) fn const_int(&self, op: &Operand<'tcx>) -> Option { + let Operand::Constant(c) = op else { + return None; + }; + let mir::Const::Val(mir::ConstValue::Scalar(rustc_middle::mir::interpret::Scalar::Int(int)), ty) = c.const_ + else { + return None; + }; + let i = Int::of(ty)?; + let size = rustc_abi::Size::from_bits(i.bits); + Some(match i.signed { + true => int.to_int(size), + false => int.to_uint(size) as i128, + }) + } + + fn repeat(&mut self, op: &Operand<'tcx>, count: ty::Const<'tcx>, span: Span, out: &mut Vec) -> Expr { + let tcx = self.tcx; + let Some(n) = count.try_to_target_usize(tcx) else { + return self.err(span, "an array length that is not a constant"); + }; + let elem_ty = op.ty(&self.mir.local_decls, tcx); + if self.b.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(op, span, out); + match elem_ty.kind() { + ty::Uint(ty::UintTy::U8) => { + let array = Expr::New(Box::new(Expr::ident("Uint8Array")), vec![Expr::lit(n.to_string())]); + match &v { + Expr::Lit(text) if text == "0" => array, + _ => array.member("fill").call(vec![v]), + } + } + _ => { + let v = self.path(v, "item", out); + let item = self.b.copied(v, elem_ty); + let item_ty = self.b.t.ty(elem_ty, span); + Expr::ident("Array").member("from").call(vec![ + Expr::Object(vec![(Key::Name("length".into()), Expr::lit(n.to_string()))]), + Expr::Arrow(Vec::new(), item_ty, vec![StmtKind::Return(Some(item.typed())).at(None)]), + ]) + } + } + } + + fn cast(&mut self, kind: CastKind, op: &Operand<'tcx>, to: Ty<'tcx>, span: Span, out: &mut Vec) -> Expr { + let tcx = self.tcx; + let from = op.ty(&self.mir.local_decls, tcx); + match kind { + CastKind::IntToInt => { + let value = self.operand(op, span, out); + let operand = num::Operand { + expr: value, + value: self.const_int(op), + }; + num::cast(self.b, operand, from, to, span) + } + CastKind::PointerCoercion(coercion, _) => match coercion { + PointerCoercion::Unsize if let Some(tr) = self.b.fmt_object(to) => { + let value = self.operand(op, span, out); + let value = self.path(value, "arg", out); + self.b.fmt_value(tr, from, value, span, out) + } + PointerCoercion::Unsize if self.b.trait_object(to) => self.err(span, format!("a coercion to `{to}`")), + PointerCoercion::Unsize | PointerCoercion::ReifyFnPointer(_) | PointerCoercion::ClosureFnPointer(_) => { + self.operand(op, span, out) + } + _ => self.err(span, format!("unsupported pointer coercion {coercion:?}")), + }, + // A box's pointer is its contents in JS. + CastKind::BoxDerefTransmute => self.operand(op, span, out), + // An async body's context passes through `ResumeTy` and `NonNull`. + CastKind::Transmute if context_like(self, from) && context_like(self, to) => self.operand(op, span, out), + _ => self.err(span, format!("a `{kind:?}` cast from `{from}` to `{to}`")), + } + } + + /// A discriminant as its number. + fn discriminant(&mut self, scrut: Expr, ty: Ty<'tcx>, span: Span, out: &mut Vec) -> Expr { + match ty.kind() { + ty::Coroutine(def_id, args) => { + let info = super::coroutine::info(self.b.t, *def_id, args, span); + scrut.field(info.state) + } + ty::Adt(adt, _) if adt.is_enum() => { + let tcx = self.tcx; + let discrs: Vec<(VariantIdx, u128)> = adt.discriminants(tcx).map(|(i, d)| (i, d.val)).collect(); + // Each discriminant in the enum's own discriminant type, as `as` reads it. + let int = Int::of(ty.discriminant_ty(tcx)).expect("an enum's discriminant is an integer"); + let discrs: Vec<(VariantIdx, u128)> = discrs + .into_iter() + .map(|(i, d)| (i, int.fold(d as i128) as u128)) + .collect(); + match Std::of(tcx, adt.did()) { + Some(Std::Option | Std::Poll | Std::Result) => { + let scrut = self.path(scrut, "scrutinee", out); + let first = self.variant_test(&scrut, ty, discrs[0].1, span); + Expr::Cond( + Box::new(first), + Box::new(int.lit(discrs[0].1 as i128)), + Box::new(int.lit(discrs[1].1 as i128)), + ) + } + None if adt.did().is_local() => { + let info = self.b.t.adt(*adt, span); + let table = discrs + .iter() + .map(|(i, d)| (Key::Name(adt.variant(*i).name.to_string()), int.lit(*d as i128))) + .collect(); + let key = match info.repr { + AdtRepr::Tags => scrut, + _ => scrut.member("tag"), + }; + Expr::Index(Box::new(Expr::Object(table)), Box::new(key)) + } + _ => self.err(span, format!("the discriminant of `{ty}`")), + } + } + _ => self.err(span, format!("the discriminant of `{ty}`")), + } + } + + fn aggregate( + &mut self, + kind: &AggregateKind<'tcx>, + ops: &IndexVec>, + ty: Ty<'tcx>, + dest: Place<'tcx>, + span: Span, + out: &mut Vec, + ) -> Option { + match kind { + AggregateKind::Closure(def_id, args) => return Some(self.closure(*def_id, args, ops, span, out)), + AggregateKind::Coroutine(def_id, args) => { + return Some(super::coroutine::construct(self, *def_id, args, ops, span, out)); + } + AggregateKind::CoroutineClosure(..) => return Some(self.err(span, "async closures are not supported")), + AggregateKind::RawPtr(..) => return Some(self.err(span, "raw pointers are not supported")), + _ => {} + } + let mut values = Vec::with_capacity(ops.len()); + for op in ops.iter() { + let v = self.operand(op, span, out); + values.push(v); + } + let _ = dest; + self.parts = Some(values.clone()); + Some(match kind { + AggregateKind::Tuple if values.is_empty() => Expr::undefined(), + AggregateKind::Tuple => { + let ts_ty = self.b.t.ty(ty, span); + Expr::As(Box::new(Expr::Array(values)), ts_ty) + } + AggregateKind::Array(elem) => match elem.kind() { + ty::Uint(ty::UintTy::U8) => Expr::ident("Uint8Array").member("of").call(values), + _ => Expr::Array(values), + }, + AggregateKind::Adt(def_id, variant, args, _, active) => { + if active.is_some() { + return Some(self.err(span, "unions are not supported")); + } + self.adt(*def_id, *variant, args, values, ty, span) + } + _ => unreachable!(), + }) + } + + pub(super) fn adt( + &mut self, + def_id: rustc_hir::def_id::DefId, + variant: VariantIdx, + args: ty::GenericArgsRef<'tcx>, + mut values: Vec, + ty: Ty<'tcx>, + span: Span, + ) -> Expr { + let tcx = self.tcx; + let adt = tcx.adt_def(def_id); + let def = adt.variant(variant); + if let Some(std) = Std::of(tcx, def_id) { + return match (std, variant.index()) { + (Std::Option, 0) => Expr::undefined(), + (Std::Poll, 1) => Expr::Rt("Pending"), + (Std::Option | Std::Poll, _) => values.pop().unwrap(), + (Std::Result, 0) => Expr::rt("Ok", values), + (Std::Result, _) => Expr::rt("Err", values), + (Std::FmtError, _) => Expr::New(Box::new(Expr::Rt("FmtError")), Vec::new()), + // `ResumeTy`, an async body's context. + (Std::Context, _) => values.pop().unwrap_or_else(Expr::undefined), + (Std::RangeTo, _) => Expr::Object(vec![(Key::Name("end".into()), values.pop().unwrap())]), + (Std::RangeFrom, _) => Expr::Object(vec![(Key::Name("start".into()), values.pop().unwrap())]), + (Std::RangeFull, _) => Expr::undefined(), + (Std::ControlFlow, _) => super::call::control_flow(self, ty, variant, values, span), + (Std::Range, _) => { + let ty::Adt(_, gargs) = ty.kind() else { + unreachable!("a range is a struct") + }; + if let Some(int) = Int::of(gargs.type_at(0)) + && int.big() + { + values.push(Expr::Rt(int.rt())); + } + Expr::New(Box::new(Expr::Rt("Range")), values) + } + (Std::AssertKind, _) => match def.name.as_str() { + "Eq" => Expr::Str("==".into()), + "Ne" => Expr::Str("!=".into()), + _ => self.err(span, format!("constructing `{ty}`")), + }, + _ => self.err(span, format!("constructing `{ty}`")), + }; + } + if super::place::transparent(self.b.t, ty) { + return values.pop().unwrap_or_else(Expr::undefined); + } + match crate::lower::library::std_path(tcx, def_id) + .iter() + .map(String::as_str) + .collect::>() + .as_slice() + { + ["marker", "PhantomData"] => return Expr::undefined(), + ["iter", "sources", "empty", "Empty"] => return Expr::Rt("iter").member("empty").call(Vec::new()), + _ => {} + } + if !def_id.is_local() { + return self.err(span, format!("constructing `{ty}` from another crate")); + } + let info = self.b.t.adt(adt, span); + match info.repr { + AdtRepr::Struct { ctor, fields } => { + let field_arg = |i: usize, value: Expr| { + let generic = tcx + .type_of(def.fields[FieldIdx::from_usize(i)].did) + .instantiate_identity() + .skip_norm_wip(); + match ty::TypeVisitableExt::has_param(&generic) { + true => value, + false => value.typed(), + } + }; + let values = values.into_iter().enumerate().map(|(i, v)| field_arg(i, v)).collect(); + self.b.t.construct(info.sym, &ctor, &fields, values) + } + AdtRepr::Tags => { + let ts_ty = self.b.t.ty(ty, span); + Expr::As(Box::new(Expr::Str(def.name.to_string())), ts_ty) + } + AdtRepr::Union => { + let mut props = vec![(Key::Name("tag".into()), Expr::Str(def.name.to_string()))]; + for (i, value) in values.into_iter().enumerate() { + let name = field_name(def.fields[FieldIdx::from_usize(i)].name.as_str(), i); + props.push((Key::Name(name), value)); + } + let _ = args; + let ts_ty = self.b.t.ty(ty, span); + Expr::As(Box::new(Expr::Object(props)), ts_ty) + } + } + } + + /// A closure that captures nothing, as an arrow function. + pub(super) fn bare_closure( + &mut self, + def_id: rustc_hir::def_id::DefId, + args: ty::GenericArgsRef<'tcx>, + span: Span, + ) -> Expr { + self.closure(def_id, args, &IndexVec::new(), span, &mut Vec::new()) + } + + /// A closure, as an arrow function over what it captured. + fn closure( + &mut self, + def_id: rustc_hir::def_id::DefId, + args: ty::GenericArgsRef<'tcx>, + ops: &IndexVec>, + span: Span, + out: &mut Vec, + ) -> Expr { + let tcx = self.tcx; + // A capture by reference reads the place it borrows; one by value is the value as + // it was when the closure was made, a reference held in a variable included. + let mut captures = Vec::with_capacity(ops.len()); + for op in ops.iter() { + let ty = op.ty(&self.mir.local_decls, tcx); + let value = self.operand(op, span, out); + let in_var = op + .place() + .and_then(|p| p.as_local()) + .is_some_and(|l| matches!(self.slots[l], Slot::Var(_))); + let snapshot = match ty.kind() { + ty::Ref(..) => in_var && !self.stable_name(&value), + _ => true, + }; + let value = match snapshot && !value.is_constant() { + false => value, + true => { + let name = self.b.fresh("captured"); + let stmt = StmtKind::decl(false, name.clone(), None, Some(value)).at(None); + self.emit(vec![stmt], None, false, out); + Expr::ident(name) + } + }; + captures.push(value); + } + let instance = Instance::new_raw(def_id, args); + let Some(mir) = super::instance_mir(self.b.t, instance, span) else { + return Expr::undefined(); + }; + let sig = tcx.instantiate_bound_regions_with_erased(args.as_closure().sig()); + let ret = self.b.t.ret_ty(sig.output(), span); + let mut inner = Fx::new(&mut *self.b, &mir, Env::Closure(captures), span); + let params = inner.params(); + let body = inner.lower(); + Expr::Arrow(params, ret, body) + } +} + +/// Whether a `ty`'s JS value is the task's context: `&mut Context`, or the `ResumeTy` and +/// `NonNull` an async body passes it in. +fn context_like<'tcx>(fx: &Fx<'_, '_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { + let tcx = fx.tcx; + match ty.kind() { + ty::Ref(_, inner, _) => context_like(fx, *inner), + ty::Adt(adt, args) => match Std::of(tcx, adt.did()) { + Some(Std::Context) => true, + _ if super::place::transparent(fx.b.t, ty) => args.types().next().is_none_or(|t| context_like(fx, t)), + _ => false, + }, + _ => false, + } +} + +/// Whether evaluating `rvalue` can do or throw anything. +pub fn effect<'tcx>(rvalue: &Rvalue<'tcx>, fx: &Fx<'_, '_, '_, 'tcx>) -> bool { + match rvalue { + Rvalue::BinaryOp(op, ops) => { + let ty = ops.0.ty(&fx.mir.local_decls, fx.tcx); + Int::of(ty.peel_refs()).is_some() + && !matches!( + op, + BinOp::Eq + | BinOp::Ne + | BinOp::Lt + | BinOp::Le + | BinOp::Gt + | BinOp::Ge + | BinOp::BitAnd + | BinOp::BitOr + | BinOp::BitXor + ) + } + Rvalue::UnaryOp(UnOp::Neg, _) => true, + _ => false, + } +} + +/// Whether reading `p` reads memory something else could change. +fn place_memory<'tcx>(p: &Place<'tcx>, fx: &Fx<'_, '_, '_, 'tcx>) -> bool { + !p.projection.is_empty() + || fx.borrowed[p.local] + || matches!(fx.slots[p.local], Slot::Alias(_)) + || p.local.as_u32() == 1 && !matches!(fx.env, Env::Plain | Env::Shim) +} + +/// Whether reading `op` reads memory something could change, a local changed in place +/// included: a copy of one held past a write to its field would see the write. +pub fn operand_memory<'tcx>(op: &Operand<'tcx>, fx: &Fx<'_, '_, '_, 'tcx>) -> bool { + match op { + Operand::Copy(p) | Operand::Move(p) => place_memory(p, fx) || fx.mutated[p.local], + _ => false, + } +} + +/// Whether evaluating `rvalue` reads memory that something else could change. +pub fn memory<'tcx>(rvalue: &Rvalue<'tcx>, fx: &Fx<'_, '_, '_, 'tcx>) -> bool { + let place = |p: &Place<'tcx>| place_memory(p, fx); + let operand = |op: &Operand<'tcx>| operand_memory(op, fx); + match rvalue { + Rvalue::Use(op, _) | Rvalue::Repeat(op, _) | Rvalue::Cast(_, op, _) | Rvalue::UnaryOp(_, op) => operand(op), + Rvalue::BinaryOp(_, ops) => operand(&ops.0) || operand(&ops.1), + Rvalue::Ref(_, _, p) | Rvalue::Discriminant(p) | Rvalue::CopyForDeref(p) | Rvalue::RawPtr(_, p) => place(p), + Rvalue::Aggregate(_, ops) => ops.iter().any(operand), + Rvalue::ThreadLocalRef(_) | Rvalue::WrapUnsafeBinder(..) | Rvalue::Reborrow(..) => true, + } +} diff --git a/ts/src/lower/mir/unwind.rs b/ts/src/lower/mir/unwind.rs new file mode 100644 index 000000000..4037c555a --- /dev/null +++ b/ts/src/lower/mir/unwind.rs @@ -0,0 +1,235 @@ +//! Unwinding: what a panic drops on its way out, as `-C panic=unwind` MIR has it. +//! +//! Each call, assert, and drop that can panic names the cleanup block it unwinds to, +//! which drops what is live there and resumes unwinding. A function with any cleanup +//! that does something JS can see holds the cleanup of the code running now in a +//! variable, set before each terminator that can panic where it changes, and catches +//! what is thrown: the catch runs that cleanup, then throws it on. + +use rustc_data_structures::fx::FxHashSet; +use rustc_middle::mir::{self, BasicBlock, StatementKind, TerminatorKind, UnwindAction}; + +use super::Fx; +use super::cfg::{self, Exit}; +use crate::ts::{Expr, Stmt, StmtKind, Type}; + +/// A function's unwinding. +pub struct Unwind { + /// The variable holding the cleanup to run: a block's index plus one, or 0 for none. + pub var: String, + /// What it holds now, where the code emitted knows. + pub known: Option, + /// The cleanup blocks that do something JS can see, from where unwinding enters them. + visible: FxHashSet, + /// The caught exception's name. + pub caught: String, +} + +impl<'tcx> Fx<'_, '_, '_, 'tcx> { + /// Find the cleanups that do something JS can see, and lower with them if any do. + pub(super) fn plan_unwind(&mut self) { + let mir = self.mir; + let mut visible = FxHashSet::default(); + for data in mir.basic_blocks.iter() { + if data.is_cleanup { + continue; + } + if let Some(UnwindAction::Cleanup(entry)) = data.terminator().unwind() + && self.cleanup_visible(*entry) + { + visible.insert(*entry); + } + } + if visible.is_empty() { + return; + } + let var = self.b.fresh("unwind"); + let caught = self.b.fresh("e"); + self.unwind = Some(Unwind { + var, + known: Some(0), + visible, + caught, + }); + } + + /// Whether the cleanup entered at `entry` drops or changes anything JS can see. + fn cleanup_visible(&mut self, entry: BasicBlock) -> bool { + let mir = self.mir; + let mut seen = FxHashSet::default(); + let mut stack = vec![entry]; + while let Some(bb) = stack.pop() { + if !seen.insert(bb) { + continue; + } + let data = &mir.basic_blocks[bb]; + if data + .statements + .iter() + .any(|s| matches!(s.kind, StatementKind::SetDiscriminant { .. })) + { + return true; + } + let term = data.terminator(); + match &term.kind { + TerminatorKind::Drop { place, .. } => { + let ty = place.ty(&mir.local_decls, self.tcx).ty; + if self.b.t.drops(ty) { + return true; + } + } + TerminatorKind::Call { .. } => return true, + _ => {} + } + stack.extend(term.successors()); + } + false + } + + /// Before a block whose terminator can panic: point the cleanup variable at what it + /// unwinds to, if that changed. + pub(super) fn set_unwind(&mut self, term: &mir::Terminator<'tcx>, out: &mut Vec) { + let Some(unwind) = &self.unwind else { + return; + }; + let can_panic = match &term.kind { + TerminatorKind::Call { .. } | TerminatorKind::Assert { .. } => true, + TerminatorKind::Drop { place, .. } => { + let ty = place.ty(&self.mir.local_decls, self.tcx).ty; + self.b.t.drops(ty) + } + _ => false, + }; + if !can_panic { + return; + } + let target = match term.unwind() { + Some(UnwindAction::Cleanup(entry)) if unwind.visible.contains(entry) => entry.as_usize() + 1, + _ => 0, + }; + if unwind.known == Some(target) { + return; + } + let var = unwind.var.clone(); + let stmt = StmtKind::Expr(Expr::ident(&var).assign(Expr::lit(target.to_string()))).at(None); + // What is held back that can panic runs first, under the cleanup it had. + self.emit_barrier(stmt, out); + if let Some(unwind) = &mut self.unwind { + unwind.known = Some(target); + } + } + + /// Wrap `body` in the catch that runs the cleanup the variable names, then throws on. + pub(super) fn catch(&mut self, body: Vec) -> Vec { + let Some(unwind) = self.unwind.take() else { + return body; + }; + let mir = self.mir; + // The cleanup blocks, numbered after a first node that dispatches on the variable. + let mut nodes: Vec = Vec::new(); + let mut index = rustc_index::IndexVec::>::from_elem_n(None, mir.basic_blocks.len()); + let mut entries: Vec = unwind.visible.iter().copied().collect(); + entries.sort(); + let mut stack = entries.clone(); + while let Some(bb) = stack.pop() { + if index[bb].is_some() { + continue; + } + index[bb] = Some(nodes.len() + 1); + nodes.push(bb); + stack.extend(mir.basic_blocks[bb].terminator().successors()); + } + let mut succs = vec![entries.iter().map(|e| index[*e].unwrap()).collect::>()]; + for &bb in &nodes { + succs.push( + mir.basic_blocks[bb] + .terminator() + .successors() + .map(|s| index[s].unwrap()) + .collect(), + ); + } + let graph = cfg::Graph { succs }; + self.in_cleanup = Some(unwind.caught.clone()); + let mut lowering = Cleanup { + fx: self, + nodes: &nodes, + index: &index, + entries: &entries, + var: &unwind.var, + }; + let mut labels = lowering.fx.labels; + let handler = cfg::structure(&graph, &mut lowering, &mut labels); + self.labels = labels; + self.in_cleanup = None; + vec![ + StmtKind::decl( + true, + unwind.var.clone(), + Some(Type::Builtin("number")), + Some(Expr::lit("0")), + ) + .at(None), + StmtKind::Catch { + body, + name: unwind.caught, + handler, + } + .at(None), + ] + } +} + +/// The cleanup blocks, lowered as nodes after one that dispatches on the variable. +struct Cleanup<'c, 'm, 'b, 'a, 'tcx> { + fx: &'c mut Fx<'m, 'b, 'a, 'tcx>, + nodes: &'c [BasicBlock], + index: &'c rustc_index::IndexVec>, + entries: &'c [BasicBlock], + var: &'c str, +} + +impl cfg::Lower for Cleanup<'_, '_, '_, '_, '_> { + fn node(&mut self, node: usize, out: &mut Vec) -> Exit { + if node == 0 { + // No cleanup: throw on at once. + let caught = self.fx.in_cleanup.clone().unwrap_or_default(); + out.push( + StmtKind::If { + cond: Expr::ident(self.var).binary("===", Expr::lit("0")), + then: vec![StmtKind::Throw(Expr::ident(&caught)).at(None)], + els: Vec::new(), + } + .at(None), + ); + let mut arms: Vec<(Expr, usize)> = self + .entries + .iter() + .map(|e| { + let test = Expr::ident(self.var).binary("===", Expr::lit((e.as_usize() + 1).to_string())); + (test, self.index[*e].unwrap()) + }) + .collect(); + let (_, last) = arms.pop().expect("a cleanup JS can see"); + return match arms.is_empty() { + true => Exit::Goto(last), + false => Exit::Branch(arms, last), + }; + } + let bb = self.nodes[node - 1]; + match self.fx.block_mapped(bb, out) { + Exit::Goto(target) => Exit::Goto(self.index[BasicBlock::from_usize(target)].unwrap()), + Exit::Branch(arms, default) => Exit::Branch( + arms.into_iter() + .map(|(c, t)| (c, self.index[BasicBlock::from_usize(t)].unwrap())) + .collect(), + self.index[BasicBlock::from_usize(default)].unwrap(), + ), + Exit::End => Exit::End, + } + } + + fn flush(&mut self, out: &mut Vec) { + cfg::Lower::flush(self.fx, out); + } +} diff --git a/ts/src/lower/mod.rs b/ts/src/lower/mod.rs index 2fd5ac791..4a01888fe 100644 --- a/ts/src/lower/mod.rs +++ b/ts/src/lower/mod.rs @@ -11,6 +11,7 @@ mod drop; mod fmt; mod iter; mod library; +mod mir; mod num; mod pat; mod task; @@ -33,6 +34,8 @@ use crate::ts::{self, Ctor, FieldId, Item, Loc, SymId, Type}; /// Each function's THIR, copied before borrow checking steals it. pub struct Bodies<'tcx> { map: rustc_data_structures::fx::FxHashMap, ExprId)>, + /// No body: what a MIR lowering's library calls see, which pass their arguments lowered. + empty: Thir<'tcx>, } impl<'tcx> Bodies<'tcx> { @@ -51,13 +54,15 @@ impl<'tcx> Bodies<'tcx> { map.insert(def_id, (thir.borrow().clone(), expr)); } } - Self { map } + let empty = Thir::new(rustc_middle::thir::BodyTy::Const(tcx.types.unit)); + Self { map, empty } } } /// Translate what the config's roots reach, or `None` after reporting why not. -pub fn translate<'tcx>(tcx: TyCtxt<'tcx>, config: &Config, bodies: &Bodies<'tcx>) -> Option { +pub fn translate<'tcx>(tcx: TyCtxt<'tcx>, config: &Config, bodies: &Bodies<'tcx>, mir: bool) -> Option { let mut t = Translator::new(tcx, config, bodies, None)?; + t.mir = mir; for root in &config.roots { t.root(root); } @@ -74,8 +79,9 @@ pub fn translate<'tcx>(tcx: TyCtxt<'tcx>, config: &Config, bodies: &Bodies<'tcx> /// Every construct translation refuses in the crate, reached from each of its items /// as if each were a root. -pub fn census<'tcx>(tcx: TyCtxt<'tcx>, config: &Config, bodies: &Bodies<'tcx>) -> Option { +pub fn census<'tcx>(tcx: TyCtxt<'tcx>, config: &Config, bodies: &Bodies<'tcx>, mir: bool) -> Option { let mut t = Translator::new(tcx, config, bodies, Some(Vec::new()))?; + t.mir = mir; for def_id in tcx.hir_crate_items(()).definitions() { if matches!(tcx.def_kind(def_id), DefKind::Struct | DefKind::Enum | DefKind::Union) { t.adt(tcx.adt_def(def_id), tcx.def_span(def_id)); @@ -169,6 +175,12 @@ pub struct Translator<'a, 'tcx> { drops_cycle: bool, /// The classes given drop glue. glued: rustc_data_structures::fx::FxHashSet, + /// Whether bodies lower from MIR, the spike's prototype, rather than THIR. + pub mir: bool, + /// The drop glue made from each type's drop shim, by a MIR lowering. + pub drop_shims: rustc_data_structures::fx::FxHashMap, SymId>, + /// The class of each coroutine instance a MIR lowering made. + pub coroutines: rustc_data_structures::fx::FxHashMap, mir::coroutine::Info<'tcx>>, } /// A local struct or enum, and how it is represented. @@ -235,6 +247,9 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { drops_open: Default::default(), drops_cycle: false, glued: Default::default(), + mir: false, + coroutines: Default::default(), + drop_shims: Default::default(), }) } @@ -753,6 +768,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { id, ty: self.ty(field.ty(tcx, identity).skip_norm_wip(), span), doc: self.doc(field.did), + init: ts::FieldInit::Ctor, } }) .collect(); @@ -977,10 +993,30 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { 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::RangeTo => Type::Object(vec![("end".into(), self.ty(args.type_at(0), span))]), + library::Std::RangeFrom => Type::Object(vec![("start".into(), self.ty(args.type_at(0), span))]), + library::Std::RangeFull => Type::Builtin("undefined"), + library::Std::Argument => Type::Rt("Fmt", vec![]), + library::Std::NonNull => self.ty(args.type_at(0), span), library::Std::Peekable => { let item = self.iter_item(Ty::new_adt(tcx, adt, args)); Type::Rt("Peekable", vec![self.item_ty(item, span)]) } + library::Std::Infallible => Type::Builtin("never"), + // The value `?` was applied to, which MIR branches on as this. + library::Std::ControlFlow => match (self.std_of(args.type_at(0)), args.type_at(0).kind()) { + (Some(library::Std::Result), ty::Adt(_, residual)) => Type::Rt( + "Result", + vec![self.ty(args.type_at(1), span), self.ty(residual.type_at(1), span)], + ), + (Some(library::Std::Option), _) => { + Type::Union(vec![self.ty(args.type_at(1), span), Type::Builtin("undefined")]) + } + _ => { + self.err(span, format!("`{}` other than from `?`", Ty::new_adt(tcx, adt, args))); + Type::Builtin("unknown") + } + }, }; } // Every other iterator std has is an `rt.Iter` of its items. @@ -1050,17 +1086,22 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { self.err(span, format!("`{}` is from another crate", tcx.def_path_str(def_id))); return; }; - let Some((thir, expr)) = self.bodies.map.get(&local) else { - self.err( - span, - format!( - "no body for `{}`: constant evaluation consumed it", - tcx.def_path_str(def_id) - ), - ); - return; + let function = match self.mir { + true => mir::function(self, instance), + false => { + let Some((thir, expr)) = self.bodies.map.get(&local) else { + self.err( + span, + format!( + "no body for `{}`: constant evaluation consumed it", + tcx.def_path_str(def_id) + ), + ); + return; + }; + body::function(self, instance, thir, *expr) + } }; - let function = body::function(self, instance, thir, *expr); let Some(mut function) = function else { return; }; @@ -1087,6 +1128,20 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { (!doc.trim().is_empty()).then_some(doc) } + /// Where the invocation of a left-out macro that made `span` is written, if one did. + pub fn ignored_site(&self, mut span: Span) -> Option { + while span.from_expansion() { + let data = span.ctxt().outer_expn_data(); + if let Some(def_id) = data.macro_def_id + && self.config.ignore_macros.contains(&self.tcx.def_path_str(def_id)) + { + return Some(data.call_site); + } + span = data.call_site; + } + None + } + /// Whether invocations of the macro that made `span` are left out. pub fn ignored_macro(&self, mut span: Span) -> bool { // Walk out through the macros this one was expanded inside. @@ -1105,7 +1160,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { } /// A short name for a type, to tell instances apart. -fn short_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> String { +pub fn short_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> String { match ty.kind() { // Its arguments, but not defaults such as a `Vec`'s allocator. ty::Adt(adt, args) => { diff --git a/ts/src/lower/pat.rs b/ts/src/lower/pat.rs index 8dfd3ec5e..d9d1bc8ad 100644 --- a/ts/src/lower/pat.rs +++ b/ts/src/lower/pat.rs @@ -263,6 +263,12 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { ty::Uint(ty::UintTy::U8) => "subarray", _ => "slice", }; + if method == "slice" && binds_mut_ref(p) { + self.t.err( + p.span, + format!("a mutable borrow of a subslice of `{elem}`, which JS would copy, having no views"), + ); + } // 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) { diff --git a/ts/src/print.rs b/ts/src/print.rs index 32dcc9bf4..653e2e378 100644 --- a/ts/src/print.rs +++ b/ts/src/print.rs @@ -7,7 +7,7 @@ use std::path::{Component, Path, PathBuf}; use oxc_sourcemap::SourceMapBuilder; use crate::program::{HEADER, ModPath, Program, SymKind, is_type_module, module_file}; -use crate::ts::{Ctor, Expr, Function, Item, Key, Loc, Param, Stmt, StmtKind, SymId, Target, Type}; +use crate::ts::{Ctor, Expr, FieldInit, Function, Item, Key, Loc, Param, Stmt, StmtKind, SymId, Target, Type}; pub struct Printed { pub code: String, @@ -331,6 +331,22 @@ impl Refs { self.stmts(program, body); self.stmts(program, finally); } + StmtKind::Catch { body, handler, .. } => { + self.stmts(program, body); + self.stmts(program, handler); + } + StmtKind::Switch { + disc, cases, default, .. + } => { + self.expr(program, disc); + for (value, body) in cases { + self.expr(program, value); + self.stmts(program, body); + } + self.stmts(program, default); + } + StmtKind::Declare { ty, .. } => self.ty(program, ty), + StmtKind::Throw(e) => self.expr(program, e), StmtKind::Return(None) | StmtKind::Break(_) | StmtKind::Continue(_) => {} } } @@ -342,7 +358,7 @@ impl Refs { 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)), - Expr::Member(e, _) | Expr::Unary(_, e) | Expr::Located(_, e) => self.expr(program, e), + Expr::Member(e, _) | Expr::Unary(_, e) | Expr::Located(_, e) | Expr::Postfix(e, _) => self.expr(program, e), Expr::Index(a, b) | Expr::Binary(a, _, b) | Expr::Assign(a, b) => { self.expr(program, a); self.expr(program, b); @@ -493,8 +509,19 @@ impl Printer<'_> { self.doc(&field.doc); self.start(); let ty = self.ty(&field.ty); - self.write(&format!("{}: {ty};\n", self.program.fields[field.id].name)); + let name = self.program.fields[field.id].name.clone(); + match &field.init { + FieldInit::Ctor => self.write(&format!("{name}: {ty};\n")), + FieldInit::Later => self.write(&format!("{name}!: {ty};\n")), + FieldInit::Value(value) => { + self.write(&format!("{name}: {ty} = ")); + self.expr(value, 2); + self.write(";\n"); + } + } } + let fields: Vec<&crate::ts::Field> = + fields.iter().filter(|f| matches!(f.init, FieldInit::Ctor)).collect(); let names: Vec = fields.iter().map(|f| self.program.fields[f.id].name.clone()).collect(); match ctor { Ctor::Unit => {} @@ -801,6 +828,53 @@ impl Printer<'_> { self.start(); self.write("}\n"); } + StmtKind::Catch { body, name, handler } => { + self.write("try {\n"); + self.block_body(body); + self.start(); + let name = self.local(name).to_string(); + self.write(&format!("}} catch ({name}) {{\n")); + self.block_body(handler); + self.start(); + self.write("}\n"); + } + StmtKind::Switch { + label, + disc, + cases, + default, + } => { + self.label(label); + self.write("switch ("); + self.expr(disc, 0); + self.write(") {\n"); + self.indent += 1; + for (value, body) in cases { + self.start(); + self.write("case "); + self.expr(value, 0); + self.write(":\n"); + self.block_body(body); + } + if !default.is_empty() { + self.start(); + self.write("default:\n"); + self.block_body(default); + } + self.indent -= 1; + self.start(); + self.write("}\n"); + } + StmtKind::Declare { name, ty } => { + let name = self.local(name).to_string(); + let ty = self.ty(ty); + self.write(&format!("let {name}!: {ty};\n")); + } + StmtKind::Throw(value) => { + self.write("throw "); + self.expr(value, 0); + self.write(";\n"); + } StmtKind::Break(label) => match label { Some(label) => self.write(&format!("break {label};\n")), None => self.write("break;\n"), @@ -898,6 +972,10 @@ impl Printer<'_> { false => self.expr(arg, 17), } } + Expr::Postfix(arg, op) => { + self.expr(arg, 19); + self.write(op); + } Expr::Binary(l, op, r) => { let p = binary_prec(op); self.operand(l, op, p); @@ -1098,6 +1176,7 @@ fn prec(e: &Expr) -> u8 { Expr::Binary(_, op, _) => binary_prec(op), Expr::As(..) => 12, Expr::Unary(..) => 17, + Expr::Postfix(..) => 18, Expr::Lit(text) if text.starts_with('-') => 17, Expr::New(..) | Expr::Call(..) | Expr::Member(..) | Expr::Index(..) => 19, Expr::Located(_, e) => prec(e), @@ -1164,6 +1243,23 @@ fn collect_locals(stmts: &[Stmt], out: &mut Vec) { collect_locals(body, out); collect_locals(finally, out); } + StmtKind::Catch { body, name, handler } => { + collect_locals(body, out); + out.push(name.clone()); + collect_locals(handler, out); + } + StmtKind::Switch { + disc, cases, default, .. + } => { + collect_expr_locals(disc, out); + for (value, body) in cases { + collect_expr_locals(value, out); + collect_locals(body, out); + } + collect_locals(default, out); + } + StmtKind::Declare { name, .. } => out.push(name.clone()), + StmtKind::Throw(e) => collect_expr_locals(e, out), StmtKind::Return(None) | StmtKind::Break(_) | StmtKind::Continue(_) => {} } } @@ -1188,7 +1284,9 @@ fn collect_expr_locals(e: &Expr, out: &mut Vec) { } Expr::Array(es) => es.iter().for_each(|e| collect_expr_locals(e, out)), Expr::Object(props) => props.iter().for_each(|(_, e)| collect_expr_locals(e, out)), - Expr::Member(e, _) | Expr::Unary(_, e) | Expr::Located(_, e) | Expr::As(e, _) => collect_expr_locals(e, out), + Expr::Member(e, _) | Expr::Unary(_, e) | Expr::Located(_, e) | Expr::As(e, _) | Expr::Postfix(e, _) => { + collect_expr_locals(e, out) + } Expr::Index(a, b) | Expr::Binary(a, _, b) | Expr::Assign(a, b) => { collect_expr_locals(a, out); collect_expr_locals(b, out); diff --git a/ts/src/program.rs b/ts/src/program.rs index a6a239a69..00af54621 100644 --- a/ts/src/program.rs +++ b/ts/src/program.rs @@ -57,7 +57,8 @@ pub enum SymKind { /// A type alias, which has no value. Union, Function, - /// A class's method, called on its receiver: only drop glue's `[Symbol.dispose]`. + /// A class's method, called on its receiver: drop glue's `[Symbol.dispose]`, and a + /// coroutine's `poll`. Method, } diff --git a/ts/src/ts.rs b/ts/src/ts.rs index 24cab1ede..4e99fcdac 100644 --- a/ts/src/ts.rs +++ b/ts/src/ts.rs @@ -38,6 +38,8 @@ pub enum Expr { Call(Box, Vec), New(Box, Vec), Unary(&'static str, Box), + /// `x++` and the like: the operand's value, before the operator changes it. + Postfix(Box, &'static str), Binary(Box, &'static str, Box), Cond(Box, Box, Box), Assign(Box, Box), @@ -219,6 +221,27 @@ pub enum StmtKind { body: Vec, finally: Vec, }, + /// `try { body } catch (name) { handler }`. + Catch { + body: Vec, + name: String, + handler: Vec, + }, + /// `switch (disc) { case a: ... default: ... }`, whose cases never fall through. + Switch { + label: Option, + disc: Expr, + cases: Vec<(Expr, Vec)>, + default: Vec, + }, + /// `let name!: ty;`: a variable every read of which some write precedes, in ways + /// `tsc` can't follow. + Declare { + name: String, + ty: Type, + }, + /// `throw value;` + Throw(Expr), } impl StmtKind { @@ -253,7 +276,7 @@ impl Stmt { /// Whether control never reaches the statement after this one. pub fn diverges(&self) -> bool { match &self.kind { - StmtKind::Return(_) | StmtKind::Break(_) | StmtKind::Continue(_) => true, + StmtKind::Return(_) | StmtKind::Break(_) | StmtKind::Continue(_) | StmtKind::Throw(_) => true, StmtKind::If { then, els, .. } => diverges(then) && diverges(els), StmtKind::Block { label: None, body } | StmtKind::Try { body, .. } => diverges(body), StmtKind::While { @@ -276,6 +299,11 @@ fn breaks_out(stmts: &[Stmt], label: Option<&str>, nested: bool) -> bool { 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::Catch { body, handler, .. } => breaks_out(body, label, nested) || breaks_out(handler, label, nested), + // An unlabeled `break` in a case leaves the switch. + StmtKind::Switch { cases, default, .. } => { + cases.iter().any(|(_, body)| breaks_out(body, label, true)) || breaks_out(default, label, true) + } StmtKind::While { body, .. } | StmtKind::ForRange { body, .. } | StmtKind::ForOf { body, .. } => { breaks_out(body, label, true) } @@ -287,8 +315,15 @@ fn breaks_out(stmts: &[Stmt], label: Option<&str>, nested: bool) -> bool { pub fn mentions(stmts: &[Stmt], name: &str) -> bool { let expr = |e: &Expr| e.mentions(name); stmts.iter().any(|stmt| match &stmt.kind { - StmtKind::Expr(e) | StmtKind::Return(Some(e)) => expr(e), + StmtKind::Expr(e) | StmtKind::Return(Some(e)) | StmtKind::Throw(e) => expr(e), StmtKind::Decl { init, .. } => init.as_ref().is_some_and(expr), + StmtKind::Catch { body, handler, .. } => mentions(body, name) || mentions(handler, name), + StmtKind::Switch { + disc, cases, default, .. + } => { + expr(disc) || cases.iter().any(|(test, body)| expr(test) || mentions(body, name)) || mentions(default, name) + } + StmtKind::Declare { .. } => false, StmtKind::If { cond, then, els } => expr(cond) || mentions(then, name) || mentions(els, name), StmtKind::While { cond, body, .. } => expr(cond) || mentions(body, name), StmtKind::ForRange { start, end, body, .. } => expr(start) || expr(end) || mentions(body, name), @@ -306,7 +341,9 @@ impl Expr { Self::This | Self::Sym(_) | Self::Rt(_) | Self::Lit(_) | Self::Str(_) | Self::Yield => false, Self::Array(es) => es.iter().any(|e| e.mentions(name)), Self::Object(props) => props.iter().any(|(_, e)| e.mentions(name)), - Self::Member(e, _) | Self::Unary(_, e) | Self::Located(_, e) | Self::As(e, _) => e.mentions(name), + Self::Member(e, _) | Self::Unary(_, e) | Self::Postfix(e, _) | Self::Located(_, e) | Self::As(e, _) => { + e.mentions(name) + } Self::Index(a, b) | Self::Binary(a, _, b) | Self::Assign(a, b) => a.mentions(name) || b.mentions(name), Self::Call(f, args) | Self::New(f, args) => f.mentions(name) || args.iter().any(|e| e.mentions(name)), Self::Cond(a, b, c) => a.mentions(name) || b.mentions(name) || c.mentions(name), @@ -358,6 +395,18 @@ pub struct Field { pub id: FieldId, pub ty: Type, pub doc: Option, + /// What the field starts as, if not a constructor argument. + pub init: FieldInit, +} + +#[derive(Clone, Debug)] +pub enum FieldInit { + /// The constructor's argument. + Ctor, + /// `name: ty = value`. + Value(Expr), + /// `name!: ty`: set before every read, in ways `tsc` can't follow. + Later, } /// How a struct is constructed, as a class; a plain object is a literal of its fields. diff --git a/ts/tests/fixtures.rs b/ts/tests/fixtures.rs index 6a6449790..38e78c361 100644 --- a/ts/tests/fixtures.rs +++ b/ts/tests/fixtures.rs @@ -56,6 +56,25 @@ fn translated(name: &str) -> PathBuf { out } +/// Run the driver on a fixture with bodies lowered from MIR, the spike's prototype. +fn translate_mir(name: &str) -> (PathBuf, Output) { + let out = scratch().join(name).join("mir"); + let config = fixture(name).join("ts.toml"); + let args: [&std::ffi::OsStr; 4] = ["--out".as_ref(), out.as_ref(), "--lower".as_ref(), "mir".as_ref()]; + let output = driver(name, &config, &args, "mir"); + (out, output) +} + +fn translated_mir(name: &str) -> PathBuf { + let (out, output) = translate_mir(name); + assert!( + output.status.success(), + "translating {name} from MIR failed:\n{}", + String::from_utf8_lossy(&output.stderr) + ); + out +} + /// The generated TypeScript, by path relative to `dir`, without the runtime. fn sources(dir: &Path) -> Vec<(PathBuf, String)> { let mut files = Vec::new(); @@ -235,6 +254,14 @@ fn shake(out: &Path) { assert_eq!(kept, want, "the bundle keeps more than the caller reaches:\n{bundle}"); } +/// The codec with bodies lowered from MIR: the same differential test, without a snapshot. +#[test] +fn codec_mir() { + let out = translated_mir("codec"); + typecheck(&out); + differential("codec-fixture", &out, "harness.ts", 20_000, &[]); +} + /// Adding a root changes only the output of what it reaches: everything else keeps its /// text and order, though translation reaches it in another order. #[test] @@ -292,13 +319,24 @@ fn lines(text: &str) -> Vec { fn lang() { let out = translated("lang"); snapshot("lang", &out); + lang_differential(&out); +} + +/// The language fixture with bodies lowered from MIR. +#[test] +fn lang_mir() { + let out = translated_mir("lang"); + lang_differential(&out); +} + +fn lang_differential(out: &Path) { // Hand-written callers, type-checked against the output they call: one runs it, and // one mixes integer types, which `tsc` must refuse. for file in ["caller.ts", "brands.ts"] { std::fs::copy(manifest_dir().join("fixtures/lang").join(file), out.join(file)).unwrap(); } - typecheck(&out); - differential("lang-fixture", &out, "caller.ts", 50_000, &["wide"]); + typecheck(out); + differential("lang-fixture", out, "caller.ts", 50_000, &["wide"]); } /// Futures and drops: the harness is hand-written TS around the translation, whose FIFO @@ -307,6 +345,17 @@ fn lang() { fn task() { let out = translated("task"); snapshot("task", &out); + task_differential(&out); +} + +/// Futures and drops with bodies lowered from MIR: each async body a state machine. +#[test] +fn task_mir() { + let out = translated_mir("task"); + task_differential(&out); +} + +fn task_differential(out: &Path) { let harness = manifest_dir().join("tests").join("task.ts").display().to_string(); let text = format!( r#"import * as rt from "./rt.ts"; @@ -317,18 +366,69 @@ import {{ 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 }}); +const fixture = {{ Pending: rt.Pending, u64: rt.u64, Log, Channel, Guard, sum, drain, blocks }}; +export const run = harness(fixture); +export const unwind = harness(fixture, true); "# ); std::fs::write(out.join("caller.ts"), text).unwrap(); - typecheck(&out); - differential("task-fixture", &out, "caller.ts", 20_000, &[]); + typecheck(out); + differential("task-fixture", out, "caller.ts", 20_000, &[]); + unwinding(out, "caller.ts", 5_000); +} + +/// Run the task fixture's scripts where a panic unwinds, natively on the host and +/// translated, logging each step's panic and going on: what a panic drops must match. +fn unwinding(out: &Path, entry: &str, scripts: u32) { + let target_dir = scratch().join("host"); + let status = cargo() + .args([ + "build", + "--locked", + "--package", + "task-fixture", + "--example", + "unwind", + "--target-dir", + ]) + .arg(&target_dir) + .arg("--manifest-path") + .arg(manifest_dir().join("Cargo.toml")) + .status() + .expect("running cargo"); + assert!(status.success(), "building the unwind example failed"); + let output = Command::new("node") + .arg(manifest_dir().join("tests").join("unwind.ts")) + .arg(target_dir.join("debug").join("examples").join("unwind")) + .arg(out.join(entry)) + .arg(scripts.to_string()) + .output() + .expect("running node, which the dev shell provides"); + let stdout = String::from_utf8_lossy(&output.stdout); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + output.status.success(), + "unwinding differs from native Rust:\n{stdout}{stderr}" + ); + println!("{stdout}"); } /// Each construct outside the subset fails with its span, and nothing is written. #[test] fn reject() { let (out, output) = translate("reject"); + rejected(&out, &output, "expected.txt"); +} + +/// What lowering from MIR refuses in the same fixture. +#[test] +fn reject_mir() { + let (out, output) = translate_mir("reject"); + rejected(&out, &output, "expected-mir.txt"); +} + +/// The errors of a failed translation, each with where it points, against `expected`. +fn rejected(out: &Path, output: &Output, expected: &str) { assert!(!output.status.success(), "translating reject succeeded"); assert!(!out.join("lib.ts").exists(), "a failed translation wrote output"); let stderr = String::from_utf8_lossy(&output.stderr); @@ -344,7 +444,7 @@ fn reject() { got.push('\n'); } } - let path = fixture("reject").join("expected.txt"); + let path = fixture("reject").join(expected); if std::env::var_os("UPDATE_EXPECT").is_some() { std::fs::write(&path, &got).unwrap(); return; diff --git a/ts/tests/task.ts b/ts/tests/task.ts index d98071acf..4cbfb666a 100644 --- a/ts/tests/task.ts +++ b/ts/tests/task.ts @@ -160,8 +160,11 @@ class Executor { } } -/** The script runner, as `harness.rs` runs it natively. */ -export function harness(f: Fixture): (input: Uint8Array) => { view(): Uint8Array } { +/** + * The script runner, as `harness.rs` runs it natively. Where a panic unwinds, as on + * the host, `unwind` logs a step's panic and goes on, as the native harness does. + */ +export function harness(f: Fixture, unwind = false): (input: Uint8Array) => { view(): Uint8Array } { return (input) => { const log = f.Log.new(); const note = (event: string) => f.Log.note(log, event); @@ -170,6 +173,25 @@ export function harness(f: Fixture): (input: Uint8Array) => { view(): Uint for (let i = 0; i < input.length; i += 2) { const op = input[i]; const arg = input[i + 1] ?? 0; + if (!unwind) { + step(op, arg); + continue; + } + try { + step(op, arg); + } catch (err) { + if (!(err instanceof Error && err.name === "Panic")) throw err; + note(`panic ${err.message}`); + } + } + for (let t = 0; t < exec.tasks.length; t++) exec.drop(t); + channels.forEach((channel, c) => { + note(`c${c} drop`); + channel[Symbol.dispose](); + }); + return f.Log.text(log); + + function step(op: number, arg: number): void { const pick = (len: number) => (len > 0 ? arg % len : undefined); const c = pick(channels.length); switch (op % 11) { @@ -223,12 +245,6 @@ export function harness(f: Fixture): (input: Uint8Array) => { view(): Uint } } } - for (let t = 0; t < exec.tasks.length; t++) exec.drop(t); - channels.forEach((channel, c) => { - note(`c${c} drop`); - channel[Symbol.dispose](); - }); - return f.Log.text(log); }; } @@ -249,4 +265,7 @@ export const cases: Uint8Array[] = [ // 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], + // A future that panics mid-loop holding guards: unwinding drops them, and a poll after + // panics again. + [0, 0, 1, 0, 1, 0, 6, 0, 3, 99, 7, 0, 3, 4, 6, 0, 8, 0, 9, 1], ].map((script) => Uint8Array.from(script)); diff --git a/ts/tests/unwind.ts b/ts/tests/unwind.ts new file mode 100644 index 000000000..8244dab6d --- /dev/null +++ b/ts/tests/unwind.ts @@ -0,0 +1,60 @@ +// Runs the task fixture's scripts where a panic unwinds: natively on the host +// (`examples/unwind.rs`), and translated, each logging a step's panic and going on. The +// logs must match byte for byte, so whatever a panic drops on its way out, and in what +// order, matches native Rust. +// +// usage: node unwind.ts [scripts] +// +// The caller exports `unwind`, the translation's harness with panics caught per step, +// and `cases`, the scripts it names. The rest are random, with sends of the value that +// panics mixed in. + +import { spawnSync } from "node:child_process"; +import { pathToFileURL } from "node:url"; + +const [bin, callerPath, countArg] = process.argv.slice(2); +if (!bin || !callerPath) throw new Error("usage: unwind.ts [scripts]"); +const count = Number(countArg ?? "5000"); +const caller = await import(pathToFileURL(callerPath).href); + +// xorshift64*: the same scripts on every run. +let state = 0x9e3779b97f4a7c15n; +function next(): number { + state ^= state >> 12n; + state ^= (state << 25n) & 0xffffffffffffffffn; + state ^= state >> 27n; + return Number(((state * 0x2545f4914f6cdd1dn) & 0xffffffffffffffffn) >> 32n); +} + +const scripts: Uint8Array[] = [...caller.cases]; +for (let i = 0; i < count; i++) { + const steps = 4 + (next() % 20); + const script = new Uint8Array(steps * 2); + for (let s = 0; s < steps; s++) { + script[2 * s] = next() % 11; + // A send of 99 panics the `sum` reading it. + script[2 * s + 1] = next() % 4 === 0 ? 99 : next() % 256; + } + scripts.push(script); +} + +const hex = (bytes: Uint8Array) => Buffer.from(bytes).toString("hex"); +const native = spawnSync(bin, { input: scripts.map(hex).join("\n") + "\n", maxBuffer: 1 << 30 }); +if (native.status !== 0) throw new Error(`${bin} failed: ${native.stderr}`); +const want = native.stdout.toString().trimEnd().split("\n"); + +let failures = 0; +let panics = 0; +scripts.forEach((script, i) => { + const got = hex(caller.unwind(script).view()); + if (want[i].includes(hex(Buffer.from("panic ")))) panics++; + if (got !== want[i]) { + failures++; + if (failures <= 10) { + const text = (h: string) => Buffer.from(h, "hex").toString(); + console.error(`script ${hex(script)}\nnative\n${text(want[i])}\nts\n${text(got)}\n`); + } + } +}); +console.log(`${scripts.length} scripts, ${panics} with panics, ${failures} differences`); +if (failures > 0) process.exit(1);