From ef66b4f3b643bff216e96fbeccc8ac4aae209ee1 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 06:00:02 -0700 Subject: [PATCH 1/6] chore(quest): claim quest/m3/ts-string-iters.md Quest-Claim: d2b60e46ffe276eb From 4d22bce0059529b586e97aa8cb3144aa57b4757a Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 06:17:54 -0700 Subject: [PATCH 2/6] feat(ts): translate collect::, String's Extend, and String::drain collect and extend read their iterator by for_each, as std's do, of chars or of text, and collect of owned strings takes the first by next. A drain holds the &mut's place and writes the string without the drained text as it drops, so a forgotten one leaves it as it was. FromIterator::from_iter translates as collect does. Proven by the lang differential against native wasm32 at every offset of text of each UTF-8 width. Co-Authored-By: Claude Opus 5.5 --- quest/m3/README.md | 2 +- quest/m3/ts-size-hints.md | 20 ++ quest/m3/ts-string-iters.md | 20 -- ts/README.md | 10 +- ts/fixtures/lang/caller.ts | 16 +- ts/fixtures/lang/expected/harness.ts | 6 +- ts/fixtures/lang/expected/lib.ts | 24 +++ ts/fixtures/lang/expected/strs.Counted.ts | 45 +++++ ts/fixtures/lang/expected/strs.ts | 222 ++++++++++++++++++++++ ts/fixtures/lang/src/harness.rs | 2 + ts/fixtures/lang/src/strs.rs | 181 ++++++++++++++++++ ts/runtime/rt/str.ts | 97 +++++++++- ts/src/lower/drop.rs | 11 +- ts/src/lower/iter.rs | 41 ++++ ts/src/lower/library.rs | 7 + ts/src/lower/mir/call.rs | 11 ++ ts/src/lower/pointer.rs | 8 +- 17 files changed, 687 insertions(+), 36 deletions(-) create mode 100644 quest/m3/ts-size-hints.md delete mode 100644 quest/m3/ts-string-iters.md create mode 100644 ts/fixtures/lang/expected/strs.Counted.ts diff --git a/quest/m3/README.md b/quest/m3/README.md index c1c171504..f392b099c 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -21,7 +21,6 @@ generic std gap is planned here even when moq never calls it. - [The capacity of a BytesMut](/quest/m3/ts-bytes-capacity.md) - `BytesMut::capacity` and what reads it translate with `bytes`' values - [BufMut's provided writes through a &mut](/quest/m3/ts-buf-mut-forward.md) - `put`, `put_bytes`, `put_uint`, and `put_int` of a `&mut Vec` or `&mut BytesMut` translate - [Trait objects](/quest/m3/ts-dyn.md) - `dyn Error`, `dyn Iterator`, `dyn Buf`, and `Arc` translate, so a consumer neither rewrites nor binds them -- [Strings from and into iterators](/quest/m3/ts-string-iters.md) - `collect::`, `String::extend`, and `drain` translate - [Equality of Result and std's errors](/quest/m3/ts-error-eq.md) - `==` of a `Result` and of std's parse and conversion errors translates - [Placeholders written to a crate's own fmt::Write](/quest/m3/ts-fmt-calls.md) - `write!` with placeholders into a crate's own writer translates, its `write_str` seeing each call std makes - [Waiting: type-check in process with tsrs](/quest/m3/ts-tsrs.md) - rust2-ts checks bindings through tsrs instead of `tsc` once it is a stable library @@ -31,6 +30,7 @@ generic std gap is planned here even when moq never calls it. - [Drop order in std's folds and iterators](/quest/m3/ts-drop-order.md) - the values that drop still refused in std's folds, zips, flattens, and collects drop as std's do - [References to byte-array keys a collection yields](/quest/m3/ts-byte-key-refs.md) - `keys`, `iter`, `get_key_value`, and `HashSet::get` of a `[u8; N]` key translate - [An array as a slice](/quest/m3/ts-array-as-slice.md) - `<[T; N]>::as_slice` and `as_mut_slice` translate +- [Size hints of std's iterators](/quest/m3/ts-size-hints.md) - `size_hint` translates, and a collect or extend that reserves past `isize::MAX` panics as std's does - [reduce, max_by, min_by, and Peekable's next_if_eq and peek_mut](/quest/m3/ts-iter-reducers.md) - the iterator consumers std returns an item from that have no mapping translate - [Closures that return an Option whose None is rt.None](/quest/m3/ts-option-closures.md) - `find_map`, `filter_map`, and `map_while` to `Option>` keep `None` apart from `Some(None)` - [A slice's edits in place](/quest/m3/ts-slice-edits.md) - `fill`, `rotate_left`, and `copy_from_slice` of other values translate diff --git a/quest/m3/ts-size-hints.md b/quest/m3/ts-size-hints.md new file mode 100644 index 000000000..67185274f --- /dev/null +++ b/quest/m3/ts-size-hints.md @@ -0,0 +1,20 @@ +# [M] Size hints of std's iterators + +## Goal + +[rust2-ts](/ts/README.md) translates `Iterator::size_hint` of std's iterators, and each +collect and extend that reserves by one, such as `String`'s `Extend` and `Vec`'s +`collect`, panics with std's `capacity overflow` where std's does, proven by differential +tests against native Rust. + +## Plan + +Found by [#284](https://github.com/moq-dev/rust2/pull/284) on 2026-10-30, which +translates `String`'s `extend` and `collect` by `for_each` but skips the `reserve` of +the lower bound std makes first. An iterator whose lower bound passes `isize::MAX` on +wasm32, such as `repeat(c)` or `(0..u32::MAX).map(f)`, panics there in std before it +yields an item, where the translation reads on until memory runs out. `rt.Iter` keeps no +size hint, and the crate's own `size_hint` is refused, so no translated code observes one +today. + +Public API: the translator's output. diff --git a/quest/m3/ts-string-iters.md b/quest/m3/ts-string-iters.md deleted file mode 100644 index 7cd8deb12..000000000 --- a/quest/m3/ts-string-iters.md +++ /dev/null @@ -1,20 +0,0 @@ -# [S] Strings from and into iterators - -## Goal - -[rust2-ts](/ts/README.md) translates `collect::`, `String`'s `Extend` of chars and -strings, and `String::drain`, proven by differential tests against native Rust. - -## Plan - -Found by [#271](https://github.com/moq-dev/rust2/pull/271) on 2026-10-30, which translates -`String`'s edits that take and give plain values. Collecting chars into a `String` is -refused too. moq-net and kio call none of these, so this is m3 under the all-of-std -decision. - -- Each reads its iterator as std does: `Extend` asks `size_hint` first, which an own - iterator may observe. -- A `Drain` is an iterator of the drained chars; the `&mut String` it holds keeps anything - from seeing the string before it drops, so it can be edited at once. - -Public API: the translator's output. diff --git a/ts/README.md b/ts/README.md index 72ce465dd..191f30c9b 100644 --- a/ts/README.md +++ b/ts/README.md @@ -213,9 +213,11 @@ returns, from `rfind` or `match_indices`, counts UTF-8 bytes, as Rust's does, an one `&s[a..b]` takes, which reads the string up to it and panics as std does inside a char. A JS string can't change in place, so `String`'s edits, `push_str`, `truncate`, `insert`, `retain`, `replace_range`, and the rest, assign the edited one to the local or -field the `&mut` borrows, taking and checking byte offsets as std does. A `String` keeps no -capacity: `with_capacity`, `reserve`, and `shrink_to_fit` do nothing, and `capacity` is -refused. A `Cow` is its text, borrowed or owned, so +field the `&mut` borrows, taking and checking byte offsets as std does. `extend` and +`collect` read their iterator by `for_each`, as std's do. A `drain` holds the `&mut`'s +place and writes the string without the drained text as it drops, so one that +`mem::forget` leaks leaves it as it was, as std's does. A `String` keeps no capacity: +`with_capacity`, `reserve`, and `shrink_to_fit` do nothing, and `capacity` is refused. A `Cow` is its text, borrowed or owned, so `to_mut` borrows the `Cow`'s own place, and code that tells its variants apart, a `match` on them or `Cow::is_borrowed`, is refused. @@ -620,7 +622,7 @@ copies that std makes, other than a map's, or hands a closure, as `iter_mut` and without a mapping in `src/lower/iter.rs` (such as `cycle`, `unzip`, and `partition`), an own `impl Iterator` with `next_back`, `len`, or another provided method of its own, a pointer's address (`{:p}`), `write!` with a placeholder into -the crate's own `fmt::Write`, `String::extend` and `drain`, a `str` pattern other than a +the crate's own `fmt::Write`, a `str` pattern other than a `char`, a `&str`, a predicate, or chars, a mutable `str` slice, a copy of a closure that changes what it captured, `mem::swap`, `replace`, or `take` of a reference's whole pointee, assigning an `Arc`, a waker, a `MutexGuard`, a pinned reference, or a future through a diff --git a/ts/fixtures/lang/caller.ts b/ts/fixtures/lang/caller.ts index 50e518c8a..d29f38d3c 100644 --- a/ts/fixtures/lang/caller.ts +++ b/ts/fixtures/lang/caller.ts @@ -359,4 +359,18 @@ const ranges = [0, 1, 2, 3, 4].flatMap((n) => { }); }); -export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...formats, ...parsed, ...dyns, ...cells, ...shared, ...locks, ...onces, ...bounds, ...unsafeCells, ...edits, ...subslices, ...consumers, ...ranges]; +/** + * `strs::drains` of each range and way to end the drain on `aé\u{800}😀`, at every byte + * offset into it and up to two past its end, reading none, two from either end, or three + * from the back. + */ +const drainsOp = op("strs::drains"); +const drains = Array.from({ length: 32 * 13 * 13 }, (_, i) => { + const [form, a, b] = [i % 32, Math.floor(i / 32) % 13, Math.floor(i / 416)]; + return Uint8Array.of(drainsOp, 4, 2, 7, 9, 14, form, a, b, [0, 0x0a, 0x1f][(form + a + b) % 3]); +}); + +/** `strs::iters` of each kind, on no text, text of each UTF-8 width, and parts between `/`s, empty ones too. */ +const iters = Array.from({ length: 12 }, (_, kind) => [[], [2, 7, 9, 14], [0, 2, 0, 0, 7, 9], [14, 0]].map((data) => Uint8Array.of(op("strs::iters"), kind, ...data))).flat(); + +export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...formats, ...parsed, ...dyns, ...cells, ...shared, ...locks, ...onces, ...bounds, ...unsafeCells, ...edits, ...subslices, ...consumers, ...ranges, ...drains, ...iters]; diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 390532a3d..eea8a1474 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -37,7 +37,7 @@ import { copies as shapes_copies, order, discriminants, loops, patterns, closure import { parts, chunked, subslices, drains, ranges as slices_ranges } from "./slices.ts"; import { splits } from "./splits.ts"; import { statics, cells as statics_cells, onces } from "./statics.ts"; -import { strs, slices, edits as strs_edits, parses as strs_parses } from "./strs.ts"; +import { strs, slices, edits as strs_edits, parses as strs_parses, iters, drains as strs_drains } from "./strs.ts"; import { decodes } from "./utf8.ts"; import { wrappers } from "./wrappers.ts"; @@ -90,7 +90,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) refs(data21, out21); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_2121: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { counts(data22, out22); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2122: rt.u8, _366: rt.ByteSlice, out23: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2122: rt.u8, _372: rt.ByteSlice, out23: rt.ByteVec): void => { rounding(out23); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::overrides", overrides] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::dyns", (_2123: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { dyns(data23, out24); @@ -172,7 +172,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) hashes_bytes(data61, out62); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::bytes", keys_bytes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::parses", strs_parses] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::rebound", (_2162: rt.u8, data62: rt.ByteSlice, out63: rt.ByteVec): void => { rebound(data62, out63); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["slices::ranges", slices_ranges] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]).map(copy$str_forabfnu8au8bmutstdvecVecu8); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["slices::ranges", slices_ranges] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::iters", iters] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::drains", strs_drains] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]).map(copy$str_forabfnu8au8bmutstdvecVecu8); } /** Run one case. */ diff --git a/ts/fixtures/lang/expected/lib.ts b/ts/fixtures/lang/expected/lib.ts index 0cf12a289..758d4e417 100644 --- a/ts/fixtures/lang/expected/lib.ts +++ b/ts/fixtures/lang/expected/lib.ts @@ -152,10 +152,22 @@ export function copy$Option_Shape(v: Shape.Shape | undefined): Shape.Shape | und return v === undefined ? v : copy$Shape(v); } +export function copy$Option_String_Strings(v: [string, string[]] | undefined): [string, string[]] | undefined { + return v === undefined ? v : copy$String_Strings(v); +} + export function copy$Option_Wrap_i16(v: Wrap.Wrap | undefined): Wrap.Wrap | undefined { return v === undefined ? v : copy$Wrap_i16(v); } +export function copy$Option_char_chars(v: [rt.char, rt.char[]] | undefined): [rt.char, rt.char[]] | undefined { + return v === undefined ? v : copy$char_chars(v); +} + +export function copy$Option_str_strs(v: [string, string[]] | undefined): [string, string[]] | undefined { + return v === undefined ? v : copy$str_strs(v); +} + export function copy$Option_u64_u64(v: [rt.u64, rt.u64] | undefined): [rt.u64, rt.u64] | undefined { return v === undefined ? v : copy$u64_u64(v); } @@ -230,6 +242,10 @@ export function copy$Shape(v: Shape.Shape): Shape.Shape { return v; } +export function copy$String_Strings(v: [string, string[]]): [string, string[]] { + return [v[0], v[1]]; +} + export function copy$Tally(v: Tally.Tally): Tally.Tally { return { n: v.n }; } @@ -258,6 +274,10 @@ export function copy$Wrap_u8(v: Wrap.Wrap): Wrap.Wrap { return new Wrap.Wrap(v.value, v.count); } +export function copy$char_chars(v: [rt.char, rt.char[]]): [rt.char, rt.char[]] { + return [v[0], v[1]]; +} + export function copy$chars(v: rt.char[]): rt.char[] { return v.slice(); } @@ -274,6 +294,10 @@ export function copy$str_forabfnu8au8bmutstdvecVecu8s(v: [string, (a0: rt.u8, a1 return v.map(copy$str_forabfnu8au8bmutstdvecVecu8); } +export function copy$str_strs(v: [string, string[]]): [string, string[]] { + return [v[0], v[1]]; +} + export function copy$u32_u32(v: [rt.u32, rt.u32]): [rt.u32, rt.u32] { return [v[0], v[1]]; } diff --git a/ts/fixtures/lang/expected/strs.Counted.ts b/ts/fixtures/lang/expected/strs.Counted.ts new file mode 100644 index 000000000..fd882504a --- /dev/null +++ b/ts/fixtures/lang/expected/strs.Counted.ts @@ -0,0 +1,45 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { copy$Option_String_Strings, copy$Option_char_chars, copy$Option_str_strs } from "./lib.ts"; + +/** A crate's own iterator over `items`, which counts each `next` it is asked. */ +export interface Counted { + items: T[]; + nexts: rt.Cell; +} + +export function next$Iterator_String(self: Counted): string | rt.Done { + rt.cellSet(self.nexts, rt.u8Add(self.nexts.v, 1)); + const _8: [string, string[]] | undefined = copy$Option_String_Strings(rt.sliceSplitFirst(self.items)); + if (_8 !== undefined) { + const val: [string, string[]] = _8; + self.items = val[1]; + return val[0]; + } + return rt.DONE; +} + +export function next$Iterator_char(self: Counted): rt.char | rt.Done { + rt.cellSet(self.nexts, rt.u8Add(self.nexts.v, 1)); + const _8: [rt.char, rt.char[]] | undefined = copy$Option_char_chars(rt.sliceSplitFirst(self.items)); + if (_8 !== undefined) { + const val: [rt.char, rt.char[]] = _8; + self.items = val[1]; + return val[0]; + } + return rt.DONE; +} + +export function next$Iterator_str(self: Counted): string | rt.Done { + rt.cellSet(self.nexts, rt.u8Add(self.nexts.v, 1)); + const _8: [string, string[]] | undefined = copy$Option_str_strs(rt.sliceSplitFirst(self.items)); + if (_8 !== undefined) { + const val: [string, string[]] = _8; + self.items = val[1]; + return val[0]; + } + return rt.DONE; +} + +//# sourceMappingURL=strs.Counted.ts.map diff --git a/ts/fixtures/lang/expected/strs.ts b/ts/fixtures/lang/expected/strs.ts index eb8cf20a8..a89cc8b21 100644 --- a/ts/fixtures/lang/expected/strs.ts +++ b/ts/fixtures/lang/expected/strs.ts @@ -7,6 +7,7 @@ import { unwrapOr$1iemzn8, unwrapOr$0r2bmpr, map$0gl6wke, mapOr$147z9gi, mapOr$1 import { parse$0ub96so, parse$1ogkfqt, parse$1n7pf37, parse$1xm5283, parse$0xeilg3, parse$0eefxsx, parse$0bwhuel, parse$0zy5dnc, parse$1o8vfr6, parse$0l0cs1u, parse$0zrdgp4, parse$0s8xu45, parse$0vh4cry } from "./core/str.ts"; import { fromStr$05d44vn } from "./core/str/traits.ts"; import { put } from "./fmt.ts"; +import * as Counted from "./strs.Counted.ts"; import * as Named from "./strs.Named.ts"; /** A char of the alphabet for each byte, by its low bits. */ @@ -1177,4 +1178,225 @@ export function parses(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { } } +/** + * `String` made from and extended by iterators of chars and of strings, each the kind + * picks, from text of chars of every UTF-8 width and its parts between `/`s. + */ +export function iters(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const s: string = text(data); + const chars: rt.char[] = rt.iterToArray(rt.strChars(s)); + const parts: string[] = rt.iterToArray(rt.strSplit(s, "/")); + const nexts: rt.Cell = new rt.Cell(0); + const named: Named.Named = { name: text(rt.byteSliceRange(data, 0, rt.usizeDiv(data.length, 2))) }; + const _21: rt.u8 = rt.u8Rem(kind, 4); + if (_21 === 0) { + put(out, rt.stringExtendChars("", rt.strChars(s))); + put(out, rt.stringExtendChars("", rt.iterFilter(rt.iterRev(rt.strChars(s)), (c: rt.char): boolean => { + return !rt.charIsAsciiDigit(c); + }))); + put(out, rt.stringExtendChars("", rt.iterSlice(chars))); + put(out, rt.stringExtendChars("", rt.iterSkip(rt.iterSlice(chars), 1))); + rt.iterEmpty(); + put(out, rt.stringExtendChars("", rt.iterEmpty())); + put(out, rt.stringExtendChars("", rt.iterOption(rt.charFromU32(kind as rt.u32)))); + put(out, rt.stringExtendChars("", rt.iterFrom({ items: chars, nexts }, (it: Counted.Counted) => Counted.next$Iterator_char(it)))); + } else if (_21 === 1) { + put(out, rt.stringExtend("", rt.iterSlice(parts))); + put(out, rt.stringExtend("", rt.iterRev(rt.strSplit(s, "/")))); + put(out, rt.stringFromStrings(rt.iterMap(rt.iterSlice(parts), (p3: string): string => { + return p3; + }))); + put(out, rt.stringFromStrings(rt.iterFilter(rt.iterMap(rt.strSplit(s, "/"), (p4: string): string => { + const args4: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtPad(f, p4)]; + return rt.fmtFormat([args4[0], args4[0]]); + }), (p5: string): boolean => { + return !((p5.length === 0)); + }))); + rt.iterEmpty(); + put(out, rt.stringFromStrings(rt.iterEmpty())); + const owned: string[] = rt.iterToArray(rt.iterMap(rt.iterSlice(parts), (p6: string): string => { + return p6; + })); + put(out, rt.stringFromStrings(rt.iterMap(rt.iterSlice(owned), (p7: string): string => { + return p7; + }))); + put(out, rt.stringFromStrings(rt.iterFrom({ items: owned, nexts }, (it: Counted.Counted) => Counted.next$Iterator_String(it)))); + } else if (_21 === 2) { + let t: string = "é"; + const _137: rt.char[] = chars; + extendChars(rt.place(() => t, (value) => t = value), _137, kind); + const _141: rt.Iter = rt.iterRev(rt.strChars(s)); + t = rt.stringExtendChars(t, _141); + named.name = rt.stringExtendChars(named.name, rt.iterSlice(chars)); + rt.iterEmpty(); + named.name = rt.stringExtendChars(named.name, rt.iterEmpty()); + const counted3: Counted.Counted = { items: chars, nexts }; + t = rt.stringExtendChars(t, rt.iterFrom(counted3, (it: Counted.Counted) => Counted.next$Iterator_char(it))); + put(out, t); + } else { + let t2: string = ""; + const _163: rt.Iter = rt.strSplit(s, "/"); + t2 = rt.stringExtend(t2, _163); + const _168: rt.Iter = rt.iterMap(rt.iterSlice(parts), (p: string): string => { + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtPad(f, p)]; + return rt.fmtFormat([args2[0], "é"]); + }); + t2 = rt.stringExtend(t2, _168); + const _174: rt.Iter = rt.iterRev(rt.iterSlice(parts)); + named.name = rt.stringExtend(named.name, _174); + const _181: rt.Iter = rt.iterMap(rt.iterSlice(parts), (p2: string): string => { + return p2; + }); + named.name = rt.stringExtend(named.name, _181); + rt.iterEmpty(); + named.name = rt.stringExtend(named.name, rt.iterEmpty()); + const counted4: Counted.Counted = { items: parts, nexts }; + t2 = rt.stringExtend(t2, rt.iterFrom(counted4, (it: Counted.Counted) => Counted.next$Iterator_str(it))); + put(out, t2); + } + put(out, named.name); + rt.byteVecPush(out, nexts.v); +} + +export function extendChars(t: rt.Place, chars: rt.char[], kind: rt.u8): void { + const _4: rt.u8 = rt.u8Rem(rt.u8Div(kind, 4), 3); + if (_4 === 0) { + const _8: rt.Iter = rt.iterSlice(chars); + t.v = rt.stringExtendChars(t.v, _8); + } else if (_4 === 1) { + const _9: rt.Iter = rt.iterFilter(rt.iterSlice(chars), (c: rt.char): boolean => { + return rt.charIsAsciiDigit(c); + }); + t.v = rt.stringExtendChars(t.v, _9); + } else { + t.v = rt.stringExtendChars(t.v, rt.iterOption(128512)); + } +} + +/** + * `String::drain` of `s` by the range `form` picks, between offsets from `a` and `b`, + * read from either end by `steps`' bits, then dropped, forgotten, or read to its end. + */ +export function drainBy(s: rt.Place, form: rt.u8, a: rt.u8, b: rt.u8, steps: rt.u8, out: rt.ByteVec): void { + let drain!: rt.Iter; + let _35!: rt.u8; + let c!: rt.char | undefined; + let _71: boolean = false; + const _9: [rt.usize, rt.usize] = [offset(s.v, a), offset(s.v, b)]; + const i: rt.usize = _9[0]; + const j: rt.usize = _9[1]; + const _17: rt.u8 = rt.u8Rem(form, 8); + if (_17 === 0) { + drain = rt.stringDrain(s, new rt.Range(i, j)); + _71 = true; + } else if (_17 === 1) { + drain = rt.stringDrain(s, { start: i }); + _71 = true; + } else if (_17 === 2) { + drain = rt.stringDrain(s, { end: j }); + _71 = true; + } else if (_17 === 3) { + drain = rt.stringDrain(s, undefined); + _71 = true; + } else if (_17 === 4) { + drain = rt.stringDrain(s, new rt.RangeInclusive(i, j, rt.usizeStep)); + _71 = true; + } else if (_17 === 5) { + drain = rt.stringDrain(s, { end: j, inclusive: true }); + _71 = true; + } else if (_17 === 6) { + drain = rt.stringDrain(s, [{ tag: "Excluded", _0: i } as rt.RangeBound, { tag: "Included", _0: j } as rt.RangeBound] as [rt.RangeBound, rt.RangeBound]); + _71 = true; + } else { + drain = rt.stringDrain(s, [{ tag: "Included", _0: i } as rt.RangeBound, { tag: "Unbounded" } as rt.RangeBound] as [rt.RangeBound, rt.RangeBound]); + _71 = true; + } + const _32: rt.u8 = rt.u8Rem(steps, 4); + let iterStart: rt.u8 = 0; + const iterEnd: rt.u8 = _32; + while ((_35 = iterStart++) < iterEnd) { + if ((rt.u8Shr(steps, rt.u8Add(2, _35)) & 1) === 0) { + c = rt.iterToOption(drain.next()); + } else { + c = rt.iterToOption(drain.nextBack()); + } + putChar(out, c); + } + const _49: rt.u8 = rt.u8Rem(rt.u8Div(form, 8), 4); + if (_49 !== 0) { + if (_49 === 1) { + _71 = false; + } else if (_49 === 2) { + _71 = false; + put(out, rt.stringExtendChars("", drain, true)); + } else { + put(out, rt.stringDrainAsStr(drain)); + _71 = false; + const iter2: rt.Iter = rt.iterRev(drain); + while (true) { + const _65: rt.char | rt.Done = iter2.next(); + if (_65 === rt.DONE) { + break; + } + putChar(out, _65); + } + rt.drop(iter2); + } + } + if (_71) { + rt.drop(drain); + } + _71 = false; +} + +/** + * `String::drain`s, four bytes each, on text of chars of every UTF-8 width: through a + * `&mut String`, on a local, and on a field, each printed after each drain. An offset off + * a char boundary or out of bounds panics as std's does. + */ +export function drains(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const mid: number = rt.min(data.length, rt.u8Rem(kind, 8) as rt.usize); + if (mid > data.length) { + rt.panic("mid > len"); + } + const head: rt.ByteSlice = new rt.ByteSlice(data.data, data.start, mid); + let s: string = text(head); + const named: Named.Named = { name: text(head) }; + const iter: rt.Iter = rt.byteSliceChunks(new rt.ByteSlice(data.data, data.start + mid, data.length - mid), 4); + while (true) { + const _18: rt.ByteSlice | rt.Done = iter.next(); + if (_18 === rt.DONE) { + break; + } + rt.idx(_18, 0); + const _26: Uint8Array = Uint8Array.of(_18.data[_18.start], unwrapOr$0r2bmpr(rt.byteSliceGet(_18, 1), 0), unwrapOr$0r2bmpr(rt.byteSliceGet(_18, 2), 16), unwrapOr$0r2bmpr(rt.byteSliceGet(_18, 3), 0)); + const form: rt.u8 = _26[0]; + const a: rt.u8 = _26[1]; + const b: rt.u8 = _26[2]; + const steps: rt.u8 = _26[3]; + const _44: rt.u8 = rt.u8Rem(rt.u8Div(form, 32), 4); + if (_44 === 0) { + drainBy(rt.place(() => named.name, (value6) => named.name = value6), form, a, b, steps, out); + } else if (_44 === 1) { + const _49: rt.u8 = rt.u8Rem(form, 4); + if (_49 === 0) { + const _56: { end: rt.usize } = { end: offset(s, a) }; + put(out, rt.stringExtendChars("", rt.stringDrain(rt.place(() => s, (value5) => s = value5), _56), true)); + } else if (_49 === 1) { + const _64: { start: rt.usize } = { start: offset(s, a) }; + rt.byteVecPush(out, rt.ownedCount(rt.stringDrain(rt.place(() => s, (value4) => s = value4), _64)) & 0xff); + } else if (_49 === 2) { + putChar(out, rt.ownedLast(rt.stringDrain(rt.place(() => s, (value3) => s = value3), undefined))); + } else { + const _73: rt.Range = new rt.Range(offset(s, a), offset(s, b)); + rt.drop(rt.stringDrain(rt.place(() => s, (value2) => s = value2), _73)); + } + } else { + drainBy(rt.place(() => s, (value) => s = value), form, a, b, steps, out); + } + put(out, s); + put(out, named.name); + } +} + //# sourceMappingURL=strs.ts.map diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 4c73e1bf6..2e8c9e091 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -144,6 +144,8 @@ fn cases() -> Vec<(&'static str, Case)> { ("strs::parses", strs::parses), ("pointers::rebound", |_, data, out| pointers::rebound(data, out)), ("slices::ranges", slices::ranges), + ("strs::iters", strs::iters), + ("strs::drains", strs::drains), ] } diff --git a/ts/fixtures/lang/src/strs.rs b/ts/fixtures/lang/src/strs.rs index 1962e8331..464f10e41 100644 --- a/ts/fixtures/lang/src/strs.rs +++ b/ts/fixtures/lang/src/strs.rs @@ -645,3 +645,184 @@ pub fn parses(kind: u8, data: &[u8], out: &mut Vec) { } } } + +/// A crate's own iterator over `items`, which counts each `next` it is asked. +struct Counted<'a, T> { + items: &'a [T], + nexts: &'a std::cell::Cell, +} + +impl Iterator for Counted<'_, T> { + type Item = T; + + fn next(&mut self) -> Option { + self.nexts.set(self.nexts.get() + 1); + let (first, rest) = self.items.split_first()?; + self.items = rest; + Some(first.clone()) + } +} + +/// `String` made from and extended by iterators of chars and of strings, each the kind +/// picks, from text of chars of every UTF-8 width and its parts between `/`s. +pub fn iters(kind: u8, data: &[u8], out: &mut Vec) { + let s = text(data); + let chars: Vec = s.chars().collect(); + let parts: Vec<&str> = s.split('/').collect(); + let nexts = std::cell::Cell::new(0); + let mut named = Named { + name: text(&data[..data.len() / 2]), + }; + match kind % 4 { + 0 => { + put(out, &s.chars().collect::()); + put( + out, + &s.chars().rev().filter(|c| !c.is_ascii_digit()).collect::(), + ); + put(out, &chars.iter().collect::()); + put(out, &String::from_iter(chars.iter().copied().skip(1))); + put(out, &std::iter::empty::().collect::()); + put(out, &char::from_u32(u32::from(kind)).into_iter().collect::()); + let counted = Counted { + items: &chars, + nexts: &nexts, + }; + put(out, &counted.collect::()); + } + 1 => { + put(out, &parts.iter().copied().collect::()); + put(out, &s.split('/').rev().collect::()); + put(out, &parts.iter().map(|p| p.to_string()).collect::()); + put( + out, + &s.split('/') + .map(|p| format!("{p}{p}")) + .filter(|p| !p.is_empty()) + .collect::(), + ); + put(out, &std::iter::empty::().collect::()); + let owned: Vec = parts.iter().map(|p| p.to_string()).collect(); + put( + out, + &owned + .iter() + .map(|p| std::borrow::Cow::Borrowed(p.as_str())) + .collect::(), + ); + let counted = Counted { + items: &owned, + nexts: &nexts, + }; + put(out, &counted.collect::()); + } + 2 => { + // Through a `&mut`, on a local, and on a field. + let mut t = String::from("é"); + extend_chars(&mut t, &chars, kind); + t.extend(s.chars().rev()); + named.name.extend(&chars); + named.name.extend(std::iter::empty::()); + let counted = Counted { + items: &chars, + nexts: &nexts, + }; + t.extend(counted); + put(out, &t); + } + _ => { + let mut t = String::new(); + t.extend(s.split('/')); + t.extend(parts.iter().map(|p| format!("{p}é"))); + named.name.extend(parts.iter().rev().copied()); + named + .name + .extend(parts.iter().map(|p| std::borrow::Cow::Owned(p.to_string()))); + named.name.extend(std::iter::empty::<&str>()); + let counted = Counted { + items: &parts, + nexts: &nexts, + }; + t.extend(counted); + put(out, &t); + } + } + put(out, &named.name); + out.push(nexts.get()); +} + +fn extend_chars(t: &mut String, chars: &[char], kind: u8) { + match kind / 4 % 3 { + 0 => t.extend(chars.iter()), + 1 => t.extend(chars.iter().copied().filter(|c| c.is_ascii_digit())), + _ => t.extend(Some('😀')), + } +} + +/// `String::drain` of `s` by the range `form` picks, between offsets from `a` and `b`, +/// read from either end by `steps`' bits, then dropped, forgotten, or read to its end. +fn drain_by(s: &mut String, form: u8, a: u8, b: u8, steps: u8, out: &mut Vec) { + let (i, j) = (offset(s, a), offset(s, b)); + let mut drain = match form % 8 { + 0 => s.drain(i..j), + 1 => s.drain(i..), + 2 => s.drain(..j), + 3 => s.drain(..), + 4 => s.drain(i..=j), + 5 => s.drain(..=j), + 6 => s.drain((Bound::Excluded(i), Bound::Included(j))), + _ => s.drain((Bound::Included(i), Bound::Unbounded)), + }; + // Up to three chars, each from the front or the back by a bit of `steps`. + for k in 0..steps % 4 { + let c = match steps >> (2 + k) & 1 { + 0 => drain.next(), + _ => drain.next_back(), + }; + put_char(out, c); + } + match form / 8 % 4 { + 0 => {} + // Forgotten, a drain leaves the string as it was. + 1 => std::mem::forget(drain), + 2 => put(out, &drain.collect::()), + _ => { + put(out, drain.as_str()); + for c in drain.rev() { + put_char(out, Some(c)); + } + } + } +} + +/// `String::drain`s, four bytes each, on text of chars of every UTF-8 width: through a +/// `&mut String`, on a local, and on a field, each printed after each drain. An offset off +/// a char boundary or out of bounds panics as std's does. +pub fn drains(kind: u8, data: &[u8], out: &mut Vec) { + let (head, ops) = data.split_at(data.len().min(usize::from(kind % 8))); + let mut s = text(head); + let mut named = Named { name: text(head) }; + for op in ops.chunks(4) { + let [form, a, b, steps] = [ + op[0], + *op.get(1).unwrap_or(&0), + *op.get(2).unwrap_or(&0x10), + *op.get(3).unwrap_or(&0), + ]; + match form / 32 % 4 { + 0 => drain_by(&mut named.name, form, a, b, steps, out), + // On the local itself, with no `&mut` between. + 1 => match form % 4 { + 0 => put(out, &s.drain(..offset(&s, a)).collect::()), + 1 => out.push(s.drain(offset(&s, a)..).count() as u8), + 2 => put_char(out, s.drain(..).last()), + _ => { + s.drain(offset(&s, a)..offset(&s, b)); + } + }, + _ => drain_by(&mut s, form, a, b, steps, out), + } + put(out, &s); + put(out, &named.name); + } +} diff --git a/ts/runtime/rt/str.ts b/ts/runtime/rt/str.ts index 916086dec..04896fa09 100644 --- a/ts/runtime/rt/str.ts +++ b/ts/runtime/rt/str.ts @@ -6,10 +6,11 @@ // well formed, so a pattern's text matches where its UTF-8 would, never across a // surrogate pair. -import { fmtCharDebug, panic, panicFmt, stringLen } from "../rt.ts"; +import { fmtCharDebug, type Place, panic, panicFmt, stringLen } from "../rt.ts"; import { type Bounds, boundsRange } from "./collections.ts"; import type { char, u8, usize } from "./int.ts"; -import { DONE, type Done, Iter, iterSlice } from "./iter.ts"; +import { DONE, type Done, Iter, iterForEach, iterSlice } from "./iter.ts"; +import { drop as dispose } from "./task.ts"; const encoder = /* @__PURE__ */ new TextEncoder(); @@ -450,3 +451,95 @@ export function stringSplitOff(s: string, i: usize): [string, string] { const at = boundary(s, i, "assertion failed: self.is_char_boundary(at)"); return [s.slice(0, at), s.slice(at)]; } + +// `String` from and into iterators. Each reads the iterator by `for_each`, as std's does, +// which `drops` makes its own `fold`. + +/** `Extend` and `Extend<&char>` of `s`, and `collect` of chars from `""`: `s` and each char. */ +export function stringExtendChars(s: string, it: Iter, drops = false): string { + iterForEach( + it, + (c) => { + s += String.fromCodePoint(c); + }, + drops, + ); + return s; +} + +/** `Extend` of `s` by strings, `&str`, `String`, or `Cow`, and `collect` of `&str`s from `""`. */ +export function stringExtend(s: string, it: Iter, drops = false): string { + iterForEach( + it, + (text) => { + s += text; + }, + drops, + ); + return s; +} + +/** `collect` of owned strings, a `String` or `Cow`: the first by `next`, then the rest extend it. */ +export function stringFromStrings(it: Iter, drops = false): string { + const first = it.next(); + if (first !== DONE) return stringExtend(first, it, drops); + if (drops) dispose(it); + return ""; +} + +/** + * `String::drain`: the chars within the byte range `r` of the string at `s`, which leaves + * the string without them as it drops, and as it was if it never does, as `mem::forget` + * leaves it. + */ +export function stringDrain(s: Place, r: Bounds): Iter { + const text = s.v; + const [start, end] = boundsRange(r, utf8Len(text, 0, text.length)); + const from = boundary(text, start, "assertion failed: self.is_char_boundary(start)"); + const to = boundary(text, end, "assertion failed: self.is_char_boundary(end)"); + return new StringDrain(s, text, from, to); +} + +/** `Drain::as_str`: the chars `drain` has yet to yield. */ +export function stringDrainAsStr(drain: Iter): string { + return (drain as StringDrain).rest(); +} + +class StringDrain extends Iter implements Disposable { + readonly #s: Place; + readonly #text: string; + readonly #from: number; + readonly #to: number; + #front: number; + #back: number; + + constructor(s: Place, text: string, from: number, to: number) { + super(); + this.#s = s; + this.#text = text; + this.#from = this.#front = from; + this.#to = this.#back = to; + } + + next(): char | Done { + if (this.#front >= this.#back) return DONE; + const c = this.#text.codePointAt(this.#front) as number; + this.#front += c > 0xffff ? 2 : 1; + return c as char; + } + + override nextBack(): char | Done { + if (this.#front >= this.#back) return DONE; + this.#back = before(this.#text, this.#back); + return this.#text.codePointAt(this.#back) as char; + } + + + rest(): string { + return this.#text.slice(this.#front, this.#back); + } + + [Symbol.dispose](): void { + this.#s.v = this.#text.slice(0, this.#from) + this.#text.slice(this.#to); + } +} diff --git a/ts/src/lower/drop.rs b/ts/src/lower/drop.rs index 981a5b507..b779a8c1d 100644 --- a/ts/src/lower/drop.rs +++ b/ts/src/lower/drop.rs @@ -65,8 +65,15 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { ) => args.types().any(|t| self.drops(t)), // An entry borrows its map, and holds only the key it was given. Some(Std::Entry) => args.types().next().is_some_and(|k| self.drops(k)), - // A `Vec`'s drain moves the vector's tail back as it drops. - None if library::std_path(tcx, adt.did()) == ["vec", "drain", "Drain"] => true, + // A `Vec`'s drain moves the vector's tail back as it drops, and a `String`'s takes + // the drained text out of it. + None if { + let path = library::std_path(tcx, adt.did()); + path == ["vec", "drain", "Drain"] || path == ["string", "Drain"] + } => + { + true + } None | Some(Std::Peekable) if self.std_iter(ty) => match adt.has_dtor(tcx) { // What owns its items, an `IntoIter` or a `Drain`, drops those it still holds. true => args.types().any(|t| self.drops(t)), diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 53bc35ad2..2be0c0720 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -325,6 +325,14 @@ pub fn call<'tcx>( _ => {} } + // What `collect` calls. + if (owner, name) == ("FromIterator", "from_iter") { + let source = targ(2)?; + let iter = iter_of(b.t, source)?; + let items = into_iter(b, source, args.pop()?, span)?; + return collect(b, targ(0)?, targ(1)?, items, iter, span); + } + // `Vec::extend` with an iterator, which it drops once done. if (owner, name) == ("Extend", "extend") { let (vec, item, source) = (targ(0)?, targ(1)?, targ(2)?); @@ -794,10 +802,43 @@ fn collect<'tcx>( let args = vec![iter, Expr::Lambda(vec![param], Box::new(values))]; Some(Expr::rt(collect, tail(args, vec![dropper]))) } + // From chars or text, as `extend` of an empty one, but for an owned string's, which + // the rest extend once `next` gives it. + Std::String if matches!(b.t.std_of(item), Some(Std::String | Std::Cow)) => { + let drops = iter_drops(b, from, span); + Some(Expr::rt("stringFromStrings", tail(vec![iter], vec![drops]))) + } + Std::String => string_extend(b, Expr::Str(String::new()), iter, item, from, span), _ => super::collections::collect(b, ty, iter, from, span), } } +/// `s` extended by `items`, an iterator of type `iter` of `item`s, as `String`'s `Extend` +/// reads it, by `for_each`: of chars, through any `&`, or of text, a `&str`, `String`, +/// `Box`, or `Cow`. +pub(super) fn string_extend<'tcx>( + b: &mut Body<'_, '_, 'tcx>, + s: Expr, + items: Expr, + item: Ty<'tcx>, + iter: Ty<'tcx>, + span: Span, +) -> Option { + let text = match *item.peel_refs().kind() { + ty::Char => false, + ty::Str => true, + ty::Adt(_, args) => match b.t.std_of(item)? { + Std::String => true, + Std::Box | Std::Cow => args.types().next().is_some_and(|t| t.is_str()), + _ => return None, + }, + _ => return None, + }; + let extend = if text { "stringExtend" } else { "stringExtendChars" }; + let drops = iter_drops(b, iter, span); + Some(Expr::rt(extend, tail(vec![s, items], vec![drops]))) +} + /// Whether an iterator of type `ty` that a runtime function takes by value drops what it /// holds, which its `rt.Iter` drops as std's does: `true`, or `undefined` for one that /// drops nothing. diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 573b55dc7..53d934132 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -1711,6 +1711,13 @@ fn mapping<'tcx>( ("String", "as_str") => args.pop()?, ("String", "new") => Expr::Str(String::new()), ("String", "with_capacity") => Expr::rt("stringWithCapacity", args), + // Of the place the `&mut` is, which it writes as it drops, and of a range, as `Vec::drain`. + ("String", "drain") => { + let range = args.pop()?; + args.push(super::collections::bounds(b, call, range)); + Expr::rt("stringDrain", args) + } + ("Drain", "as_str") => Expr::rt("stringDrainAsStr", args), ("ParseIntError", "kind") => args.pop()?.member("kind"), // `str::parse::`, by std's grammar rather than `Number`'s. ("str", "parse") | ("FromStr", "from_str") if super::float::is(targ(gargs, 0)) => { diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs index c2878c932..4ed074fc9 100644 --- a/ts/src/lower/mir/call.rs +++ b/ts/src/lower/mir/call.rs @@ -553,6 +553,17 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { self.set_borrowed(&place, pushed, span, out); done(Expr::undefined(), false) } + // The extended string, from the iterator read as std's `Extend` reads it. + ("Extend", "extend") if targ(0).is_some_and(|t| self.b.t.std_of(t) == Some(Std::String)) => { + let (item, source) = (targ(1)?, targ(2)?); + let place = self.borrowed_place(&args[0].node, span, out)?; + let items = self.operand(&args[1].node, span, out); + let iter = crate::lower::iter::iter_of(self.b.t, source)?; + let items = crate::lower::iter::into_iter(self.b, source, items, span)?; + let extended = crate::lower::iter::string_extend(self.b, place.0.clone(), items, item, iter, span)?; + self.set_borrowed(&place, extended, span, out); + done(Expr::undefined(), false) + } // No capacity is kept, and reading one is refused, but reserving past `isize::MAX` // panics as std's does. ("String", "reserve" | "reserve_exact") => { diff --git a/ts/src/lower/pointer.rs b/ts/src/lower/pointer.rs index 088887549..94177bd2f 100644 --- a/ts/src/lower/pointer.rs +++ b/ts/src/lower/pointer.rs @@ -247,12 +247,14 @@ pub fn keeps(tcx: ty::TyCtxt<'_>, def_id: rustc_hir::def_id::DefId) -> bool { /// Whether std's `def_id` is one a mapping takes the `rt.Place` a `&mut` is for, rather /// than what it points at: a `MaybeUninit`'s, which it points into, an `Arc`'s that -/// `make_mut` replaces, and a well-known crate's, such as a `Bytes` that `Buf::advance` -/// moves past its front. +/// `make_mut` replaces, a `String`'s that a drain edits as it drops, and a well-known +/// crate's, such as a `Bytes` that `Buf::advance` moves past its front. pub fn takes_place(tcx: ty::TyCtxt<'_>, def_id: rustc_hir::def_id::DefId) -> bool { matches!( super::library::key(tcx, def_id), - Some((owner, name)) if owner == "MaybeUninit" || matches!(owner.as_str(), "Arc" | "Rc") && name == "make_mut" + Some((owner, name)) if owner == "MaybeUninit" + || matches!(owner.as_str(), "Arc" | "Rc") && name == "make_mut" + || (owner.as_str(), name.as_str()) == ("String", "drain") ) || super::stock::is_stock(tcx, def_id) } From ce85d834314ec6746697ec527526145bbe079953 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 07:42:58 -0700 Subject: [PATCH 3/6] feat(ts): collect Cow and Box as String's from_iter does Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/strs.ts | 26 ++++++++++++++------------ ts/fixtures/lang/src/strs.rs | 4 ++++ ts/src/lower/iter.rs | 15 ++++++++++----- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/ts/fixtures/lang/expected/strs.ts b/ts/fixtures/lang/expected/strs.ts index a89cc8b21..0d2cfc88c 100644 --- a/ts/fixtures/lang/expected/strs.ts +++ b/ts/fixtures/lang/expected/strs.ts @@ -1214,6 +1214,8 @@ export function iters(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { }))); rt.iterEmpty(); put(out, rt.stringFromStrings(rt.iterEmpty())); + put(out, rt.stringExtendChars("", rt.iterRev(rt.strChars(s)))); + put(out, rt.stringExtend("", rt.iterSlice(parts))); const owned: string[] = rt.iterToArray(rt.iterMap(rt.iterSlice(parts), (p6: string): string => { return p6; })); @@ -1223,10 +1225,10 @@ export function iters(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { put(out, rt.stringFromStrings(rt.iterFrom({ items: owned, nexts }, (it: Counted.Counted) => Counted.next$Iterator_String(it)))); } else if (_21 === 2) { let t: string = "é"; - const _137: rt.char[] = chars; - extendChars(rt.place(() => t, (value) => t = value), _137, kind); - const _141: rt.Iter = rt.iterRev(rt.strChars(s)); - t = rt.stringExtendChars(t, _141); + const _153: rt.char[] = chars; + extendChars(rt.place(() => t, (value) => t = value), _153, kind); + const _157: rt.Iter = rt.iterRev(rt.strChars(s)); + t = rt.stringExtendChars(t, _157); named.name = rt.stringExtendChars(named.name, rt.iterSlice(chars)); rt.iterEmpty(); named.name = rt.stringExtendChars(named.name, rt.iterEmpty()); @@ -1235,19 +1237,19 @@ export function iters(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { put(out, t); } else { let t2: string = ""; - const _163: rt.Iter = rt.strSplit(s, "/"); - t2 = rt.stringExtend(t2, _163); - const _168: rt.Iter = rt.iterMap(rt.iterSlice(parts), (p: string): string => { + const _179: rt.Iter = rt.strSplit(s, "/"); + t2 = rt.stringExtend(t2, _179); + const _184: rt.Iter = rt.iterMap(rt.iterSlice(parts), (p: string): string => { const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtPad(f, p)]; return rt.fmtFormat([args2[0], "é"]); }); - t2 = rt.stringExtend(t2, _168); - const _174: rt.Iter = rt.iterRev(rt.iterSlice(parts)); - named.name = rt.stringExtend(named.name, _174); - const _181: rt.Iter = rt.iterMap(rt.iterSlice(parts), (p2: string): string => { + t2 = rt.stringExtend(t2, _184); + const _190: rt.Iter = rt.iterRev(rt.iterSlice(parts)); + named.name = rt.stringExtend(named.name, _190); + const _197: rt.Iter = rt.iterMap(rt.iterSlice(parts), (p2: string): string => { return p2; }); - named.name = rt.stringExtend(named.name, _181); + named.name = rt.stringExtend(named.name, _197); rt.iterEmpty(); named.name = rt.stringExtend(named.name, rt.iterEmpty()); const counted4: Counted.Counted = { items: parts, nexts }; diff --git a/ts/fixtures/lang/src/strs.rs b/ts/fixtures/lang/src/strs.rs index 464f10e41..576857607 100644 --- a/ts/fixtures/lang/src/strs.rs +++ b/ts/fixtures/lang/src/strs.rs @@ -702,6 +702,10 @@ pub fn iters(kind: u8, data: &[u8], out: &mut Vec) { .collect::(), ); put(out, &std::iter::empty::().collect::()); + let cow: std::borrow::Cow = s.chars().rev().collect(); + put(out, &cow); + let boxed: Box = parts.iter().copied().collect(); + put(out, &boxed); let owned: Vec = parts.iter().map(|p| p.to_string()).collect(); put( out, diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 2be0c0720..c9ee041ae 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -803,12 +803,17 @@ fn collect<'tcx>( Some(Expr::rt(collect, tail(args, vec![dropper]))) } // From chars or text, as `extend` of an empty one, but for an owned string's, which - // the rest extend once `next` gives it. - Std::String if matches!(b.t.std_of(item), Some(Std::String | Std::Cow)) => { - let drops = iter_drops(b, from, span); - Some(Expr::rt("stringFromStrings", tail(vec![iter], vec![drops]))) + // the rest extend once `next` gives it. A `Cow` and a `Box` own the `String` + // std's collects. + Std::String | Std::Cow | Std::Box if std == Std::String || args.types().next().is_some_and(|t| t.is_str()) => { + match b.t.std_of(item) { + Some(Std::String | Std::Cow) => { + let drops = iter_drops(b, from, span); + Some(Expr::rt("stringFromStrings", tail(vec![iter], vec![drops]))) + } + _ => string_extend(b, Expr::Str(String::new()), iter, item, from, span), + } } - Std::String => string_extend(b, Expr::Str(String::new()), iter, item, from, span), _ => super::collections::collect(b, ty, iter, from, span), } } From 78478259d58d669f731d30427bb97832418df3ab Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 07:58:21 -0700 Subject: [PATCH 4/6] feat(ts): reserve the lower size hint as String's Extend does rt.Iter gives a lower bound, exact for ranges and what adapts them, which String's extend and collect of chars reserve first, panicking with capacity overflow past isize::MAX before reading a char, as std's do. flatten passes an array item's length, which std's size hint counts. Co-Authored-By: Claude Opus 5.5 --- quest/m3/README.md | 2 +- quest/m3/ts-size-hints.md | 18 ++-- ts/README.md | 3 +- ts/fixtures/lang/caller.ts | 6 +- ts/fixtures/lang/expected/collections.ts | 6 +- ts/fixtures/lang/expected/core/option.ts | 7 ++ ts/fixtures/lang/expected/harness.ts | 6 +- ts/fixtures/lang/expected/holds.ts | 2 +- ts/fixtures/lang/expected/orders.ts | 6 +- ts/fixtures/lang/expected/refs.ts | 2 +- ts/fixtures/lang/expected/strs.ts | 93 ++++++++++++++++++- ts/fixtures/lang/src/harness.rs | 1 + ts/fixtures/lang/src/strs.rs | 49 ++++++++++ ts/runtime/rt/iter.ts | 113 +++++++++++++++++++++-- ts/runtime/rt/str.ts | 6 +- ts/src/lower/iter.rs | 21 ++++- 16 files changed, 305 insertions(+), 36 deletions(-) diff --git a/quest/m3/README.md b/quest/m3/README.md index f392b099c..2bbf874b4 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -30,7 +30,7 @@ generic std gap is planned here even when moq never calls it. - [Drop order in std's folds and iterators](/quest/m3/ts-drop-order.md) - the values that drop still refused in std's folds, zips, flattens, and collects drop as std's do - [References to byte-array keys a collection yields](/quest/m3/ts-byte-key-refs.md) - `keys`, `iter`, `get_key_value`, and `HashSet::get` of a `[u8; N]` key translate - [An array as a slice](/quest/m3/ts-array-as-slice.md) - `<[T; N]>::as_slice` and `as_mut_slice` translate -- [Size hints of std's iterators](/quest/m3/ts-size-hints.md) - `size_hint` translates, and a collect or extend that reserves past `isize::MAX` panics as std's does +- [Size hints of std's iterators](/quest/m3/ts-size-hints.md) - `size_hint` translates, and `Vec`'s collect and extend panic past `isize::MAX` as std's do - [reduce, max_by, min_by, and Peekable's next_if_eq and peek_mut](/quest/m3/ts-iter-reducers.md) - the iterator consumers std returns an item from that have no mapping translate - [Closures that return an Option whose None is rt.None](/quest/m3/ts-option-closures.md) - `find_map`, `filter_map`, and `map_while` to `Option>` keep `None` apart from `Some(None)` - [A slice's edits in place](/quest/m3/ts-slice-edits.md) - `fill`, `rotate_left`, and `copy_from_slice` of other values translate diff --git a/quest/m3/ts-size-hints.md b/quest/m3/ts-size-hints.md index 67185274f..e421f7a4a 100644 --- a/quest/m3/ts-size-hints.md +++ b/quest/m3/ts-size-hints.md @@ -3,18 +3,18 @@ ## Goal [rust2-ts](/ts/README.md) translates `Iterator::size_hint` of std's iterators, and each -collect and extend that reserves by one, such as `String`'s `Extend` and `Vec`'s -`collect`, panics with std's `capacity overflow` where std's does, proven by differential +collect and extend of a collection that reserves by one, such as `Vec`'s `collect` and +`extend`, panics with std's `capacity overflow` where std's does, proven by differential tests against native Rust. ## Plan -Found by [#284](https://github.com/moq-dev/rust2/pull/284) on 2026-10-30, which -translates `String`'s `extend` and `collect` by `for_each` but skips the `reserve` of -the lower bound std makes first. An iterator whose lower bound passes `isize::MAX` on -wasm32, such as `repeat(c)` or `(0..u32::MAX).map(f)`, panics there in std before it -yields an item, where the translation reads on until memory runs out. `rt.Iter` keeps no -size hint, and the crate's own `size_hint` is refused, so no translated code observes one -today. +Found by [#284](https://github.com/moq-dev/rust2/pull/284) on 2026-10-30, which gives +`rt.Iter` a `lower` bound, exact for ranges and what adapts them, and reserves it in +`String`'s `Extend`. `Vec`'s `collect` of a `TrustedLen` iterator reserves its upper +bound and panics without one, and its `extend` reserves the lower bound; `rt.iterToArray` +and `rt.iterExtend` reserve neither, so `(0..u32::MAX).map(f).collect::>()` reads +on until memory runs out where std's panics before calling `f`. A call of `size_hint` +itself needs the upper bound too, and the crate's own `size_hint` is refused. Public API: the translator's output. diff --git a/ts/README.md b/ts/README.md index 191f30c9b..760fc3a68 100644 --- a/ts/README.md +++ b/ts/README.md @@ -214,7 +214,8 @@ one `&s[a..b]` takes, which reads the string up to it and panics as std does ins char. A JS string can't change in place, so `String`'s edits, `push_str`, `truncate`, `insert`, `retain`, `replace_range`, and the rest, assign the edited one to the local or field the `&mut` borrows, taking and checking byte offsets as std does. `extend` and -`collect` read their iterator by `for_each`, as std's do. A `drain` holds the `&mut`'s +`collect` read their iterator by `for_each`, as std's do, and of chars first reserve its +size hint's lower bound, which panics past `isize::MAX`. A `drain` holds the `&mut`'s place and writes the string without the drained text as it drops, so one that `mem::forget` leaks leaves it as it was, as std's does. A `String` keeps no capacity: `with_capacity`, `reserve`, and `shrink_to_fit` do nothing, and `capacity` is refused. A `Cow` is its text, borrowed or owned, so diff --git a/ts/fixtures/lang/caller.ts b/ts/fixtures/lang/caller.ts index d29f38d3c..d40a85b86 100644 --- a/ts/fixtures/lang/caller.ts +++ b/ts/fixtures/lang/caller.ts @@ -373,4 +373,8 @@ const drains = Array.from({ length: 32 * 13 * 13 }, (_, i) => { /** `strs::iters` of each kind, on no text, text of each UTF-8 width, and parts between `/`s, empty ones too. */ const iters = Array.from({ length: 12 }, (_, kind) => [[], [2, 7, 9, 14], [0, 2, 0, 0, 7, 9], [14, 0]].map((data) => Uint8Array.of(op("strs::iters"), kind, ...data))).flat(); -export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...formats, ...parsed, ...dyns, ...cells, ...shared, ...locks, ...onces, ...bounds, ...unsafeCells, ...edits, ...subslices, ...consumers, ...ranges, ...drains, ...iters]; +/** `strs::hinted` of each adaptor and each pair of range lengths, on `aé` alone. */ +const hintedOp = op("strs::hinted"); +const hinted = Array.from({ length: 8 * 7 * 7 }, (_, i) => Uint8Array.of(hintedOp, i % 8, Math.floor(i / 8) % 7, Math.floor(i / 56))); + +export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...formats, ...parsed, ...dyns, ...cells, ...shared, ...locks, ...onces, ...bounds, ...unsafeCells, ...edits, ...subslices, ...consumers, ...ranges, ...drains, ...iters, ...hinted]; diff --git a/ts/fixtures/lang/expected/collections.ts b/ts/fixtures/lang/expected/collections.ts index 4a1938487..6159d8b96 100644 --- a/ts/fixtures/lang/expected/collections.ts +++ b/ts/fixtures/lang/expected/collections.ts @@ -1116,7 +1116,7 @@ export function stores(data: rt.ByteSlice, out: rt.ByteVec): void { v[at8][1] = v[at8][1] ^ 16; rt.iterExtendBytes(out, rt.iterFlatMap(rt.iterSlice(v), (p: [rt.u8, rt.u8]): Uint8Array => { return Uint8Array.of(p[0], p[1]); - }, (value: Uint8Array) => rt.iterSlice(value))); + }, (value: Uint8Array) => rt.iterSlice(value), false, 2)); rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(changed[0], changed[1], old[0], old[1]))); const tally: Tally.Tally = { n: a as rt.u16 }; const q: rt.VecDeque = rt.dequeNew() as rt.VecDeque; @@ -1148,7 +1148,7 @@ export function stores(data: rt.ByteSlice, out: rt.ByteVec): void { return mapOr$1nlkf9b(rt.hashGet(hash, k), rt.bytes(2).fill(255), (p2: [rt.u8, rt.u8]): Uint8Array => { return Uint8Array.of(p2[0], p2[1]); }); - }, (value: Uint8Array) => rt.iterSlice(value))); + }, (value: Uint8Array) => rt.iterSlice(value), false, 2)); const tree: rt.BTreeMap = rt.btreeNew(rt.cmp) as rt.BTreeMap; rt.btreeInsert(tree, 0, copy$u8_u8(pair)); rt.btreeInsert(tree, 1, copy$u8_u8(pair)); @@ -1158,7 +1158,7 @@ export function stores(data: rt.ByteSlice, out: rt.ByteVec): void { } rt.iterExtendBytes(out, rt.iterFlatMap(rt.btreeValues(tree), (p3: [rt.u8, rt.u8]): Uint8Array => { return Uint8Array.of(p3[0], p3[1]); - }, (value: Uint8Array) => rt.iterSlice(value))); + }, (value: Uint8Array) => rt.iterSlice(value), false, 2)); const boxed: [rt.u8, rt.u8] = copy$u8_u8(pair); boxed[0] = boxed[0] ^ 1; const holders: Holder.Holder[] = [{ pair: copy$u8_u8(pair) }]; diff --git a/ts/fixtures/lang/expected/core/option.ts b/ts/fixtures/lang/expected/core/option.ts index 7af9b96e9..07abe5e1b 100644 --- a/ts/fixtures/lang/expected/core/option.ts +++ b/ts/fixtures/lang/expected/core/option.ts @@ -127,6 +127,13 @@ export function unwrapOr$0x6fy3l(self: [rt.u8, rt.u8] | undefined, default_: [rt return self; } +export function unwrapOr$1rbuegs(self: rt.ByteSlice | undefined, default_: rt.ByteSlice): rt.ByteSlice { + if (self === undefined) { + return default_; + } + return self; +} + export function unwrapOr$14m5xjm(self: [rt.usize, rt.u8] | undefined, default_: [rt.usize, rt.u8]): [rt.usize, rt.u8] { if (self === undefined) { return default_; diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index eea8a1474..4213ac57f 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -37,7 +37,7 @@ import { copies as shapes_copies, order, discriminants, loops, patterns, closure import { parts, chunked, subslices, drains, ranges as slices_ranges } from "./slices.ts"; import { splits } from "./splits.ts"; import { statics, cells as statics_cells, onces } from "./statics.ts"; -import { strs, slices, edits as strs_edits, parses as strs_parses, iters, drains as strs_drains } from "./strs.ts"; +import { strs, slices, edits as strs_edits, parses as strs_parses, iters, drains as strs_drains, hinted } from "./strs.ts"; import { decodes } from "./utf8.ts"; import { wrappers } from "./wrappers.ts"; @@ -90,7 +90,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) refs(data21, out21); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_2121: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { counts(data22, out22); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2122: rt.u8, _372: rt.ByteSlice, out23: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2122: rt.u8, _375: rt.ByteSlice, out23: rt.ByteVec): void => { rounding(out23); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::overrides", overrides] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::dyns", (_2123: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { dyns(data23, out24); @@ -172,7 +172,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) hashes_bytes(data61, out62); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::bytes", keys_bytes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::parses", strs_parses] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::rebound", (_2162: rt.u8, data62: rt.ByteSlice, out63: rt.ByteVec): void => { rebound(data62, out63); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["slices::ranges", slices_ranges] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::iters", iters] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::drains", strs_drains] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]).map(copy$str_forabfnu8au8bmutstdvecVecu8); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["slices::ranges", slices_ranges] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::iters", iters] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::drains", strs_drains] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::hinted", hinted] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]).map(copy$str_forabfnu8au8bmutstdvecVecu8); } /** Run one case. */ diff --git a/ts/fixtures/lang/expected/holds.ts b/ts/fixtures/lang/expected/holds.ts index dfdf7bbac..5efa4d95a 100644 --- a/ts/fixtures/lang/expected/holds.ts +++ b/ts/fixtures/lang/expected/holds.ts @@ -69,7 +69,7 @@ export function places(a: rt.u8, b: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): set(rt.place(() => grid[_30][_32], (value) => grid[_30][_32] = value), k); set(rt.place(() => rows[_35].cells[_39], (value2) => rows[_35].cells[_39] = value2), k ^ 1); } - rt.iterExtendBytes(out, rt.iterFlatten(rt.iterMap(rt.iterSlice(grid), rt.byteSliceFrom), (value: rt.ByteSlice) => rt.iterSlice(rt.byteSliceView(value)))); + rt.iterExtendBytes(out, rt.iterFlatten(rt.iterMap(rt.iterSlice(grid), rt.byteSliceFrom), (value: rt.ByteSlice) => rt.iterSlice(rt.byteSliceView(value)), false, 4)); for (const _61 of rows) { rt.iterExtendBytes(out, rt.iterSlice(_61.cells)); } diff --git a/ts/fixtures/lang/expected/orders.ts b/ts/fixtures/lang/expected/orders.ts index 9ad55b661..5b947534d 100644 --- a/ts/fixtures/lang/expected/orders.ts +++ b/ts/fixtures/lang/expected/orders.ts @@ -436,7 +436,7 @@ export function values(data: rt.ByteSlice, out: rt.ByteVec): void { } rt.iterExtendBytes(out, rt.iterFlatMap(rt.iterSlice(rt.heapIntoSortedVec(pairs, rt.cmpLe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpGe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpLt(rt.cmpTuple([rt.cmp, rt.cmp])))), (_25: [rt.u8, rt.u8]): Uint8Array => { return Uint8Array.of(_25[0], _25[1]); - }, (value: Uint8Array) => rt.iterSlice(value))); + }, (value: Uint8Array) => rt.iterSlice(value), false, 2)); } /** @@ -531,9 +531,9 @@ export function bytes(data: rt.ByteSlice, out: rt.ByteVec): void { rt.iterExtendBytes(out, rt.iterFlatMap(rt.btreeIter(rt.btreeExtend(rt.btreeNew(rt.cmpBytes) as rt.BTreeMap, _185, true)), (_22: [Uint8Array, rt.usize]): Uint8Array => { const k: Uint8Array = _22[0]; return Uint8Array.of(k[0], k[1], _22[1] & 0xff); - }, (value: Uint8Array) => rt.iterSlice(value))); + }, (value: Uint8Array) => rt.iterSlice(value), false, 3)); rt.sliceSort(arrays, rt.cmpLt(rt.cmpBytes)); - rt.iterExtendBytes(out, rt.iterFlatten(rt.iterMap(rt.iterSlice(arrays), rt.byteSliceFrom), (value: rt.ByteSlice) => rt.iterSlice(rt.byteSliceView(value)))); + rt.iterExtendBytes(out, rt.iterFlatten(rt.iterMap(rt.iterSlice(arrays), rt.byteSliceFrom), (value: rt.ByteSlice) => rt.iterSlice(rt.byteSliceView(value)), false, 2)); const slices: rt.ByteSlice[] = rt.iterToArray(rt.byteSliceChunks(data, 3)); rt.sliceSortBy(slices, (a7: rt.ByteSlice, b3: rt.ByteSlice): rt.Ordering => { return rt.cmpBytes(b3, a7); diff --git a/ts/fixtures/lang/expected/refs.ts b/ts/fixtures/lang/expected/refs.ts index 3645810d0..aed6f6b84 100644 --- a/ts/fixtures/lang/expected/refs.ts +++ b/ts/fixtures/lang/expected/refs.ts @@ -741,7 +741,7 @@ export function elements(data: rt.ByteSlice, out: rt.ByteVec): void { for (const _168 of pairs) { rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(_168[0], unwrapOr$0r2bmpr(_168[1], 238)))); } - const iter10: rt.Iter = rt.iterFlatten(rt.iterSlice(grid), (value: rt.u32[]) => rt.iterSlice(value)); + const iter10: rt.Iter = rt.iterFlatten(rt.iterSlice(grid), (value: rt.u32[]) => rt.iterSlice(value), false, 2); while (true) { const _186: rt.u32 | rt.Done = iter10.next(); if (_186 === rt.DONE) { diff --git a/ts/fixtures/lang/expected/strs.ts b/ts/fixtures/lang/expected/strs.ts index 0d2cfc88c..6a4da7ac5 100644 --- a/ts/fixtures/lang/expected/strs.ts +++ b/ts/fixtures/lang/expected/strs.ts @@ -3,7 +3,7 @@ import * as rt from "./rt.ts"; import { then$0havnzo } from "./core/bool.ts"; import { fromStr$1d7pnbk, fromStrRadix$1ae2pnm, fromStr$15isx69, fromStrRadix$1mr00o6, fromStr$04uijf8, fromStrRadix$1wqmb68, fromStr$0l2zu9x, fromStrRadix$0cd0jvv, fromStr$0h0ptos, fromStrRadix$10vntu7, fromStr$10svit2, fromStrRadix$0wc95fw, fromStr$19kbj04, fromStrRadix$1o9c7gd, fromStr$0br814e, fromStrRadix$188cokx, fromStr$1471qz1, fromStrRadix$0l5ymb0, fromStr$0k8sfhf, fromStrRadix$0fpnm5c, fromStr$0b2xa80, fromStrRadix$1095n8m, fromStr$17dd66m, fromStrRadix$1yrk04t } from "./core/num/nonzero.ts"; -import { unwrapOr$1iemzn8, unwrapOr$0r2bmpr, map$0gl6wke, mapOr$147z9gi, mapOr$16umieg } from "./core/option.ts"; +import { unwrapOr$1iemzn8, unwrapOr$0r2bmpr, unwrapOr$1rbuegs, map$0gl6wke, mapOr$147z9gi, mapOr$16umieg } from "./core/option.ts"; import { parse$0ub96so, parse$1ogkfqt, parse$1n7pf37, parse$1xm5283, parse$0xeilg3, parse$0eefxsx, parse$0bwhuel, parse$0zy5dnc, parse$1o8vfr6, parse$0l0cs1u, parse$0zrdgp4, parse$0s8xu45, parse$0vh4cry } from "./core/str.ts"; import { fromStr$05d44vn } from "./core/str/traits.ts"; import { put } from "./fmt.ts"; @@ -1401,4 +1401,95 @@ export function drains(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { } } +/** + * Chars from ranges as long as `data`'s first two bytes pick, through the adaptors `kind` + * picks, into a `String` holding `aé` and the text of the rest: std's reserves the size + * hint's lower bound first, which panics past `isize::MAX` before it reads a char, and + * else the chars panic with `consumed` past the eighth. + */ +export function hinted(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + let _61!: rt.usize; + const pick: (a0: rt.usize) => rt.u32 = (i: rt.usize): rt.u32 => { + const _4: rt.usize = rt.usizeRem(unwrapOr$0r2bmpr(rt.byteSliceGet(data, i), 0) as rt.usize, [0, 5, 8, 9, 2147483645, 2147483648, 4294967295].length); + if (!(_4 < 7)) { + rt.panic("index out of bounds: the len is " + 7 + " but the index is " + _4); + } + return [0, 5, 8, 9, 2147483645, 2147483648, 4294967295][_4]; + }; + const _7: [rt.u32, rt.u32] = [pick(0), pick(1)]; + const n: rt.u32 = _7[0]; + const m: rt.u32 = _7[1]; + let t: string = "aé"; + const _16: rt.Place = rt.place(() => t, (value) => t = value); + const _17: string = text(unwrapOr$1rbuegs(rt.byteSliceGetRange(data, 2), rt.byteSliceFrom(Uint8Array.of()))); + _16.v = _16.v + _17; + const calls: rt.Cell = new rt.Cell(0); + const f: (a0: rt.u32) => rt.char = (_2: rt.u32): rt.char => { + rt.cellSet(calls, rt.usizeAdd(calls.v, 1)); + if (!(calls.v <= 8)) { + rt.panicFmt(["consumed"]); + } else { + const _122: rt.usize = calls.v; + if (!(_122 < 16)) { + rt.panic("index out of bounds: the len is " + 16 + " but the index is " + _122); + } + return [47, 92, 97, 48, 57, 127, 128, 233, 2047, 2048, 55295, 57344, 65535, 65536, 128512, 1114111][_122]; + } + }; + const _29: rt.u8 = rt.u8Rem(kind, 8); + if (_29 === 0) { + const _31: rt.Place = rt.place(() => t, (value9) => t = value9); + const _32: rt.Iter = rt.iterMap(new rt.Range(0, n), f); + _31.v = rt.stringExtendChars(_31.v, _32); + } else if (_29 === 1) { + t = rt.stringExtendChars("", rt.iterMap(rt.iterChain(new rt.RangeInclusive(0, n, rt.u32Step), new rt.Range(0, n)), f)); + } else if (_29 === 2) { + const _39: rt.Place = rt.place(() => t, (value8) => t = value8); + const _40: rt.Iter = rt.iterSkip(rt.iterMap(new rt.Range(0, rt.u64Or(rt.u64Shl(n as rt.u64, 8), m as rt.u64), rt.u64Step), (i4: rt.u64): rt.char => { + return f(rt.u64Low(i4)); + }), m as rt.usize); + _39.v = rt.stringExtendChars(_39.v, _40); + } else if (_29 === 3) { + const _52: rt.Place = rt.place(() => t, (value7) => t = value7); + const _53: rt.Iter = rt.iterTake(rt.iterMap(new rt.Range(0, n), f), m as rt.usize); + _52.v = rt.stringExtendChars(_52.v, _53); + } else if (_29 === 4) { + const _57: rt.Place = rt.place(() => t, (value6) => t = value6); + const _60: rt.Range = new rt.Range(0, n); + if (!(n === 4294967295)) { + _61 = 1; + } else { + _61 = rt.usizeAdd(1, rt.usizeRem(m as rt.usize, 2)); + } + const _58: rt.Iter = rt.iterMap(rt.iterStepBy(_60, _61), f); + _57.v = rt.stringExtendChars(_57.v, _58); + } else if (_29 === 5) { + if (rt.u32Rem(m, 2) === 0) { + const _69: rt.Place = rt.place(() => t, (value5) => t = value5); + const _70: rt.Iter = rt.iterChain(rt.iterMap(new rt.Range(0, n), f), rt.iterMap(new rt.Range(0, m), f)); + _69.v = rt.stringExtendChars(_69.v, _70); + } else { + const _75: rt.Place = rt.place(() => t, (value4) => t = value4); + const _76: rt.Iter = rt.iterMap(rt.iterZip(new rt.Range(0, n), new rt.Range(0, m), true), (_26: [rt.u32, rt.u32]): rt.char => { + return f(_26[0]); + }); + _75.v = rt.stringExtendChars(_75.v, _76); + } + } else if (_29 === 6) { + const _82: rt.Place = rt.place(() => t, (value3) => t = value3); + const _83: rt.Iter = rt.iterFlatMap(new rt.Range(0, n), (i2: rt.u32): rt.char[] => { + return [f(i2), f(i2)]; + }, (value: rt.char[]) => rt.iterSlice(value), false, 2); + _82.v = rt.stringExtendChars(_82.v, _83); + } else { + const chars: rt.Peekable = rt.iterPeekable(rt.iterFuse(rt.iterRev(rt.iterMap(rt.iterEnumerate(rt.iterMap(new rt.Range(0, n), f)), (_25: [rt.usize, rt.char]): rt.char => { + return _25[1]; + })))); + putChar(out, rt.peekablePeek(chars)); + const _99: rt.Place = rt.place(() => t, (value2) => t = value2); + _99.v = rt.stringExtendChars(_99.v, chars); + } + put(out, t); +} + //# sourceMappingURL=strs.ts.map diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 2e8c9e091..977544371 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -146,6 +146,7 @@ fn cases() -> Vec<(&'static str, Case)> { ("slices::ranges", slices::ranges), ("strs::iters", strs::iters), ("strs::drains", strs::drains), + ("strs::hinted", strs::hinted), ] } diff --git a/ts/fixtures/lang/src/strs.rs b/ts/fixtures/lang/src/strs.rs index 576857607..18ddff70b 100644 --- a/ts/fixtures/lang/src/strs.rs +++ b/ts/fixtures/lang/src/strs.rs @@ -830,3 +830,52 @@ pub fn drains(kind: u8, data: &[u8], out: &mut Vec) { put(out, &named.name); } } + +/// Range lengths whose size hint, through `hinted`'s adaptors, is either a few chars or +/// past where `isize::MAX` bytes of text start: std's `String` would reserve any between, +/// up to 2 GiB. +const HINTED: [u32; 7] = [0, 5, 8, 9, 0x7fff_fffd, 0x8000_0000, u32::MAX]; + +/// Chars from ranges as long as `data`'s first two bytes pick, through the adaptors `kind` +/// picks, into a `String` holding `aé` and the text of the rest: std's reserves the size +/// hint's lower bound first, which panics past `isize::MAX` before it reads a char, and +/// else the chars panic with `consumed` past the eighth. +pub fn hinted(kind: u8, data: &[u8], out: &mut Vec) { + let pick = |i: usize| HINTED[usize::from(data.get(i).copied().unwrap_or(0)) % HINTED.len()]; + let (n, m) = (pick(0), pick(1)); + let mut t = String::from("aé"); + t.push_str(&text(data.get(2..).unwrap_or(&[]))); + let calls = std::cell::Cell::new(0usize); + let f = |_: u32| { + calls.set(calls.get() + 1); + assert!(calls.get() <= 8, "consumed"); + ALPHABET[calls.get()] + }; + match kind % 8 { + 0 => t.extend((0..n).map(f)), + 1 => t = (0..=n).chain(0..n).map(f).collect(), + 2 => t.extend( + (0..u64::from(n) << 8 | u64::from(m)) + .map(|i| f(i as u32)) + .skip(m as usize), + ), + 3 => t.extend((0..n).map(f).take(m as usize)), + // Only twice the longest is past `isize::MAX` stepped by two. + 4 => t.extend( + (0..n) + .step_by(if n == u32::MAX { 1 + m as usize % 2 } else { 1 }) + .map(f), + ), + 5 => match m % 2 { + 0 => t.extend((0..n).map(f).chain((0..m).map(f))), + _ => t.extend((0..n).zip(0..m).map(|(i, _)| f(i))), + }, + 6 => t.extend((0..n).flat_map(|i| [f(i), f(i)])), + _ => { + let mut chars = (0..n).map(f).enumerate().map(|(_, c)| c).rev().fuse().peekable(); + put_char(out, chars.peek().copied()); + t.extend(chars); + } + } + put(out, &t); +} diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index 23157897b..f2ce35fe3 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -36,6 +36,11 @@ const USIZE_MAX = 0xffffffff; /** What drops a value. */ type Drop = (value: T) => void; +/** `a + b`, saturating at `usize::MAX` as std's size hints do. */ +function satAdd(a: number, b: number): number { + return Math.min(a + b, USIZE_MAX); +} + /** * What only the iterators std implements these traits for have, which Rust calls only * there: declared, not stubbed, so no bundle carries a stub. @@ -87,6 +92,15 @@ export abstract class Iter implements Iterable { return this; } + /** + * `size_hint`'s lower bound, which std's `String` reserves before it reads chars: exact + * for a range and what adapts one, the only iterators whose bound reaches `isize::MAX`, + * and else 0. + */ + lower(): number { + return 0; + } + /** * `fold`, as std's of its kind, which takes the iterator by value and drops what it * holds as std's does. `release` drops what `f` owns, as std drops the closure it was @@ -481,16 +495,19 @@ export function iterPeekable(it: Iter, drop?: Drop): Peekable { * source drops once it ends unless it is `fused`, as `fuse` does. */ export function iterFlatMap(it: Iter, f: (item: T) => Iter): Iter; -export function iterFlatMap(it: Iter, f: (item: T) => U, into?: (value: U) => Iter, fused?: boolean): Iter; -export function iterFlatMap(it: Iter, f: (item: T) => U, into?: (value: U) => Iter, fused = false): Iter { - return new FlattenIter(new MapIter(it, f), into ?? (itself as (value: U) => Iter), fused); +export function iterFlatMap(it: Iter, f: (item: T) => U, into?: (value: U) => Iter, fused?: boolean, size?: number): Iter; +export function iterFlatMap(it: Iter, f: (item: T) => U, into?: (value: U) => Iter, fused = false, size?: number): Iter { + return new FlattenIter(new MapIter(it, f), into ?? (itself as (value: U) => Iter), fused, size); } -/** `flatten`: `into` makes each item an iterator, as `IntoIterator` does, and `fused` is as `flat_map`'s. */ +/** + * `flatten`: `into` makes each item an iterator, as `IntoIterator` does, `fused` is as + * `flat_map`'s, and `size` is each item's length if it is an array. + */ export function iterFlatten(it: Iter>): Iter; -export function iterFlatten(it: Iter, into?: (value: T) => Iter, fused?: boolean): Iter; -export function iterFlatten(it: Iter, into?: (value: T) => Iter, fused = false): Iter { - return new FlattenIter(it, into ?? (itself as (value: T) => Iter), fused); +export function iterFlatten(it: Iter, into?: (value: T) => Iter, fused?: boolean, size?: number): Iter; +export function iterFlatten(it: Iter, into?: (value: T) => Iter, fused = false, size?: number): Iter { + return new FlattenIter(it, into ?? (itself as (value: T) => Iter), fused, size); } // Consumers. Each that takes an iterator by value that `drops` takes it as std's does: by @@ -1239,6 +1256,10 @@ class Borrowed extends Iter { this.inner = inner; } + override lower(): number { + return this.inner.lower(); + } + next(): T | Done { return this.inner.next(); } @@ -1278,6 +1299,10 @@ class MapIter extends Calling U> { super(inner, f); } + override lower(): number { + return this.inner.lower(); + } + next(): U | Done { const item = this.inner.next(); return item === DONE ? DONE : this.f(item); @@ -1400,6 +1425,10 @@ class FilterMapIter extends Calling U | undefined> { class EnumerateIter extends Adaptor { index = 0; + override lower(): number { + return this.inner.lower(); + } + next(): [number, T] | Done { const item = this.inner.next(); return item === DONE ? DONE : [this.index++, item]; @@ -1478,6 +1507,10 @@ class ChainIter extends Iter implements Disposable { dispose(b); } + override lower(): number { + return satAdd(this.a?.lower() ?? 0, this.b?.lower() ?? 0); + } + next(): T | Done { if (this.a !== undefined) { const item = this.a.next(); @@ -1610,6 +1643,10 @@ class ZipIter extends Iter<[A, B]> implements Disposable { return Math.min(this.a.len(), this.b.len()); } + override lower(): number { + return Math.min(this.a.lower(), this.b.lower()); + } + [Symbol.dispose](): void { dispose(this.a); dispose(this.b); @@ -1644,6 +1681,10 @@ class TrustedZipIter extends ZipIter { } class RevIter extends Adaptor { + override lower(): number { + return this.inner.lower(); + } + next(): T | Done { return this.inner.nextBack(); } @@ -1692,6 +1733,10 @@ class SkipIter extends Adaptor { this.drop = drop; } + override lower(): number { + return Math.max(this.inner.lower() - this.n, 0); + } + next(): T | Done { if (this.n === 0) return this.inner.next(); const n = this.n; @@ -1776,6 +1821,10 @@ class TakeIter extends Adaptor { this.drop = drop; } + override lower(): number { + return this.n === 0 ? 0 : Math.min(this.inner.lower(), this.n); + } + next(): T | Done { if (this.n === 0) return DONE; this.n--; @@ -1952,7 +2001,15 @@ class StepByIter extends Adaptor { } override len(): number { - const n = this.inner.len(); + return this.size(this.inner.len()); + } + + override lower(): number { + return this.size(this.inner.lower()); + } + + /** How many of `n` items it yields. */ + private size(n: number): number { if (this.firstTake) return n === 0 ? 0 : 1 + Math.floor((n - 1) / (this.step + 1)); return Math.floor(n / (this.step + 1)); } @@ -2019,6 +2076,10 @@ class StepByIter extends Adaptor { } class InspectIter extends Calling void> { + override lower(): number { + return this.inner.lower(); + } + next(): T | Done { return this.inspect(this.inner.next()); } @@ -2067,6 +2128,10 @@ class FuseIter extends Iter implements Disposable { this.fused = fused; } + override lower(): number { + return this.inner?.lower() ?? 0; + } + /** `item`, dropping its iterator first if it ended there and is not fused. */ clear(item: T | Done): T | Done { if (item === DONE && !this.fused) { @@ -2128,14 +2193,24 @@ class FlattenIter extends Iter implements Disposable { outer: Iter | undefined; readonly into: (value: T) => Iter; readonly fused: boolean; + /** How many items each of its source's has, if each is an array. */ + readonly size: number | undefined; front: Iter | undefined; back: Iter | undefined; - constructor(outer: Iter, into: (value: T) => Iter, fused: boolean) { + constructor(outer: Iter, into: (value: T) => Iter, fused: boolean, size?: number) { super(); this.outer = outer; this.into = into; this.fused = fused; + this.size = size; + } + + /** std's: what its front and back hold, and its source's arrays, if it knows their size. */ + override lower(): number { + const held = satAdd(this.front?.lower() ?? 0, this.back?.lower() ?? 0); + if (this.size === undefined) return held; + return satAdd((this.outer?.lower() ?? 0) * this.size, held); } /** The source's next item, or `DONE`, which drops it unless it is fused. */ @@ -2307,6 +2382,12 @@ export class Peekable extends Adaptor { return item; } + /** std's: none past a peeked end, and one more than its iterator's for a peeked item. */ + override lower(): number { + if (!this.peeked) return this.inner.lower(); + return this.item === DONE ? 0 : satAdd(this.inner.lower(), 1); + } + next(): T | Done { if (!this.peeked) return this.inner.next(); return this.take(); @@ -2437,6 +2518,12 @@ function step(n: T, by: number, int: Step | undefined): T | undefined { return over ? undefined : sum; } +/** std's `Step::steps_between` of `a` and `b`, at most `b`, saturating at `usize::MAX`. */ +function stepsBetween(a: T, b: T): number { + const n = typeof a === "bigint" ? Number((b as bigint) - (a as bigint)) : (b as number) - (a as number); + return Math.min(n, USIZE_MAX); +} + /** * `ops::Range`: `start..end`, which iterates its integers by moving `start`. A 64- or * 128-bit range takes its type's `Step`, `rt.u64Step`, to step past 2^53. @@ -2491,6 +2578,10 @@ export class Range extends Iter { override len(): number { return lt(this.start, this.end) ? Number(this.end) - Number(this.start) : 0; } + + override lower(): number { + return lt(this.start, this.end) ? stepsBetween(this.start, this.end) : 0; + } } /** `Range::is_empty`. */ @@ -2546,6 +2637,10 @@ export class RangeInclusive extends Iter { return rangeInclusiveIsEmpty(this) ? 0 : Number(this.hi) - Number(this.lo) + 1; } + override lower(): number { + return rangeInclusiveIsEmpty(this) ? 0 : satAdd(stepsBetween(this.lo, this.hi), 1); + } + /** std's `nth`: `start` moves past the item, wrapping if it overflows, which ends the range. */ override nth(n: number): T | Done { if (rangeInclusiveIsEmpty(this)) return DONE; diff --git a/ts/runtime/rt/str.ts b/ts/runtime/rt/str.ts index 04896fa09..95fb6c790 100644 --- a/ts/runtime/rt/str.ts +++ b/ts/runtime/rt/str.ts @@ -455,8 +455,12 @@ export function stringSplitOff(s: string, i: usize): [string, string] { // `String` from and into iterators. Each reads the iterator by `for_each`, as std's does, // which `drops` makes its own `fold`. -/** `Extend` and `Extend<&char>` of `s`, and `collect` of chars from `""`: `s` and each char. */ +/** + * `Extend` and `Extend<&char>` of `s`, and `collect` of chars from `""`: `s` and each + * char, once it reserved the iterator's lower bound, which panics past `isize::MAX`. + */ export function stringExtendChars(s: string, it: Iter, drops = false): string { + stringReserve(s, it.lower()); iterForEach( it, (c) => { diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index c9ee041ae..da107ec0f 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -574,14 +574,16 @@ pub fn call<'tcx>( flattens(b, inner, span); let into = into_fn(b, targ(1)?, span)?.unwrap_or_else(Expr::undefined); let fused = fused(b, self_ty, Some(targ(2)?)); - method("flatMap", tail(rest, vec![into, fused])) + let size = array_size(b, targ(1)?); + method("flatMap", tail(rest, vec![into, fused, size])) } "flatten" => { let inner = iter_of(b.t, item)?; flattens(b, inner, span); let into = into_fn(b, item, span)?.unwrap_or_else(Expr::undefined); let fused = fused(b, self_ty, None); - method("flatten", tail(Vec::new(), vec![into, fused])) + let size = array_size(b, item); + method("flatten", tail(Vec::new(), vec![into, fused, size])) } "copied" | "cloned" => { let ty::Ref(_, inner, _) = item.kind() else { @@ -639,6 +641,21 @@ fn fused<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, held: Option>) }) } +/// How many items `ty` has, if it is an array or a reference to one, which std's `flatten` +/// counts toward its size hint: else `undefined`. +fn array_size<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> Expr { + let ty = match *ty.kind() { + ty::Ref(_, inner, _) => inner, + _ => ty, + }; + match ty.kind() { + ty::Array(_, len) => len + .try_to_target_usize(b.t.tcx) + .map_or_else(Expr::undefined, |n| Expr::lit(n.to_string())), + _ => Expr::undefined(), + } +} + /// The path of the std type `ty` is, if it is one. fn std_iter_path(tcx: ty::TyCtxt<'_>, ty: Ty<'_>) -> Option> { let ty::Adt(adt, _) = ty.kind() else { From 75dd54191b70eb64c01a2fb8d422ef1c09a11152 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 08:08:42 -0700 Subject: [PATCH 5/6] fix(ts): give each source its size hint's lower bound, and wide ranges theirs Slices, chunks, options, maps, deques, drains, chars, and successors report std's lower bound, so a range chained with them passes isize::MAX where std's does, and a range of integers past 2^53 counts its limbs exactly. Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/caller.ts | 4 +- ts/fixtures/lang/expected/strs.ts | 116 ++++++++++++-------- ts/fixtures/lang/src/strs.rs | 18 ++- ts/fixtures/task/expected/drops/closures.ts | 2 +- ts/runtime/rt/collections.ts | 25 +++++ ts/runtime/rt/iter.ts | 27 ++++- ts/runtime/rt/str.ts | 40 +++---- 7 files changed, 155 insertions(+), 77 deletions(-) diff --git a/ts/fixtures/lang/caller.ts b/ts/fixtures/lang/caller.ts index d40a85b86..6aad5c5ad 100644 --- a/ts/fixtures/lang/caller.ts +++ b/ts/fixtures/lang/caller.ts @@ -373,8 +373,8 @@ const drains = Array.from({ length: 32 * 13 * 13 }, (_, i) => { /** `strs::iters` of each kind, on no text, text of each UTF-8 width, and parts between `/`s, empty ones too. */ const iters = Array.from({ length: 12 }, (_, kind) => [[], [2, 7, 9, 14], [0, 2, 0, 0, 7, 9], [14, 0]].map((data) => Uint8Array.of(op("strs::iters"), kind, ...data))).flat(); -/** `strs::hinted` of each adaptor and each pair of range lengths, on `aé` alone. */ +/** `strs::hinted` of each adaptor and each pair of range lengths, each way, on `aé` alone. */ const hintedOp = op("strs::hinted"); -const hinted = Array.from({ length: 8 * 7 * 7 }, (_, i) => Uint8Array.of(hintedOp, i % 8, Math.floor(i / 8) % 7, Math.floor(i / 56))); +const hinted = Array.from({ length: 10 * 7 * 21 }, (_, i) => Uint8Array.of(hintedOp, i % 10, Math.floor(i / 10) % 7, Math.floor(i / 70))); export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...formats, ...parsed, ...dyns, ...cells, ...shared, ...locks, ...onces, ...bounds, ...unsafeCells, ...edits, ...subslices, ...consumers, ...ranges, ...drains, ...iters, ...hinted]; diff --git a/ts/fixtures/lang/expected/strs.ts b/ts/fixtures/lang/expected/strs.ts index 6a4da7ac5..a51a77d1b 100644 --- a/ts/fixtures/lang/expected/strs.ts +++ b/ts/fixtures/lang/expected/strs.ts @@ -1408,7 +1408,7 @@ export function drains(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { * else the chars panic with `consumed` past the eighth. */ export function hinted(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { - let _61!: rt.usize; + let _70!: rt.usize; const pick: (a0: rt.usize) => rt.u32 = (i: rt.usize): rt.u32 => { const _4: rt.usize = rt.usizeRem(unwrapOr$0r2bmpr(rt.byteSliceGet(data, i), 0) as rt.usize, [0, 5, 8, 9, 2147483645, 2147483648, 4294967295].length); if (!(_4 < 7)) { @@ -1419,75 +1419,101 @@ export function hinted(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { const _7: [rt.u32, rt.u32] = [pick(0), pick(1)]; const n: rt.u32 = _7[0]; const m: rt.u32 = _7[1]; + const sub: rt.usize = rt.usizeDiv(unwrapOr$0r2bmpr(rt.byteSliceGet(data, 1), 0) as rt.usize, [0, 5, 8, 9, 2147483645, 2147483648, 4294967295].length); let t: string = "aé"; - const _16: rt.Place = rt.place(() => t, (value) => t = value); - const _17: string = text(unwrapOr$1rbuegs(rt.byteSliceGetRange(data, 2), rt.byteSliceFrom(Uint8Array.of()))); - _16.v = _16.v + _17; + const _25: rt.Place = rt.place(() => t, (value) => t = value); + const _26: string = text(unwrapOr$1rbuegs(rt.byteSliceGetRange(data, 2), rt.byteSliceFrom(Uint8Array.of()))); + _25.v = _25.v + _26; const calls: rt.Cell = new rt.Cell(0); const f: (a0: rt.u32) => rt.char = (_2: rt.u32): rt.char => { rt.cellSet(calls, rt.usizeAdd(calls.v, 1)); if (!(calls.v <= 8)) { rt.panicFmt(["consumed"]); } else { - const _122: rt.usize = calls.v; - if (!(_122 < 16)) { - rt.panic("index out of bounds: the len is " + 16 + " but the index is " + _122); + const _127: rt.usize = calls.v; + if (!(_127 < 16)) { + rt.panic("index out of bounds: the len is " + 16 + " but the index is " + _127); } - return [47, 92, 97, 48, 57, 127, 128, 233, 2047, 2048, 55295, 57344, 65535, 65536, 128512, 1114111][_122]; + return [47, 92, 97, 48, 57, 127, 128, 233, 2047, 2048, 55295, 57344, 65535, 65536, 128512, 1114111][_127]; } }; - const _29: rt.u8 = rt.u8Rem(kind, 8); - if (_29 === 0) { - const _31: rt.Place = rt.place(() => t, (value9) => t = value9); - const _32: rt.Iter = rt.iterMap(new rt.Range(0, n), f); - _31.v = rt.stringExtendChars(_31.v, _32); - } else if (_29 === 1) { + const _38: rt.u8 = rt.u8Rem(kind, 10); + if (_38 === 0) { + const _40: rt.Place = rt.place(() => t, (value13) => t = value13); + const _41: rt.Iter = rt.iterMap(new rt.Range(0, n), f); + _40.v = rt.stringExtendChars(_40.v, _41); + } else if (_38 === 1) { t = rt.stringExtendChars("", rt.iterMap(rt.iterChain(new rt.RangeInclusive(0, n, rt.u32Step), new rt.Range(0, n)), f)); - } else if (_29 === 2) { - const _39: rt.Place = rt.place(() => t, (value8) => t = value8); - const _40: rt.Iter = rt.iterSkip(rt.iterMap(new rt.Range(0, rt.u64Or(rt.u64Shl(n as rt.u64, 8), m as rt.u64), rt.u64Step), (i4: rt.u64): rt.char => { + } else if (_38 === 2) { + const _48: rt.Place = rt.place(() => t, (value12) => t = value12); + const _49: rt.Iter = rt.iterSkip(rt.iterMap(new rt.Range(0, rt.u64Or(rt.u64Shl(n as rt.u64, 8), m as rt.u64), rt.u64Step), (i4: rt.u64): rt.char => { return f(rt.u64Low(i4)); }), m as rt.usize); - _39.v = rt.stringExtendChars(_39.v, _40); - } else if (_29 === 3) { - const _52: rt.Place = rt.place(() => t, (value7) => t = value7); - const _53: rt.Iter = rt.iterTake(rt.iterMap(new rt.Range(0, n), f), m as rt.usize); - _52.v = rt.stringExtendChars(_52.v, _53); - } else if (_29 === 4) { - const _57: rt.Place = rt.place(() => t, (value6) => t = value6); - const _60: rt.Range = new rt.Range(0, n); + _48.v = rt.stringExtendChars(_48.v, _49); + } else if (_38 === 3) { + const _61: rt.Place = rt.place(() => t, (value11) => t = value11); + const _62: rt.Iter = rt.iterTake(rt.iterMap(new rt.Range(0, n), f), m as rt.usize); + _61.v = rt.stringExtendChars(_61.v, _62); + } else if (_38 === 4) { + const _66: rt.Place = rt.place(() => t, (value10) => t = value10); + const _69: rt.Range = new rt.Range(0, n); if (!(n === 4294967295)) { - _61 = 1; + _70 = 1; } else { - _61 = rt.usizeAdd(1, rt.usizeRem(m as rt.usize, 2)); + _70 = rt.usizeAdd(1, rt.usizeRem(m as rt.usize, 2)); } - const _58: rt.Iter = rt.iterMap(rt.iterStepBy(_60, _61), f); - _57.v = rt.stringExtendChars(_57.v, _58); - } else if (_29 === 5) { + const _67: rt.Iter = rt.iterMap(rt.iterStepBy(_69, _70), f); + _66.v = rt.stringExtendChars(_66.v, _67); + } else if (_38 === 5) { if (rt.u32Rem(m, 2) === 0) { - const _69: rt.Place = rt.place(() => t, (value5) => t = value5); - const _70: rt.Iter = rt.iterChain(rt.iterMap(new rt.Range(0, n), f), rt.iterMap(new rt.Range(0, m), f)); - _69.v = rt.stringExtendChars(_69.v, _70); + const _78: rt.Place = rt.place(() => t, (value9) => t = value9); + const _79: rt.Iter = rt.iterChain(rt.iterMap(new rt.Range(0, n), f), rt.iterMap(new rt.Range(0, m), f)); + _78.v = rt.stringExtendChars(_78.v, _79); } else { - const _75: rt.Place = rt.place(() => t, (value4) => t = value4); - const _76: rt.Iter = rt.iterMap(rt.iterZip(new rt.Range(0, n), new rt.Range(0, m), true), (_26: [rt.u32, rt.u32]): rt.char => { - return f(_26[0]); + const _84: rt.Place = rt.place(() => t, (value8) => t = value8); + const _85: rt.Iter = rt.iterMap(rt.iterZip(new rt.Range(0, n), new rt.Range(0, m), true), (_211: [rt.u32, rt.u32]): rt.char => { + return f(_211[0]); }); - _75.v = rt.stringExtendChars(_75.v, _76); + _84.v = rt.stringExtendChars(_84.v, _85); } - } else if (_29 === 6) { - const _82: rt.Place = rt.place(() => t, (value3) => t = value3); - const _83: rt.Iter = rt.iterFlatMap(new rt.Range(0, n), (i2: rt.u32): rt.char[] => { + } else if (_38 === 6) { + const _91: rt.Place = rt.place(() => t, (value7) => t = value7); + const _92: rt.Iter = rt.iterFlatMap(new rt.Range(0, n), (i2: rt.u32): rt.char[] => { return [f(i2), f(i2)]; }, (value: rt.char[]) => rt.iterSlice(value), false, 2); - _82.v = rt.stringExtendChars(_82.v, _83); + _91.v = rt.stringExtendChars(_91.v, _92); + } else if (_38 === 7) { + const _96: rt.Place = rt.place(() => t, (value6) => t = value6); + const _97: rt.Iter = rt.iterChain(rt.iterChain(rt.iterChain(rt.iterChain(rt.iterMap(new rt.Range(0, rt.u32SaturatingSub(n, 5)), f), rt.iterOnce(120)), rt.iterOption(121)), rt.strChars("é😀")), rt.iterSlice([122])); + _96.v = rt.stringExtendChars(_96.v, _97); + } else if (_38 === 8) { + const _112: rt.usize = rt.usizeRem(sub, 3); + if (_112 === 0) { + const _114: rt.Place = rt.place(() => t, (value5) => t = value5); + const _115: rt.Iter = rt.iterMap(new rt.Range({ hi: 0x10000000, lo: 0 } as rt.u64, rt.u64Add({ hi: 0x10000000, lo: 0 } as rt.u64, n as rt.u64), rt.u64Step), (_210: rt.u64): rt.char => { + return f(0); + }); + _114.v = rt.stringExtendChars(_114.v, _115); + } else if (_112 === 1) { + const _128: rt.Place = rt.place(() => t, (value4) => t = value4); + const _129: rt.Iter = rt.iterMap(new rt.Range(rt.i128Neg(rt.i128Shl(1, 100)), rt.i128Add(rt.i128Neg(rt.i128Shl(1, 100)), n as rt.i128), rt.i128Step), (_27: rt.i128): rt.char => { + return f(0); + }); + _128.v = rt.stringExtendChars(_128.v, _129); + } else { + const _146: rt.Place = rt.place(() => t, (value3) => t = value3); + const _147: rt.Iter = rt.iterMap(new rt.RangeInclusive({ hi: 0x10000000, lo: 0 } as rt.u64, rt.u64Add({ hi: 0x10000000, lo: 0 } as rt.u64, n as rt.u64), rt.u64Step), (_24: rt.u64): rt.char => { + return f(0); + }); + _146.v = rt.stringExtendChars(_146.v, _147); + } } else { - const chars: rt.Peekable = rt.iterPeekable(rt.iterFuse(rt.iterRev(rt.iterMap(rt.iterEnumerate(rt.iterMap(new rt.Range(0, n), f)), (_25: [rt.usize, rt.char]): rt.char => { - return _25[1]; + const chars: rt.Peekable = rt.iterPeekable(rt.iterFuse(rt.iterRev(rt.iterMap(rt.iterEnumerate(rt.iterMap(new rt.Range(0, n), f)), (_23: [rt.usize, rt.char]): rt.char => { + return _23[1]; })))); putChar(out, rt.peekablePeek(chars)); - const _99: rt.Place = rt.place(() => t, (value2) => t = value2); - _99.v = rt.stringExtendChars(_99.v, chars); + const _172: rt.Place = rt.place(() => t, (value2) => t = value2); + _172.v = rt.stringExtendChars(_172.v, chars); } put(out, t); } diff --git a/ts/fixtures/lang/src/strs.rs b/ts/fixtures/lang/src/strs.rs index 18ddff70b..1c1698b04 100644 --- a/ts/fixtures/lang/src/strs.rs +++ b/ts/fixtures/lang/src/strs.rs @@ -843,6 +843,7 @@ const HINTED: [u32; 7] = [0, 5, 8, 9, 0x7fff_fffd, 0x8000_0000, u32::MAX]; pub fn hinted(kind: u8, data: &[u8], out: &mut Vec) { let pick = |i: usize| HINTED[usize::from(data.get(i).copied().unwrap_or(0)) % HINTED.len()]; let (n, m) = (pick(0), pick(1)); + let sub = usize::from(data.get(1).copied().unwrap_or(0)) / HINTED.len(); let mut t = String::from("aé"); t.push_str(&text(data.get(2..).unwrap_or(&[]))); let calls = std::cell::Cell::new(0usize); @@ -851,7 +852,7 @@ pub fn hinted(kind: u8, data: &[u8], out: &mut Vec) { assert!(calls.get() <= 8, "consumed"); ALPHABET[calls.get()] }; - match kind % 8 { + match kind % 10 { 0 => t.extend((0..n).map(f)), 1 => t = (0..=n).chain(0..n).map(f).collect(), 2 => t.extend( @@ -871,6 +872,21 @@ pub fn hinted(kind: u8, data: &[u8], out: &mut Vec) { _ => t.extend((0..n).zip(0..m).map(|(i, _)| f(i))), }, 6 => t.extend((0..n).flat_map(|i| [f(i), f(i)])), + // Sources whose hint is a few chars, which make it `n` only if each counts its own. + 7 => t.extend( + (0..n.saturating_sub(5)) + .map(f) + .chain(std::iter::once('x')) + .chain(Some('y')) + .chain("é😀".chars()) + .chain(['z'].iter().copied()), + ), + // Integers past 2^53. + 8 => match sub % 3 { + 0 => t.extend((1u64 << 60..(1u64 << 60) + u64::from(n)).map(|_| f(0))), + 1 => t.extend((-(1i128 << 100)..-(1i128 << 100) + i128::from(n)).map(|_| f(0))), + _ => t.extend((1u64 << 60..=(1u64 << 60) + u64::from(n)).map(|_| f(0))), + }, _ => { let mut chars = (0..n).map(f).enumerate().map(|(_, c)| c).rev().fuse().peekable(); put_char(out, chars.peek().copied()); diff --git a/ts/fixtures/task/expected/drops/closures.ts b/ts/fixtures/task/expected/drops/closures.ts index 32688f68b..a0b5b00a6 100644 --- a/ts/fixtures/task/expected/drops/closures.ts +++ b/ts/fixtures/task/expected/drops/closures.ts @@ -498,7 +498,7 @@ export function iterated(log: Log.Log, id: rt.u32): void { return [_2, Guard.id(captured8)]; }, (): void => { captured8[Symbol.dispose](); - }), (value: rt.u32[]) => rt.iterSlice(value), true); + }), (value: rt.u32[]) => rt.iterSlice(value), true, 2); while (rt.iterToOption(ended.next()) !== undefined) { } Log.note(log, rt.fmtFormat(["flat_map ended"])); diff --git a/ts/runtime/rt/collections.ts b/ts/runtime/rt/collections.ts index 1bca021fa..eac9474cc 100644 --- a/ts/runtime/rt/collections.ts +++ b/ts/runtime/rt/collections.ts @@ -650,6 +650,10 @@ export class HashIter extends Iter implements Disposable { return this.left; } + override lower(): number { + return this.len(); + } + [Symbol.dispose](): void { if (this.k === undefined && this.v === undefined) return; const n = this.left; @@ -1291,6 +1295,11 @@ export class BTreeIter extends Iter implements Disposable { return this.n; } + override lower(): number { + // A range's count is unknown, and std's hint of one is 0. + return Math.max(this.n, 0); + } + [Symbol.dispose](): void { if (this.k === undefined && this.v === undefined) return; const keys: K[] = []; @@ -1892,6 +1901,10 @@ class DequeIter extends Iter implements Disposable { return this.back - this.front; } + override lower(): number { + return this.len(); + } + [Symbol.dispose](): void { if (this.drop === undefined) return; const items: T[] = []; @@ -1939,6 +1952,10 @@ class DequePlaces extends Iter> { override len(): number { return this.back - this.front; } + + override lower(): number { + return this.len(); + } } /** `drain`: the items within `r`, taken out at once, which drop by `drop` as `dequeIter`'s. */ @@ -2050,6 +2067,10 @@ class VecDrain extends Iter implements Disposable { return this.back - this.front; } + override lower(): number { + return this.len(); + } + [Symbol.dispose](): void { const from = this.front; this.front = this.back; @@ -2099,6 +2120,10 @@ class ByteVecDrain extends Iter implements Disposable { return this.back - this.front; } + override lower(): number { + return this.len(); + } + [Symbol.dispose](): void { if (this.length < 0) return; this.v.data.copyWithin(this.start, this.end, this.length); diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index f2ce35fe3..fd782ab21 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -21,7 +21,7 @@ import { ByteSlice, ByteVec, byteVecPush, Err, type None, Ok, type Place, panic, type RangeBound, type Result, sliceView } from "../rt.ts"; import { AtIndex, BTreeIter, HashIter } from "./collections.ts"; -import { type Limbs, lt as wideLt, type Step } from "./int.ts"; +import { intToBig, type Limbs, lt as wideLt, type Step } from "./int.ts"; import { drop as dispose, dropRange } from "./task.ts"; /** What `next` returns once an iterator is exhausted, which no item is. */ @@ -1021,6 +1021,10 @@ class SliceIter extends Iter implements Disposable { return this.back - this.front; } + override lower(): number { + return this.len(); + } + [Symbol.dispose](): void { if (this.drop === undefined) return; const from = this.front; @@ -1070,6 +1074,10 @@ class SliceMutIter extends Iter> { return this.back - this.front; } + override lower(): number { + return this.len(); + } + override advanceBy(n: number): number { const step = Math.min(n, this.back - this.front); this.front += step; @@ -1140,6 +1148,10 @@ class PartsIter extends Iter { return this.back - this.front; } + override lower(): number { + return this.len(); + } + override advanceBy(n: number): number { const step = Math.min(n, this.back - this.front); this.front += step; @@ -1182,6 +1194,10 @@ class OptionIter extends Iter implements Disposable { return this.some ? 1 : 0; } + override lower(): number { + return this.len(); + } + [Symbol.dispose](): void { if (this.drop === undefined) return; const item = this.next(); @@ -1234,6 +1250,10 @@ class SuccessorsIter extends Iter implements Disposable { return item; } + override lower(): number { + return this.item === undefined ? 0 : 1; + } + [Symbol.dispose](): void { if (this.dropped) return; this.dropped = true; @@ -2520,8 +2540,9 @@ function step(n: T, by: number, int: Step | undefined): T | undefined { /** std's `Step::steps_between` of `a` and `b`, at most `b`, saturating at `usize::MAX`. */ function stepsBetween(a: T, b: T): number { - const n = typeof a === "bigint" ? Number((b as bigint) - (a as bigint)) : (b as number) - (a as number); - return Math.min(n, USIZE_MAX); + if (typeof a === "number" && typeof b === "number") return Math.min(b - a, USIZE_MAX); + const n = intToBig(b as number | Limbs) - intToBig(a as number | Limbs); + return n < USIZE_MAX ? Number(n) : USIZE_MAX; } /** diff --git a/ts/runtime/rt/str.ts b/ts/runtime/rt/str.ts index 95fb6c790..3b375fcaa 100644 --- a/ts/runtime/rt/str.ts +++ b/ts/runtime/rt/str.ts @@ -120,6 +120,16 @@ class Chars extends Iter { this.#back = before(this.#s, this.#back); return this.#s.codePointAt(this.#back) as char; } + + /** std's: a char per four bytes of what is left. */ + override lower(): number { + return Math.ceil(utf8Len(this.#s, this.#front, this.#back) / 4); + } + + /** `as_str`: what is left. */ + rest(): string { + return this.#s.slice(this.#front, this.#back); + } } /** @@ -506,41 +516,21 @@ export function stringDrain(s: Place, r: Bounds): Iter { /** `Drain::as_str`: the chars `drain` has yet to yield. */ export function stringDrainAsStr(drain: Iter): string { - return (drain as StringDrain).rest(); + return (drain as Chars).rest(); } -class StringDrain extends Iter implements Disposable { +class StringDrain extends Chars implements Disposable { readonly #s: Place; readonly #text: string; readonly #from: number; readonly #to: number; - #front: number; - #back: number; constructor(s: Place, text: string, from: number, to: number) { - super(); + super(text.slice(from, to)); this.#s = s; this.#text = text; - this.#from = this.#front = from; - this.#to = this.#back = to; - } - - next(): char | Done { - if (this.#front >= this.#back) return DONE; - const c = this.#text.codePointAt(this.#front) as number; - this.#front += c > 0xffff ? 2 : 1; - return c as char; - } - - override nextBack(): char | Done { - if (this.#front >= this.#back) return DONE; - this.#back = before(this.#text, this.#back); - return this.#text.codePointAt(this.#back) as char; - } - - - rest(): string { - return this.#text.slice(this.#front, this.#back); + this.#from = from; + this.#to = to; } [Symbol.dispose](): void { From 62414dbca0589048da9e7a92b77c43b8d50dd262 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 08:34:27 -0700 Subject: [PATCH 6/6] test(ts): regenerate the lang harness after merging main Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/harness.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index abf735a5d..16676cccb 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -37,7 +37,7 @@ import { copies as shapes_copies, order, discriminants, loops, patterns, closure import { parts, chunked, subslices, drains, ranges as slices_ranges, edits as slices_edits } from "./slices.ts"; import { splits } from "./splits.ts"; import { statics, cells as statics_cells, onces } from "./statics.ts"; -import { strs, slices, edits as strs_edits, parses as strs_parses } from "./strs.ts"; +import { strs, slices, edits as strs_edits, parses as strs_parses, iters, drains as strs_drains, hinted } from "./strs.ts"; import { decodes } from "./utf8.ts"; import { wrappers } from "./wrappers.ts"; @@ -90,7 +90,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) refs(data21, out21); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_2121: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { counts(data22, out22); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2122: rt.u8, _378: rt.ByteSlice, out23: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2122: rt.u8, _387: rt.ByteSlice, out23: rt.ByteVec): void => { rounding(out23); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::overrides", overrides] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::dyns", (_2123: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { dyns(data23, out24); @@ -176,7 +176,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) reducers(data63, out64); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::writers", (_2164: rt.u8, data64: rt.ByteSlice, out65: rt.ByteVec): void => { writers(data64, out65); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["errors::eqs", eqs] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["slices::edits", slices_edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]; + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["errors::eqs", eqs] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["slices::edits", slices_edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::iters", iters] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::drains", strs_drains] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::hinted", hinted] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]; } /** Run one case. */