From b6836dde499c52556e7ecda7b00d465acc27e576 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 10:45:42 -0700 Subject: [PATCH 1/2] chore(quest): add quest/m2/ts-closure-write.md Co-Authored-By: Claude Opus 5.5 --- quest/m2/README.md | 1 + quest/m2/ts-closure-write.md | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 quest/m2/ts-closure-write.md diff --git a/quest/m2/README.md b/quest/m2/README.md index 372185fcc..ebfdcf63e 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -26,6 +26,7 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export/README ## Required +- [A closure writing through a captured mutable reference](/quest/m2/ts-closure-write.md) - a closure's write to a captured integer reaches the caller - [Byte slices without objects](/quest/m2/ts-byte-views/README.md) - reading through a translated cursor allocates no object per read - [Async in the TypeScript translator](/quest/m2/ts-async.md) - moq-net's and kio's async code translates with Rust's lazy futures - [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values diff --git a/quest/m2/ts-closure-write.md b/quest/m2/ts-closure-write.md new file mode 100644 index 000000000..97b1cdc5e --- /dev/null +++ b/quest/m2/ts-closure-write.md @@ -0,0 +1,13 @@ +# [S] A closure writing through a captured mutable reference + +## Goal + +A closure that captures an integer by `&mut` and writes it, called once or more, through +`FnOnce`, `FnMut`, or a generic, leaves the caller's variable or field written, as native +Rust does, proven by differential tests against native Rust. + +## Plan + +Found writing [#147](https://github.com/kixelated/rust2/pull/147)'s stock fixture: a closure +capturing a `u8` by `&mut`, called once, loses its write. Find where lowering decides the +captured place needs no `rt.Place` and fix it at the source for every capture path. From c04d447c2e56c67fc90c35861d78128c688cdf5c Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 11:05:06 -0700 Subject: [PATCH 2/2] fix(ts): keep a closure's write to what it captured when it returns it return_assigned folded `x = v; return x` into `return v` for any name, but in a closure body `x` may be a capture: the caller's variable by `&mut`, or a `move` closure's own copy. It now folds only the body's own locals. Co-Authored-By: Claude Opus 5.5 --- quest/m2/README.md | 1 - quest/m2/ts-closure-write.md | 13 - ts/fixtures/lang/expected/captures.ts | 1343 +++++++++++++++++++++++++ ts/fixtures/lang/expected/harness.ts | 3 +- ts/fixtures/lang/src/captures.rs | 156 +++ ts/fixtures/lang/src/harness.rs | 5 +- ts/fixtures/lang/src/lib.rs | 10 +- ts/src/lower/mir/mod.rs | 41 +- 8 files changed, 1535 insertions(+), 37 deletions(-) delete mode 100644 quest/m2/ts-closure-write.md create mode 100644 ts/fixtures/lang/expected/captures.ts create mode 100644 ts/fixtures/lang/src/captures.rs diff --git a/quest/m2/README.md b/quest/m2/README.md index ebfdcf63e..372185fcc 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -26,7 +26,6 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export/README ## Required -- [A closure writing through a captured mutable reference](/quest/m2/ts-closure-write.md) - a closure's write to a captured integer reaches the caller - [Byte slices without objects](/quest/m2/ts-byte-views/README.md) - reading through a translated cursor allocates no object per read - [Async in the TypeScript translator](/quest/m2/ts-async.md) - moq-net's and kio's async code translates with Rust's lazy futures - [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values diff --git a/quest/m2/ts-closure-write.md b/quest/m2/ts-closure-write.md deleted file mode 100644 index 97b1cdc5e..000000000 --- a/quest/m2/ts-closure-write.md +++ /dev/null @@ -1,13 +0,0 @@ -# [S] A closure writing through a captured mutable reference - -## Goal - -A closure that captures an integer by `&mut` and writes it, called once or more, through -`FnOnce`, `FnMut`, or a generic, leaves the caller's variable or field written, as native -Rust does, proven by differential tests against native Rust. - -## Plan - -Found writing [#147](https://github.com/kixelated/rust2/pull/147)'s stock fixture: a closure -capturing a `u8` by `&mut`, called once, loses its write. Find where lowering decides the -captured place needs no `rt.Place` and fix it at the source for every capture path. diff --git a/ts/fixtures/lang/expected/captures.ts b/ts/fixtures/lang/expected/captures.ts new file mode 100644 index 000000000..9543627de --- /dev/null +++ b/ts/fixtures/lang/expected/captures.ts @@ -0,0 +1,1343 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +export function through(n: rt.Place, b: rt.u8): rt.u8 { + return ((): rt.u8 => { + n.v = rt.u8WrappingAdd(n.v, b); + return n.v; + })(); +} + +/** Each way a closure writes a captured integer, from two operands taken from `data`. */ +export function u8s(data: rt.ByteSlice, out: rt.ByteVec): void { + const buf: Uint8Array = Uint8Array.of(0); + const n: rt.usize = rt.min(data.length, rt.byteSliceFrom(buf).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf), 0, n), rt.byteSliceRange(data, 0, n)); + const _18: rt.u8 = rt.u8FromBeBytes(buf); + const _20: rt.ByteSlice = rt.byteSliceRange(data, n); + const a: rt.u8 = _18; + const buf2: Uint8Array = Uint8Array.of(0); + const n2: rt.usize = rt.min(_20.length, rt.byteSliceFrom(buf2).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf2), 0, n2), rt.byteSliceRange(_20, 0, n2)); + const b: rt.u8 = ([rt.u8FromBeBytes(buf2), rt.byteSliceRange(_20, n2)] as [rt.u8, rt.ByteSlice])[0]; + const put: (a0: rt.u8) => void = (v: rt.u8): void => { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u8ToBeBytes(v))); + }; + let n3: rt.u8 = a; + const part = ((): rt.u8 => { + n3 = rt.u8WrappingAdd(n3, b); + return n3; + })(); + put(part); + put(n3); + const bump2: () => rt.u8 = (): rt.u8 => { + n3 = rt.u8WrappingMul(n3, b); + return n3; + }; + const part3 = bump2(); + put(part3); + const part4 = bump2(); + put(part4); + put(n3); + const part6 = ((): rt.u8 => { + if (!((b & 1) === 1)) { + return n3; + } + n3 = rt.u8WrappingSub(n3, a); + return n3; + })(); + put(part6); + put(n3); + const captured = rt.byteSliceToByteVec(_20); + const part8 = ((): rt.u8 => { + n3 = rt.u8WrappingAdd(n3, captured.length & 0xff); + return n3; + })(); + put(part8); + put(n3); + const part10 = once$u8_u8s_closure5((): rt.u8 => { + n3 = rt.u8WrappingAdd(n3, b); + return n3; + }); + put(part10); + put(n3); + const _108: [rt.u8, rt.u8] = twice$u8_u8s_closure6((): rt.u8 => { + n3 = rt.u8WrappingAdd(n3, a); + return n3; + }); + const x: rt.u8 = _108[0]; + const y: rt.u8 = _108[1]; + put(x); + put(y); + put(n3); + let m: rt.u8 = a; + let captured2 = m; + const captured3 = b; + const own: () => rt.u8 = (): rt.u8 => { + captured2 = rt.u8WrappingAdd(captured2, captured3); + return captured2; + }; + const part13 = own(); + put(part13); + const part14 = own(); + put(part14); + put(m); + const pair: [rt.u8, rt.u8] = [a, b]; + const part16 = ((): rt.u8 => { + pair[0] = rt.u8WrappingAdd(pair[0], pair[1]); + return pair[0]; + })(); + put(part16); + put(pair[0]); + const part18 = through(rt.place(() => m, (value) => m = value), b); + put(part18); + put(m); + const part20 = ((): rt.u8 => { + return ((): rt.u8 => { + n3 = rt.u8WrappingSub(n3, b); + return n3; + })(); + })(); + put(part20); + put(n3); +} + +export function through$2(n: rt.Place, b: rt.u16): rt.u16 { + return ((): rt.u16 => { + n.v = rt.u16WrappingAdd(n.v, b); + return n.v; + })(); +} + +/** Each way a closure writes a captured integer, from two operands taken from `data`. */ +export function u16s(data: rt.ByteSlice, out: rt.ByteVec): void { + const buf: Uint8Array = rt.bytes(2); + const n: rt.usize = rt.min(data.length, rt.byteSliceFrom(buf).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf), 0, n), rt.byteSliceRange(data, 0, n)); + const _18: rt.u16 = rt.u16FromBeBytes(buf); + const _20: rt.ByteSlice = rt.byteSliceRange(data, n); + const a: rt.u16 = _18; + const buf2: Uint8Array = rt.bytes(2); + const n2: rt.usize = rt.min(_20.length, rt.byteSliceFrom(buf2).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf2), 0, n2), rt.byteSliceRange(_20, 0, n2)); + const b: rt.u16 = ([rt.u16FromBeBytes(buf2), rt.byteSliceRange(_20, n2)] as [rt.u16, rt.ByteSlice])[0]; + const put: (a0: rt.u16) => void = (v: rt.u16): void => { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u16ToBeBytes(v))); + }; + let n3: rt.u16 = a; + const part = ((): rt.u16 => { + n3 = rt.u16WrappingAdd(n3, b); + return n3; + })(); + put(part); + put(n3); + const bump2: () => rt.u16 = (): rt.u16 => { + n3 = rt.u16WrappingMul(n3, b); + return n3; + }; + const part3 = bump2(); + put(part3); + const part4 = bump2(); + put(part4); + put(n3); + const part6 = ((): rt.u16 => { + if (!((b & 1) === 1)) { + return n3; + } + n3 = rt.u16WrappingSub(n3, a); + return n3; + })(); + put(part6); + put(n3); + const captured = rt.byteSliceToByteVec(_20); + const part8 = ((): rt.u16 => { + n3 = rt.u16WrappingAdd(n3, captured.length & 0xffff); + return n3; + })(); + put(part8); + put(n3); + const part10 = once$u16_u16s_closure5((): rt.u16 => { + n3 = rt.u16WrappingAdd(n3, b); + return n3; + }); + put(part10); + put(n3); + const _108: [rt.u16, rt.u16] = twice$u16_u16s_closure6((): rt.u16 => { + n3 = rt.u16WrappingAdd(n3, a); + return n3; + }); + const x: rt.u16 = _108[0]; + const y: rt.u16 = _108[1]; + put(x); + put(y); + put(n3); + let m: rt.u16 = a; + let captured2 = m; + const captured3 = b; + const own: () => rt.u16 = (): rt.u16 => { + captured2 = rt.u16WrappingAdd(captured2, captured3); + return captured2; + }; + const part13 = own(); + put(part13); + const part14 = own(); + put(part14); + put(m); + const pair: [rt.u16, rt.u16] = [a, b]; + const part16 = ((): rt.u16 => { + pair[0] = rt.u16WrappingAdd(pair[0], pair[1]); + return pair[0]; + })(); + put(part16); + put(pair[0]); + const part18 = through$2(rt.place(() => m, (value) => m = value), b); + put(part18); + put(m); + const part20 = ((): rt.u16 => { + return ((): rt.u16 => { + n3 = rt.u16WrappingSub(n3, b); + return n3; + })(); + })(); + put(part20); + put(n3); +} + +export function through$3(n: rt.Place, b: rt.u32): rt.u32 { + return ((): rt.u32 => { + n.v = rt.u32WrappingAdd(n.v, b); + return n.v; + })(); +} + +/** Each way a closure writes a captured integer, from two operands taken from `data`. */ +export function u32s(data: rt.ByteSlice, out: rt.ByteVec): void { + const buf: Uint8Array = rt.bytes(4); + const n: rt.usize = rt.min(data.length, rt.byteSliceFrom(buf).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf), 0, n), rt.byteSliceRange(data, 0, n)); + const _18: rt.u32 = rt.u32FromBeBytes(buf); + const _20: rt.ByteSlice = rt.byteSliceRange(data, n); + const a: rt.u32 = _18; + const buf2: Uint8Array = rt.bytes(4); + const n2: rt.usize = rt.min(_20.length, rt.byteSliceFrom(buf2).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf2), 0, n2), rt.byteSliceRange(_20, 0, n2)); + const b: rt.u32 = ([rt.u32FromBeBytes(buf2), rt.byteSliceRange(_20, n2)] as [rt.u32, rt.ByteSlice])[0]; + const put: (a0: rt.u32) => void = (v: rt.u32): void => { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u32ToBeBytes(v))); + }; + let n3: rt.u32 = a; + const part = ((): rt.u32 => { + n3 = rt.u32WrappingAdd(n3, b); + return n3; + })(); + put(part); + put(n3); + const bump2: () => rt.u32 = (): rt.u32 => { + n3 = rt.u32WrappingMul(n3, b); + return n3; + }; + const part3 = bump2(); + put(part3); + const part4 = bump2(); + put(part4); + put(n3); + const part6 = ((): rt.u32 => { + if (!(((b & 1) >>> 0) === 1)) { + return n3; + } + n3 = rt.u32WrappingSub(n3, a); + return n3; + })(); + put(part6); + put(n3); + const captured = rt.byteSliceToByteVec(_20); + const part8 = ((): rt.u32 => { + n3 = rt.u32WrappingAdd(n3, captured.length as rt.u32); + return n3; + })(); + put(part8); + put(n3); + const part10 = once$u32_u32s_closure5((): rt.u32 => { + n3 = rt.u32WrappingAdd(n3, b); + return n3; + }); + put(part10); + put(n3); + const _108: [rt.u32, rt.u32] = twice$u32_u32s_closure6((): rt.u32 => { + n3 = rt.u32WrappingAdd(n3, a); + return n3; + }); + const x: rt.u32 = _108[0]; + const y: rt.u32 = _108[1]; + put(x); + put(y); + put(n3); + let m: rt.u32 = a; + let captured2 = m; + const captured3 = b; + const own: () => rt.u32 = (): rt.u32 => { + captured2 = rt.u32WrappingAdd(captured2, captured3); + return captured2; + }; + const part13 = own(); + put(part13); + const part14 = own(); + put(part14); + put(m); + const pair: [rt.u32, rt.u32] = [a, b]; + const part16 = ((): rt.u32 => { + pair[0] = rt.u32WrappingAdd(pair[0], pair[1]); + return pair[0]; + })(); + put(part16); + put(pair[0]); + const part18 = through$3(rt.place(() => m, (value) => m = value), b); + put(part18); + put(m); + const part20 = ((): rt.u32 => { + return ((): rt.u32 => { + n3 = rt.u32WrappingSub(n3, b); + return n3; + })(); + })(); + put(part20); + put(n3); +} + +export function through$4(n: rt.Place, b: rt.u64): rt.u64 { + return ((): rt.u64 => { + n.v = rt.u64WrappingAdd(n.v, b); + return n.v; + })(); +} + +/** Each way a closure writes a captured integer, from two operands taken from `data`. */ +export function u64s(data: rt.ByteSlice, out: rt.ByteVec): void { + const buf: Uint8Array = rt.bytes(8); + const n: rt.usize = rt.min(data.length, rt.byteSliceFrom(buf).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf), 0, n), rt.byteSliceRange(data, 0, n)); + const _18: rt.u64 = rt.u64FromBeBytes(buf); + const _20: rt.ByteSlice = rt.byteSliceRange(data, n); + const a: rt.u64 = _18; + const buf2: Uint8Array = rt.bytes(8); + const n2: rt.usize = rt.min(_20.length, rt.byteSliceFrom(buf2).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf2), 0, n2), rt.byteSliceRange(_20, 0, n2)); + const b: rt.u64 = ([rt.u64FromBeBytes(buf2), rt.byteSliceRange(_20, n2)] as [rt.u64, rt.ByteSlice])[0]; + const put: (a0: rt.u64) => void = (v: rt.u64): void => { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(v))); + }; + let n3: rt.u64 = a; + const part = ((): rt.u64 => { + n3 = rt.u64WrappingAdd(n3, b); + return n3; + })(); + put(part); + put(n3); + const bump2: () => rt.u64 = (): rt.u64 => { + n3 = rt.u64WrappingMul(n3, b); + return n3; + }; + const part3 = bump2(); + put(part3); + const part4 = bump2(); + put(part4); + put(n3); + const part6 = ((): rt.u64 => { + if (!(rt.u64And(b, 1) === 1)) { + return n3; + } + n3 = rt.u64WrappingSub(n3, a); + return n3; + })(); + put(part6); + put(n3); + const captured = rt.byteSliceToByteVec(_20); + const part8 = ((): rt.u64 => { + n3 = rt.u64WrappingAdd(n3, captured.length as rt.u64); + return n3; + })(); + put(part8); + put(n3); + const part10 = once$u64_u64s_closure5((): rt.u64 => { + n3 = rt.u64WrappingAdd(n3, b); + return n3; + }); + put(part10); + put(n3); + const _108: [rt.u64, rt.u64] = twice$u64_u64s_closure6((): rt.u64 => { + n3 = rt.u64WrappingAdd(n3, a); + return n3; + }); + const x: rt.u64 = _108[0]; + const y: rt.u64 = _108[1]; + put(x); + put(y); + put(n3); + let m: rt.u64 = a; + let captured2 = m; + const captured3 = b; + const own: () => rt.u64 = (): rt.u64 => { + captured2 = rt.u64WrappingAdd(captured2, captured3); + return captured2; + }; + const part13 = own(); + put(part13); + const part14 = own(); + put(part14); + put(m); + const pair: [rt.u64, rt.u64] = [a, b]; + const part16 = ((): rt.u64 => { + pair[0] = rt.u64WrappingAdd(pair[0], pair[1]); + return pair[0]; + })(); + put(part16); + put(pair[0]); + const part18 = through$4(rt.place(() => m, (value) => m = value), b); + put(part18); + put(m); + const part20 = ((): rt.u64 => { + return ((): rt.u64 => { + n3 = rt.u64WrappingSub(n3, b); + return n3; + })(); + })(); + put(part20); + put(n3); +} + +export function through$5(n: rt.Place, b: rt.u128): rt.u128 { + return ((): rt.u128 => { + n.v = rt.u128WrappingAdd(n.v, b); + return n.v; + })(); +} + +/** Each way a closure writes a captured integer, from two operands taken from `data`. */ +export function u128s(data: rt.ByteSlice, out: rt.ByteVec): void { + const buf: Uint8Array = rt.bytes(16); + const n: rt.usize = rt.min(data.length, rt.byteSliceFrom(buf).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf), 0, n), rt.byteSliceRange(data, 0, n)); + const _18: rt.u128 = rt.u128FromBeBytes(buf); + const _20: rt.ByteSlice = rt.byteSliceRange(data, n); + const a: rt.u128 = _18; + const buf2: Uint8Array = rt.bytes(16); + const n2: rt.usize = rt.min(_20.length, rt.byteSliceFrom(buf2).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf2), 0, n2), rt.byteSliceRange(_20, 0, n2)); + const b: rt.u128 = ([rt.u128FromBeBytes(buf2), rt.byteSliceRange(_20, n2)] as [rt.u128, rt.ByteSlice])[0]; + const put: (a0: rt.u128) => void = (v: rt.u128): void => { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u128ToBeBytes(v))); + }; + let n3: rt.u128 = a; + const part = ((): rt.u128 => { + n3 = rt.u128WrappingAdd(n3, b); + return n3; + })(); + put(part); + put(n3); + const bump2: () => rt.u128 = (): rt.u128 => { + n3 = rt.u128WrappingMul(n3, b); + return n3; + }; + const part3 = bump2(); + put(part3); + const part4 = bump2(); + put(part4); + put(n3); + const part6 = ((): rt.u128 => { + if (!(rt.u128And(b, 1) === 1)) { + return n3; + } + n3 = rt.u128WrappingSub(n3, a); + return n3; + })(); + put(part6); + put(n3); + const captured = rt.byteSliceToByteVec(_20); + const part8 = ((): rt.u128 => { + n3 = rt.u128WrappingAdd(n3, captured.length as rt.u128); + return n3; + })(); + put(part8); + put(n3); + const part10 = once$u128_u128s_closure5((): rt.u128 => { + n3 = rt.u128WrappingAdd(n3, b); + return n3; + }); + put(part10); + put(n3); + const _108: [rt.u128, rt.u128] = twice$u128_u128s_closure6((): rt.u128 => { + n3 = rt.u128WrappingAdd(n3, a); + return n3; + }); + const x: rt.u128 = _108[0]; + const y: rt.u128 = _108[1]; + put(x); + put(y); + put(n3); + let m: rt.u128 = a; + let captured2 = m; + const captured3 = b; + const own: () => rt.u128 = (): rt.u128 => { + captured2 = rt.u128WrappingAdd(captured2, captured3); + return captured2; + }; + const part13 = own(); + put(part13); + const part14 = own(); + put(part14); + put(m); + const pair: [rt.u128, rt.u128] = [a, b]; + const part16 = ((): rt.u128 => { + pair[0] = rt.u128WrappingAdd(pair[0], pair[1]); + return pair[0]; + })(); + put(part16); + put(pair[0]); + const part18 = through$5(rt.place(() => m, (value) => m = value), b); + put(part18); + put(m); + const part20 = ((): rt.u128 => { + return ((): rt.u128 => { + n3 = rt.u128WrappingSub(n3, b); + return n3; + })(); + })(); + put(part20); + put(n3); +} + +export function through$6(n: rt.Place, b: rt.usize): rt.usize { + return ((): rt.usize => { + n.v = rt.usizeWrappingAdd(n.v, b); + return n.v; + })(); +} + +/** Each way a closure writes a captured integer, from two operands taken from `data`. */ +export function usizes(data: rt.ByteSlice, out: rt.ByteVec): void { + const buf: Uint8Array = rt.bytes(4); + const n: rt.usize = rt.min(data.length, rt.byteSliceFrom(buf).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf), 0, n), rt.byteSliceRange(data, 0, n)); + const _18: rt.usize = rt.usizeFromBeBytes(buf); + const _20: rt.ByteSlice = rt.byteSliceRange(data, n); + const a: rt.usize = _18; + const buf2: Uint8Array = rt.bytes(4); + const n2: rt.usize = rt.min(_20.length, rt.byteSliceFrom(buf2).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf2), 0, n2), rt.byteSliceRange(_20, 0, n2)); + const b: rt.usize = ([rt.usizeFromBeBytes(buf2), rt.byteSliceRange(_20, n2)] as [rt.usize, rt.ByteSlice])[0]; + const put: (a0: rt.usize) => void = (v: rt.usize): void => { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.usizeToBeBytes(v))); + }; + let n3: rt.usize = a; + const part = ((): rt.usize => { + n3 = rt.usizeWrappingAdd(n3, b); + return n3; + })(); + put(part); + put(n3); + const bump2: () => rt.usize = (): rt.usize => { + n3 = rt.usizeWrappingMul(n3, b); + return n3; + }; + const part3 = bump2(); + put(part3); + const part4 = bump2(); + put(part4); + put(n3); + const part6 = ((): rt.usize => { + if (!(((b & 1) >>> 0) === 1)) { + return n3; + } + n3 = rt.usizeWrappingSub(n3, a); + return n3; + })(); + put(part6); + put(n3); + const captured = rt.byteSliceToByteVec(_20); + const part8 = ((): rt.usize => { + n3 = rt.usizeWrappingAdd(n3, captured.length); + return n3; + })(); + put(part8); + put(n3); + const part10 = once$usize_usizes_closure5((): rt.usize => { + n3 = rt.usizeWrappingAdd(n3, b); + return n3; + }); + put(part10); + put(n3); + const _108: [rt.usize, rt.usize] = twice$usize_usizes_closure6((): rt.usize => { + n3 = rt.usizeWrappingAdd(n3, a); + return n3; + }); + const x: rt.usize = _108[0]; + const y: rt.usize = _108[1]; + put(x); + put(y); + put(n3); + let m: rt.usize = a; + let captured2 = m; + const captured3 = b; + const own: () => rt.usize = (): rt.usize => { + captured2 = rt.usizeWrappingAdd(captured2, captured3); + return captured2; + }; + const part13 = own(); + put(part13); + const part14 = own(); + put(part14); + put(m); + const pair: [rt.usize, rt.usize] = [a, b]; + const part16 = ((): rt.usize => { + pair[0] = rt.usizeWrappingAdd(pair[0], pair[1]); + return pair[0]; + })(); + put(part16); + put(pair[0]); + const part18 = through$6(rt.place(() => m, (value) => m = value), b); + put(part18); + put(m); + const part20 = ((): rt.usize => { + return ((): rt.usize => { + n3 = rt.usizeWrappingSub(n3, b); + return n3; + })(); + })(); + put(part20); + put(n3); +} + +/** Each way a closure writes a captured integer, from two operands taken from `data`. */ +export function i8s(data: rt.ByteSlice, out: rt.ByteVec): void { + const buf: Uint8Array = Uint8Array.of(0); + const n: rt.usize = rt.min(data.length, rt.byteSliceFrom(buf).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf), 0, n), rt.byteSliceRange(data, 0, n)); + const _18: rt.i8 = rt.i8FromBeBytes(buf); + const _20: rt.ByteSlice = rt.byteSliceRange(data, n); + const a: rt.i8 = _18; + const buf2: Uint8Array = Uint8Array.of(0); + const n2: rt.usize = rt.min(_20.length, rt.byteSliceFrom(buf2).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf2), 0, n2), rt.byteSliceRange(_20, 0, n2)); + const b: rt.i8 = ([rt.i8FromBeBytes(buf2), rt.byteSliceRange(_20, n2)] as [rt.i8, rt.ByteSlice])[0]; + const put: (a0: rt.i8) => void = (v: rt.i8): void => { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.i8ToBeBytes(v))); + }; + let n3: rt.i8 = a; + const part = ((): rt.i8 => { + n3 = rt.i8WrappingAdd(n3, b); + return n3; + })(); + put(part); + put(n3); + const bump2: () => rt.i8 = (): rt.i8 => { + n3 = rt.i8WrappingMul(n3, b); + return n3; + }; + const part3 = bump2(); + put(part3); + const part4 = bump2(); + put(part4); + put(n3); + const part6 = ((): rt.i8 => { + if (!((b & 1) === 1)) { + return n3; + } + n3 = rt.i8WrappingSub(n3, a); + return n3; + })(); + put(part6); + put(n3); + const captured = rt.byteSliceToByteVec(_20); + const part8 = ((): rt.i8 => { + n3 = rt.i8WrappingAdd(n3, (captured.length << 24) >> 24); + return n3; + })(); + put(part8); + put(n3); + const part10 = once$i8_i8s_closure5((): rt.i8 => { + n3 = rt.i8WrappingAdd(n3, b); + return n3; + }); + put(part10); + put(n3); + const _108: [rt.i8, rt.i8] = twice$i8_i8s_closure6((): rt.i8 => { + n3 = rt.i8WrappingAdd(n3, a); + return n3; + }); + const x: rt.i8 = _108[0]; + const y: rt.i8 = _108[1]; + put(x); + put(y); + put(n3); + let m: rt.i8 = a; + let captured2 = m; + const captured3 = b; + const own: () => rt.i8 = (): rt.i8 => { + captured2 = rt.i8WrappingAdd(captured2, captured3); + return captured2; + }; + const part13 = own(); + put(part13); + const part14 = own(); + put(part14); + put(m); + const pair: [rt.i8, rt.i8] = [a, b]; + const part16 = ((): rt.i8 => { + pair[0] = rt.i8WrappingAdd(pair[0], pair[1]); + return pair[0]; + })(); + put(part16); + put(pair[0]); + const part18 = through$7(rt.place(() => m, (value) => m = value), b); + put(part18); + put(m); + const part20 = ((): rt.i8 => { + return ((): rt.i8 => { + n3 = rt.i8WrappingSub(n3, b); + return n3; + })(); + })(); + put(part20); + put(n3); +} + +export function through$7(n: rt.Place, b: rt.i8): rt.i8 { + return ((): rt.i8 => { + n.v = rt.i8WrappingAdd(n.v, b); + return n.v; + })(); +} + +/** Each way a closure writes a captured integer, from two operands taken from `data`. */ +export function i16s(data: rt.ByteSlice, out: rt.ByteVec): void { + const buf: Uint8Array = rt.bytes(2); + const n: rt.usize = rt.min(data.length, rt.byteSliceFrom(buf).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf), 0, n), rt.byteSliceRange(data, 0, n)); + const _18: rt.i16 = rt.i16FromBeBytes(buf); + const _20: rt.ByteSlice = rt.byteSliceRange(data, n); + const a: rt.i16 = _18; + const buf2: Uint8Array = rt.bytes(2); + const n2: rt.usize = rt.min(_20.length, rt.byteSliceFrom(buf2).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf2), 0, n2), rt.byteSliceRange(_20, 0, n2)); + const b: rt.i16 = ([rt.i16FromBeBytes(buf2), rt.byteSliceRange(_20, n2)] as [rt.i16, rt.ByteSlice])[0]; + const put: (a0: rt.i16) => void = (v: rt.i16): void => { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.i16ToBeBytes(v))); + }; + let n3: rt.i16 = a; + const part = ((): rt.i16 => { + n3 = rt.i16WrappingAdd(n3, b); + return n3; + })(); + put(part); + put(n3); + const bump2: () => rt.i16 = (): rt.i16 => { + n3 = rt.i16WrappingMul(n3, b); + return n3; + }; + const part3 = bump2(); + put(part3); + const part4 = bump2(); + put(part4); + put(n3); + const part6 = ((): rt.i16 => { + if (!((b & 1) === 1)) { + return n3; + } + n3 = rt.i16WrappingSub(n3, a); + return n3; + })(); + put(part6); + put(n3); + const captured = rt.byteSliceToByteVec(_20); + const part8 = ((): rt.i16 => { + n3 = rt.i16WrappingAdd(n3, (captured.length << 16) >> 16); + return n3; + })(); + put(part8); + put(n3); + const part10 = once$i16_i16s_closure5((): rt.i16 => { + n3 = rt.i16WrappingAdd(n3, b); + return n3; + }); + put(part10); + put(n3); + const _108: [rt.i16, rt.i16] = twice$i16_i16s_closure6((): rt.i16 => { + n3 = rt.i16WrappingAdd(n3, a); + return n3; + }); + const x: rt.i16 = _108[0]; + const y: rt.i16 = _108[1]; + put(x); + put(y); + put(n3); + let m: rt.i16 = a; + let captured2 = m; + const captured3 = b; + const own: () => rt.i16 = (): rt.i16 => { + captured2 = rt.i16WrappingAdd(captured2, captured3); + return captured2; + }; + const part13 = own(); + put(part13); + const part14 = own(); + put(part14); + put(m); + const pair: [rt.i16, rt.i16] = [a, b]; + const part16 = ((): rt.i16 => { + pair[0] = rt.i16WrappingAdd(pair[0], pair[1]); + return pair[0]; + })(); + put(part16); + put(pair[0]); + const part18 = through$8(rt.place(() => m, (value) => m = value), b); + put(part18); + put(m); + const part20 = ((): rt.i16 => { + return ((): rt.i16 => { + n3 = rt.i16WrappingSub(n3, b); + return n3; + })(); + })(); + put(part20); + put(n3); +} + +export function through$8(n: rt.Place, b: rt.i16): rt.i16 { + return ((): rt.i16 => { + n.v = rt.i16WrappingAdd(n.v, b); + return n.v; + })(); +} + +/** Each way a closure writes a captured integer, from two operands taken from `data`. */ +export function i32s(data: rt.ByteSlice, out: rt.ByteVec): void { + const buf: Uint8Array = rt.bytes(4); + const n: rt.usize = rt.min(data.length, rt.byteSliceFrom(buf).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf), 0, n), rt.byteSliceRange(data, 0, n)); + const _18: rt.i32 = rt.i32FromBeBytes(buf); + const _20: rt.ByteSlice = rt.byteSliceRange(data, n); + const a: rt.i32 = _18; + const buf2: Uint8Array = rt.bytes(4); + const n2: rt.usize = rt.min(_20.length, rt.byteSliceFrom(buf2).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf2), 0, n2), rt.byteSliceRange(_20, 0, n2)); + const b: rt.i32 = ([rt.i32FromBeBytes(buf2), rt.byteSliceRange(_20, n2)] as [rt.i32, rt.ByteSlice])[0]; + const put: (a0: rt.i32) => void = (v: rt.i32): void => { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.i32ToBeBytes(v))); + }; + let n3: rt.i32 = a; + const part = ((): rt.i32 => { + n3 = rt.i32WrappingAdd(n3, b); + return n3; + })(); + put(part); + put(n3); + const bump2: () => rt.i32 = (): rt.i32 => { + n3 = rt.i32WrappingMul(n3, b); + return n3; + }; + const part3 = bump2(); + put(part3); + const part4 = bump2(); + put(part4); + put(n3); + const part6 = ((): rt.i32 => { + if (!((b & 1) === 1)) { + return n3; + } + n3 = rt.i32WrappingSub(n3, a); + return n3; + })(); + put(part6); + put(n3); + const captured = rt.byteSliceToByteVec(_20); + const part8 = ((): rt.i32 => { + n3 = rt.i32WrappingAdd(n3, captured.length | 0); + return n3; + })(); + put(part8); + put(n3); + const part10 = once$i32_i32s_closure5((): rt.i32 => { + n3 = rt.i32WrappingAdd(n3, b); + return n3; + }); + put(part10); + put(n3); + const _108: [rt.i32, rt.i32] = twice$i32_i32s_closure6((): rt.i32 => { + n3 = rt.i32WrappingAdd(n3, a); + return n3; + }); + const x: rt.i32 = _108[0]; + const y: rt.i32 = _108[1]; + put(x); + put(y); + put(n3); + let m: rt.i32 = a; + let captured2 = m; + const captured3 = b; + const own: () => rt.i32 = (): rt.i32 => { + captured2 = rt.i32WrappingAdd(captured2, captured3); + return captured2; + }; + const part13 = own(); + put(part13); + const part14 = own(); + put(part14); + put(m); + const pair: [rt.i32, rt.i32] = [a, b]; + const part16 = ((): rt.i32 => { + pair[0] = rt.i32WrappingAdd(pair[0], pair[1]); + return pair[0]; + })(); + put(part16); + put(pair[0]); + const part18 = through$9(rt.place(() => m, (value) => m = value), b); + put(part18); + put(m); + const part20 = ((): rt.i32 => { + return ((): rt.i32 => { + n3 = rt.i32WrappingSub(n3, b); + return n3; + })(); + })(); + put(part20); + put(n3); +} + +export function through$9(n: rt.Place, b: rt.i32): rt.i32 { + return ((): rt.i32 => { + n.v = rt.i32WrappingAdd(n.v, b); + return n.v; + })(); +} + +/** Each way a closure writes a captured integer, from two operands taken from `data`. */ +export function i64s(data: rt.ByteSlice, out: rt.ByteVec): void { + const buf: Uint8Array = rt.bytes(8); + const n: rt.usize = rt.min(data.length, rt.byteSliceFrom(buf).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf), 0, n), rt.byteSliceRange(data, 0, n)); + const _18: rt.i64 = rt.i64FromBeBytes(buf); + const _20: rt.ByteSlice = rt.byteSliceRange(data, n); + const a: rt.i64 = _18; + const buf2: Uint8Array = rt.bytes(8); + const n2: rt.usize = rt.min(_20.length, rt.byteSliceFrom(buf2).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf2), 0, n2), rt.byteSliceRange(_20, 0, n2)); + const b: rt.i64 = ([rt.i64FromBeBytes(buf2), rt.byteSliceRange(_20, n2)] as [rt.i64, rt.ByteSlice])[0]; + const put: (a0: rt.i64) => void = (v: rt.i64): void => { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.i64ToBeBytes(v))); + }; + let n3: rt.i64 = a; + const part = ((): rt.i64 => { + n3 = rt.i64WrappingAdd(n3, b); + return n3; + })(); + put(part); + put(n3); + const bump2: () => rt.i64 = (): rt.i64 => { + n3 = rt.i64WrappingMul(n3, b); + return n3; + }; + const part3 = bump2(); + put(part3); + const part4 = bump2(); + put(part4); + put(n3); + const part6 = ((): rt.i64 => { + if (!(rt.i64And(b, 1) === 1)) { + return n3; + } + n3 = rt.i64WrappingSub(n3, a); + return n3; + })(); + put(part6); + put(n3); + const captured = rt.byteSliceToByteVec(_20); + const part8 = ((): rt.i64 => { + n3 = rt.i64WrappingAdd(n3, captured.length as rt.i64); + return n3; + })(); + put(part8); + put(n3); + const part10 = once$i64_i64s_closure5((): rt.i64 => { + n3 = rt.i64WrappingAdd(n3, b); + return n3; + }); + put(part10); + put(n3); + const _108: [rt.i64, rt.i64] = twice$i64_i64s_closure6((): rt.i64 => { + n3 = rt.i64WrappingAdd(n3, a); + return n3; + }); + const x: rt.i64 = _108[0]; + const y: rt.i64 = _108[1]; + put(x); + put(y); + put(n3); + let m: rt.i64 = a; + let captured2 = m; + const captured3 = b; + const own: () => rt.i64 = (): rt.i64 => { + captured2 = rt.i64WrappingAdd(captured2, captured3); + return captured2; + }; + const part13 = own(); + put(part13); + const part14 = own(); + put(part14); + put(m); + const pair: [rt.i64, rt.i64] = [a, b]; + const part16 = ((): rt.i64 => { + pair[0] = rt.i64WrappingAdd(pair[0], pair[1]); + return pair[0]; + })(); + put(part16); + put(pair[0]); + const part18 = through$10(rt.place(() => m, (value) => m = value), b); + put(part18); + put(m); + const part20 = ((): rt.i64 => { + return ((): rt.i64 => { + n3 = rt.i64WrappingSub(n3, b); + return n3; + })(); + })(); + put(part20); + put(n3); +} + +export function through$10(n: rt.Place, b: rt.i64): rt.i64 { + return ((): rt.i64 => { + n.v = rt.i64WrappingAdd(n.v, b); + return n.v; + })(); +} + +/** Each way a closure writes a captured integer, from two operands taken from `data`. */ +export function i128s(data: rt.ByteSlice, out: rt.ByteVec): void { + const buf: Uint8Array = rt.bytes(16); + const n: rt.usize = rt.min(data.length, rt.byteSliceFrom(buf).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf), 0, n), rt.byteSliceRange(data, 0, n)); + const _18: rt.i128 = rt.i128FromBeBytes(buf); + const _20: rt.ByteSlice = rt.byteSliceRange(data, n); + const a: rt.i128 = _18; + const buf2: Uint8Array = rt.bytes(16); + const n2: rt.usize = rt.min(_20.length, rt.byteSliceFrom(buf2).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf2), 0, n2), rt.byteSliceRange(_20, 0, n2)); + const b: rt.i128 = ([rt.i128FromBeBytes(buf2), rt.byteSliceRange(_20, n2)] as [rt.i128, rt.ByteSlice])[0]; + const put: (a0: rt.i128) => void = (v: rt.i128): void => { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.i128ToBeBytes(v))); + }; + let n3: rt.i128 = a; + const part = ((): rt.i128 => { + n3 = rt.i128WrappingAdd(n3, b); + return n3; + })(); + put(part); + put(n3); + const bump2: () => rt.i128 = (): rt.i128 => { + n3 = rt.i128WrappingMul(n3, b); + return n3; + }; + const part3 = bump2(); + put(part3); + const part4 = bump2(); + put(part4); + put(n3); + const part6 = ((): rt.i128 => { + if (!(rt.i128And(b, 1) === 1)) { + return n3; + } + n3 = rt.i128WrappingSub(n3, a); + return n3; + })(); + put(part6); + put(n3); + const captured = rt.byteSliceToByteVec(_20); + const part8 = ((): rt.i128 => { + n3 = rt.i128WrappingAdd(n3, captured.length as rt.i128); + return n3; + })(); + put(part8); + put(n3); + const part10 = once$i128_i128s_closure5((): rt.i128 => { + n3 = rt.i128WrappingAdd(n3, b); + return n3; + }); + put(part10); + put(n3); + const _108: [rt.i128, rt.i128] = twice$i128_i128s_closure6((): rt.i128 => { + n3 = rt.i128WrappingAdd(n3, a); + return n3; + }); + const x: rt.i128 = _108[0]; + const y: rt.i128 = _108[1]; + put(x); + put(y); + put(n3); + let m: rt.i128 = a; + let captured2 = m; + const captured3 = b; + const own: () => rt.i128 = (): rt.i128 => { + captured2 = rt.i128WrappingAdd(captured2, captured3); + return captured2; + }; + const part13 = own(); + put(part13); + const part14 = own(); + put(part14); + put(m); + const pair: [rt.i128, rt.i128] = [a, b]; + const part16 = ((): rt.i128 => { + pair[0] = rt.i128WrappingAdd(pair[0], pair[1]); + return pair[0]; + })(); + put(part16); + put(pair[0]); + const part18 = through$11(rt.place(() => m, (value) => m = value), b); + put(part18); + put(m); + const part20 = ((): rt.i128 => { + return ((): rt.i128 => { + n3 = rt.i128WrappingSub(n3, b); + return n3; + })(); + })(); + put(part20); + put(n3); +} + +export function through$11(n: rt.Place, b: rt.i128): rt.i128 { + return ((): rt.i128 => { + n.v = rt.i128WrappingAdd(n.v, b); + return n.v; + })(); +} + +/** Each way a closure writes a captured integer, from two operands taken from `data`. */ +export function isizes(data: rt.ByteSlice, out: rt.ByteVec): void { + const buf: Uint8Array = rt.bytes(4); + const n: rt.usize = rt.min(data.length, rt.byteSliceFrom(buf).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf), 0, n), rt.byteSliceRange(data, 0, n)); + const _18: rt.isize = rt.isizeFromBeBytes(buf); + const _20: rt.ByteSlice = rt.byteSliceRange(data, n); + const a: rt.isize = _18; + const buf2: Uint8Array = rt.bytes(4); + const n2: rt.usize = rt.min(_20.length, rt.byteSliceFrom(buf2).length); + rt.byteSliceCopyFromSlice(rt.byteSliceRange(rt.byteSliceFrom(buf2), 0, n2), rt.byteSliceRange(_20, 0, n2)); + const b: rt.isize = ([rt.isizeFromBeBytes(buf2), rt.byteSliceRange(_20, n2)] as [rt.isize, rt.ByteSlice])[0]; + const put: (a0: rt.isize) => void = (v: rt.isize): void => { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.isizeToBeBytes(v))); + }; + let n3: rt.isize = a; + const part = ((): rt.isize => { + n3 = rt.isizeWrappingAdd(n3, b); + return n3; + })(); + put(part); + put(n3); + const bump2: () => rt.isize = (): rt.isize => { + n3 = rt.isizeWrappingMul(n3, b); + return n3; + }; + const part3 = bump2(); + put(part3); + const part4 = bump2(); + put(part4); + put(n3); + const part6 = ((): rt.isize => { + if (!((b & 1) === 1)) { + return n3; + } + n3 = rt.isizeWrappingSub(n3, a); + return n3; + })(); + put(part6); + put(n3); + const captured = rt.byteSliceToByteVec(_20); + const part8 = ((): rt.isize => { + n3 = rt.isizeWrappingAdd(n3, captured.length | 0); + return n3; + })(); + put(part8); + put(n3); + const part10 = once$isize_isizes_closure5((): rt.isize => { + n3 = rt.isizeWrappingAdd(n3, b); + return n3; + }); + put(part10); + put(n3); + const _108: [rt.isize, rt.isize] = twice$isize_isizes_closure6((): rt.isize => { + n3 = rt.isizeWrappingAdd(n3, a); + return n3; + }); + const x: rt.isize = _108[0]; + const y: rt.isize = _108[1]; + put(x); + put(y); + put(n3); + let m: rt.isize = a; + let captured2 = m; + const captured3 = b; + const own: () => rt.isize = (): rt.isize => { + captured2 = rt.isizeWrappingAdd(captured2, captured3); + return captured2; + }; + const part13 = own(); + put(part13); + const part14 = own(); + put(part14); + put(m); + const pair: [rt.isize, rt.isize] = [a, b]; + const part16 = ((): rt.isize => { + pair[0] = rt.isizeWrappingAdd(pair[0], pair[1]); + return pair[0]; + })(); + put(part16); + put(pair[0]); + const part18 = through$12(rt.place(() => m, (value) => m = value), b); + put(part18); + put(m); + const part20 = ((): rt.isize => { + return ((): rt.isize => { + n3 = rt.isizeWrappingSub(n3, b); + return n3; + })(); + })(); + put(part20); + put(n3); +} + +export function through$12(n: rt.Place, b: rt.isize): rt.isize { + return ((): rt.isize => { + n.v = rt.isizeWrappingAdd(n.v, b); + return n.v; + })(); +} + +/** Run [`writes`] over the integer type `kind` picks. */ +export function captures(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const _4: rt.u8 = rt.u8Rem(kind, 12); + if (_4 === 0) { + u8s(data, out); + } else if (_4 === 1) { + u16s(data, out); + } else if (_4 === 2) { + u32s(data, out); + } else if (_4 === 3) { + u64s(data, out); + } else if (_4 === 4) { + u128s(data, out); + } else if (_4 === 5) { + usizes(data, out); + } else if (_4 === 6) { + i8s(data, out); + } else if (_4 === 7) { + i16s(data, out); + } else if (_4 === 8) { + i32s(data, out); + } else if (_4 === 9) { + i64s(data, out); + } else if (_4 === 10) { + i128s(data, out); + } else { + isizes(data, out); + } +} + +export function once$i128_i128s_closure5(f: () => rt.i128): rt.i128 { + return f(); +} + +export function once$i16_i16s_closure5(f: () => rt.i16): rt.i16 { + return f(); +} + +export function once$i32_i32s_closure5(f: () => rt.i32): rt.i32 { + return f(); +} + +export function once$i64_i64s_closure5(f: () => rt.i64): rt.i64 { + return f(); +} + +export function once$i8_i8s_closure5(f: () => rt.i8): rt.i8 { + return f(); +} + +export function once$isize_isizes_closure5(f: () => rt.isize): rt.isize { + return f(); +} + +export function once$u128_u128s_closure5(f: () => rt.u128): rt.u128 { + return f(); +} + +export function once$u16_u16s_closure5(f: () => rt.u16): rt.u16 { + return f(); +} + +export function once$u32_u32s_closure5(f: () => rt.u32): rt.u32 { + return f(); +} + +export function once$u64_u64s_closure5(f: () => rt.u64): rt.u64 { + return f(); +} + +export function once$u8_u8s_closure5(f: () => rt.u8): rt.u8 { + return f(); +} + +export function once$usize_usizes_closure5(f: () => rt.usize): rt.usize { + return f(); +} + +export function twice$i128_i128s_closure6(f: () => rt.i128): [rt.i128, rt.i128] { + return [f(), f()] as [rt.i128, rt.i128]; +} + +export function twice$i16_i16s_closure6(f: () => rt.i16): [rt.i16, rt.i16] { + return [f(), f()] as [rt.i16, rt.i16]; +} + +export function twice$i32_i32s_closure6(f: () => rt.i32): [rt.i32, rt.i32] { + return [f(), f()] as [rt.i32, rt.i32]; +} + +export function twice$i64_i64s_closure6(f: () => rt.i64): [rt.i64, rt.i64] { + return [f(), f()] as [rt.i64, rt.i64]; +} + +export function twice$i8_i8s_closure6(f: () => rt.i8): [rt.i8, rt.i8] { + return [f(), f()] as [rt.i8, rt.i8]; +} + +export function twice$isize_isizes_closure6(f: () => rt.isize): [rt.isize, rt.isize] { + return [f(), f()] as [rt.isize, rt.isize]; +} + +export function twice$u128_u128s_closure6(f: () => rt.u128): [rt.u128, rt.u128] { + return [f(), f()] as [rt.u128, rt.u128]; +} + +export function twice$u16_u16s_closure6(f: () => rt.u16): [rt.u16, rt.u16] { + return [f(), f()] as [rt.u16, rt.u16]; +} + +export function twice$u32_u32s_closure6(f: () => rt.u32): [rt.u32, rt.u32] { + return [f(), f()] as [rt.u32, rt.u32]; +} + +export function twice$u64_u64s_closure6(f: () => rt.u64): [rt.u64, rt.u64] { + return [f(), f()] as [rt.u64, rt.u64]; +} + +export function twice$u8_u8s_closure6(f: () => rt.u8): [rt.u8, rt.u8] { + return [f(), f()] as [rt.u8, rt.u8]; +} + +export function twice$usize_usizes_closure6(f: () => rt.usize): [rt.usize, rt.usize] { + return [f(), f()] as [rt.usize, rt.usize]; +} + +//# sourceMappingURL=captures.ts.map diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index c09d484d5..c5aaa6209 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -2,6 +2,7 @@ import * as rt from "./rt.ts"; import { atomics } from "./atomics.ts"; +import { captures } from "./captures.ts"; import { cells } from "./cells.ts"; import { chars } from "./chars.ts"; import { chunks } from "./chunks.ts"; @@ -80,7 +81,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) dyns(data23, out24); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cells::cells", cells] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::shared", (_234: rt.u8, data24: rt.ByteSlice, out25: rt.ByteVec): void => { shared(data24, out25); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]; + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["captures::captures", captures] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]; } /** Run one case. */ diff --git a/ts/fixtures/lang/src/captures.rs b/ts/fixtures/lang/src/captures.rs new file mode 100644 index 000000000..836c1400b --- /dev/null +++ b/ts/fixtures/lang/src/captures.rs @@ -0,0 +1,156 @@ +//! Closures that write what they captured and return it, read after by the caller or by +//! the closure's next call. + +/// Take an integer's big-endian bytes from the front of `data`, zero-padded. +macro_rules! take { + ($t:ty, $data:expr) => {{ + let data: &[u8] = $data; + let mut buf = [0u8; core::mem::size_of::<$t>()]; + let n = data.len().min(buf.len()); + buf[..n].copy_from_slice(&data[..n]); + (<$t>::from_be_bytes(buf), &data[n..]) + }}; +} + +macro_rules! writes { + ($name:ident, $t:ty) => { + /// Each way a closure writes a captured integer, from two operands taken from `data`. + fn $name(data: &[u8], out: &mut Vec) { + let (a, data) = take!($t, data); + let (b, _) = take!($t, data); + let mut put = |v: $t| out.extend_from_slice(&v.to_be_bytes()); + + // By reference, called once and twice. + let mut n = a; + let mut bump = || { + n = n.wrapping_add(b); + n + }; + put(bump()); + put(n); + let mut bump = || { + n = n.wrapping_mul(b); + n + }; + put(bump()); + put(bump()); + put(n); + + // Only on one branch. + let mut maybe = || { + if b & 1 == 1 { + n = n.wrapping_sub(a); + n + } else { + n + } + }; + put(maybe()); + put(n); + + // An `FnOnce`, which moves what it owns out. + let owned = data.to_vec(); + let consume = || { + n = n.wrapping_add(owned.len() as $t); + drop(owned); + n + }; + put(consume()); + put(n); + + // Through a type parameter. + put(once(|| { + n = n.wrapping_add(b); + n + })); + put(n); + let (x, y) = twice(|| { + n = n.wrapping_add(a); + n + }); + put(x); + put(y); + put(n); + + // By value, so the closure's copy changes and the caller's doesn't. + let mut m = a; + let mut own = move || { + m = m.wrapping_add(b); + m + }; + put(own()); + put(own()); + put(m); + + // A field, captured alone. + let mut pair = (a, b); + let mut first = || { + pair.0 = pair.0.wrapping_add(pair.1); + pair.0 + }; + put(first()); + put(pair.0); + + // What a `&mut` parameter points at. + fn through(n: &mut $t, b: $t) -> $t { + let mut add = || { + *n = n.wrapping_add(b); + *n + }; + add() + } + put(through(&mut m, b)); + put(m); + + // From a closure inside another. + let mut outer = || { + let mut inner = || { + n = n.wrapping_sub(b); + n + }; + inner() + }; + put(outer()); + put(n); + } + }; +} + +writes!(u8s, u8); +writes!(u16s, u16); +writes!(u32s, u32); +writes!(u64s, u64); +writes!(u128s, u128); +writes!(usizes, usize); +writes!(i8s, i8); +writes!(i16s, i16); +writes!(i32s, i32); +writes!(i64s, i64); +writes!(i128s, i128); +writes!(isizes, isize); + +/// Run [`writes`] over the integer type `kind` picks. +pub fn captures(kind: u8, data: &[u8], out: &mut Vec) { + match kind % 12 { + 0 => u8s(data, out), + 1 => u16s(data, out), + 2 => u32s(data, out), + 3 => u64s(data, out), + 4 => u128s(data, out), + 5 => usizes(data, out), + 6 => i8s(data, out), + 7 => i16s(data, out), + 8 => i32s(data, out), + 9 => i64s(data, out), + 10 => i128s(data, out), + _ => isizes(data, out), + } +} + +fn once T>(f: F) -> T { + f() +} + +fn twice T>(mut f: F) -> (T, T) { + (f(), f()) +} diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 7b65f8411..4f18de815 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -2,8 +2,8 @@ //! case and the rest is its input. use crate::{ - atomics, cells, chars, chunks, collections, combinators, cows, durations, dyns, edits, floats, fmt, generics, ints, - iters, keys, nested, refs, shapes, splits, strs, utf8, wrappers, + atomics, captures, cells, chars, chunks, collections, combinators, cows, durations, dyns, edits, floats, fmt, + generics, ints, iters, keys, nested, refs, shapes, splits, strs, utf8, wrappers, }; /// The native side's entry: [`run`], except that a case starting with 0xff prints a @@ -86,6 +86,7 @@ fn cases() -> Vec<(&'static str, Case)> { ("dyns::dyns", |_, data, out| dyns::dyns(data, out)), ("cells::cells", cells::cells), ("dyns::shared", |_, data, out| dyns::shared(data, out)), + ("captures::captures", captures::captures), ] } diff --git a/ts/fixtures/lang/src/lib.rs b/ts/fixtures/lang/src/lib.rs index 54f53d853..d233c8890 100644 --- a/ts/fixtures/lang/src/lib.rs +++ b/ts/fixtures/lang/src/lib.rs @@ -1,15 +1,17 @@ //! The language the driver translates, exercised against native Rust: every integer //! width's arithmetic and casts, `f64`, `char`s and the `str` methods over them, //! `Cow`, `Duration`, control flow, patterns, byte slices split apart, `Copy` -//! aggregates, closures, traits, generic types, formatting, collections, UTF-8 -//! decoding, `&mut` to values JS copies, the standard library calls it maps, -//! `Option`'s and `Result`'s combinators from core's MIR, `Option`s of values that can -//! already be `undefined`, atomics, trait objects, and one-time cells. +//! aggregates, closures and what they write of what they captured, traits, generic +//! types, formatting, collections, UTF-8 decoding, `&mut` to values JS copies, the +//! standard library calls it maps, `Option`'s and `Result`'s combinators from core's +//! MIR, `Option`s of values that can already be `undefined`, atomics, trait objects, and +//! one-time cells. //! //! `harness::run` is the entry point both sides of the differential test run, and //! `fmt::Fault` an error only `caller.ts` prints. mod atomics; +mod captures; mod cells; mod chars; mod chunks; diff --git a/ts/src/lower/mir/mod.rs b/ts/src/lower/mir/mod.rs index 0558378b6..60af329b5 100644 --- a/ts/src/lower/mir/mod.rs +++ b/ts/src/lower/mir/mod.rs @@ -1503,7 +1503,17 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { drop_self_assignments(&mut out); let mut caught = Vec::new(); handler_reads(&out, &mut caught); - return_in_place(&mut out, &caught.into_iter().collect()); + // A return folds in the value assigned only to a local: a closure's captures are + // read after it returns, and a cleanup that reads a local runs after its assignment, + // were the statements between to throw. + let caught: FxHashSet = caught.into_iter().collect(); + let returnable = (self.slots.iter()) + .filter_map(|slot| match slot { + Slot::Var(name) if !caught.contains(name) => Some(name.clone()), + _ => None, + }) + .collect(); + return_in_place(&mut out, &returnable); prune_unread(&mut out); if self.zero_sized(self.mir.local_decls[mir::RETURN_PLACE].ty) { cfg::strip_return(&mut out); @@ -3736,36 +3746,37 @@ fn same_path(a: &Expr, b: &Expr) -> bool { } } -/// Return values in place throughout `stmts`, given the names cleanups read. -fn return_in_place(stmts: &mut Vec, caught: &FxHashSet) { +/// Return values in place throughout `stmts`, given the names of the body's own locals +/// that no cleanup reads. +fn return_in_place(stmts: &mut Vec, returnable: &FxHashSet) { sink_return(stmts); for stmt in stmts.iter_mut() { match &mut stmt.kind { StmtKind::If { then, els, .. } => { - return_in_place(then, caught); - return_in_place(els, caught); + return_in_place(then, returnable); + return_in_place(els, returnable); } StmtKind::While { body, .. } | StmtKind::ForOf { body, .. } | StmtKind::Block { body, .. } => { - return_in_place(body, caught) + return_in_place(body, returnable) } StmtKind::Switch { cases, default, .. } => { - cases.iter_mut().for_each(|(_, body)| return_in_place(body, caught)); - return_in_place(default, caught); + cases.iter_mut().for_each(|(_, body)| return_in_place(body, returnable)); + return_in_place(default, returnable); } StmtKind::Catch { body, handler, .. } => { - return_in_place(body, caught); - return_in_place(handler, caught); + return_in_place(body, returnable); + return_in_place(handler, returnable); } _ => {} } } - while return_assigned(stmts, caught) {} + while return_assigned(stmts, returnable) {} } /// `x = v; ...; return x;` at the end of `stmts` as `...; return v;`, where moving `v` /// past the statements between changes nothing (there are none, or `v` is a constant or a -/// name none of them assigns) and no cleanup reads `x`; whether it was. -fn return_assigned(stmts: &mut Vec, caught: &FxHashSet) -> bool { +/// name none of them assigns) and `x` is `returnable`; whether it was. +fn return_assigned(stmts: &mut Vec, returnable: &FxHashSet) -> bool { let Some(Stmt { kind: StmtKind::Return(Some(Expr::Ident(name))), .. @@ -3773,9 +3784,7 @@ fn return_assigned(stmts: &mut Vec, caught: &FxHashSet) -> bool { else { return false; }; - // A cleanup reading it would run after its assignment, were the statements between to - // throw. - if caught.contains(name) { + if !returnable.contains(name) { return false; } let name = name.clone();