diff --git a/quest/m3/README.md b/quest/m3/README.md index 0eed7a053..d7e34b8ea 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -30,7 +30,6 @@ generic std gap is planned here even when moq never calls it. - [A B-tree's drop order](/quest/m3/ts-btree-drop.md) - dropping a whole `BTreeMap` or `BTreeSet` drops its entries node by node, as std's does - [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 - [Vec and slice accessors of elements whose None is rt.None](/quest/m3/ts-option-elements.md) - `Vec::pop` and `slice::last` of `Option` elements keep `None` apart from `Some(None)` - [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 - [Peekable::next_if_eq against another type](/quest/m3/ts-next-if-eq-other.md) - `next_if_eq` with an expected value of another type than the item, such as a `&str` of `String` items, translates diff --git a/quest/m3/ts-array-as-slice.md b/quest/m3/ts-array-as-slice.md deleted file mode 100644 index 3c815701e..000000000 --- a/quest/m3/ts-array-as-slice.md +++ /dev/null @@ -1,14 +0,0 @@ -# [XS] An array as a slice - -## Goal - -rust2-ts translates `<[T; N]>::as_slice` and `as_mut_slice`, for bytes and other values. -Proven by differential tests against native Rust. - -## Plan - -`as_slice` refuses with a span today, as "unsupported call to `std::array::::as_slice`". -A `&[u8; N]` is already an `rt.ByteSlice` and a reference to another array its JS array, -so each is the reference it is given. - -Public API: the translator's output. diff --git a/ts/fixtures/lang/expected/arrays.ts b/ts/fixtures/lang/expected/arrays.ts index cfc2e7204..948ba8f01 100644 --- a/ts/fixtures/lang/expected/arrays.ts +++ b/ts/fixtures/lang/expected/arrays.ts @@ -1,6 +1,7 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; +import { copy$u32s, copy$u8s } from "./lib.ts"; /** Each byte an operation, its value the operand; each puts what it read. */ export function fromFns(data: rt.ByteSlice, out: rt.ByteVec): void { @@ -94,4 +95,108 @@ export function counted$5(op: rt.u8): rt.usize[] { }); } +/** + * Arrays as slices, of bytes and of other values, empty ones included; each byte an + * operation, its value the operand. Writes through `as_mut_slice` land in the array. + */ +export function asSlices(data: rt.ByteSlice, out: rt.ByteVec): void { + const bytes: Uint8Array = rt.bytes(4); + const wide: rt.u16[] = Array.from({ length: 3 }, (): rt.u16 => { + return 0; + }); + const words: string[] = ["", ""]; + const item = copy$u8s(rt.bytes(2)); + const grid: Uint8Array[] = Array.from({ length: 3 }, (): Uint8Array => { + return copy$u8s(item); + }); + const item2 = copy$u32s(Array.from({ length: 2 }, (): rt.u32 => { + return 0; + })); + const pairs: rt.u32[][] = Array.from({ length: 2 }, (): rt.u32[] => { + return copy$u32s(item2); + }); + for (const op of rt.byteSliceView(data)) { + const i: rt.usize = op as rt.usize; + const _22: rt.u8 = rt.u8Rem(op, 8); + if (_22 === 0) { + const s: rt.ByteSlice = rt.byteSliceFrom(bytes); + const _26: rt.usize = rt.usizeRem(i, 4); + rt.idx(s, _26); + s.data[s.start + _26] = op; + rt.byteSliceRotateLeft(s, 1); + } else if (_22 === 1) { + const _32: rt.u16 = op as rt.u16; + const _35: rt.u16[] = wide; + const _37: rt.usize = rt.usizeRem(i, 3); + rt.idx(_35, _37); + _35[_37] = (_32 << 7) & 0xffff; + rt.sliceRotateRight(wide, 1); + } else if (_22 === 2) { + const _46: string[] = words; + const _48: rt.usize = rt.usizeRem(i, 2); + rt.idx(_46, _48); + _46[_48] = _46[_48] + String.fromCodePoint(rt.u8Add(97, rt.u8Rem(op, 26)) as rt.char); + } else if (_22 === 3) { + const _59: Uint8Array[] = grid; + const _61: rt.usize = rt.usizeRem(i, 3); + rt.idx(_59, _61); + const row: rt.ByteSlice = rt.byteSliceFrom(_59[_61]); + const _66: rt.ByteSlice = row; + const _67: rt.usize = rt.usizeRem(i, 2); + rt.idx(_66, _67); + _66.data[_66.start + _67] = op; + const _73: rt.ByteSlice = row; + const _75: rt.usize = rt.usizeRem(rt.usizeAdd(i, 1), 2); + rt.idx(_73, _75); + const read: rt.u8 = _73.data[_73.start + _75]; + const _81: rt.usize = rt.usizeRem(rt.usizeAdd(i, 1), 3); + if (!(_81 < 3)) { + rt.panic("index out of bounds: the len is " + 3 + " but the index is " + _81); + } + grid[_81][0] = grid[_81][0] ^ read; + } else if (_22 === 4) { + const all: rt.u32[][] = pairs; + const _93: rt.usize = rt.usizeRem(i, 2); + rt.idx(all, _93); + rt.sliceCopyFromSlice(all[_93], [op as rt.u32, (((op as rt.u32)) << 20) >>> 0]); + rt.sliceSwap(all, 0, 1); + } else if (_22 === 5) { + (([] as rt.u16[])).reverse(); + rt.byteSliceFill(rt.byteSliceFrom(Uint8Array.of()), op); + rt.byteVecPush(out, rt.u8Add((([] as rt.u16[])).length & 0xff, rt.byteSliceFrom(Uint8Array.of()).length & 0xff)); + } else if (_22 === 6) { + written(rt.byteSliceFrom(bytes), wide, op); + } else { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u32ToBeBytes(rt.iterSum(rt.iterMap(rt.iterSlice(wide), (_2: rt.u16): rt.u32 => { + return _2 as rt.u32; + }), rt.u32Add, 0)).reverse())); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(bytes)); + } + } + const _143: rt.ByteSlice = rt.byteSliceFrom(bytes); + const _146: rt.u16[] = wide; + const _149: string[] = words; + const _152: Uint8Array[] = grid; + const _155: rt.u32[][] = pairs; + const _158: rt.u16[] = []; + const _161: rt.ByteSlice = rt.byteSliceFrom(Uint8Array.of()); + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtList(rt.byteSliceView(_143), f, rt.u8Debug), (f: rt.Formatter) => rt.fmtList(_146, f, rt.u16Debug), (f: rt.Formatter) => rt.fmtList(_149, f, rt.fmtStr), (f: rt.Formatter) => rt.fmtList(_152, f, (value: Uint8Array, f: rt.Formatter) => rt.fmtList(value, f, rt.u8Debug)), (f: rt.Formatter) => rt.fmtList(_155, f, (value: rt.u32[], f: rt.Formatter) => rt.fmtList(value, f, rt.u32Debug)), (f: rt.Formatter) => rt.fmtList(_158, f, rt.u16Debug), (f: rt.Formatter) => rt.fmtList(rt.byteSliceView(_161), f, rt.u8Debug)]; + rt.byteVecExtendFromSlice(out, rt.stringAsBytes(rt.fmtFormat([args2[0], " ", args2[1], " ", args2[2], " ", args2[3], " ", args2[4], " ", args2[5], " ", args2[6]]))); +} + +/** Arrays lent by `&mut`, written through their slices. */ +export function written(bytes: rt.ByteSlice, wide: rt.u16[], op: rt.u8): void { + const s: rt.ByteSlice = bytes; + rt.idx(s, 0); + const _5: rt.u8 = rt.u8WrappingAdd(s.data[s.start], op); + rt.idx(s, 3); + s.data[s.start + 3] = _5; + const w: rt.u16[] = wide; + const _16: rt.u16 = op as rt.u16; + rt.idx(w, 0); + w[0] = _16; + rt.sliceSwap(w, 1, 2); + rt.sliceRangeMut(w, 1).fill(rt.u16Add(op as rt.u16, 1)); +} + //# sourceMappingURL=arrays.ts.map diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 0d4f4e551..95f4f47f4 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -1,7 +1,7 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; -import { fromFns } from "./arrays.ts"; +import { fromFns, asSlices } from "./arrays.ts"; import { atomics } from "./atomics.ts"; import { borrows } from "./borrows.ts"; import { bounds } from "./bounds.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, _390: rt.ByteSlice, out23: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2122: rt.u8, _393: 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); @@ -178,6 +178,8 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) 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], ["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], ["pointers::made", (_2165: rt.u8, data65: rt.ByteSlice, out66: rt.ByteVec): void => { made(data65, out66); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["arrays::as_slices", (_2166: rt.u8, data66: rt.ByteSlice, out67: rt.ByteVec): void => { + asSlices(data66, out67); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]; } diff --git a/ts/fixtures/lang/src/arrays.rs b/ts/fixtures/lang/src/arrays.rs index 0126762f6..6101e2077 100644 --- a/ts/fixtures/lang/src/arrays.rs +++ b/ts/fixtures/lang/src/arrays.rs @@ -1,6 +1,6 @@ //! Arrays `array::from_fn` makes, calling its function on each index in order: of bytes, //! wide integers, atomics each its own, strings, nested arrays, none at all, and a length -//! a const parameter gives. +//! a const parameter gives; and arrays as slices, written through. use std::sync::atomic::{AtomicU64, Ordering}; @@ -55,3 +55,76 @@ fn doubled(i: usize) -> u16 { fn counted(op: u8) -> [usize; N] { std::array::from_fn(|i| i * N + usize::from(op)) } + +/// Arrays as slices, of bytes and of other values, empty ones included; each byte an +/// operation, its value the operand. Writes through `as_mut_slice` land in the array. +pub fn as_slices(data: &[u8], out: &mut Vec) { + let mut bytes = [0u8; 4]; + let mut wide = [0u16; 3]; + let mut words = [String::new(), String::new()]; + let mut grid = [[0u8; 2]; 3]; + let mut pairs = [[0u32; 2]; 2]; + let mut none: [u16; 0] = []; + let mut empty: [u8; 0] = []; + for &op in data { + let i = usize::from(op); + match op % 8 { + 0 => { + let s = bytes.as_mut_slice(); + s[i % 4] = op; + s.rotate_left(1); + } + 1 => { + wide.as_mut_slice()[i % 3] = u16::from(op) << 7; + wide.as_mut_slice().rotate_right(1); + } + 2 => words.as_mut_slice()[i % 2].push(char::from(b'a' + op % 26)), + // An element of one array as a slice, as the slice of the outer array lends it. + 3 => { + let row = &mut grid.as_mut_slice()[i % 3]; + row.as_mut_slice()[i % 2] = op; + let read = row.as_slice()[(i + 1) % 2]; + grid[(i + 1) % 3][0] ^= read; + } + 4 => { + let all = pairs.as_mut_slice(); + all[i % 2] + .as_mut_slice() + .copy_from_slice(&[u32::from(op), u32::from(op) << 20]); + all.swap(0, 1); + } + 5 => { + none.as_mut_slice().reverse(); + empty.as_mut_slice().fill(op); + out.push(none.as_slice().len() as u8 + empty.as_slice().len() as u8); + } + 6 => written(&mut bytes, &mut wide, op), + _ => { + let sum: u32 = wide.as_slice().iter().map(|&w| u32::from(w)).sum(); + out.extend_from_slice(&sum.to_le_bytes()); + out.extend_from_slice(bytes.as_slice()); + } + } + } + let s = format!( + "{:?} {:?} {:?} {:?} {:?} {:?} {:?}", + bytes.as_slice(), + wide.as_slice(), + words.as_slice(), + grid.as_slice(), + pairs.as_slice(), + none.as_slice(), + empty.as_slice() + ); + out.extend_from_slice(s.as_bytes()); +} + +/// Arrays lent by `&mut`, written through their slices. +fn written(bytes: &mut [u8; 4], wide: &mut [u16; 3], op: u8) { + let s = bytes.as_mut_slice(); + s[3] = s[0].wrapping_add(op); + let w = wide.as_mut_slice(); + w[0] = u16::from(op); + w.swap(1, 2); + w[1..].fill(u16::from(op) + 1); +} diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 2c66ff35d..8fa182161 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -152,6 +152,7 @@ fn cases() -> Vec<(&'static str, Case)> { ("strs::drains", strs::drains), ("strs::hinted", strs::hinted), ("pointers::made", |_, data, out| pointers::made(data, out)), + ("arrays::as_slices", |_, data, out| arrays::as_slices(data, out)), ] } diff --git a/ts/fixtures/lang/src/lib.rs b/ts/fixtures/lang/src/lib.rs index 758ff3872..c1784c0b8 100644 --- a/ts/fixtures/lang/src/lib.rs +++ b/ts/fixtures/lang/src/lib.rs @@ -7,12 +7,12 @@ //! and `Result`'s combinators from core's MIR, `Option`s of values that can already be //! `undefined`, atomics, `static` ones included, trait objects, one-time cells, mutexes, //! `Cell`s, `UnsafeCell`s, `RefCell`s, read-write locks, and thread-locals, the defaults -//! of std types, the counts of shared pointers, arrays `array::from_fn` makes, maps of -//! `Option`s, values held back past a write of a variable they name, what `Hash` writes -//! into `DefaultHasher` and `RandomState`, slices' `windows` and chunks, mutable ones -//! written through, their parts by inclusive and checked ranges and by fixed size, -//! `Vec::drain`, the order, equality, and hashes of bytes, ranges of `Bound`s and of the -//! crate's own `RangeBounds`, and the equality of `Result`s and std's errors. +//! of std types, the counts of shared pointers, arrays `array::from_fn` makes, arrays as +//! slices, maps of `Option`s, values held back past a write of a variable they name, what +//! `Hash` writes into `DefaultHasher` and `RandomState`, slices' `windows` and chunks, +//! mutable ones written through, their parts by inclusive and checked ranges and by fixed +//! size, `Vec::drain`, the order, equality, and hashes of bytes, ranges of `Bound`s and of +//! the crate's own `RangeBounds`, and the equality of `Result`s and std's errors. //! //! `harness::run` is the entry point both sides of the differential test run, and //! `fmt::Fault` an error only `caller.ts` prints. diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 308a9a110..3877c12c0 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -1874,6 +1874,9 @@ fn mapping<'tcx>( let layout = tcx.layout_of(env.as_query_input(targ(gargs, 0))).ok()?; Expr::lit(layout.size.bytes().to_string()) } + // A reference to an array of bytes is an `rt.ByteSlice` already, and to one of other + // values its JS array, so each is its own slice, and writes through it land in it. + ("array", "as_slice" | "as_mut_slice") => args.pop()?, ("array", "from_fn") => { let n = gargs.consts().next()?.try_to_target_usize(tcx)?; let f = match targ(gargs, 0).kind() {