From a6b731a3fc6c14dd5d53b79c0742b350b7fc41ed Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 20:46:32 -0700 Subject: [PATCH 1/2] chore(quest): claim quest/m2/ts-cmp-defaults.md Quest-Claim: f04be12ccc3a6bd2 From 4a1f1fa0a43a4ba8ddd476f46fe2d9c792c64abe Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 21:15:40 -0700 Subject: [PATCH 2/2] feat(ts): translate the inherited ne, lt, le, gt, and ge, and byte slice equality A `PartialEq` or `PartialOrd` impl that leaves `ne` or the orderings to its trait, as every derive does, now lowers them as std's defaults: `!eq`, and a test of `partial_cmp` that is false for `None`. `<[u8] as PartialEq>::eq` maps to `rt.byteSliceEq`. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- quest/m2/README.md | 1 - quest/m2/ts-cmp-defaults.md | 23 --- ts/fixtures/lang/expected/keys.Hop.ts | 4 + ts/fixtures/lang/expected/keys.Horizon.ts | 4 + ts/fixtures/lang/expected/keys.Name.ts | 4 + ts/fixtures/lang/expected/keys.Rough.ts | 30 ++++ ts/fixtures/lang/expected/keys.ts | 177 +++++++++++++++++++++- ts/fixtures/lang/src/keys.rs | 77 +++++++++- ts/runtime/rt.ts | 7 + ts/src/lower/body.rs | 4 + ts/src/lower/collections.rs | 2 +- ts/src/lower/library.rs | 61 ++++++++ 12 files changed, 366 insertions(+), 28 deletions(-) delete mode 100644 quest/m2/ts-cmp-defaults.md create mode 100644 ts/fixtures/lang/expected/keys.Rough.ts diff --git a/quest/m2/README.md b/quest/m2/README.md index 85b9233b7..1424fe965 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -36,7 +36,6 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export.md); - [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values - [Share what the crate never changes in place](/quest/m2/ts-shared-read.md) - copies of `Copy` aggregates no one writes into in place go away, recovering #83's decode cost - [Output tsc accepts for writes through Result and Poll compares](/quest/m2/ts-tsc-shapes.md) - two shapes whose output fails `tsc` type-check -- [Default comparison methods](/quest/m2/ts-cmp-defaults.md) - inherited `ne`, `lt`, `le`, `gt`, `ge` and `[u8]` equality translate - [Copy-on-write strings](/quest/m2/ts-cow-str.md) - `Cow` translates, finishing `ietf/namespace.rs` - [str slicing and &str patterns](/quest/m2/ts-str-slice.md) - `&s[a..b]` and `&str` patterns translate, so `rfind`'s offsets are usable - [Translate moq-net's model to TypeScript](/quest/m2/ts-model.md) - the driver grows from moq-net's codec to its model, for moq to compare against WASM diff --git a/quest/m2/ts-cmp-defaults.md b/quest/m2/ts-cmp-defaults.md deleted file mode 100644 index 6e5aef8f2..000000000 --- a/quest/m2/ts-cmp-defaults.md +++ /dev/null @@ -1,23 +0,0 @@ -# [S] Default comparison methods - -## Goal - -rust2-ts translates the default `ne`, `lt`, `le`, `gt`, and `ge` that a crate's own -`PartialEq` and `PartialOrd` impls inherit, and `<[u8] as PartialEq>::eq`, so `a != b` -and `a < b` on derived types translate, proven by differential tests against native -Rust. - -## Plan - -Found by [#98](https://github.com/kixelated/rust2/pull/98) on 2026-10-27: comparisons -through references now reach the referent's impl, which still refuses an inherited -method. moq-net's census refuses about 60 `ne` calls this way, `path::Path`'s among -them. - -- Lower `ne` as `!eq` and the ordering methods from `partial_cmp`, as std's defaults do, - so a `partial_cmp` returning `None` makes all four false. -- Cover a hand-written `partial_cmp` that returns `None`, and a type whose `eq` and - `partial_cmp` disagree, in the differential tests. -- Report moq-net's census before and after. - -Public API: the translator's output. diff --git a/ts/fixtures/lang/expected/keys.Hop.ts b/ts/fixtures/lang/expected/keys.Hop.ts index 1b529c635..01a0bb72c 100644 --- a/ts/fixtures/lang/expected/keys.Hop.ts +++ b/ts/fixtures/lang/expected/keys.Hop.ts @@ -10,6 +10,10 @@ export function hash$Hash_DefaultHasher(self: Hop, state: rt.Hasher): void { rt.hashInt(self._0, state); } +export function partialCmp$PartialOrd(self: Hop, other: Hop): rt.Ordering | undefined { + return cmp$Ord(self, other); +} + export function cmp$Ord(self: Hop, other: Hop): rt.Ordering { return rt.cmp(self._0, other._0); } diff --git a/ts/fixtures/lang/expected/keys.Horizon.ts b/ts/fixtures/lang/expected/keys.Horizon.ts index 678a54f55..ec0e3f310 100644 --- a/ts/fixtures/lang/expected/keys.Horizon.ts +++ b/ts/fixtures/lang/expected/keys.Horizon.ts @@ -15,6 +15,10 @@ export function hash$Hash_DefaultHasher(self: Horizon, state: rt.Hasher): void { rt.hashInt(self.local, state); } +export function partialCmp$PartialOrd(self: Horizon, other: Horizon): rt.Ordering | undefined { + return cmp$Ord(self, other); +} + export function cmp$Ord(self: Horizon, other: Horizon): rt.Ordering { const cmp: rt.Ordering = rt.cmpOption(Hop.cmp$Ord)(self.exclude, other.exclude); if (cmp === 0) { diff --git a/ts/fixtures/lang/expected/keys.Name.ts b/ts/fixtures/lang/expected/keys.Name.ts index 7fb53ffa7..046fc8ea8 100644 --- a/ts/fixtures/lang/expected/keys.Name.ts +++ b/ts/fixtures/lang/expected/keys.Name.ts @@ -14,6 +14,10 @@ export function hash$Hash_DefaultHasher(self: Name, state: rt.Hasher): void { rt.hashOption(rt.hashInt)(self.rank, state); } +export function partialCmp$PartialOrd(self: Name, other: Name): rt.Ordering | undefined { + return cmp$Ord(self, other); +} + export function cmp$Ord(self: Name, other: Name): rt.Ordering { const cmp: rt.Ordering = rt.cmpArc(rt.cmpStr)(self.text, other.text); if (cmp === 0) { diff --git a/ts/fixtures/lang/expected/keys.Rough.ts b/ts/fixtures/lang/expected/keys.Rough.ts new file mode 100644 index 000000000..297afe83f --- /dev/null +++ b/ts/fixtures/lang/expected/keys.Rough.ts @@ -0,0 +1,30 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +/** + * An `eq` by value and a `partial_cmp` by quarter, unordered past 200: `!=` and `<=` + * and `>=` all hold between some, and none of `<`, `<=`, `>`, and `>=` past 200. + */ +export interface Rough { + _0: rt.u8; +} + +export function eq$PartialEq(self: Rough, other: Rough): boolean { + return self._0 === other._0; +} + +export function partialCmp$PartialOrd(self: Rough, other: Rough): rt.Ordering | undefined { + let _3!: boolean; + if (!(self._0 > 200)) { + _3 = other._0 > 200; + } else { + _3 = true; + } + if (!_3) { + return rt.cmp(rt.u8Div(self._0, 4), rt.u8Div(other._0, 4)); + } + return undefined; +} + +//# sourceMappingURL=keys.Rough.ts.map diff --git a/ts/fixtures/lang/expected/keys.ts b/ts/fixtures/lang/expected/keys.ts index 94663b96b..b6f0e3470 100644 --- a/ts/fixtures/lang/expected/keys.ts +++ b/ts/fixtures/lang/expected/keys.ts @@ -8,6 +8,7 @@ import * as Liar from "./keys.Liar.ts"; import * as Name from "./keys.Name.ts"; import * as Never from "./keys.Never.ts"; import * as Picky from "./keys.Picky.ts"; +import * as Rough from "./keys.Rough.ts"; import * as Seen from "./keys.Seen.ts"; import * as Side from "./keys.Side.ts"; import * as Skew from "./keys.Skew.ts"; @@ -185,18 +186,192 @@ export function find$u8_u8(keys_: rt.ByteSlice, q: rt.u8): rt.u8 { /** Run one case. */ export function keys(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { - const _4: rt.u8 = rt.u8Rem(kind, 4); + const _4: rt.u8 = rt.u8Rem(kind, 5); if (_4 === 0) { options(data, out); } else if (_4 === 1) { arcs(data, out); } else if (_4 === 2) { borrows(data, out); + } else if (_4 === 3) { + defaults(data, out); } else { places(data, out); } } +/** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ +export function compare$Horizon(l: Horizon.Horizon, r: Horizon.Horizon): rt.u8 { + const _5: rt.u8 = (!Horizon.eq$PartialEq(l, r) ? 1 : 0) | (((Horizon.partialCmp$PartialOrd(l, r) === -1 ? 1 : 0) << 1) & 0xff); + const order_ = Horizon.partialCmp$PartialOrd(l, r); + const _3: rt.u8 = _5 | (((order_ !== undefined && order_ <= 0 ? 1 : 0) << 2) & 0xff) | (((Horizon.partialCmp$PartialOrd(l, r) === 1 ? 1 : 0) << 3) & 0xff); + const order2 = Horizon.partialCmp$PartialOrd(l, r); + return _3 | (((order2 !== undefined && order2 >= 0 ? 1 : 0) << 4) & 0xff); +} + +/** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ +export function compare$Name(l: Name.Name, r: Name.Name): rt.u8 { + const _5: rt.u8 = (!Name.eq$PartialEq(l, r) ? 1 : 0) | (((Name.partialCmp$PartialOrd(l, r) === -1 ? 1 : 0) << 1) & 0xff); + const order_ = Name.partialCmp$PartialOrd(l, r); + const _3: rt.u8 = _5 | (((order_ !== undefined && order_ <= 0 ? 1 : 0) << 2) & 0xff) | (((Name.partialCmp$PartialOrd(l, r) === 1 ? 1 : 0) << 3) & 0xff); + const order2 = Name.partialCmp$PartialOrd(l, r); + return _3 | (((order2 !== undefined && order2 >= 0 ? 1 : 0) << 4) & 0xff); +} + +/** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ +export function compare$Option_Hop(l: Hop.Hop | undefined, r: Hop.Hop | undefined): rt.u8 { + const _5: rt.u8 = (!rt.eqOption(Hop.eq$PartialEq)(l, r) ? 1 : 0) | (((rt.cmpOption(Hop.partialCmp$PartialOrd)(l, r) === -1 ? 1 : 0) << 1) & 0xff); + const order_ = rt.cmpOption(Hop.partialCmp$PartialOrd)(l, r); + const _3: rt.u8 = _5 | (((order_ !== undefined && order_ <= 0 ? 1 : 0) << 2) & 0xff) | (((rt.cmpOption(Hop.partialCmp$PartialOrd)(l, r) === 1 ? 1 : 0) << 3) & 0xff); + const order2 = rt.cmpOption(Hop.partialCmp$PartialOrd)(l, r); + return _3 | (((order2 !== undefined && order2 >= 0 ? 1 : 0) << 4) & 0xff); +} + +/** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ +export function compare$Option_Rough(l: Rough.Rough | undefined, r: Rough.Rough | undefined): rt.u8 { + const _5: rt.u8 = (!rt.eqOption(Rough.eq$PartialEq)(l, r) ? 1 : 0) | (((rt.cmpOption(Rough.partialCmp$PartialOrd)(l, r) === -1 ? 1 : 0) << 1) & 0xff); + const order_ = rt.cmpOption(Rough.partialCmp$PartialOrd)(l, r); + const _3: rt.u8 = _5 | (((order_ !== undefined && order_ <= 0 ? 1 : 0) << 2) & 0xff) | (((rt.cmpOption(Rough.partialCmp$PartialOrd)(l, r) === 1 ? 1 : 0) << 3) & 0xff); + const order2 = rt.cmpOption(Rough.partialCmp$PartialOrd)(l, r); + return _3 | (((order2 !== undefined && order2 >= 0 ? 1 : 0) << 4) & 0xff); +} + +/** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ +export function compare$Rough(l: Rough.Rough, r: Rough.Rough): rt.u8 { + const _5: rt.u8 = (!Rough.eq$PartialEq(l, r) ? 1 : 0) | (((Rough.partialCmp$PartialOrd(l, r) === -1 ? 1 : 0) << 1) & 0xff); + const order_ = Rough.partialCmp$PartialOrd(l, r); + const _3: rt.u8 = _5 | (((order_ !== undefined && order_ <= 0 ? 1 : 0) << 2) & 0xff) | (((Rough.partialCmp$PartialOrd(l, r) === 1 ? 1 : 0) << 3) & 0xff); + const order2 = Rough.partialCmp$PartialOrd(l, r); + return _3 | (((order2 !== undefined && order2 >= 0 ? 1 : 0) << 4) & 0xff); +} + +/** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ +export function compare$Skew(l: Skew.Skew, r: Skew.Skew): rt.u8 { + const _5: rt.u8 = (!Skew.eq$PartialEq(l, r) ? 1 : 0) | (((Skew.partialCmp$PartialOrd(l, r) === -1 ? 1 : 0) << 1) & 0xff); + const order_ = Skew.partialCmp$PartialOrd(l, r); + const _3: rt.u8 = _5 | (((order_ !== undefined && order_ <= 0 ? 1 : 0) << 2) & 0xff) | (((Skew.partialCmp$PartialOrd(l, r) === 1 ? 1 : 0) << 3) & 0xff); + const order2 = Skew.partialCmp$PartialOrd(l, r); + return _3 | (((order2 !== undefined && order2 >= 0 ? 1 : 0) << 4) & 0xff); +} + +/** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ +export function compare$Skewed(l: Skewed.Skewed, r: Skewed.Skewed): rt.u8 { + const _5: rt.u8 = (!Skewed.eq$PartialEq(l, r) ? 1 : 0) | (((Skewed.partialCmp$PartialOrd(l, r) === -1 ? 1 : 0) << 1) & 0xff); + const order_ = Skewed.partialCmp$PartialOrd(l, r); + const _3: rt.u8 = _5 | (((order_ !== undefined && order_ <= 0 ? 1 : 0) << 2) & 0xff) | (((Skewed.partialCmp$PartialOrd(l, r) === 1 ? 1 : 0) << 3) & 0xff); + const order2 = Skewed.partialCmp$PartialOrd(l, r); + return _3 | (((order2 !== undefined && order2 >= 0 ? 1 : 0) << 4) & 0xff); +} + +/** + * The comparisons a type's `PartialEq` and `PartialOrd` inherit: `!=` as `!eq`, and + * the orderings from `partial_cmp`, false when it is `None`, whatever `eq` says. + */ +export function defaults(data: rt.ByteSlice, out: rt.ByteVec): void { + let _7!: rt.usize; + let m!: Name.Name; + let n!: Name.Name; + let _177!: Name.Name; + let unwind: number = 0; + try { + let iterStart: rt.usize = 1; + const iterEnd: rt.usize = data.length; + while ((_7 = iterStart++) < iterEnd) { + const i: rt.usize = _7; + const _15: rt.usize = rt.usizeSub(i, 1); + rt.idx(data, _15); + const _14: rt.u8 = data.data[data.start + _15]; + rt.idx(data, i); + const _13: [rt.u8, rt.u8] = [_14, data.data[data.start + i]]; + const c: rt.u8 = _14; + const d: rt.u8 = _13[1]; + const _24: [Rough.Rough, Rough.Rough] = [{ _0: c }, { _0: d }]; + const a: Rough.Rough = _24[0]; + const b: Rough.Rough = _24[1]; + const _29: rt.u8 = compare$Rough(a, b) | (((!Rough.eq$PartialEq(a, b) ? 1 : 0) << 5) & 0xff) | (((Rough.partialCmp$PartialOrd(a, b) === -1 ? 1 : 0) << 6) & 0xff); + const order_ = Rough.partialCmp$PartialOrd(a, b); + rt.byteVecPush(out, _29 | (((order_ !== undefined && order_ >= 0 ? 1 : 0) << 7) & 0xff)); + const _58: rt.u8 = compare$Option_Rough(a, b); + const _67: Rough.Rough | undefined = a; + const _68: Rough.Rough | undefined = undefined; + const order2 = rt.cmpOption(Rough.partialCmp$PartialOrd)(_67, _68); + const _57: rt.u8 = _58 | (((order2 !== undefined && order2 <= 0 ? 1 : 0) << 5) & 0xff); + const _74: Rough.Rough | undefined = undefined; + const _76: Rough.Rough | undefined = b; + rt.byteVecPush(out, _57 | (((rt.cmpOption(Rough.partialCmp$PartialOrd)(_74, _76) === -1 ? 1 : 0) << 6) & 0xff)); + const _81: [Skew.Skew, Skew.Skew] = [{ _0: rt.u8Rem(c, 4) }, { _0: rt.u8Rem(d, 4) }]; + const s: Skew.Skew = _81[0]; + const t: Skew.Skew = _81[1]; + const _91: rt.u8 = compare$Skew(s, t); + const order3 = Skew.partialCmp$PartialOrd(s, t); + rt.byteVecPush(out, _91 | (((order3 !== undefined && order3 <= 0 ? 1 : 0) << 5) & 0xff) | (((Skew.partialCmp$PartialOrd(s, t) === 1 ? 1 : 0) << 6) & 0xff)); + rt.byteVecPush(out, compare$Skewed({ _0: s }, { _0: t })); + const _118: [Hop.Hop | undefined, Hop.Hop | undefined] = [hop(c), hop(d)]; + const g: Hop.Hop | undefined = _118[0]; + const h: Hop.Hop | undefined = _118[1]; + const _123: rt.u8 = compare$Option_Hop(g, h); + const _130: rt.u64 | undefined = rt.optionMap(g, (h2: Hop.Hop): rt.u64 => { + return h2._0; + }); + const _132: rt.u64 | undefined = rt.optionMap(h, (h3: Hop.Hop): rt.u64 => { + return h3._0; + }); + rt.byteVecPush(out, _123 | (((rt.cmpOption(rt.cmp)(_130, _132) === 1 ? 1 : 0) << 5) & 0xff)); + const _139: boolean = rt.u8Rem(c, 2) === 0; + const _138: Horizon.Horizon = { exclude: g, local: _139 }; + unwind = 0; + const _143: boolean = rt.u8Rem(d, 2) === 0; + const _137: [Horizon.Horizon, Horizon.Horizon] = [_138, { exclude: h, local: _143 }]; + const x: Horizon.Horizon = _138; + const y: Horizon.Horizon = _137[1]; + const _149: rt.u8 = compare$Horizon(x, y); + unwind = 0; + const _148: rt.u8 = _149 | (((Horizon.partialCmp$PartialOrd(x, y) === -1 ? 1 : 0) << 5) & 0xff); + const _167: Horizon.Horizon = x; + const order4 = Horizon.partialCmp$PartialOrd(_167, y); + rt.byteVecPush(out, _148 | (((order4 !== undefined && order4 >= 0 ? 1 : 0) << 6) & 0xff)); + _177 = name(c); + unwind = 99; + const _178: Name.Name = name(d); + unwind = 97; + m = _177; + n = _178; + rt.byteVecPush(out, compare$Name(m, n) | (((!rt.eqArc(rt.same, true)(m.text, n.text) ? 1 : 0) << 5) & 0xff)); + const _194: rt.u8 = !Side.eq$PartialEq(side(rt.u8Rem(c, 2)), side(rt.u8Rem(d, 2))) ? 1 : 0; + const _208: Way.Way | undefined = way(c); + const _210: Way.Way | undefined = way(d); + rt.byteVecPush(out, _194 | (((!rt.eqOption(Way.eq$PartialEq)(_208, _210) ? 1 : 0) << 1) & 0xff) | (((rt.optionMap(way(c), (w: Way.Way): boolean => { + return !Way.eq$PartialEq(w, "Top" as Way.Way); + }) === true ? 1 : 0) << 2) & 0xff)); + [rt.byteSliceRange(data, 0, i), rt.byteSliceRange(data, i)] as [rt.ByteSlice, rt.ByteSlice]; + const l: rt.ByteSlice = rt.byteSliceRange(data, 0, i); + const r: rt.ByteSlice = rt.byteSliceRange(data, i); + [rt.byteSliceRange(data, rt.usizeSub(i, 1), i), rt.byteSliceRange(data, rt.usizeRem(c as rt.usize, i), rt.usizeAdd(rt.usizeRem(c as rt.usize, i), 1))] as [rt.ByteSlice, rt.ByteSlice]; + const p: rt.ByteSlice = rt.byteSliceRange(data, rt.usizeSub(i, 1), i); + const q: rt.ByteSlice = rt.byteSliceRange(data, rt.usizeRem(c as rt.usize, i), rt.usizeAdd(rt.usizeRem(c as rt.usize, i), 1)); + rt.byteVecPush(out, (rt.byteSliceEq(l, r) ? 1 : 0) | (((!rt.byteSliceEq(l, r) ? 1 : 0) << 1) & 0xff) | (((rt.byteSliceEq(p, q) ? 1 : 0) << 2) & 0xff) | (((!rt.byteSliceEq(p, q) ? 1 : 0) << 3) & 0xff) | (((rt.byteSliceEq(rt.byteSliceRange(l, 0, 0), rt.byteSliceRange(r, 0, 0)) ? 1 : 0) << 4) & 0xff) | (((rt.byteSliceEq(rt.byteSliceRange(data, 0, i), rt.byteSliceRange(data, rt.usizeSub(data.length, i))) ? 1 : 0) << 5) & 0xff)); + unwind = 98; + rt.drop(n); + unwind = 0; + rt.drop(m); + } + } catch (e) { + b2: { + if (unwind === 0) { + throw e; + } + if (unwind === 97) { + rt.drop(n); + } else if (unwind !== 98) { + rt.drop(_177); + break b2; + } + rt.drop(m); + } + throw e; + } +} + /** One of `Seen`'s variants, by `k`. */ export function seen(k: rt.u8, hop_: Hop.Hop | undefined, horizon: Horizon.Horizon, name_: Name.Name, mine: Hop.Hop): Seen.Seen { const _6: rt.u8 = rt.u8Rem(k, 5); diff --git a/ts/fixtures/lang/src/keys.rs b/ts/fixtures/lang/src/keys.rs index 89522f6a7..a0aeb23b8 100644 --- a/ts/fixtures/lang/src/keys.rs +++ b/ts/fixtures/lang/src/keys.rs @@ -1,6 +1,7 @@ //! Keys a map compares, orders, and hashes through std's impls: `Option`s, which order //! `None` first, and `Arc`s, which compare as what they hold; lookups through `Borrow`; -//! and the references to values a map holds that JS copies, changed in place. +//! the references to values a map holds that JS copies, changed in place; and the `!=`, +//! `<`, `<=`, `>`, and `>=` a type's `PartialEq` and `PartialOrd` inherit. use std::borrow::Borrow; use std::cmp::Ordering; @@ -164,6 +165,26 @@ enum Seen<'a> { #[derive(PartialEq, PartialOrd)] struct Skewed<'a>(&'a Skew); +/// An `eq` by value and a `partial_cmp` by quarter, unordered past 200: `!=` and `<=` +/// and `>=` all hold between some, and none of `<`, `<=`, `>`, and `>=` past 200. +#[derive(Clone, Copy)] +struct Rough(u8); + +impl PartialEq for Rough { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } +} + +impl PartialOrd for Rough { + fn partial_cmp(&self, other: &Self) -> Option { + match self.0 > 200 || other.0 > 200 { + true => None, + false => (self.0 / 4).partial_cmp(&(other.0 / 4)), + } + } +} + /// Strings whose UTF-8 order differs from their UTF-16 order past U+FFFF. const WORDS: [&str; 6] = ["", "a", "ab", "\u{e000}", "\u{ffff}", "\u{10000}"]; @@ -232,14 +253,66 @@ fn find, Q: PartialEq + ?Sized>(keys: &[K], q: &Q) -> u8 { /// Run one case. pub fn keys(kind: u8, data: &[u8], out: &mut Vec) { - match kind % 4 { + match kind % 5 { 0 => options(data, out), 1 => arcs(data, out), 2 => borrows(data, out), + 3 => defaults(data, out), _ => places(data, out), } } +/// `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. +fn compare(l: &T, r: &T) -> u8 { + (l != r) as u8 | ((l < r) as u8) << 1 | ((l <= r) as u8) << 2 | ((l > r) as u8) << 3 | ((l >= r) as u8) << 4 +} + +/// The comparisons a type's `PartialEq` and `PartialOrd` inherit: `!=` as `!eq`, and +/// the orderings from `partial_cmp`, false when it is `None`, whatever `eq` says. +fn defaults(data: &[u8], out: &mut Vec) { + for i in 1..data.len() { + let (c, d) = (data[i - 1], data[i]); + let (a, b) = (Rough(c), Rough(d)); + out.push(compare(&a, &b) | ((a != b) as u8) << 5 | ((a < b) as u8) << 6 | ((a >= b) as u8) << 7); + out.push(compare(&Some(a), &Some(b)) | ((Some(a) <= None) as u8) << 5 | ((None < Some(b)) as u8) << 6); + let (s, t) = (Skew(c % 4), Skew(d % 4)); + out.push(compare(&s, &t) | ((s <= t) as u8) << 5 | ((s > t) as u8) << 6); + out.push(compare(&Skewed(&s), &Skewed(&t))); + let (g, h) = (hop(c), hop(d)); + out.push(compare(&g, &h) | ((g.map(|h| h.0) > h.map(|h| h.0)) as u8) << 5); + let (x, y) = ( + Horizon { + exclude: g, + local: c % 2 == 0, + }, + Horizon { + exclude: h, + local: d % 2 == 0, + }, + ); + out.push(compare(&x, &y) | ((&x < &y) as u8) << 5 | ((&&x >= &&y) as u8) << 6); + let (m, n) = (name(c), name(d)); + out.push(compare(&m, &n) | ((m.text != n.text) as u8) << 5); + // `Side`'s derived `eq` and `Way`'s own, which finds `Up` and `Top` equal. + out.push( + (side(c % 2) != side(d % 2)) as u8 + | ((way(c) != way(d)) as u8) << 1 + | ((way(c).map(|w| w != Way::Top) == Some(true)) as u8) << 2, + ); + // Byte slices, through references and not. + let (l, r) = (&data[..i], &data[i..]); + let (p, q) = (&data[i - 1..i], &data[c as usize % i..c as usize % i + 1]); + out.push( + (l == r) as u8 + | ((l != r) as u8) << 1 + | ((p == q) as u8) << 2 + | ((*p != *q) as u8) << 3 + | ((&l[..0] == &r[..0]) as u8) << 4 + | ((data[..i] == data[data.len() - i..]) as u8) << 5, + ); + } +} + /// One of `Seen`'s variants, by `k`. fn seen<'a>(k: u8, hop: &'a Option, horizon: &'a &'a Horizon, name: &'a Name, mine: &'a mut Hop) -> Seen<'a> { match k % 5 { diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index 690dfc41f..1d32dbcf8 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -321,6 +321,13 @@ export function byteSliceGet(s: ByteSlice, index: number): number | undefined { return index < s.length ? s.data[s.start + index] : undefined; } +/** `==` of byte slices: the same length and bytes. */ +export function byteSliceEq(a: ByteSlice, b: ByteSlice): boolean { + if (a.length !== b.length) return false; + for (let i = 0; i < a.length; i++) if (a.data[a.start + i] !== b.data[b.start + i]) return false; + return true; +} + /** Copy `src` into `dst` at `at`; a few bytes copy faster than a view of them is made. */ function copyBytes(dst: Uint8Array, at: number, src: Uint8Array, start: number, length: number): void { if (length <= 16) { diff --git a/ts/src/lower/body.rs b/ts/src/lower/body.rs index 4ab8c686f..6ec7f2894 100644 --- a/ts/src/lower/body.rs +++ b/ts/src/lower/body.rs @@ -125,6 +125,10 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { if let Some(referent) = library::referent(tcx, def_id, gargs, instance) { return self.call_def(def_id, referent, args, span, out); } + if let Some(inherited) = library::Inherited::of(tcx, instance) { + let value = self.call_def(inherited.via(tcx), gargs, args, span, out); + return inherited.test(self, value, out); + } let call = library::Call { def_id, args: gargs, diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index 3c5130046..58e97d85a 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -923,7 +923,7 @@ pub fn clone_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) -> E } /// An item of one of std's traits, by the trait's diagnostic name: `Ord`'s `cmp`. -fn trait_fn(tcx: ty::TyCtxt<'_>, tr: &str, item: &str) -> rustc_hir::def_id::DefId { +pub fn trait_fn(tcx: ty::TyCtxt<'_>, tr: &str, item: &str) -> rustc_hir::def_id::DefId { let tr = tcx .get_diagnostic_item(rustc_span::Symbol::intern(tr)) .expect("a trait of std"); diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 8bacb3fde..5f6e6b78e 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -271,6 +271,63 @@ pub fn referent<'tcx>( Some(tcx.mk_args(&args)) } +/// A comparison an impl of `PartialEq` or `PartialOrd` inherits from its trait, as a +/// derived one inherits all but `eq` and `partial_cmp`. +#[derive(Clone, Copy)] +pub enum Inherited { + Ne, + Lt, + Le, + Gt, + Ge, +} + +impl Inherited { + /// The comparison `instance` is, if it is the trait's own default. + pub fn of(tcx: TyCtxt<'_>, instance: Instance<'_>) -> Option { + let ty::InstanceKind::Item(did) = instance.def else { + return None; + }; + let tr = tcx.trait_of_assoc(did)?; + let inherited = match (tcx.get_diagnostic_name(tr)?.as_str(), tcx.item_name(did).as_str()) { + ("PartialEq", "ne") => Self::Ne, + ("PartialOrd", "lt") => Self::Lt, + ("PartialOrd", "le") => Self::Le, + ("PartialOrd", "gt") => Self::Gt, + ("PartialOrd", "ge") => Self::Ge, + _ => return None, + }; + Some(inherited) + } + + /// What the default calls, with the same generic arguments and operands: `eq` or + /// `partial_cmp`. + pub fn via(self, tcx: TyCtxt<'_>) -> DefId { + match self { + Self::Ne => super::collections::trait_fn(tcx, "PartialEq", "eq"), + _ => super::collections::trait_fn(tcx, "PartialOrd", "partial_cmp"), + } + } + + /// The comparison, from what `via` returned: `!eq`, or whether the `partial_cmp` is + /// `Some` and of the order wanted, as std's defaults test it. + pub fn test(self, b: &mut Body<'_, '_, '_>, value: Expr, out: &mut Vec) -> Expr { + let (op, order) = match self { + Self::Ne => return value.not(), + Self::Lt => return value.binary("===", Expr::lit("-1")), + Self::Gt => return value.binary("===", Expr::lit("1")), + Self::Le => ("<=", "0"), + Self::Ge => (">=", "0"), + }; + let value = match value.is_path() { + true => value, + false => b.spill(value, "order", out), + }; + let some = value.clone().binary("!==", Expr::undefined()); + some.binary("&&", value.binary(op, Expr::lit(order))) + } +} + /// Whether `Clone` of `ty`, a `Copy` type, only copies: no `Clone` written by hand runs /// for it or anything in it, as a derived or std `Clone` clones each field. pub fn clone_copies<'tcx>(t: &super::Translator<'_, 'tcx>, ty: Ty<'tcx>) -> bool { @@ -445,6 +502,7 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Vec let self_ty = || gargs.types().next(); let is_u8 = |t: Ty<'tcx>| matches!(t.kind(), ty::Uint(ty::UintTy::U8)); + let byte_slice = |t: Ty<'tcx>| matches!(t.kind(), ty::Slice(e) if is_u8(*e)); let range = matches!(inherent_std(tcx, call.def_id), Some(Std::Range | Std::RangeInclusive)); // A range of integers, whose comparisons and clones JS makes as Rust does. let int_range = |t: Ty<'tcx>| match t.peel_refs().kind() { @@ -646,6 +704,9 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Vec } } ("slice", "copy_from_slice") if is_u8(targ(gargs, 0)) => rt2(args, "byteSlice", "copyFromSlice"), + ("PartialEq", "eq") if byte_slice(targ(gargs, 0)) && byte_slice(targ(gargs, 1)) => { + Expr::rt("byteSliceEq", args) + } ("slice", "to_vec") => { let elem = targ(gargs, 0); let values = args;