diff --git a/quest/m2/ts-shared.md b/quest/m2/ts-shared.md index b75a651fe..4c63ad1e0 100644 --- a/quest/m2/ts-shared.md +++ b/quest/m2/ts-shared.md @@ -24,11 +24,11 @@ guard's drop unlocks it, `try_lock` while held gives `WouldBlock`, and a re-entr upgrades once the function returns. `rt.Atomic` holds a `bool` or an integer, and its operations check their orderings as std does. `rt.OnceCell` and `rt.OnceLock` hold their `Option`, and a `OnceLock` initialized from within its own initialization panics as wasm32's does; -their `Clone`, `Debug`, `PartialEq`, `get_mut`, and `get_or_try_init` are still refused. +their `Clone`, `Debug`, `get_mut`, and `get_or_try_init` are still refused. `rt.Cell` is the place that holds its value. `rt.RefCell`, a `RefCell` or an `RwLock`, is its own guard and counts its borrows: a conflicting borrow panics, and a conflicting -lock aborts, each with wasm32's message. Their `get_mut`, `into_inner`, `try_borrow`, -`Ref::map`, and `Ref::clone` are still refused. +lock aborts, each with wasm32's message. Their `Clone`, `get_mut`, `into_inner`, +`try_borrow`, `Ref::map`, and `Ref::clone` are still refused. A `thread_local!` key is an `rt.LocalKey`, a module's value that makes its value on first use, as wasm32's `LazyStorage` does, and never drops it. A `static` atomic is a module's value too, an `rt.Atomic` of the value the static's memory starts with. diff --git a/quest/m3/README.md b/quest/m3/README.md index e7fb787a2..3194dd105 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -29,7 +29,6 @@ generic std gap is planned here even when moq never calls it. - [Waiting: a slice object for a mutable part of other values](/quest/m3/ts-slice-object.md) - waits on a moq profile showing the `Proxy` view's traps matter, then decides on an `rt.ByteSlice`-like form - [Port the workflow guard to kixelated/quest](/quest/m3/quest-workflow-guard.md) - quest's environment guard parses YAML as this repository's does - [A deque's two slices](/quest/m3/ts-deque-slices.md) - `VecDeque::as_slices` and `{:?}` of a deque's `Iter`, `IterMut`, and `Drain` translate, split where std's are -- [Equality of Cell and RefCell](/quest/m3/ts-eq-cells.md) - `==` and `!=` of `Cell`s and `RefCell`s translate - [Cow of a slice, and Wrapping](/quest/m3/ts-cow-slices.md) - `Cow<[T]>` and `Wrapping` translate, `PartialEq` included - [LinkedList](/quest/m3/ts-linked-list.md) - `LinkedList` and its methods translate - [Zip's next_back in constant time](/quest/m3/ts-zip-back.md) - a reversed zip of random-access iterators skips the longer side's excess at once diff --git a/quest/m3/ts-eq-cells.md b/quest/m3/ts-eq-cells.md deleted file mode 100644 index 80b7a5f54..000000000 --- a/quest/m3/ts-eq-cells.md +++ /dev/null @@ -1,15 +0,0 @@ -# [S] Equality of Cell and RefCell - -## Goal - -[rust2-ts](/ts/README.md) translates `==` and `!=` of `Cell`s and `RefCell`s, proven by -differential tests against native Rust, a `RefCell` mutably borrowed while compared -included. - -## Plan - -Found auditing std's `PartialEq` impls that forward `ne`: both types translate, but -` as PartialEq>::eq` and ` as PartialEq>::eq` refuse as an -unsupported call. std's compare what they hold by `eq`, and their `ne` is `!eq`, even -where the held type overrides `ne`; a `RefCell`'s borrows each side and panics if either -is mutably borrowed. diff --git a/ts/README.md b/ts/README.md index fe624a37b..41dbec868 100644 --- a/ts/README.md +++ b/ts/README.md @@ -311,8 +311,9 @@ number, string, or boolean in a JS `Map`, which compares those as Rust's `Eq` do finds any other key, a 64-bit one past 2^53 included, through its translated `Hash` and `Eq`; a B-tree orders every key by its translated `Ord`. std's impls are the runtime's: an `Option` orders `None` first, an `Arc` compares, orders, and hashes as what it holds, -so a map of `Arc` finds a `&str`, and arrays, slices, and vectors, of bytes or of -other elements, order by their elements and then their length, and compare and hash alike +so a map of `Arc` finds a `&str`, a `Cell` and a `RefCell` compare and order as +what they hold, a `RefCell` borrowing each, a one-time cell compares its `Option`, and +arrays, slices, and vectors, of bytes or of other elements, order by their elements and then their length, and compare and hash alike whatever their form, so a map of `Vec` finds a `&[u8]` and one of `[u16; 2]` a `&[u16]`. Slices, arrays, `Vec`s, and `VecDeque`s compare against each other as std's do, by their lengths and then each pair of items in order, through the @@ -714,8 +715,9 @@ built natively for wasm32 and run beside its translation on the same inputs by closures that log what each adaptor reads, `Option`'s and `Result`'s combinators, with closures that log each call, and std's collections, over keys whose own `Hash`, `Eq`, and `Ord` - disagree with JS's, sequences, tuples, and `Arc`s compared across forms and item types by an - `eq` and `ne` that disagree, `vec![x; n]` of types whose `Clone` counts its clones, `&mut` + disagree with JS's, sequences, tuples, `Arc`s, and cells compared across forms and item + types by an `eq` and `ne` that disagree, a `RefCell` compared while mutably borrowed, + `vec![x; n]` of types whose `Clone` counts its clones, `&mut` to values JS copies, of locals, fields, elements, and a map's values, passed on, returned, kept in a struct, and captured, a `Vec`'s edits and sorts, with closures that log what each is called with, std's `Bound`, `Reverse`, and `NonZero`, and raw diff --git a/ts/fixtures/lang/expected/collections.Quirky.ts b/ts/fixtures/lang/expected/collections.Quirky.ts new file mode 100644 index 000000000..5308246e5 --- /dev/null +++ b/ts/fixtures/lang/expected/collections.Quirky.ts @@ -0,0 +1,68 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { then$0wlzj76 } from "./core/bool.ts"; + +/** + * An item whose `ne` and `PartialOrd`'s tests see other remainders than its `eq` and + * `partial_cmp`, and whose `cmp` yet another, so what each comparison finds tells which + * std calls. Each call adds to the log, in order. + */ +export interface Quirky { + _0: rt.u16; + _1: rt.Cell; +} + +export function log(self: Quirky, other: Quirky, by: rt.u16): void { + rt.cellSet(self._1, rt.u16WrappingAdd(rt.u16WrappingMul(self._1.v, 31), self._0 ^ ((other._0 << 4) & 0xffff) ^ by)); +} + +export function eq$PartialEq(self: Quirky, other: Quirky): boolean { + log(self, other, 256); + return rt.u16Rem(self._0, 4) === rt.u16Rem(other._0, 4); +} + +export function ne$PartialEq(self: Quirky, other: Quirky): boolean { + log(self, other, 512); + return rt.u16Rem(self._0, 3) !== rt.u16Rem(other._0, 3); +} + +export function partialCmp$PartialOrd(self: Quirky, other: Quirky): rt.Ordering | undefined { + let _4!: boolean; + log(self, other, 768); + if (!(rt.u16Rem(self._0, 7) !== 0)) { + _4 = false; + } else { + _4 = rt.u16Rem(other._0, 7) !== 0; + } + return then$0wlzj76(_4, (): rt.Ordering => { + return rt.cmp(rt.u16Rem(self._0, 5), rt.u16Rem(other._0, 5)); + }); +} + +export function lt$PartialOrd(self: Quirky, other: Quirky): boolean { + log(self, other, 1024); + return rt.u16Rem(self._0, 6) < rt.u16Rem(other._0, 6); +} + +export function le$PartialOrd(self: Quirky, other: Quirky): boolean { + log(self, other, 1280); + return rt.u16Rem(self._0, 6) <= rt.u16Rem(other._0, 6); +} + +export function gt$PartialOrd(self: Quirky, other: Quirky): boolean { + log(self, other, 1536); + return rt.u16Rem(self._0, 6) > rt.u16Rem(other._0, 6); +} + +export function ge$PartialOrd(self: Quirky, other: Quirky): boolean { + log(self, other, 1792); + return rt.u16Rem(self._0, 6) >= rt.u16Rem(other._0, 6); +} + +export function cmp$Ord(self: Quirky, other: Quirky): rt.Ordering { + log(self, other, 2048); + return rt.cmp(rt.u16Rem(self._0, 8), rt.u16Rem(other._0, 8)); +} + +//# sourceMappingURL=collections.Quirky.ts.map diff --git a/ts/fixtures/lang/expected/collections.ts b/ts/fixtures/lang/expected/collections.ts index 8b28d5622..0e4e12b13 100644 --- a/ts/fixtures/lang/expected/collections.ts +++ b/ts/fixtures/lang/expected/collections.ts @@ -1,7 +1,7 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; -import { copy$Bumped, copy$Option_Tally, copy$Tally, copy$Wrap_u8, copy$u8_Tally, copy$u8_u16, copy$u8_u16s, copy$u8_u8, copy$u8_u8s, copy$u8s, drop$Arc_Picky_u8 } from "./lib.ts"; +import { copy$Bumped, copy$Option_Tally, copy$Quirky, copy$Quirkys, copy$Tally, copy$Wrap_u8, copy$u8_Tally, copy$u8_u16, copy$u8_u16s, copy$u8_u8, copy$u8_u8s, copy$u8s, drop$Arc_Picky_u8 } from "./lib.ts"; import * as Clash from "./collections.Clash.ts"; import * as Cloned from "./collections.Cloned.ts"; import * as Dir from "./collections.Dir.ts"; @@ -12,11 +12,12 @@ import * as Nibble from "./collections.Nibble.ts"; import * as Odd from "./collections.Odd.ts"; import * as Pair from "./collections.Pair.ts"; import * as Picky from "./collections.Picky.ts"; +import * as Quirky from "./collections.Quirky.ts"; import * as Rank from "./collections.Rank.ts"; import * as Right from "./collections.Right.ts"; import * as Tally from "./collections.Tally.ts"; import { thenSome$0d5am30 } from "./core/bool.ts"; -import { unwrapOr$1j4op5y, unwrapOr$0r2bmpr, map$09ua5et, map$0oc32h1, mapOr$0q0ymkh, mapOr$1xztl8h, mapOr$0bnr5nu, mapOr$1to2ul4, mapOr$1ht002r, mapOr$099335t, mapOr$0z4mj8g, mapOr$1surhce, mapOr$0mmpeqx, mapOr$1nlkf9b, mapOr$1t0muvr, mapOr$1w1gbwl, mapOr$02k4ito, clone$14nprly, flatten$1nwzx42 } from "./core/option.ts"; +import { unwrapOr$1j4op5y, unwrapOr$0r2bmpr, map$09ua5et, map$0oc32h1, mapOr$0q0ymkh, mapOr$1xztl8h, mapOr$1e4a1e0, mapOr$0bnr5nu, mapOr$1to2ul4, mapOr$1ht002r, mapOr$099335t, mapOr$0z4mj8g, mapOr$1surhce, mapOr$0mmpeqx, mapOr$1nlkf9b, mapOr$1t0muvr, mapOr$1w1gbwl, mapOr$02k4ito, clone$14nprly, flatten$1nwzx42 } from "./core/option.ts"; import * as Wrap from "./generics.Wrap.ts"; import * as Bumped from "./iters.Bumped.ts"; @@ -3863,6 +3864,26 @@ export function each$Picky_Reverse_Picky_own_nes_closure13(v: Picky.Picky[], f: return rt.iterToArray(rt.iterMap(rt.iterSlice(v), f), undefined, 8, true); } +/** `f` of each of `v`. */ +export function each$Quirky_Cell_Quirky_cell_eqs_closure2(v: Quirky.Quirky[], f: (a0: Quirky.Quirky) => rt.Cell): rt.Cell[] { + return rt.iterToArray(rt.iterMap(rt.iterSlice(v), f), undefined, 8, true); +} + +/** `f` of each of `v`. */ +export function each$Quirky_Cell_Quirky_cell_eqs_closure3(v: Quirky.Quirky[], f: (a0: Quirky.Quirky) => rt.Cell): rt.Cell[] { + return rt.iterToArray(rt.iterMap(rt.iterSlice(v), f), undefined, 8, true); +} + +/** `f` of each of `v`. */ +export function each$Quirky_RefCell_Quirky_cell_eqs_closure4(v: Quirky.Quirky[], f: (a0: Quirky.Quirky) => rt.RefCell): rt.RefCell[] { + return rt.iterToArray(rt.iterMap(rt.iterSlice(v), f), undefined, 12, true); +} + +/** `f` of each of `v`. */ +export function each$Quirky_RefCell_Quirky_cell_eqs_closure5(v: Quirky.Quirky[], f: (a0: Quirky.Quirky) => rt.RefCell): rt.RefCell[] { + return rt.iterToArray(rt.iterMap(rt.iterSlice(v), f), undefined, 12, true); +} + /** * `==` and `!=` of what holds a `Picky`, whose `ne` std's of tuples, `Arc`s, `Rc`s, and * `Box`es call, and std's `==` of slices calls of each item, but which an `Option`'s and @@ -4107,4 +4128,366 @@ export function ownNes(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { arc[Symbol.dispose](); } +export function putOrder(order: rt.Ordering | undefined, log: rt.Cell, out: rt.ByteVec): void { + rt.byteVecPush(out, mapOr$1e4a1e0(order, 9, (o: rt.Ordering): rt.u8 => { + return o & 0xff; + })); + rt.iterExtendBytes(out, rt.iterSlice(rt.u16ToBeBytes(rt.cellReplace(log, 0)).reverse()), undefined, true); +} + +/** + * Comparisons of `Cell`s and `RefCell`s, which compare what they hold by its `eq`, its + * tests, its `partial_cmp`, and its `cmp`, and whose `ne` is `!eq`, and `==` of one-time + * cells: of a `Quirky`, numbers, and `Option`s, nested, held by what compares by `ne`, as + * B-tree keys, and a `RefCell` against itself. Then, as `kind` picks, a `RefCell` compared + * while mutably borrowed, which panics, or held by an `Arc` that finds itself equal first. + */ +export function cellEqs(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + let _699!: [Quirky.Quirky, Quirky.Quirky]; + let _mutable!: rt.RefCell; + const log: rt.Cell = new rt.Cell(0); + const at: (a0: rt.usize) => rt.u16 = (i: rt.usize): rt.u16 => { + return unwrapOr$0r2bmpr(rt.byteSliceGet(data, i), 0) as rt.u16; + }; + const _8: [Quirky.Quirky, Quirky.Quirky] = [{ _0: at(0), _1: log }, { _0: at(1), _1: log }]; + const x: Quirky.Quirky = _8[0]; + const y: Quirky.Quirky = _8[1]; + const maybe: (a0: rt.usize) => rt.u8 | undefined = (i2: rt.usize): rt.u8 | undefined => { + return thenSome$0d5am30(rt.u16Rem(at(i2), 3) !== 0, at(i2) & 0xff); + }; + const _23: [rt.Cell, rt.Cell] = [new rt.Cell(copy$Quirky(x)), new rt.Cell(copy$Quirky(y))]; + const ca: rt.Cell = _23[0]; + const cb: rt.Cell = _23[1]; + putEq(rt.cellBy(Quirky.eq$PartialEq)(ca, cb), log, out); + putEq(!rt.cellBy(Quirky.eq$PartialEq)(ca, cb), log, out); + putEq(rt.cellBy(Quirky.lt$PartialOrd)(ca, cb), log, out); + putEq(rt.cellBy(Quirky.le$PartialOrd)(ca, cb), log, out); + putEq(rt.cellBy(Quirky.gt$PartialOrd)(ca, cb), log, out); + putEq(rt.cellBy(Quirky.ge$PartialOrd)(ca, cb), log, out); + putOrder(rt.cellBy(Quirky.partialCmp$PartialOrd)(ca, cb), log, out); + putOrder(rt.cellBy(Quirky.cmp$Ord)(ca, cb), log, out); + const _69: [rt.RefCell, rt.RefCell] = [new rt.RefCell(copy$Quirky(x)), new rt.RefCell(copy$Quirky(y))]; + const ra: rt.RefCell = _69[0]; + const rb: rt.RefCell = _69[1]; + putEq(rt.refCellBy(Quirky.eq$PartialEq)(ra, rb), log, out); + putEq(!rt.refCellBy(Quirky.eq$PartialEq)(ra, rb), log, out); + putEq(rt.refCellBy(Quirky.lt$PartialOrd)(ra, rb), log, out); + putEq(rt.refCellBy(Quirky.le$PartialOrd)(ra, rb), log, out); + putEq(rt.refCellBy(Quirky.gt$PartialOrd)(ra, rb), log, out); + putEq(rt.refCellBy(Quirky.ge$PartialOrd)(ra, rb), log, out); + putOrder(rt.refCellBy(Quirky.partialCmp$PartialOrd)(ra, rb), log, out); + putOrder(rt.refCellBy(Quirky.cmp$Ord)(ra, rb), log, out); + putEq(rt.refCellBy(Quirky.eq$PartialEq)(ra, ra), log, out); + putEq(!rt.refCellBy(Quirky.eq$PartialEq)(ra, ra), log, out); + putEq(rt.refCellBy(Quirky.lt$PartialOrd)(ra, ra), log, out); + putEq(rt.refCellBy(Quirky.le$PartialOrd)(ra, ra), log, out); + putEq(rt.refCellBy(Quirky.gt$PartialOrd)(ra, ra), log, out); + putEq(rt.refCellBy(Quirky.ge$PartialOrd)(ra, ra), log, out); + putOrder(rt.refCellBy(Quirky.partialCmp$PartialOrd)(ra, ra), log, out); + putOrder(rt.refCellBy(Quirky.cmp$Ord)(ra, ra), log, out); + const _156: [rt.Cell, rt.Cell] = [new rt.Cell(at(0) & 0xff), new rt.Cell(at(1) & 0xff)]; + const na: rt.Cell = _156[0]; + const nb: rt.Cell = _156[1]; + putEq(rt.cellBy(rt.same)(na, nb), log, out); + putEq(!rt.cellBy(rt.same)(na, nb), log, out); + putEq(rt.cellBy(rt.cmp)(na, nb) === -1, log, out); + const order = rt.cellBy(rt.cmp)(na, nb); + putEq(order !== undefined && order <= 0, log, out); + putEq(rt.cellBy(rt.cmp)(na, nb) === 1, log, out); + const order2 = rt.cellBy(rt.cmp)(na, nb); + putEq(order2 !== undefined && order2 >= 0, log, out); + putOrder(rt.cellBy(rt.cmp)(na, nb), log, out); + putOrder(rt.cellBy(rt.cmp)(na, nb), log, out); + const _210: [rt.Cell, rt.Cell] = [new rt.Cell(maybe(0)), new rt.Cell(maybe(1))]; + const oa: rt.Cell = _210[0]; + const ob: rt.Cell = _210[1]; + putEq(rt.cellBy(rt.same)(oa, ob), log, out); + putEq(!rt.cellBy(rt.same)(oa, ob), log, out); + putEq(rt.cellBy(rt.cmpOption(rt.cmp))(oa, ob) === -1, log, out); + const order3 = rt.cellBy(rt.cmpOption(rt.cmp))(oa, ob); + putEq(order3 !== undefined && order3 <= 0, log, out); + putEq(rt.cellBy(rt.cmpOption(rt.cmp))(oa, ob) === 1, log, out); + const order4 = rt.cellBy(rt.cmpOption(rt.cmp))(oa, ob); + putEq(order4 !== undefined && order4 >= 0, log, out); + putOrder(rt.cellBy(rt.cmpOption(rt.cmp))(oa, ob), log, out); + putOrder(rt.cellBy(rt.cmpOption(rt.cmp))(oa, ob), log, out); + const _266: rt.u16 = at(0); + const _272: rt.u16 = at(1); + const _262: [rt.RefCell, rt.RefCell] = [new rt.RefCell(rt.fmtToString((f: rt.Formatter) => rt.u16Display(_266, f))), new rt.RefCell(rt.fmtToString((f: rt.Formatter) => rt.u16Display(_272, f)))]; + const sa: rt.RefCell = _262[0]; + const sb: rt.RefCell = _262[1]; + putEq(rt.refCellBy(rt.same)(sa, sb), log, out); + putEq(!rt.refCellBy(rt.same)(sa, sb), log, out); + putEq(rt.refCellBy(rt.cmpStr)(sa, sb) === -1, log, out); + const order5 = rt.refCellBy(rt.cmpStr)(sa, sb); + putEq(order5 !== undefined && order5 <= 0, log, out); + putEq(rt.refCellBy(rt.cmpStr)(sa, sb) === 1, log, out); + const order6 = rt.refCellBy(rt.cmpStr)(sa, sb); + putEq(order6 !== undefined && order6 >= 0, log, out); + putOrder(rt.refCellBy(rt.cmpStr)(sa, sb), log, out); + putOrder(rt.refCellBy(rt.cmpStr)(sa, sb), log, out); + const _318: [rt.RefCell>, rt.RefCell>] = [new rt.RefCell(new rt.RefCell(copy$Quirky(x))), new rt.RefCell(new rt.RefCell(copy$Quirky(y)))]; + const rra: rt.RefCell> = _318[0]; + const rrb: rt.RefCell> = _318[1]; + const held: rt.RefCell> = rt.refCellBorrow(rra); + const _inner: rt.RefCell = rt.refCellBorrow(held.value); + putEq(rt.refCellBy(rt.refCellBy(Quirky.eq$PartialEq))(rra, rrb), log, out); + putEq(!rt.refCellBy(rt.refCellBy(Quirky.eq$PartialEq))(rra, rrb), log, out); + putEq(rt.refCellBy(rt.refCellBy(Quirky.lt$PartialOrd))(rra, rrb), log, out); + putEq(rt.refCellBy(rt.refCellBy(Quirky.le$PartialOrd))(rra, rrb), log, out); + putEq(rt.refCellBy(rt.refCellBy(Quirky.gt$PartialOrd))(rra, rrb), log, out); + putEq(rt.refCellBy(rt.refCellBy(Quirky.ge$PartialOrd))(rra, rrb), log, out); + putOrder(rt.refCellBy(rt.refCellBy(Quirky.partialCmp$PartialOrd))(rra, rrb), log, out); + putOrder(rt.refCellBy(rt.refCellBy(Quirky.cmp$Ord))(rra, rrb), log, out); + putEq(rt.refCellBy(rt.refCellBy(Quirky.eq$PartialEq))(rra, rra), log, out); + putEq(!rt.refCellBy(rt.refCellBy(Quirky.eq$PartialEq))(rra, rra), log, out); + putEq(rt.refCellBy(rt.refCellBy(Quirky.lt$PartialOrd))(rra, rra), log, out); + putEq(rt.refCellBy(rt.refCellBy(Quirky.le$PartialOrd))(rra, rra), log, out); + putEq(rt.refCellBy(rt.refCellBy(Quirky.gt$PartialOrd))(rra, rra), log, out); + putEq(rt.refCellBy(rt.refCellBy(Quirky.ge$PartialOrd))(rra, rra), log, out); + putOrder(rt.refCellBy(rt.refCellBy(Quirky.partialCmp$PartialOrd))(rra, rra), log, out); + putOrder(rt.refCellBy(rt.refCellBy(Quirky.cmp$Ord))(rra, rra), log, out); + rt.refRelease(_inner); + rt.refRelease(held); + const _412: [rt.RefCell>, rt.RefCell>] = [new rt.RefCell(new rt.Cell(copy$Quirky(x))), new rt.RefCell(new rt.Cell(copy$Quirky(y)))]; + const rca: rt.RefCell> = _412[0]; + const rcb: rt.RefCell> = _412[1]; + putEq(rt.refCellBy(rt.cellBy(Quirky.eq$PartialEq))(rca, rcb), log, out); + putEq(!rt.refCellBy(rt.cellBy(Quirky.eq$PartialEq))(rca, rcb), log, out); + putEq(rt.refCellBy(rt.cellBy(Quirky.lt$PartialOrd))(rca, rcb), log, out); + putEq(rt.refCellBy(rt.cellBy(Quirky.le$PartialOrd))(rca, rcb), log, out); + putEq(rt.refCellBy(rt.cellBy(Quirky.gt$PartialOrd))(rca, rcb), log, out); + putEq(rt.refCellBy(rt.cellBy(Quirky.ge$PartialOrd))(rca, rcb), log, out); + putOrder(rt.refCellBy(rt.cellBy(Quirky.partialCmp$PartialOrd))(rca, rcb), log, out); + putOrder(rt.refCellBy(rt.cellBy(Quirky.cmp$Ord))(rca, rcb), log, out); + const _461: rt.Cell | undefined = new rt.Cell(copy$Quirky(x)); + const _464: rt.Cell | undefined = new rt.Cell(copy$Quirky(y)); + putEq(rt.eqOption(rt.cellBy(Quirky.eq$PartialEq))(_461, _464), log, out); + const _470: rt.Cell | undefined = new rt.Cell(copy$Quirky(x)); + const _473: rt.Cell | undefined = new rt.Cell(copy$Quirky(y)); + putEq(!rt.eqOption(rt.cellBy(Quirky.eq$PartialEq))(_470, _473), log, out); + const _479: rt.Cell | undefined = new rt.Cell(copy$Quirky(x)); + const _482: rt.Cell | undefined = new rt.Cell(copy$Quirky(y)); + putEq(rt.cmpOption(rt.cellBy(Quirky.partialCmp$PartialOrd))(_479, _482) === -1, log, out); + const _488: rt.Cell | undefined = new rt.Cell(copy$Quirky(x)); + const _491: rt.Cell | undefined = new rt.Cell(copy$Quirky(y)); + const order7 = rt.cmpOption(rt.cellBy(Quirky.partialCmp$PartialOrd))(_488, _491); + putEq(order7 !== undefined && order7 <= 0, log, out); + const _497: rt.Cell | undefined = new rt.Cell(copy$Quirky(x)); + const _500: rt.Cell | undefined = new rt.Cell(copy$Quirky(y)); + putEq(rt.cmpOption(rt.cellBy(Quirky.partialCmp$PartialOrd))(_497, _500) === 1, log, out); + const _506: rt.Cell | undefined = new rt.Cell(copy$Quirky(x)); + const _509: rt.Cell | undefined = new rt.Cell(copy$Quirky(y)); + const order8 = rt.cmpOption(rt.cellBy(Quirky.partialCmp$PartialOrd))(_506, _509); + putEq(order8 !== undefined && order8 >= 0, log, out); + const _515: rt.Cell | undefined = new rt.Cell(copy$Quirky(x)); + const _518: rt.Cell | undefined = new rt.Cell(copy$Quirky(y)); + putOrder(rt.cmpOption(rt.cellBy(Quirky.partialCmp$PartialOrd))(_515, _518), log, out); + const _525: rt.Cell | undefined = new rt.Cell(copy$Quirky(x)); + const _528: rt.Cell | undefined = new rt.Cell(copy$Quirky(y)); + putOrder(rt.cmpOption(rt.cellBy(Quirky.cmp$Ord))(_525, _528), log, out); + const _534: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(x)), x]; + const _537: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(y)), y]; + putEq(rt.eqTuple([rt.refCellBy(Quirky.eq$PartialEq), Quirky.eq$PartialEq])(_534, _537), log, out); + const _543: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(x)), x]; + const _546: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(y)), y]; + putEq(!rt.eqTuple([rt.refCellBy(Quirky.eq$PartialEq), (a: Quirky.Quirky, b: Quirky.Quirky): boolean => { + return !Quirky.ne$PartialEq(a, b); + }])(_543, _546), log, out); + const _552: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(x)), x]; + const _555: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(y)), y]; + putEq(rt.testTuple([rt.refCellBy(Quirky.partialCmp$PartialOrd)], Quirky.lt$PartialOrd, -1)(_552, _555), log, out); + const _561: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(x)), x]; + const _564: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(y)), y]; + putEq(rt.testTuple([rt.refCellBy(Quirky.partialCmp$PartialOrd)], Quirky.le$PartialOrd, -1)(_561, _564), log, out); + const _570: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(x)), x]; + const _573: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(y)), y]; + putEq(rt.testTuple([rt.refCellBy(Quirky.partialCmp$PartialOrd)], Quirky.gt$PartialOrd, 1)(_570, _573), log, out); + const _579: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(x)), x]; + const _582: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(y)), y]; + putEq(rt.testTuple([rt.refCellBy(Quirky.partialCmp$PartialOrd)], Quirky.ge$PartialOrd, 1)(_579, _582), log, out); + const _588: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(x)), x]; + const _591: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(y)), y]; + putOrder(rt.cmpTuple([rt.refCellBy(Quirky.partialCmp$PartialOrd), Quirky.partialCmp$PartialOrd])(_588, _591), log, out); + const _598: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(x)), x]; + const _601: [rt.RefCell, Quirky.Quirky] = [new rt.RefCell(copy$Quirky(y)), y]; + putOrder(rt.cmpTuple([rt.refCellBy(Quirky.cmp$Ord), Quirky.cmp$Ord])(_598, _601), log, out); + const _606: [Quirky.Quirky[], Quirky.Quirky[]] = [copy$Quirkys([x, y, x]).map(copy$Quirky), copy$Quirkys([y, y, x]).map(copy$Quirky)]; + const va: Quirky.Quirky[] = _606[0]; + const vb: Quirky.Quirky[] = _606[1]; + const _614: rt.RefCell = new rt.RefCell(va.map(copy$Quirky)); + const _618: rt.RefCell = new rt.RefCell(vb.map(copy$Quirky)); + putEq(rt.refCellBy(rt.eqSlice((a2: Quirky.Quirky, b2: Quirky.Quirky): boolean => { + return !Quirky.ne$PartialEq(a2, b2); + }))(_614, _618), log, out); + const _625: rt.Cell = new rt.Cell(rt.sliceRange(va, 0)); + const _629: rt.Cell = new rt.Cell(rt.sliceRange(vb, 0)); + putEq(!rt.cellBy(rt.eqSlice((a3: Quirky.Quirky, b3: Quirky.Quirky): boolean => { + return !Quirky.ne$PartialEq(a3, b3); + }))(_625, _629), log, out); + const _636: rt.Cell[] = each$Quirky_Cell_Quirky_cell_eqs_closure2(va, (_2: Quirky.Quirky): rt.Cell => { + return new rt.Cell(copy$Quirky(copy$Quirky(_2))); + }); + const _640: rt.Cell[] = each$Quirky_Cell_Quirky_cell_eqs_closure3(vb, (_22: Quirky.Quirky): rt.Cell => { + return new rt.Cell(copy$Quirky(copy$Quirky(_22))); + }); + putEq(rt.eqSlice(rt.cellBy(Quirky.eq$PartialEq))(_636, _640), log, out); + const _647: rt.RefCell[] = each$Quirky_RefCell_Quirky_cell_eqs_closure4(va, (_26: Quirky.Quirky): rt.RefCell => { + return new rt.RefCell(copy$Quirky(copy$Quirky(_26))); + }); + const _651: rt.RefCell[] = each$Quirky_RefCell_Quirky_cell_eqs_closure5(vb, (_28: Quirky.Quirky): rt.RefCell => { + return new rt.RefCell(copy$Quirky(copy$Quirky(_28))); + }); + putEq(!rt.eqSlice(rt.refCellBy(Quirky.eq$PartialEq))(_647, _651), log, out); + const _658: [rt.Cell, rt.RefCell] = [new rt.Cell(copy$Quirky(x)), new rt.RefCell(copy$Quirky(y))]; + const _662: [rt.Cell, rt.RefCell] = [new rt.Cell(copy$Quirky(y)), new rt.RefCell(copy$Quirky(x))]; + putEq(!rt.eqTuple([rt.cellBy(Quirky.eq$PartialEq), rt.refCellBy(Quirky.eq$PartialEq)])(_658, _662), log, out); + const _669: rt.Arc> = new rt.Arc(new rt.RefCell(copy$Quirky(x))); + const _672: rt.Arc> = new rt.Arc(new rt.RefCell(copy$Quirky(y))); + putEq(!rt.eqArc(rt.refCellBy(Quirky.eq$PartialEq), true)(_669, _672), log, out); + _672[Symbol.dispose](); + _669[Symbol.dispose](); + putEq(copy$Quirky(rt.ordMax(new rt.Cell(copy$Quirky(x)), new rt.Cell(copy$Quirky(y)), rt.cellBy(Quirky.lt$PartialOrd)).v)._0 === x._0, log, out); + const _690: rt.RefCell = rt.refCellBorrow(rt.ordMin(new rt.RefCell(copy$Quirky(x)), new rt.RefCell(copy$Quirky(y)), rt.refCellBy(Quirky.lt$PartialOrd))); + putEq(_690.value._0 === x._0, log, out); + rt.refRelease(_690); + if (!(rt.u16Rem(x._0, 6) <= rt.u16Rem(y._0, 6))) { + _699 = [y, x] as [Quirky.Quirky, Quirky.Quirky]; + } else { + _699 = [x, y] as [Quirky.Quirky, Quirky.Quirky]; + } + const lo: Quirky.Quirky = _699[0]; + const hi: Quirky.Quirky = _699[1]; + rt.byteVecPush(out, copy$Quirky(rt.ordClamp(new rt.Cell({ _0: at(2), _1: log }), new rt.Cell(copy$Quirky(lo)), new rt.Cell(copy$Quirky(hi)), rt.cellBy(Quirky.le$PartialOrd), rt.cellBy(Quirky.lt$PartialOrd), rt.cellBy(Quirky.gt$PartialOrd)).v)._0 & 0xff); + rt.iterExtendBytes(out, rt.iterSlice(rt.u16ToBeBytes(rt.cellReplace(log, 0)).reverse()), undefined, true); + const once: (a0: rt.usize, a1: Quirky.Quirky) => rt.OnceCell = (i3: rt.usize, v: Quirky.Quirky): rt.OnceCell => { + const cell: rt.OnceCell = new rt.OnceCell(undefined); + if (rt.u16Rem(at(i3), 4) !== 0) { + rt.onceSet(cell, copy$Quirky(v)); + return cell; + } + return cell; + }; + const _730: rt.OnceCell = once(2, x); + const _734: rt.OnceCell = once(3, y); + putEq(rt.cellBy(rt.eqOption(Quirky.eq$PartialEq))(_730, _734), log, out); + const _741: rt.OnceCell = once(2, x); + const _745: rt.OnceCell = once(3, y); + putEq(!rt.cellBy(rt.eqOption(Quirky.eq$PartialEq))(_741, _745), log, out); + const _752: rt.OnceCell = new rt.OnceCell(maybe(0)); + const _757: rt.OnceCell = new rt.OnceCell(maybe(1)); + putEq(rt.cellBy(rt.same)(_752, _757), log, out); + const lock: (a0: rt.usize, a1: Quirky.Quirky) => rt.OnceLock = (i4: rt.usize, v2: Quirky.Quirky): rt.OnceLock => { + const lock2: rt.OnceLock = new rt.OnceLock(undefined); + if (rt.u16Rem(at(i4), 4) !== 1) { + rt.onceSet(lock2, copy$Quirky(v2)); + return lock2; + } + return lock2; + }; + const _767: rt.OnceLock = lock(2, x); + const _771: rt.OnceLock = lock(3, y); + putEq(!rt.cellBy(rt.eqOption(Quirky.eq$PartialEq))(_767, _771), log, out); + const _778: rt.OnceCell> = new rt.OnceCell(new rt.RefCell(copy$Quirky(x))); + const _781: rt.OnceCell> = new rt.OnceCell(new rt.RefCell(copy$Quirky(y))); + putEq(!rt.cellBy(rt.eqOption(rt.refCellBy(Quirky.eq$PartialEq)))(_778, _781), log, out); + const _785: rt.Iter> = rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (_29: rt.u8): rt.Cell => { + return new rt.Cell(rt.u8Rem(_29, 8)); + }); + const set: rt.BTreeSet> = rt.btreeExtendKeys(rt.btreeNew(rt.cellBy(rt.cmp)) as rt.BTreeSet>, _785, true, undefined, undefined, 1, true); + rt.byteVecPush(out, rt.btreeLen(set) & 0xff); + const _796: rt.Cell = new rt.Cell(rt.u8Rem(at(0) & 0xff, 8)); + rt.byteVecPush(out, rt.btreeContains(set, _796) ? 1 : 0); + rt.iterExtendBytes(out, rt.iterMap(rt.btreeKeys(set), (c: rt.Cell): rt.u8 => { + return c.v; + }), undefined, true); + const _808: rt.Iter<[rt.RefCell, rt.usize]> = rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))), (_213: [rt.usize, rt.u8]): [rt.RefCell, rt.usize] => { + const i5: rt.usize = _213[0]; + return [new rt.RefCell(rt.u8Rem(_213[1], 8)), i5] as [rt.RefCell, rt.usize]; + }); + rt.byteVecPush(out, mapOr$02k4ito(rt.btreeGet(rt.btreeExtend(rt.btreeNew(rt.refCellBy(rt.cmp)) as rt.BTreeMap, rt.usize>, _808, true, undefined, undefined, undefined, 12, true), new rt.RefCell(rt.u8Rem(at(1) & 0xff, 8))), 255, (_217: rt.usize): rt.u8 => { + return _217 & 0xff; + })); + const _825: rt.RefCell = rt.refCellBorrowMut(ra); + _825.value = copy$Quirky(y); + rt.refMutRelease(_825); + const _832: rt.RefCell> = rt.refCellBorrowMut(rra); + const _829: rt.RefCell = rt.refCellBorrowMut(_832.value); + _829.value = copy$Quirky(x); + rt.refMutRelease(_829); + rt.refMutRelease(_832); + const _839: rt.RefCell> = rt.refCellBorrow(rra); + const _837: rt.RefCell = _839.value; + putEq(rt.refCellBy(Quirky.eq$PartialEq)(ra, _837), log, out); + rt.refRelease(_839); + const _847: rt.RefCell> = rt.refCellBorrow(rra); + const _845: rt.RefCell = _847.value; + putEq(!rt.refCellBy(Quirky.eq$PartialEq)(ra, _845), log, out); + rt.refRelease(_847); + const _855: rt.RefCell> = rt.refCellBorrow(rra); + const _853: rt.RefCell = _855.value; + putEq(rt.refCellBy(Quirky.lt$PartialOrd)(ra, _853), log, out); + rt.refRelease(_855); + const _863: rt.RefCell> = rt.refCellBorrow(rra); + const _861: rt.RefCell = _863.value; + putEq(rt.refCellBy(Quirky.le$PartialOrd)(ra, _861), log, out); + rt.refRelease(_863); + const _871: rt.RefCell> = rt.refCellBorrow(rra); + const _869: rt.RefCell = _871.value; + putEq(rt.refCellBy(Quirky.gt$PartialOrd)(ra, _869), log, out); + rt.refRelease(_871); + const _879: rt.RefCell> = rt.refCellBorrow(rra); + const _877: rt.RefCell = _879.value; + putEq(rt.refCellBy(Quirky.ge$PartialOrd)(ra, _877), log, out); + rt.refRelease(_879); + const _887: rt.RefCell> = rt.refCellBorrow(rra); + const _885: rt.RefCell = _887.value; + putOrder(rt.refCellBy(Quirky.partialCmp$PartialOrd)(ra, _885), log, out); + rt.refRelease(_887); + const _896: rt.RefCell> = rt.refCellBorrow(rra); + const _894: rt.RefCell = _896.value; + putOrder(rt.refCellBy(Quirky.cmp$Ord)(ra, _894), log, out); + rt.refRelease(_896); + const shared: rt.Arc> = new rt.Arc(new rt.RefCell(copy$Quirky(x))); + b1: { + const _902: rt.u8 = rt.u8Rem(kind, 6); + if (_902 === 0) { + shared[Symbol.dispose](); + break b1; + } + if (_902 === 1) { + _mutable = rt.refCellBorrowMut(ra); + } else if (_902 === 2) { + _mutable = rt.refCellBorrowMut(rb); + } else { + _mutable = rt.refCellBorrowMut(shared.value); + } + const _909: rt.u8 = rt.u8Rem(kind, 6); + if (_909 === 1) { + putEq(rt.refCellBy(Quirky.eq$PartialEq)(ra, rb), log, out); + } else if (_909 === 2) { + putEq(rt.refCellBy(Quirky.lt$PartialOrd)(ra, rb), log, out); + } else if (_909 === 3) { + const _922: rt.Arc> = rt.arcClone(shared); + putEq(rt.eqArc(rt.refCellBy(Quirky.eq$PartialEq), true)(shared, _922), log, out); + _922[Symbol.dispose](); + } else if (_909 === 4) { + const _928: rt.Arc> = new rt.Arc(new rt.RefCell(copy$Quirky(y))); + putEq(!rt.eqArc(rt.refCellBy(Quirky.eq$PartialEq), true)(shared, _928), log, out); + _928[Symbol.dispose](); + } else { + const _934: rt.Arc> = rt.arcClone(shared); + putOrder(rt.cmpArc(rt.refCellBy(Quirky.partialCmp$PartialOrd))(shared, _934), log, out); + _934[Symbol.dispose](); + } + const held2: rt.RefCell> = rt.refCellBorrow(rra); + const _inner2: rt.RefCell = rt.refCellBorrowMut(held2.value); + putEq(rt.refCellBy(rt.refCellBy(Quirky.eq$PartialEq))(rra, rrb), log, out); + rt.refMutRelease(_inner2); + rt.refRelease(held2); + rt.refMutRelease(_mutable); + shared[Symbol.dispose](); + } +} + //# sourceMappingURL=collections.ts.map diff --git a/ts/fixtures/lang/expected/core/bool.ts b/ts/fixtures/lang/expected/core/bool.ts index 7b7c84928..ae7540e6c 100644 --- a/ts/fixtures/lang/expected/core/bool.ts +++ b/ts/fixtures/lang/expected/core/bool.ts @@ -153,6 +153,13 @@ export function then$0b2eeh4(self: boolean, f: () => Array): return f(); } +export function then$0wlzj76(self: boolean, f: () => rt.Ordering): rt.Ordering | undefined { + if (!self) { + return undefined; + } + return f(); +} + export function then$0afp4no(self: boolean, f: () => rt.Result): rt.Result | undefined { if (!self) { return undefined; diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 383b88a07..17cdd6c68 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -10,7 +10,7 @@ import { cells } from "./cells.ts"; import { chars } from "./chars.ts"; import { chunks } from "./chunks.ts"; import { iters } from "./clones.ts"; -import { hashMaps, sets, btreeMaps, deques, vecs, stores, optionals, setOps, unlikeEqs, ownNes } from "./collections.ts"; +import { hashMaps, sets, btreeMaps, deques, vecs, stores, optionals, setOps, unlikeEqs, ownNes, cellEqs } from "./collections.ts"; import { combinators } from "./combinators.ts"; import { arcs, clonedArcs, cyclicArcs, uniqueArcs, clonedRcs, cyclicRcs, rcs, uniqueRcs, copies } from "./counts.ts"; import { cows } from "./cows.ts"; @@ -197,7 +197,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) collections(data71, out72); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["collections::unlike_eqs", unlikeEqs as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["zeros::impls", impls as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["zeros::keys", zeros_keys as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["keys::arrays", arrays as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["iters::zips", zips as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["iters::zips_back", zipsBack as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["collections::own_nes", ownNes as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["fmt::iters", (_2172: rt.u8, data72: rt.ByteSlice, out73: rt.ByteVec): undefined => { fmt_iters(data72, out73); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["zeros::unread", unread as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["iters::range_froms", rangeFroms as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["clones::iters", iters as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined]]; + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["zeros::unread", unread as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["iters::range_froms", rangeFroms as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["clones::iters", iters as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["collections::cell_eqs", cellEqs as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined]]; } /** Run one case. */ diff --git a/ts/fixtures/lang/expected/lib.ts b/ts/fixtures/lang/expected/lib.ts index ebd05ca8e..cc63a036f 100644 --- a/ts/fixtures/lang/expected/lib.ts +++ b/ts/fixtures/lang/expected/lib.ts @@ -3,6 +3,7 @@ import * as rt from "./rt.ts"; import type * as Logged from "./clones.Logged.ts"; import type * as Picky from "./collections.Picky.ts"; +import type * as Quirky from "./collections.Quirky.ts"; import type * as Tally from "./collections.Tally.ts"; import type * as Any from "./core/any.Any.ts"; import type * as ArcReleasing from "./counts.ArcReleasing.ts"; @@ -232,6 +233,14 @@ export function copy$Poll_u8s(v: rt.Poll): rt.Poll { return v === rt.Pending ? v : copy$u8s(v); } +export function copy$Quirky(v: Quirky.Quirky): Quirky.Quirky { + return { _0: v._0, _1: v._1 }; +} + +export function copy$Quirkys(v: Quirky.Quirky[]): Quirky.Quirky[] { + return v.map(copy$Quirky); +} + export function copy$Result_Mutex_u8_u8_u8(v: rt.Result<[rt.Mutex, rt.u8], rt.u8>): rt.Result<[rt.Mutex, rt.u8], rt.u8> { return v.ok ? rt.Ok(copy$Mutex_u8_u8(v.value)) : rt.Err(v.error); } diff --git a/ts/fixtures/lang/src/collections.rs b/ts/fixtures/lang/src/collections.rs index d6d64c5f3..b088d248f 100644 --- a/ts/fixtures/lang/src/collections.rs +++ b/ts/fixtures/lang/src/collections.rs @@ -1493,3 +1493,212 @@ pub fn own_nes(kind: u8, data: &[u8], out: &mut Vec) { put_eq(wrapped(&a) == b, &log, out); put_eq(wrapped(&a) == &b[..], &log, out); } + +/// An item whose `ne` and `PartialOrd`'s tests see other remainders than its `eq` and +/// `partial_cmp`, and whose `cmp` yet another, so what each comparison finds tells which +/// std calls. Each call adds to the log, in order. +#[derive(Clone, Copy)] +struct Quirky<'a>(u16, &'a std::cell::Cell); + +impl Quirky<'_> { + fn log(&self, other: &Self, by: u16) { + self.1 + .set(self.1.get().wrapping_mul(31).wrapping_add(self.0 ^ other.0 << 4 ^ by)); + } +} + +impl PartialEq for Quirky<'_> { + fn eq(&self, other: &Self) -> bool { + self.log(other, 0x100); + self.0 % 4 == other.0 % 4 + } + + fn ne(&self, other: &Self) -> bool { + self.log(other, 0x200); + self.0 % 3 != other.0 % 3 + } +} + +impl Eq for Quirky<'_> {} + +/// Unordered where either is a multiple of 7. +impl PartialOrd for Quirky<'_> { + fn partial_cmp(&self, other: &Self) -> Option { + self.log(other, 0x300); + (self.0 % 7 != 0 && other.0 % 7 != 0).then(|| (self.0 % 5).cmp(&(other.0 % 5))) + } + + fn lt(&self, other: &Self) -> bool { + self.log(other, 0x400); + self.0 % 6 < other.0 % 6 + } + + fn le(&self, other: &Self) -> bool { + self.log(other, 0x500); + self.0 % 6 <= other.0 % 6 + } + + fn gt(&self, other: &Self) -> bool { + self.log(other, 0x600); + self.0 % 6 > other.0 % 6 + } + + fn ge(&self, other: &Self) -> bool { + self.log(other, 0x700); + self.0 % 6 >= other.0 % 6 + } +} + +impl Ord for Quirky<'_> { + fn cmp(&self, other: &Self) -> Ordering { + self.log(other, 0x800); + (self.0 % 8).cmp(&(other.0 % 8)) + } +} + +fn put_order(order: Option, log: &std::cell::Cell, out: &mut Vec) { + out.push(order.map_or(9, |o| o as i8 as u8)); + out.extend(log.replace(0).to_le_bytes()); +} + +/// Each of `PartialEq`'s, `PartialOrd`'s, and `Ord`'s comparisons of `$a` and `$b`. +macro_rules! compared { + ($a:expr, $b:expr, $log:expr, $out:expr) => {{ + put_eq($a == $b, $log, $out); + put_eq($a != $b, $log, $out); + put_eq($a < $b, $log, $out); + put_eq($a <= $b, $log, $out); + put_eq($a > $b, $log, $out); + put_eq($a >= $b, $log, $out); + put_order($a.partial_cmp(&$b), $log, $out); + put_order(Some($a.cmp(&$b)), $log, $out); + }}; +} + +/// Comparisons of `Cell`s and `RefCell`s, which compare what they hold by its `eq`, its +/// tests, its `partial_cmp`, and its `cmp`, and whose `ne` is `!eq`, and `==` of one-time +/// cells: of a `Quirky`, numbers, and `Option`s, nested, held by what compares by `ne`, as +/// B-tree keys, and a `RefCell` against itself. Then, as `kind` picks, a `RefCell` compared +/// while mutably borrowed, which panics, or held by an `Arc` that finds itself equal first. +pub fn cell_eqs(kind: u8, data: &[u8], out: &mut Vec) { + use std::cell::{Cell, OnceCell, RefCell}; + use std::sync::{Arc, OnceLock}; + + let log = Cell::new(0); + let at = |i: usize| u16::from(data.get(i).copied().unwrap_or(0)); + let (x, y) = (Quirky(at(0), &log), Quirky(at(1), &log)); + let maybe = |i: usize| (at(i) % 3 != 0).then_some(at(i) as u8); + + let (ca, cb) = (Cell::new(x), Cell::new(y)); + compared!(ca, cb, &log, out); + let (ra, rb) = (RefCell::new(x), RefCell::new(y)); + compared!(ra, rb, &log, out); + compared!(ra, ra, &log, out); + let (na, nb) = (Cell::new(at(0) as u8), Cell::new(at(1) as u8)); + compared!(na, nb, &log, out); + let (oa, ob) = (Cell::new(maybe(0)), Cell::new(maybe(1))); + compared!(oa, ob, &log, out); + let (sa, sb) = (RefCell::new(at(0).to_string()), RefCell::new(at(1).to_string())); + compared!(sa, sb, &log, out); + + // Nested, and against itself while a shared borrow is held. + let (rra, rrb) = (RefCell::new(RefCell::new(x)), RefCell::new(RefCell::new(y))); + { + let held = rra.borrow(); + let _inner = held.borrow(); + compared!(rra, rrb, &log, out); + compared!(rra, rra, &log, out); + } + let (rca, rcb) = (RefCell::new(Cell::new(x)), RefCell::new(Cell::new(y))); + compared!(rca, rcb, &log, out); + compared!(Some(Cell::new(x)), Some(Cell::new(y)), &log, out); + compared!((RefCell::new(x), x), (RefCell::new(y), y), &log, out); + + // A slice compares its items by `!=`, which a cell's is `!eq` of, but which a slice's + // within a cell is still the items' own `ne`. + let (va, vb) = (vec![x, y, x], vec![y, y, x]); + put_eq(RefCell::new(va.clone()) == RefCell::new(vb.clone()), &log, out); + put_eq(Cell::new(&va[..]) != Cell::new(&vb[..]), &log, out); + put_eq(each(&va, |&p| Cell::new(p)) == each(&vb, |&p| Cell::new(p)), &log, out); + put_eq( + each(&va, |&p| RefCell::new(p)) != each(&vb, |&p| RefCell::new(p)), + &log, + out, + ); + put_eq( + (Cell::new(x), RefCell::new(y)) != (Cell::new(y), RefCell::new(x)), + &log, + out, + ); + put_eq(Arc::new(RefCell::new(x)) != Arc::new(RefCell::new(y)), &log, out); + + // `Ord`'s defaults, by the cells' `lt` and `le`. + put_eq(Cell::new(x).max(Cell::new(y)).get().0 == x.0, &log, out); + put_eq(RefCell::new(x).min(RefCell::new(y)).borrow().0 == x.0, &log, out); + // Within bounds that `le` finds in order, which `clamp` asserts. + let (lo, hi) = if x.0 % 6 <= y.0 % 6 { (x, y) } else { (y, x) }; + let clamped = Cell::new(Quirky(at(2), &log)).clamp(Cell::new(lo), Cell::new(hi)); + out.push(clamped.get().0 as u8); + out.extend(log.replace(0).to_le_bytes()); + + // One-time cells, empty or set, by their `Option`'s `eq`, whose `ne` is `!eq`. + let once = |i: usize, v| { + let cell = OnceCell::new(); + if at(i) % 4 != 0 { + _ = cell.set(v); + } + cell + }; + put_eq(once(2, x) == once(3, y), &log, out); + put_eq(once(2, x) != once(3, y), &log, out); + put_eq(OnceCell::from(maybe(0)) == OnceCell::from(maybe(1)), &log, out); + let lock = |i: usize, v| { + let lock = OnceLock::new(); + if at(i) % 4 != 1 { + _ = lock.set(v); + } + lock + }; + put_eq(lock(2, x) != lock(3, y), &log, out); + put_eq( + OnceCell::from(RefCell::new(x)) != OnceCell::from(RefCell::new(y)), + &log, + out, + ); + + // As keys, ordered by what they hold. + let set: BTreeSet> = data.iter().map(|&v| Cell::new(v % 8)).collect(); + out.push(set.len() as u8); + out.push(set.contains(&Cell::new(at(0) as u8 % 8)) as u8); + out.extend(set.iter().map(|c| c.get())); + let map: BTreeMap, usize> = data + .iter() + .enumerate() + .map(|(i, &v)| (RefCell::new(v % 8), i)) + .collect(); + out.push(map.get(&RefCell::new(at(1) as u8 % 8)).map_or(0xff, |&i| i as u8)); + + // What each compared left borrowed is released. + *ra.borrow_mut() = y; + *rra.borrow_mut().borrow_mut() = x; + compared!(ra, *rra.borrow(), &log, out); + + let shared = Arc::new(RefCell::new(x)); + let _mutable = match kind % 6 { + 0 => return, + 1 => ra.borrow_mut(), + 2 => rb.borrow_mut(), + _ => shared.borrow_mut(), + }; + match kind % 6 { + 1 => put_eq(ra == rb, &log, out), + 2 => put_eq(ra < rb, &log, out), + 3 => put_eq(shared == shared.clone(), &log, out), + 4 => put_eq(shared != Arc::new(RefCell::new(y)), &log, out), + _ => put_order(shared.partial_cmp(&shared.clone()), &log, out), + } + // Nested, with only the inner mutably borrowed. + let held = rra.borrow(); + let _inner = held.borrow_mut(); + put_eq(rra == rrb, &log, out); +} diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index eff38b187..b915c9dc1 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -183,6 +183,7 @@ fn cases() -> Vec<(&'static str, Case)> { ("zeros::unread", zeros::unread), ("iters::range_froms", iters::range_froms), ("clones::iters", clones::iters), + ("collections::cell_eqs", collections::cell_eqs), ] } diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index dcf22a8b8..b1619fcd5 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,7 +1,7 @@ # Census of `reject-fixture` -- Refusals: 117 -- Function instances: 139 +- Refusals: 118 +- Function instances: 143 - Generic functions no instance reached, lowered over their type parameters: 10 ## By construct @@ -40,6 +40,7 @@ | 1 | `_` with a mutable reference to a value JS copies, which its mapping would copy | fixtures/reject/src/lib.rs:534:12 | | 1 | `_`, which is not supported yet | fixtures/reject/src/lib.rs:279:9 | | 1 | `_`, which reads a capacity that depends on whether the other handles of its storage dropped, which is not supported | fixtures/reject/src/lib.rs:401:6 | +| 1 | `collect` of `_`, which sorts by `_`'s own `Ord`, of entries that drop, which is not supported yet | fixtures/reject/src/lib.rs:985:22 | | 1 | `fmt::Arguments` with a placeholder, or not made in place, written to `_`'s own `fmt::Write`, which is not supported yet: std splits what a placeholder writes into `write_str` calls the runtime does not make | fixtures/reject/src/lib.rs:62:10 | | 1 | a `collect` in place of `_`, a zip std reads by index, holding values that drop, which is not supported yet | fixtures/reject/src/lib.rs:221:40 | | 1 | a `skip` of `_`, a zip std reads by index, of items that drop, which is not supported yet | fixtures/reject/src/lib.rs:214:8 | @@ -132,6 +133,8 @@ The type or module each refused type or path belongs to. | 1 | `poll` | fixtures/reject/src/lib.rs:515:2 | | 1 | `rt.Poll` | fixtures/reject/src/lib.rs:515:2 | | 1 | `std::any::Any` | fixtures/reject/src/lib.rs:558:4 | +| 1 | `std::cell::Cell` | fixtures/reject/src/lib.rs:985:22 | +| 1 | `std::collections::BTreeMap` | fixtures/reject/src/lib.rs:985:22 | | 1 | `std::iter::Extend` | fixtures/reject/src/lib.rs:955:6 | | 1 | `std::iter::Map` | fixtures/reject/src/lib.rs:221:40 | | 1 | `std::iter::Zip` | fixtures/reject/src/lib.rs:214:8 | @@ -186,6 +189,7 @@ The type or module each refused type or path belongs to. | 1 | `_` with a mutable reference to a value JS copies, which its mapping would copy | `std::sync::Mutex::get_mut` | fixtures/reject/src/lib.rs:534:12 | | 1 | `_`, which is not supported yet | `Hasher::write` | fixtures/reject/src/lib.rs:279:9 | | 1 | `_`, which reads a capacity that depends on whether the other handles of its storage dropped, which is not supported | `BytesMut::capacity` | fixtures/reject/src/lib.rs:401:6 | +| 1 | `collect` of `_`, which sorts by `_`'s own `Ord`, of entries that drop, which is not supported yet | `std::collections::BTreeMap`, `std::cell::Cell` | fixtures/reject/src/lib.rs:985:22 | | 1 | `fmt::Arguments` with a placeholder, or not made in place, written to `_`'s own `fmt::Write`, which is not supported yet: std splits what a placeholder writes into `write_str` calls the runtime does not make | `Sink` | fixtures/reject/src/lib.rs:62:10 | | 1 | a `collect` in place of `_`, a zip std reads by index, holding values that drop, which is not supported yet | `std::iter::Map` | fixtures/reject/src/lib.rs:221:40 | | 1 | a `skip` of `_`, a zip std reads by index, of items that drop, which is not supported yet | `std::iter::Zip` | fixtures/reject/src/lib.rs:214:8 | @@ -493,6 +497,12 @@ The type or module each refused type or path belongs to. +
`collect` of `_`, which sorts by `_`'s own `Ord`, of entries that drop, which is not supported yet (1) + +- fixtures/reject/src/lib.rs:985:22: `collect` of `std::collections::BTreeMap, Noisy>`, which sorts by `std::cell::Cell`'s own `Ord`, of entries that drop, which is not supported yet + +
+
`fmt::Arguments` with a placeholder, or not made in place, written to `_`'s own `fmt::Write`, which is not supported yet: std splits what a placeholder writes into `write_str` calls the runtime does not make (1) - fixtures/reject/src/lib.rs:62:10: `fmt::Arguments` with a placeholder, or not made in place, written to `Sink`'s own `fmt::Write`, which is not supported yet: std splits what a placeholder writes into `write_str` calls the runtime does not make diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index 0557fb059..a7556f84b 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -110,6 +110,7 @@ a raw pointer to `(u32, u32)` in `Gap`, which the crate may put another object i a raw pointer to `std::mem::MaybeUninit<(u32, u32)>` in `Gap`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:947:19 a raw pointer to `std::mem::MaybeUninit<(u32, u32)>` in `Gap`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:948:19 `std::iter::Extend::extend` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it at fixtures/reject/src/lib.rs:955:6 +`collect` of `std::collections::BTreeMap, Noisy>`, which sorts by `std::cell::Cell`'s own `Ord`, of entries that drop, which is not supported yet at fixtures/reject/src/lib.rs:985:22 overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:137:2 an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:157:1 overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:185:2 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index 0c5ee99b5..3b675d0bb 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -954,3 +954,33 @@ pub fn uninit_part_replaced(n: u32) -> u32 { pub fn byte_extended(set: &mut std::collections::HashSet<[u8; 2]>, pairs: &[[u8; 2]]) { set.extend(pairs); } + +/// A key ordered by its own `Ord`. +#[derive(Clone, Copy)] +pub struct Ranked(pub u8); + +impl PartialEq for Ranked { + fn eq(&self, other: &Self) -> bool { + self.0 % 4 == other.0 % 4 + } +} + +impl Eq for Ranked {} + +impl PartialOrd for Ranked { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for Ranked { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + (self.0 % 4).cmp(&(other.0 % 4)) + } +} + +/// Entries that drop, sorted by what a cell holds, whose own `Ord` the runtime's sort +/// calls where std's does not. +pub fn ranked_cells(entries: Vec<(std::cell::Cell, Noisy)>) -> usize { + entries.into_iter().collect::>().len() +} diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index 932979747..a05f8bf6f 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -95,5 +95,6 @@ roots = [ "made_part_replaced", "uninit_part_replaced", "byte_extended", + "ranked_cells", ] printable = ["Meters"] diff --git a/ts/runtime/rt/task.ts b/ts/runtime/rt/task.ts index 694f0eb84..0a803e708 100644 --- a/ts/runtime/rt/task.ts +++ b/ts/runtime/rt/task.ts @@ -390,6 +390,21 @@ export function refMutRelease(cell: RefCell): void { cell.borrows = 0; } +/** + * `PartialEq`, `PartialOrd`, or `Ord` of `RefCell`s, by `f` of their values: each borrowed + * in turn, which panics while it is mutably borrowed, and released the other way. + */ +export function refCellBy(f: (a: T, b: T) => R): (a: RefCell, b: RefCell) => R { + return (a, b) => { + refCellBorrow(a); + refCellBorrow(b); + const r = f(a.value, b.value); + refRelease(b); + refRelease(a); + return r; + }; +} + /** `Cell`: the place that holds its value. */ export class Cell implements Place, Disposable { v: T; @@ -403,6 +418,14 @@ export class Cell implements Place, Disposable { } } +/** + * `PartialEq`, `PartialOrd`, or `Ord` of `Cell`s, or `PartialEq` of `OnceCell`s or + * `OnceLock`s, by `f` of what each holds. + */ +export function cellBy(f: (a: T, b: T) => R): (a: { v: T }, b: { v: T }) => R { + return (a, b) => f(a.v, b.v); +} + /** `Cell::replace`: the value it held, `v` in its place. */ export function cellReplace(cell: Cell, v: T): T { const old = cell.v; diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index fa91ec28a..fd3577771 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -846,7 +846,7 @@ fn local<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { } /// Whether `ty`'s `ne` reaches the crate's own `ne` of a type it holds: through tuples and -/// `Arc`s, but not slices, `Option`s, or `Reverse`s, whose `ne` is `!eq`. +/// `Arc`s, but not slices, `Option`s, `Reverse`s, or cells, whose `ne` is `!eq`. fn own_ne<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { let tcx = b.t.tcx; let ty = peel(b, ty); @@ -866,13 +866,14 @@ fn own_ne<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { /// Whether `ty`'s `PartialOrd::{test}`, `lt` or another of its tests, reaches the crate's /// own `test` of a type it holds, which `partial_cmp` stands for only while that is the -/// default: through a tuple's last element, `Arc`s, and `Reverse`s. +/// default: through a tuple's last element, `Arc`s, `Reverse`s, and cells. fn own_test<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>, test: &str) -> bool { let tcx = b.t.tcx; let ty = peel(b, ty); match ty.kind() { ty::Tuple(tys) => tys.last().is_some_and(|&t| own_test(b, t, test)), ty::Adt(..) if let Some(inner) = arc_of(b, ty).or_else(|| reverse_of(b, ty)) => own_test(b, inner, test), + ty::Adt(..) if let Some((inner, _)) = cell_of(b, ty) => own_test(b, inner, test), _ if local(b, ty) => { let f = trait_fn(tcx, "PartialOrd", test); Instance::try_resolve(tcx, b.t.typing_env_pub(), f, tcx.mk_args(&[ty.into(), ty.into()])) @@ -914,6 +915,7 @@ pub fn test_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, test: &str, span: ty::Adt(..) if let Some(inner) = reverse_of(b, ty) => { Expr::rt("cmpReverse", vec![test_fn(b, inner, test, span)]) } + ty::Adt(..) if let Some((inner, by)) = cell_of(b, ty) => Expr::rt(by, vec![test_fn(b, inner, test, span)]), _ => { let f = trait_fn(tcx, "PartialOrd", test); b.fn_value(f, tcx.mk_args(&[ty.into(), ty.into()]), span) @@ -1137,6 +1139,7 @@ fn own_ord<'tcx>( match ty.kind() { ty::Tuple(tys) => tys.iter().any(|t| own_ord(b, t, seen)), ty::Adt(..) if let Some(inner) = option_of(b, ty).or_else(|| arc_of(b, ty)) => own_ord(b, inner, seen), + ty::Adt(..) if let Some((inner, _)) = cell_of(b, ty) => own_ord(b, inner, seen), // A binding's, hand-written even where the Rust impl is derived. ty::Adt(adt, _) if b.t.bound_drops(adt.did()).is_some() => true, ty::Adt(adt, args) if b.t.own(adt.did()) => { @@ -1588,6 +1591,7 @@ fn std_key<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { ty::Adt(..) if let Some(inner) = option_of(b, ty).or_else(|| arc_of(b, ty)).or_else(|| reverse_of(b, ty)) => { held(inner) } + ty::Adt(..) if let Some((inner, _)) = cell_of(b, ty) => held(inner), // Whose derived `PartialEq` lowers from its MIR. ty::Adt(_, args) if b.t.std_of(ty) == Some(Std::Result) => args.types().all(held), ty::Adt(..) if b.t.std_of(ty).is_some_and(Std::error) => true, @@ -1734,6 +1738,24 @@ fn arc_of<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> Option> { (b.t.std_of(ty) == Some(Std::Arc)).then(|| arg(ty, 0)) } +/// What one of std's cells of type `ty` compares by, and the runtime's function that reads +/// it from each: a `Cell`'s and a `RefCell`'s value, which a `RefCell`'s borrows, or a +/// one-time cell's `Option`, which only `PartialEq` compares. +fn cell_of<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> Option<(Ty<'tcx>, &'static str)> { + let ty::Adt(adt, args) = ty.kind() else { + return None; + }; + match b.t.std_of(ty)? { + Std::Cell => Some((args.type_at(0), "cellBy")), + // Not an `RwLock`, which compares by nothing. + Std::RefCell if b.t.tcx.is_diagnostic_item(rustc_span::sym::RefCell, adt.did()) => { + Some((args.type_at(0), "refCellBy")) + } + Std::OnceCell | Std::OnceLock => Some((Ty::new_option(b.t.tcx, args.type_at(0)), "cellBy")), + _ => None, + } +} + /// `Ord::cmp` of `ty` as a function, or `PartialOrd::partial_cmp` if `partial`: the /// runtime's for a std type, which calls the same of what it holds, or the crate's. pub fn cmp_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, partial: bool, span: Span) -> Expr { @@ -1757,6 +1779,7 @@ pub fn cmp_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, partial: bool, spa Expr::rt("cmpOption", [cmp].into_iter().chain(b.t.none_arg(inner)).collect()) } ty::Adt(..) if let Some(inner) = arc_of(b, ty) => Expr::rt("cmpArc", vec![cmp_fn(b, inner, partial, span)]), + ty::Adt(..) if let Some((inner, by)) = cell_of(b, ty) => Expr::rt(by, vec![cmp_fn(b, inner, partial, span)]), ty::Adt(..) if let Some(inner) = reverse_of(b, ty) => { Expr::rt("cmpReverse", vec![cmp_fn(b, inner, partial, span)]) } @@ -1811,6 +1834,8 @@ pub fn eq_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) -> Expr let eq = eq_fn(b, inner, span); arc_eq(b, inner, eq) } + // Whose `ne` is `!eq`, which compares what it holds by `eq`. + ty::Adt(..) if let Some((inner, by)) = cell_of(b, ty) => Expr::rt(by, vec![eq_fn(b, inner, span)]), ty::Adt(..) if local(b, ty) || b.t.std_of(ty).is_some_and(|std| std == Std::Result || std.error()) => { let eq = trait_fn(tcx, "PartialEq", "eq"); b.fn_value(eq, tcx.mk_args(&[ty.into(), ty.into()]), span)