From 1ed98b5e3efc29a199640fa3a8dd2d2aee055c5e Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 27 Oct 2026 13:30:03 -0700 Subject: [PATCH 1/8] chore(quest): claim quest/m2/ts-model/bind.md Quest-Claim: b66d932ca76d56d2 From 9cce611f805d65975971fe6121fe8362c2a63171 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 27 Oct 2026 14:01:22 -0700 Subject: [PATCH 2/8] feat(ts): bind hand-written TypeScript in place of Rust items `ts.toml`'s `[bind]` names TS that stands in for a function or a type, by path from the crate or a crate it depends on. Calls go to the binding file's exports, which `tsc` checks against every instance's translated signature, failing at the Rust item's span. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3 --- flake.nix | 4 +- quest/m2/ts-async.md | 2 +- quest/m2/ts-model/README.md | 1 - quest/m2/ts-model/bind.md | 30 -- quest/m2/ts-model/crates.md | 2 +- quest/m2/ts-model/time.md | 2 +- quest/m3/ts-unsafe.md | 2 +- ts/Cargo.lock | 7 + ts/README.md | 46 +- ts/fixtures/bind/Cargo.toml | 17 + ts/fixtures/bind/bind/bytes.ts | 53 ++ ts/fixtures/bind/bind/checksum.ts | 9 + ts/fixtures/bind/bind/tally.ts | 25 + ts/fixtures/bind/expected/bind/bytes.ts | 54 ++ ts/fixtures/bind/expected/bind/checksum.ts | 10 + ts/fixtures/bind/expected/bind/tally.ts | 26 + ts/fixtures/bind/expected/harness.ts | 123 +++++ ts/fixtures/bind/refused.toml | 15 + ts/fixtures/bind/refused.txt | 13 + ts/fixtures/bind/src/harness.rs | 46 ++ ts/fixtures/bind/src/lib.rs | 13 + ts/fixtures/bind/src/raw.rs | 48 ++ ts/fixtures/bind/ts.toml | 7 + ts/fixtures/bind/wrong.toml | 9 + ts/fixtures/bind/wrong.txt | 8 + ts/fixtures/bind/wrong/bytes.ts | 34 ++ ts/fixtures/bind/wrong/tally.ts | 16 + ts/src/config.rs | 50 +- ts/src/driver.rs | 2 +- ts/src/lower/bind.rs | 547 +++++++++++++++++++++ ts/src/lower/body.rs | 18 +- ts/src/lower/collections.rs | 16 +- ts/src/lower/consts.rs | 2 +- ts/src/lower/copy.rs | 2 +- ts/src/lower/drop.rs | 6 + ts/src/lower/iter.rs | 2 +- ts/src/lower/mir/call.rs | 7 +- ts/src/lower/mir/mod.rs | 10 +- ts/src/lower/mir/place.rs | 10 +- ts/src/lower/mir/rvalue.rs | 4 +- ts/src/lower/mod.rs | 73 +-- ts/src/print.rs | 6 +- ts/src/program.rs | 53 +- ts/tests/fixtures.rs | 46 +- 44 files changed, 1357 insertions(+), 119 deletions(-) delete mode 100644 quest/m2/ts-model/bind.md create mode 100644 ts/fixtures/bind/Cargo.toml create mode 100644 ts/fixtures/bind/bind/bytes.ts create mode 100644 ts/fixtures/bind/bind/checksum.ts create mode 100644 ts/fixtures/bind/bind/tally.ts create mode 100644 ts/fixtures/bind/expected/bind/bytes.ts create mode 100644 ts/fixtures/bind/expected/bind/checksum.ts create mode 100644 ts/fixtures/bind/expected/bind/tally.ts create mode 100644 ts/fixtures/bind/expected/harness.ts create mode 100644 ts/fixtures/bind/refused.toml create mode 100644 ts/fixtures/bind/refused.txt create mode 100644 ts/fixtures/bind/src/harness.rs create mode 100644 ts/fixtures/bind/src/lib.rs create mode 100644 ts/fixtures/bind/src/raw.rs create mode 100644 ts/fixtures/bind/ts.toml create mode 100644 ts/fixtures/bind/wrong.toml create mode 100644 ts/fixtures/bind/wrong.txt create mode 100644 ts/fixtures/bind/wrong/bytes.ts create mode 100644 ts/fixtures/bind/wrong/tally.ts create mode 100644 ts/src/lower/bind.rs diff --git a/flake.nix b/flake.nix index 9abef83b1..94c3c90e6 100644 --- a/flake.nix +++ b/flake.nix @@ -98,8 +98,10 @@ ]; RUSTFLAGS = "-C link-args=-Wl,-rpath,${tsToolchain}/lib"; nativeBuildInputs = [ pkgs.makeWrapper ]; + # tsc checks bindings, from the caller's PATH first. postInstall = '' - wrapProgram $out/bin/rust2-ts --set-default CARGO ${tsToolchain}/bin/cargo + wrapProgram $out/bin/rust2-ts --set-default CARGO ${tsToolchain}/bin/cargo \ + --suffix PATH : ${pkgs.typescript}/bin ''; # The tests need node, tsc, and wasm builds: `just ts test` runs them. doCheck = false; diff --git a/quest/m2/ts-async.md b/quest/m2/ts-async.md index f75be950f..aa57a4070 100644 --- a/quest/m2/ts-async.md +++ b/quest/m2/ts-async.md @@ -28,7 +28,7 @@ What moq-net still needs, by its census from MIR at the pinned revision: - kio's own API, reached across crates (`Pending`, `Consumer::poll`, `Waiter::poll_future`, `Tasks`), translates through [safe dependencies](/quest/m2/ts-model/crates.md), and kio's `RawWaker` vtable and - `Box` tasks are [bindings](/quest/m2/ts-model/bind.md). + `Box` tasks are [bindings](/ts/README.md#bindings). The model itself (its census of 2026-10-27) needs only `Poll`'s helpers and `Waker::from(Arc)`, 61 refusals; `pin!`, `poll_fn`, `Box::pin`, and async closures are diff --git a/quest/m2/ts-model/README.md b/quest/m2/ts-model/README.md index c788f1b2c..ea7a8c8d5 100644 --- a/quest/m2/ts-model/README.md +++ b/quest/m2/ts-model/README.md @@ -46,7 +46,6 @@ Public API: the translator's output for moq-net's model. - [Drop in the TypeScript translator](/quest/m2/ts-drop.md) - kio closes a channel when its last producer drops - [Shared ownership](/quest/m2/ts-shared.md) - kio's waiters are `Arc`/`Weak` with observable counts, and its atomics and locks are plain values -- [Bind to hand-written TypeScript](/quest/m2/ts-model/bind.md) - a config names TS that stands in for a Rust item at a boundary - [Translate safe dependencies with the package](/quest/m2/ts-model/crates.md) - kio, moq-clock, moq-pattern, and rangemap translate as one program with moq-net - [Std combinators from core's MIR](/quest/m2/ts-model/core-mir.md) - `Option`'s and `Result`'s combinators lower from core's own bodies - [Duration](/quest/m2/ts-model/time.md) - `Duration` translates as std's seconds and nanoseconds diff --git a/quest/m2/ts-model/bind.md b/quest/m2/ts-model/bind.md deleted file mode 100644 index ca5e67de1..000000000 --- a/quest/m2/ts-model/bind.md +++ /dev/null @@ -1,30 +0,0 @@ -# [M] Bind to hand-written TypeScript - -## Goal - -[rust2-ts](/ts/README.md) calls hand-written TypeScript that `ts.toml` names in place of a -Rust function or type, typed by its Rust signature and checked by `tsc`, so a boundary the -subset refuses for good translates around it. - -## Plan - -Decided with the maintainer on 2026-10-23 and 2026-10-27: `unsafe` and boundary trait -objects bind to hand-written TS through config, and a dependency containing `unsafe` is -bound by the consumer. The mechanism is general; nothing in it names moq. - -What the [model](/quest/m2/ts-model/README.md) needs bound, from its census on 2026-10-27: - -- `Box` in moq-net's `util.rs` and kio's `Tasks`, and kio's `RawWaker` vtable. -- moq-clock's browser clock and `SystemTime`. -- `rand::rng`. -- `flate2`'s inflater, bound to a synchronous JS inflater such as fflate, since browsers - have no synchronous DEFLATE. -- `bytes::Bytes` and `BytesMut` (86 refusals), which contain `unsafe`. - -moq-net's and kio's internal `unsafe` gets safe `cfg` twins in moq instead. - -- A binding's TS is type-checked against the Rust signature's translated type, and a - missing or mismatched binding fails the translation with the Rust item's span. -- Report the model's census before and after. - -Public API: `ts.toml` gains bindings. diff --git a/quest/m2/ts-model/crates.md b/quest/m2/ts-model/crates.md index 2c6811243..6e7df9c9d 100644 --- a/quest/m2/ts-model/crates.md +++ b/quest/m2/ts-model/crates.md @@ -23,7 +23,7 @@ else. rangemap adds 266. - Each crate's output goes in its own module directory, and the instances a crate's generics get from the package go with the crate that defines them. - The rule is general: a dependency without `unsafe` translates from source; one with - `unsafe` is refused with a span unless the consumer [binds](/quest/m2/ts-model/bind.md) + `unsafe` is refused with a span unless the consumer [binds](/ts/README.md#bindings) it. - A named crate's own refusals belong to other quests: kio's atomics and `thread_local!` to [shared ownership](/quest/m2/ts-shared.md), its `RawWaker` vtable to bindings, and diff --git a/quest/m2/ts-model/time.md b/quest/m2/ts-model/time.md index a7cf5964a..72cf626fb 100644 --- a/quest/m2/ts-model/time.md +++ b/quest/m2/ts-model/time.md @@ -14,6 +14,6 @@ the model and 67 in moq-clock, including its constants (`Duration::ZERO`, `MAX`) Decided with the maintainer on 2026-10-27: std's seconds (`u64`) and nanoseconds (`u32`), so overflow, `checked_*`, and `Ord` match std, and core's MIR can lower its methods later. -- The clocks that read the time are [bindings](/quest/m2/ts-model/bind.md). +- The clocks that read the time are [bindings](/ts/README.md#bindings). Public API: the translator's output. diff --git a/quest/m3/ts-unsafe.md b/quest/m3/ts-unsafe.md index 489aaf413..b2d01d7f0 100644 --- a/quest/m3/ts-unsafe.md +++ b/quest/m3/ts-unsafe.md @@ -10,7 +10,7 @@ source instead of being bound by hand. ## Plan Decided with the maintainer on 2026-10-27: a dependency containing `unsafe` is bound by -the consumer [at first](/quest/m2/ts-model/bind.md); this follows once a consumer binds +the consumer [at first](/ts/README.md#bindings); this follows once a consumer binds enough of them to justify it. The census of moq-net's model found `bytes`, `hashlink`, `slab`, and `smallvec` bound, and kio's lock-free `log`. diff --git a/ts/Cargo.lock b/ts/Cargo.lock index b4f565afc..e16200c2a 100644 --- a/ts/Cargo.lock +++ b/ts/Cargo.lock @@ -12,6 +12,13 @@ dependencies = [ "vsimd", ] +[[package]] +name = "bind-fixture" +version = "0.0.0" +dependencies = [ + "bytes", +] + [[package]] name = "bytes" version = "1.12.1" diff --git a/ts/README.md b/ts/README.md index df72cc199..ff79614ae 100644 --- a/ts/README.md +++ b/ts/README.md @@ -26,6 +26,12 @@ roots = ["coding::Decoder", "lite::Group"] printable = ["coding::DecodeError"] # Macros whose invocations are left out, such as logging. Each must evaluate to `()`. ignore-macros = ["tracing::trace"] + +# Hand-written TypeScript that stands in for an item, by its path from the crate root or +# else from a crate it depends on: a function, or a type and its functions. +[bind] +"bytes::Bytes" = "bind/bytes.ts" +"util::TaskSet" = { file = "bind/tasks.ts", drop = true } ``` moq consumes it as the `ts` package of this repository's flake. In this repository, @@ -202,6 +208,37 @@ full type arguments. Imports name the `.ts` file, which Node, Bun, and bundlers run as is. A `tsc` build sets `rewriteRelativeImportExtensions`, or `allowImportingTsExtensions` with `noEmit`. +## Bindings + +A binding stands in for what the subset refuses, such as `unsafe` code or a boxed future. +Its file, relative to `ts.toml`, is copied to the same path in the output, beside the +runtime. It imports `rt.ts` as the output's modules do, `../rt.ts` from `bind/bytes.ts`, +and may import the translated modules and npm packages too. Callers import it whole, as a +type's module, `import * as Bytes from "./bind/bytes.ts"`. + +A bound type is the file's export of its name, `Bytes.Bytes`, with the Rust type's type +parameters. A call of a bound function, or of a function of a bound type that its own +crate defines, trait impls included, calls the file's export. The export is named as the +translated function would be, without the type's or the function's type arguments, so one +export serves every instance and a generic one is a generic TS function. `Bytes::slice` is +`Bytes.slice`, `::eq` is `Bytes.eq$PartialEq`, and +`>>::from` is `Bytes.from$From_Vec_u8`. std's blanket impls and its +traits' provided functions, such as `ToOwned` and `ne`, stay std's, as for any other type. +Translation can't see into a bound type, so a field, variant, or constructor of one is +refused, as is iterating one. + +Once the output is written, `tsc`, from `PATH`, checks each export against the translated +signature of every instance translation calls, and each bound type against the Rust type's +type parameters. A missing or mismatched binding fails the translation at the Rust item's +span, and the output is removed. + +A bound type with `drop = true` drops where Rust drops it, through its +`[Symbol.dispose]`, which `tsc` checks it has. Without it, dropping one does nothing JS +sees, and its value has no `[Symbol.dispose]`. A `&mut` to a bound type passes its JS +object, which the binding changes in place. Where the type is `Copy`, such a `&mut` is +refused, since JS shares the value where Rust copies it, and a binding never changes a +`Copy` value in place. + ## Design Output is ranked correctness first, speed second, and bundle size third: the @@ -321,8 +358,8 @@ they need. ## Never Outside the subset for good: `unsafe` and raw pointers, and trait objects other than -`&dyn Debug` and `&dyn Display`. A boundary that needs one, such as a boxed future, will -bind to hand-written TS through config ([model](/quest/m2/ts-model/README.md)). +`&dyn Debug` and `&dyn Display`. A boundary that needs one, such as a boxed future, binds +to hand-written TS ([bindings](#bindings)). ## Not yet @@ -386,6 +423,11 @@ built natively for wasm32 and run beside its translation on the same inputs by also runs the scripts natively on the host (`examples/unwind.rs`), where a panic unwinds, catching and logging each step's panic and going on, so what a panic drops must match too. +- `bind` binds `bytes::Bytes`, and a function and a type of its own that read through raw + pointers, the type dropping an `Arc` where Rust drops it, to the TS in `bind/`. Its + differential test runs them against native Rust. `wrong.toml` binds TS that doesn't match + the Rust signatures, which `tsc` refuses at each item, with the errors in `wrong.txt`. + `refused.toml` holds bindings the config can't make, with the errors in `refused.txt`. - `reject` holds constructs outside the subset; its expected errors are `expected.txt`, and its expected census is `census.md`. diff --git a/ts/fixtures/bind/Cargo.toml b/ts/fixtures/bind/Cargo.toml new file mode 100644 index 000000000..3ce1733d3 --- /dev/null +++ b/ts/fixtures/bind/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "bind-fixture" +description = "Items outside rust2-ts's subset, bound to hand-written TypeScript" +version = "0.0.0" +edition.workspace = true +license.workspace = true +publish = false + +[lib] +# The cdylib is the native side of the differential test, built for wasm32. +crate-type = ["lib", "cdylib"] + +[dependencies] +bytes = "1.12.1" + +[lints] +workspace = true diff --git a/ts/fixtures/bind/bind/bytes.ts b/ts/fixtures/bind/bind/bytes.ts new file mode 100644 index 000000000..f1d3fa3ea --- /dev/null +++ b/ts/fixtures/bind/bind/bytes.ts @@ -0,0 +1,53 @@ +// `bytes::Bytes`, which contains `unsafe`: immutable bytes that its slices and clones +// share, as a byte slice of them is. + +import * as rt from "../rt.ts"; + +export type Bytes = rt.ByteSlice; + +export function copyFromSlice(data: rt.ByteSlice): Bytes { + return rt.byteSliceFrom(rt.byteSliceView(data).slice()); +} + +export function from$From_Vec_u8(vec: rt.ByteVec): Bytes { + return rt.byteVecSlice(vec); +} + +export function len(self: Bytes): rt.usize { + return self.length; +} + +export function isEmpty(self: Bytes): boolean { + return self.length === 0; +} + +/** Over each range translated code makes: `a..b`, `a..=b`, `a..`, `..b`, and `..`. */ +export function slice( + self: Bytes, + range: rt.Range | rt.RangeInclusive | { start: rt.usize } | { end: rt.usize } | undefined, +): Bytes { + let begin = 0; + let end = self.length; + if (range instanceof rt.RangeInclusive) { + begin = range.lo; + end = rt.usizeAdd(range.hi, 1); + } else if (range !== undefined) { + if ("start" in range) begin = range.start; + if ("end" in range) end = range.end; + } + if (begin > end) rt.panic(`range start must not be greater than end: ${begin} <= ${end}`); + if (end > self.length) rt.panic(`range end out of bounds: ${end} <= ${self.length}`); + return new rt.ByteSlice(self.data, self.start + begin, end - begin); +} + +export function clone$Clone(self: Bytes): Bytes { + return self; +} + +export function deref$Deref(self: Bytes): rt.ByteSlice { + return self; +} + +export function eq$PartialEq(self: Bytes, other: Bytes): boolean { + return rt.byteSliceEq(self, other); +} diff --git a/ts/fixtures/bind/bind/checksum.ts b/ts/fixtures/bind/bind/checksum.ts new file mode 100644 index 000000000..53d1b1a45 --- /dev/null +++ b/ts/fixtures/bind/bind/checksum.ts @@ -0,0 +1,9 @@ +// `raw::checksum`, which reads through a raw pointer. + +import type * as rt from "../rt.ts"; + +export function checksum(bytes: rt.ByteSlice): rt.u32 { + let sum = 0; + for (let i = 0; i < bytes.length; i++) sum = (Math.imul(sum, 31) + bytes.data[bytes.start + i]) >>> 0; + return sum; +} diff --git a/ts/fixtures/bind/bind/tally.ts b/ts/fixtures/bind/bind/tally.ts new file mode 100644 index 000000000..dd9fc8be8 --- /dev/null +++ b/ts/fixtures/bind/bind/tally.ts @@ -0,0 +1,25 @@ +// `raw::Tally`, which holds its `Arc` by a raw pointer, and drops it where Rust drops +// the tally. + +import * as rt from "../rt.ts"; + +export class Tally implements Disposable { + readonly count: rt.Arc; + + constructor(count: rt.Arc) { + this.count = rt.arcClone(count); + } + + [Symbol.dispose](): void { + rt.arcDrop(this.count); + } +} + +function make(count: rt.Arc): Tally { + return new Tally(count); +} +export { make as new }; + +export function get(self: Tally): rt.u32 { + return self.count.value; +} diff --git a/ts/fixtures/bind/expected/bind/bytes.ts b/ts/fixtures/bind/expected/bind/bytes.ts new file mode 100644 index 000000000..7f8eff5ce --- /dev/null +++ b/ts/fixtures/bind/expected/bind/bytes.ts @@ -0,0 +1,54 @@ +// Generated by rust2-ts. Do not edit. +// `bytes::Bytes`, which contains `unsafe`: immutable bytes that its slices and clones +// share, as a byte slice of them is. + +import * as rt from "../rt.ts"; + +export type Bytes = rt.ByteSlice; + +export function copyFromSlice(data: rt.ByteSlice): Bytes { + return rt.byteSliceFrom(rt.byteSliceView(data).slice()); +} + +export function from$From_Vec_u8(vec: rt.ByteVec): Bytes { + return rt.byteVecSlice(vec); +} + +export function len(self: Bytes): rt.usize { + return self.length; +} + +export function isEmpty(self: Bytes): boolean { + return self.length === 0; +} + +/** Over each range translated code makes: `a..b`, `a..=b`, `a..`, `..b`, and `..`. */ +export function slice( + self: Bytes, + range: rt.Range | rt.RangeInclusive | { start: rt.usize } | { end: rt.usize } | undefined, +): Bytes { + let begin = 0; + let end = self.length; + if (range instanceof rt.RangeInclusive) { + begin = range.lo; + end = rt.usizeAdd(range.hi, 1); + } else if (range !== undefined) { + if ("start" in range) begin = range.start; + if ("end" in range) end = range.end; + } + if (begin > end) rt.panic(`range start must not be greater than end: ${begin} <= ${end}`); + if (end > self.length) rt.panic(`range end out of bounds: ${end} <= ${self.length}`); + return new rt.ByteSlice(self.data, self.start + begin, end - begin); +} + +export function clone$Clone(self: Bytes): Bytes { + return self; +} + +export function deref$Deref(self: Bytes): rt.ByteSlice { + return self; +} + +export function eq$PartialEq(self: Bytes, other: Bytes): boolean { + return rt.byteSliceEq(self, other); +} diff --git a/ts/fixtures/bind/expected/bind/checksum.ts b/ts/fixtures/bind/expected/bind/checksum.ts new file mode 100644 index 000000000..d683fc9c9 --- /dev/null +++ b/ts/fixtures/bind/expected/bind/checksum.ts @@ -0,0 +1,10 @@ +// Generated by rust2-ts. Do not edit. +// `raw::checksum`, which reads through a raw pointer. + +import type * as rt from "../rt.ts"; + +export function checksum(bytes: rt.ByteSlice): rt.u32 { + let sum = 0; + for (let i = 0; i < bytes.length; i++) sum = (Math.imul(sum, 31) + bytes.data[bytes.start + i]) >>> 0; + return sum; +} diff --git a/ts/fixtures/bind/expected/bind/tally.ts b/ts/fixtures/bind/expected/bind/tally.ts new file mode 100644 index 000000000..007d0ed42 --- /dev/null +++ b/ts/fixtures/bind/expected/bind/tally.ts @@ -0,0 +1,26 @@ +// Generated by rust2-ts. Do not edit. +// `raw::Tally`, which holds its `Arc` by a raw pointer, and drops it where Rust drops +// the tally. + +import * as rt from "../rt.ts"; + +export class Tally implements Disposable { + readonly count: rt.Arc; + + constructor(count: rt.Arc) { + this.count = rt.arcClone(count); + } + + [Symbol.dispose](): void { + rt.arcDrop(this.count); + } +} + +function make(count: rt.Arc): Tally { + return new Tally(count); +} +export { make as new }; + +export function get(self: Tally): rt.u32 { + return self.count.value; +} diff --git a/ts/fixtures/bind/expected/harness.ts b/ts/fixtures/bind/expected/harness.ts new file mode 100644 index 000000000..6a8c82b93 --- /dev/null +++ b/ts/fixtures/bind/expected/harness.ts @@ -0,0 +1,123 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import * as Bytes from "./bind/bytes.ts"; +import * as checksum from "./bind/checksum.ts"; +import * as Tally from "./bind/tally.ts"; + +/** + * Split the input as `Bytes`, compare and hash the parts, and count tallies down as they + * drop. A first byte of 1 slices out of order, which panics. + */ +export function run(input: rt.ByteSlice): rt.ByteVec { + let count!: rt.Arc; + let a!: Tally.Tally; + let b!: Tally.Tally; + let held!: Tally.Tally | undefined; + let _120!: boolean; + let _121!: boolean; + let _122!: boolean; + let unwind: number = 0; + try { + _122 = false; + _121 = false; + _120 = false; + const out: rt.ByteVec = new rt.ByteVec(); + const bytes: Bytes.Bytes = Bytes.copyFromSlice(input); + const mid: rt.usize = rt.usizeDiv(Bytes.len(bytes), 2); + const head: Bytes.Bytes = Bytes.slice(bytes, { end: mid }); + const tail: Bytes.Bytes = Bytes.slice(bytes, { start: mid }); + const _16: rt.Range = new rt.Range(rt.min(1, mid), mid); + const inner: Bytes.Bytes = Bytes.slice(bytes, _16); + const _20: rt.u8 = Bytes.len(head) & 0xff; + rt.byteVecPush(out, _20); + const _25: rt.u8 = Bytes.len(inner) & 0xff; + rt.byteVecPush(out, _25); + const _30: rt.ByteSlice = Bytes.deref$Deref(tail); + rt.byteVecExtendFromSlice(out, _30); + const _34: rt.u8 = Bytes.eq$PartialEq(head, tail) ? 1 : 0; + rt.byteVecPush(out, _34); + const _40: rt.u8 = Bytes.eq$PartialEq(Bytes.clone$Clone(head), head) ? 1 : 0; + rt.byteVecPush(out, _40); + const _48: rt.u8 = Bytes.eq$PartialEq(Bytes.from$From_Vec_u8(rt.byteSliceToByteVec(input)), bytes) ? 1 : 0; + rt.byteVecPush(out, _48); + const _56: rt.u8 = Bytes.isEmpty(bytes) ? 1 : 0; + rt.byteVecPush(out, _56); + const sum: rt.u32 = checksum.checksum(Bytes.deref$Deref(tail)); + const iter: rt.Iter = rt.iterSlice([0, 8, 16, 24]); + while (true) { + const _65: rt.i32 | rt.Done = iter.next(); + if (_65 === rt.DONE) { + break; + } + rt.byteVecPush(out, rt.u32Shr(sum, _65) & 0xff); + } + if (rt.byteSliceFirst(input) === 1) { + const _81: rt.Range = new rt.Range(mid, rt.usizeSaturatingSub(mid, 1)); + Bytes.slice(bytes, _81); + } + count = new rt.Arc(input.length as rt.u32); + unwind = 67; + a = Tally.new(count); + unwind = 82; + _122 = true; + b = Tally.new(count); + unwind = 80; + _121 = true; + rt.byteVecPush(out, count.strong & 0xff); + _122 = false; + rt.drop(a); + rt.byteVecPush(out, count.strong & 0xff); + const _104: rt.u8 = Tally.get(b) & 0xff; + rt.byteVecPush(out, _104); + unwind = 78; + _121 = false; + const _108: Tally.Tally = b; + _120 = true; + held = _108; + if (rt.byteSliceFirst(input) === 2) { + _120 = false; + rt.drop(held); + } + rt.byteVecPush(out, count.strong & 0xff); + const _0: rt.ByteVec = out; + if (_120) { + unwind = 80; + rt.drop(held); + } + unwind = 0; + _120 = false; + _121 = false; + _122 = false; + rt.drop(count); + return _0; + } catch (e) { + b5: { + b6: { + if (unwind === 0) { + throw e; + } + if (unwind === 67) { + break b5; + } + if (unwind === 78) { + if (_120) { + rt.drop(held); + } + } else if (unwind !== 80) { + break b6; + } + if (_121) { + rt.drop(b); + } + } + if (_122) { + rt.drop(a); + } + } + rt.drop(count); + throw e; + } +} + +//# sourceMappingURL=harness.ts.map diff --git a/ts/fixtures/bind/refused.toml b/ts/fixtures/bind/refused.toml new file mode 100644 index 000000000..e348ce5f0 --- /dev/null +++ b/ts/fixtures/bind/refused.toml @@ -0,0 +1,15 @@ +# Bindings `ts.toml` can't make: `refused.txt` holds the errors. +package = "bind-fixture" +roots = ["harness::run"] + +[bind] +"nowhere::Thing" = "bind/thing.ts" +"std::vec::Vec" = "bind/vec.ts" +"raw" = "bind/raw.ts" +"bytes::Buf" = "bind/buf.ts" +"raw::Tally::get" = { file = "bind/get.ts", drop = true } +"raw::Tally::new" = "rt/tally.ts" +"bytes::BytesMut" = "../bytes.ts" +"bytes::Bytes" = "bind/bytes.ts" +"raw::Tally" = "bind/bytes.ts" +"raw::checksum" = "bind/missing.ts" diff --git a/ts/fixtures/bind/refused.txt b/ts/fixtures/bind/refused.txt new file mode 100644 index 000000000..534ad0614 --- /dev/null +++ b/ts/fixtures/bind/refused.txt @@ -0,0 +1,13 @@ +binding `bytes::Buf`: a binding is of a function or a type, not Trait +binding `bytes::BytesMut`: ../bytes.ts must be relative to ts.toml, inside its directory +binding `nowhere::Thing`: no such item in the crate or a crate it depends on +binding `raw`: a binding is of a function or a type, not Mod +binding `raw::Tally`: bind/bytes.ts binds another type +binding `raw::Tally::get`: only a type's value drops +binding `raw::Tally::new`: rt/tally.ts would replace the runtime, rt.ts and rt/ +binding `std::vec::Vec`: std's type has a representation of its own +reading bind/missing.ts: No such file or directory (os error 2) at fixtures/bind/src/harness.rs:25:12 +unsupported type `*const u32` at fixtures/bind/src/raw.rs:27:2 +unsupported call to `std::sync::Arc::::from_raw` at fixtures/bind/src/raw.rs:46:17 +unsupported call to `std::sync::Arc::::into_raw` at fixtures/bind/src/raw.rs:33:11 +a raw pointer dereferenced at fixtures/bind/src/raw.rs:39:12 diff --git a/ts/fixtures/bind/src/harness.rs b/ts/fixtures/bind/src/harness.rs new file mode 100644 index 000000000..6548a448a --- /dev/null +++ b/ts/fixtures/bind/src/harness.rs @@ -0,0 +1,46 @@ +//! The entry point both sides of the differential test run. + +use std::sync::Arc; + +use bytes::Bytes; + +use crate::raw::{Tally, checksum}; + +/// Split the input as `Bytes`, compare and hash the parts, and count tallies down as they +/// drop. A first byte of 1 slices out of order, which panics. +pub fn run(input: &[u8]) -> Vec { + let mut out = Vec::new(); + let bytes = Bytes::copy_from_slice(input); + let mid = bytes.len() / 2; + let head = bytes.slice(..mid); + let tail = bytes.slice(mid..); + let inner = bytes.slice(1.min(mid)..mid); + out.push(head.len() as u8); + out.push(inner.len() as u8); + out.extend_from_slice(&tail); + out.push((head == tail) as u8); + out.push((head.clone() == head) as u8); + out.push((Bytes::from(input.to_vec()) == bytes) as u8); + out.push(bytes.is_empty() as u8); + let sum = checksum(&tail); + for shift in [0, 8, 16, 24] { + out.push((sum >> shift) as u8); + } + if input.first() == Some(&1) { + let _ = bytes.slice(mid..mid.saturating_sub(1)); + } + + let count = Arc::new(input.len() as u32); + let a = Tally::new(&count); + let b = Tally::new(&count); + out.push(Arc::strong_count(&count) as u8); + drop(a); + out.push(Arc::strong_count(&count) as u8); + out.push(b.get() as u8); + let held = Some(b); + if input.first() == Some(&2) { + drop(held); + } + out.push(Arc::strong_count(&count) as u8); + out +} diff --git a/ts/fixtures/bind/src/lib.rs b/ts/fixtures/bind/src/lib.rs new file mode 100644 index 000000000..cec347048 --- /dev/null +++ b/ts/fixtures/bind/src/lib.rs @@ -0,0 +1,13 @@ +//! Items outside the subset, bound to hand-written TypeScript: `bytes::Bytes`, which +//! contains `unsafe`, and the crate's own function and type that read through raw +//! pointers, one of them dropping where Rust drops it. +//! +//! `harness::run` is the entry point both sides of the differential test run. + +pub mod harness; +pub mod raw; +#[cfg(target_arch = "wasm32")] +#[path = "../../wasm.rs"] +mod wasm; +#[cfg(target_arch = "wasm32")] +use harness::run as entry; diff --git a/ts/fixtures/bind/src/raw.rs b/ts/fixtures/bind/src/raw.rs new file mode 100644 index 000000000..0a7f309ea --- /dev/null +++ b/ts/fixtures/bind/src/raw.rs @@ -0,0 +1,48 @@ +//! What the crate does through raw pointers, which `bind/` stands in for. + +use std::sync::Arc; + +/// A module of the function's name, as `rand::rng` has, which the binding's path must not +/// resolve to. +mod checksum { + pub(super) fn step(sum: u32, byte: u8) -> u32 { + sum.wrapping_mul(31).wrapping_add(byte as u32) + } +} + +/// A hash of the bytes, read through a raw pointer. +pub fn checksum(bytes: &[u8]) -> u32 { + let ptr = bytes.as_ptr(); + let mut sum = 0u32; + for i in 0..bytes.len() { + // SAFETY: `i` is in bounds. + let byte = unsafe { *ptr.add(i) }; + sum = checksum::step(sum, byte); + } + sum +} + +/// A count held by a raw pointer to its `Arc`, which drops it where Rust drops the tally. +pub struct Tally { + count: *const u32, +} + +impl Tally { + pub fn new(count: &Arc) -> Self { + Self { + count: Arc::into_raw(count.clone()), + } + } + + pub fn get(&self) -> u32 { + // SAFETY: the tally holds the `Arc` it points into. + unsafe { *self.count } + } +} + +impl Drop for Tally { + fn drop(&mut self) { + // SAFETY: `new` made the pointer with `Arc::into_raw`, once. + drop(unsafe { Arc::from_raw(self.count) }); + } +} diff --git a/ts/fixtures/bind/ts.toml b/ts/fixtures/bind/ts.toml new file mode 100644 index 000000000..6a2d07c53 --- /dev/null +++ b/ts/fixtures/bind/ts.toml @@ -0,0 +1,7 @@ +package = "bind-fixture" +roots = ["harness::run"] + +[bind] +"bytes::Bytes" = "bind/bytes.ts" +"raw::checksum" = "bind/checksum.ts" +"raw::Tally" = { file = "bind/tally.ts", drop = true } diff --git a/ts/fixtures/bind/wrong.toml b/ts/fixtures/bind/wrong.toml new file mode 100644 index 000000000..99460b41e --- /dev/null +++ b/ts/fixtures/bind/wrong.toml @@ -0,0 +1,9 @@ +# Bindings that don't match what translated code takes them for, each refused at its Rust +# item: `wrong.txt` holds the errors. +package = "bind-fixture" +roots = ["harness::run"] + +[bind] +"bytes::Bytes" = "wrong/bytes.ts" +"raw::checksum" = "bind/checksum.ts" +"raw::Tally" = { file = "wrong/tally.ts", drop = true } diff --git a/ts/fixtures/bind/wrong.txt b/ts/fixtures/bind/wrong.txt new file mode 100644 index 000000000..63a11d4ac --- /dev/null +++ b/ts/fixtures/bind/wrong.txt @@ -0,0 +1,8 @@ +the binding of `bytes::Bytes::len` in wrong/bytes.ts doesn't type-check: error TS2322: Type '(self: ByteSlice) => string' is not assignable to type '(a0: ByteSlice) => usize'. + Type 'string' is not assignable to type 'usize'. + Type 'string' is not assignable to type 'number'. at bytes-1.12.1/src/bytes.rs:303:5 +the binding of `bytes::Bytes::slice::>` in wrong/bytes.ts doesn't type-check: error TS2339: Property 'slice' does not exist on type 'typeof import("wrong/bytes")'. at bytes-1.12.1/src/bytes.rs:373:5 +the binding of `::eq` in wrong/bytes.ts doesn't type-check: error TS2322: Type '(self: ByteSlice, other: string) => boolean' is not assignable to type '(a0: ByteSlice, a1: ByteSlice) => boolean'. + Types of parameters 'other' and 'a1' are incompatible. + Type 'ByteSlice' is not assignable to type 'string'. at bytes-1.12.1/src/bytes.rs:769:5 +the binding of `raw::Tally` in wrong/tally.ts doesn't type-check: error TS2741: Property '[Symbol.dispose]' is missing in type 'Tally' but required in type 'Disposable'. at fixtures/bind/src/raw.rs:26:1 diff --git a/ts/fixtures/bind/wrong/bytes.ts b/ts/fixtures/bind/wrong/bytes.ts new file mode 100644 index 000000000..030964d95 --- /dev/null +++ b/ts/fixtures/bind/wrong/bytes.ts @@ -0,0 +1,34 @@ +// `bytes::Bytes`, bound wrong: `slice` is missing, `len` returns a string, and `eq` takes +// what no `Bytes` is. + +import * as rt from "../rt.ts"; + +export type Bytes = rt.ByteSlice; + +export function copyFromSlice(data: rt.ByteSlice): Bytes { + return data; +} + +export function from$From_Vec_u8(vec: rt.ByteVec): Bytes { + return rt.byteVecSlice(vec); +} + +export function len(self: Bytes): string { + return `${self.length}`; +} + +export function isEmpty(self: Bytes): boolean { + return self.length === 0; +} + +export function clone$Clone(self: Bytes): Bytes { + return self; +} + +export function deref$Deref(self: Bytes): rt.ByteSlice { + return self; +} + +export function eq$PartialEq(self: Bytes, other: string): boolean { + return self.length === other.length; +} diff --git a/ts/fixtures/bind/wrong/tally.ts b/ts/fixtures/bind/wrong/tally.ts new file mode 100644 index 000000000..e036fb5ba --- /dev/null +++ b/ts/fixtures/bind/wrong/tally.ts @@ -0,0 +1,16 @@ +// `raw::Tally`, bound wrong: it drops, but has no `[Symbol.dispose]`. + +import type * as rt from "../rt.ts"; + +export interface Tally { + count: rt.Arc; +} + +function make(count: rt.Arc): Tally { + return { count }; +} +export { make as new }; + +export function get(self: Tally): rt.u32 { + return self.count.value; +} diff --git a/ts/src/config.rs b/ts/src/config.rs index f5f65437e..5330c9bab 100644 --- a/ts/src/config.rs +++ b/ts/src/config.rs @@ -1,6 +1,7 @@ //! The `ts.toml` that says what to translate. -use std::path::Path; +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; use serde::Deserialize; @@ -21,12 +22,57 @@ pub struct Config { /// evaluate to `()`. #[serde(default)] pub ignore_macros: Vec, + /// Hand-written TypeScript that stands in for a Rust item, by the item's path: from + /// the crate root, or else from a crate it depends on. + #[serde(default)] + pub bind: BTreeMap, + /// The directory `ts.toml` is in, which binding files are relative to. + #[serde(skip)] + pub dir: PathBuf, +} + +/// The TypeScript file that stands in for an item. +#[derive(Debug, Deserialize)] +#[serde(from = "BindToml")] +pub struct Bind { + /// Relative to `ts.toml`, and copied to the same path in the output. + pub file: PathBuf, + /// Whether a bound type's value drops through its `[Symbol.dispose]` where Rust drops + /// it. Without it, dropping one does nothing JS sees. + pub drop: bool, +} + +/// A binding as written: its file alone, or a table. +#[derive(Deserialize)] +#[serde(untagged)] +enum BindToml { + File(PathBuf), + Table(BindTable), +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct BindTable { + file: PathBuf, + #[serde(default)] + drop: bool, +} + +impl From for Bind { + fn from(bind: BindToml) -> Self { + match bind { + BindToml::File(file) => Self { file, drop: false }, + BindToml::Table(BindTable { file, drop }) => Self { file, drop }, + } + } } impl Config { pub fn load(path: &Path) -> Result { let text = std::fs::read_to_string(path).map_err(|err| format!("reading {}: {err}", path.display()))?; - toml::from_str(&text).map_err(|err| format!("parsing {}: {err}", path.display())) + let mut config: Self = toml::from_str(&text).map_err(|err| format!("parsing {}: {err}", path.display()))?; + config.dir = path.parent().unwrap_or(Path::new("")).to_path_buf(); + Ok(config) } } diff --git a/ts/src/driver.rs b/ts/src/driver.rs index 3269071f0..25017a190 100644 --- a/ts/src/driver.rs +++ b/ts/src/driver.rs @@ -78,7 +78,7 @@ impl Callbacks for Translate { // Only a program rustc accepts is translated. let written = match &self.output { Output::Dir(out) => match lower::translate(tcx, &self.config) { - Some(program) if tcx.dcx().has_errors().is_none() => program.write(out), + Some(program) if tcx.dcx().has_errors().is_none() => program.write(out).map(|()| lower::typecheck(tcx, &program, out)), _ => Ok(()), }, Output::Census(path) => match lower::census(tcx, &self.config) { diff --git a/ts/src/lower/bind.rs b/ts/src/lower/bind.rs new file mode 100644 index 000000000..35c36adc3 --- /dev/null +++ b/ts/src/lower/bind.rs @@ -0,0 +1,547 @@ +//! Bindings: hand-written TypeScript that `ts.toml` names in place of a Rust function or +//! type. A call of a bound function, or of a function of a bound type that the type's crate +//! defines, trait impls included, calls the binding file's export named for the Rust +//! function: one export serves every instance, so a generic one is a generic TS function. +//! `tsc` checks each export against every instance's translated signature once the output +//! is written. + +use std::path::{Component, Path, PathBuf}; + +use rustc_hir::def::{DefKind, Res}; +use rustc_hir::def_id::{CRATE_DEF_ID, DefId}; +use rustc_middle::ty::{self, Instance, InstanceKind, TyCtxt}; +use rustc_span::Span; + +use super::{Translator, camel, short_name}; +use crate::program::{Bound, Check, ModPath, Program, SymKind, Symbol, module_file}; +use crate::ts::{Expr, Function, Item, Param, StmtKind, SymId, Type}; + +/// An item `ts.toml` binds. +struct Binding { + /// Its path, as `ts.toml` writes it. + path: String, + /// The module of its file, which is that file's path in the output. + module: ModPath, + kind: Kind, +} + +#[derive(Clone, Copy, PartialEq, Eq)] +enum Kind { + Function, + /// A type, and whether its value drops through its `[Symbol.dispose]`. + Type { drop: bool }, +} + +/// A binding's file. +struct File { + /// Relative to `ts.toml`. + path: PathBuf, + /// What its importers call it: the type bound to it, or its file's name. + namespace: String, +} + +/// What the translation knows of the config's bindings. +#[derive(Default)] +pub struct Bindings<'tcx> { + items: rustc_data_structures::fx::FxHashMap, + files: rustc_data_structures::fx::FxHashMap, + /// The symbol of each bound type, and of each export, by its module and name. + types: rustc_data_structures::fx::FxHashMap, + exports: rustc_data_structures::fx::FxHashMap<(ModPath, String), (DefId, SymId)>, + /// The instances checked against their export. + checked: rustc_data_structures::fx::FxHashSet>, +} + +impl<'a, 'tcx> Translator<'a, 'tcx> { + /// Resolve every binding the config names, reporting each it can't. + pub(super) fn bind(&mut self) { + let tcx = self.tcx; + let config = self.config; + for (path, bind) in &config.bind { + let fail = |t: &mut Self, msg: String| { + t.tcx.dcx().err(format!("rust2-ts: binding `{path}`: {msg}")); + t.failed = true; + }; + let Some(def_id) = self.resolve_bound(path) else { + fail(self, "no such item in the crate or a crate it depends on".into()); + continue; + }; + let kind = match tcx.def_kind(def_id) { + DefKind::Fn => Kind::Function, + DefKind::AssocFn if tcx.trait_impl_of_assoc(def_id).is_none() && tcx.impl_of_assoc(def_id).is_some() => { + Kind::Function + } + DefKind::Struct | DefKind::Enum | DefKind::Union => { + if super::library::Std::of(tcx, def_id).is_some() { + fail(self, "std's type has a representation of its own".into()); + continue; + } + Kind::Type { drop: bind.drop } + } + kind => { + fail(self, format!("a binding is of a function or a type, not {kind:?}")); + continue; + } + }; + if bind.drop && kind == Kind::Function { + fail(self, "only a type's value drops".into()); + continue; + } + let module = match module_of(&bind.file) { + Ok(module) => module, + Err(msg) => { + fail(self, msg); + continue; + } + }; + let name = tcx.item_name(def_id).to_string(); + let file = self.bindings.files.entry(module.clone()).or_insert_with(|| File { + path: bind.file.clone(), + namespace: ident(module.last().expect("a file name")), + }); + if let Kind::Type { .. } = kind { + let types = self + .bindings + .items + .values() + .filter(|b| b.module == module && b.kind != Kind::Function) + .count(); + if types > 0 { + fail(self, format!("{} binds another type", bind.file.display())); + continue; + } + file.namespace = name; + } + self.bindings.items.insert( + def_id, + Binding { + path: path.clone(), + module, + kind, + }, + ); + } + } + + /// The item a binding's path names: from the crate root, or else from a crate the crate + /// depends on, through modules and then a type's inherent functions. + fn resolve_bound(&self, path: &str) -> Option { + let segments: Vec<&str> = path.split("::").collect(); + if let Some(def_id) = resolve_in(self.tcx, CRATE_DEF_ID.to_def_id(), &segments) { + return Some(def_id); + } + let (name, rest) = segments.split_first()?; + let tcx = self.tcx; + let named: Vec<_> = tcx + .crates(()) + .iter() + .copied() + .filter(|&cnum| tcx.crate_name(cnum).as_str() == *name) + .collect(); + // The one the crate names, where several versions are built. + let cnum = match named[..] { + [cnum] => cnum, + _ => *named + .iter() + .find(|&&cnum| tcx.extern_crate(cnum).is_some_and(|e| e.is_direct()))?, + }; + resolve_in(tcx, cnum.as_def_id(), rest) + } + + /// Whether `def_id` is a bound type or function, or a function of a bound type in an + /// impl of the type's own crate: translation never reads it. + pub fn bound(&self, def_id: DefId) -> bool { + let tcx = self.tcx; + if self.bindings.items.contains_key(&def_id) { + return true; + } + let Some(imp) = tcx.impl_of_assoc(def_id) else { + return false; + }; + let self_ty = tcx.type_of(imp).instantiate_identity().skip_norm_wip(); + matches!(self_ty.peel_refs().kind(), ty::Adt(adt, _) + if adt.did().krate == def_id.krate && self.bound_drops(adt.did()).is_some()) + } + + /// Whether `def_id` is the crate's own, which it translates, rather than bound. + pub fn own(&self, def_id: DefId) -> bool { + def_id.is_local() && !self.bindings.items.contains_key(&def_id) + } + + /// Whether the trait impls of `def_id`, a type, are called as they are: the crate's + /// own, or a binding's exports. + pub fn own_or_bound(&self, def_id: DefId) -> bool { + def_id.is_local() || self.bindings.items.contains_key(&def_id) + } + + /// Whether a bound type's value drops through its `[Symbol.dispose]`, if it is one. + pub fn bound_drops(&self, def_id: DefId) -> Option { + match self.bindings.items.get(&def_id)?.kind { + Kind::Type { drop } => Some(drop), + Kind::Function => None, + } + } + + /// The symbol of a bound type, from its binding's file. + pub fn bound_type(&mut self, def_id: DefId, span: Span) -> Option { + self.bound_drops(def_id)?; + if let Some(&sym) = self.bindings.types.get(&def_id) { + return Some(sym); + } + let module = self.bindings.items[&def_id].module.clone(); + let name = self.tcx.item_name(def_id).to_string(); + let sym = self.bound_sym(&module, name, SymKind::Struct, span); + self.bindings.types.insert(def_id, sym); + // It takes as many type parameters as the Rust type. + let params = self.tcx.generics_of(def_id).own_counts().types; + let value = Param { + name: "value".into(), + ty: Some(Type::Sym(sym, vec![Type::Builtin("unknown"); params])), + }; + let (ret, body) = match self.bound_drops(def_id) { + Some(true) => ( + Type::Builtin("Disposable"), + vec![StmtKind::Return(Some(Expr::ident("value"))).at(None)], + ), + _ => (Type::Builtin("void"), Vec::new()), + }; + let what = format!("the binding of `{}` in {}", self.bindings.items[&def_id].path, module_file(&module).display()); + self.check(vec![value], ret, body, self.tcx.def_span(def_id), what); + Some(sym) + } + + /// The export a call of `instance` calls, if a binding stands in for it, checked + /// against the instance's signature. `span` is where it is reached. + pub fn bound_fn(&mut self, instance: Instance<'tcx>, span: Span) -> Option { + let tcx = self.tcx; + let module = self.bound_module(instance)?.clone(); + let def_id = instance.def_id(); + let name = export_name(tcx, instance); + let sym = match self.bindings.exports.get(&(module.clone(), name.clone())) { + Some(&(other, sym)) if other == def_id => sym, + Some(&(other, _)) => { + self.err( + tcx.def_span(def_id), + format!( + "`{}` and `{}` would both be the export `{name}` of {}", + tcx.def_path_str(def_id), + tcx.def_path_str(other), + module_file(&module).display() + ), + ); + return None; + } + None => { + let sym = self.bound_sym(&module, name.clone(), SymKind::Function, span); + self.bindings.exports.insert((module.clone(), name), (def_id, sym)); + sym + } + }; + if self.bindings.checked.insert(instance) { + if crate::lower::iter::is_next(tcx, def_id) { + self.err(span, "iterating a bound type, which is not supported yet"); + } + let sig = tcx.instantiate_bound_regions_with_erased( + tcx.fn_sig(def_id).instantiate(tcx, instance.args).skip_norm_wip(), + ); + let sig = tcx.normalize_erasing_regions(self.typing_env_pub(), ty::Unnormalized::new_wip(sig)); + let ty = self.fn_ty(sig.inputs(), sig.output(), span); + let body = vec![StmtKind::Return(Some(Expr::Sym(sym))).at(None)]; + let what = ty::print::with_no_trimmed_paths!(format!("the binding of `{instance}` in {}", module_file(&module).display())); + self.check(Vec::new(), ty, body, tcx.def_span(def_id), what); + } + Some(sym) + } + + /// The module of the binding that stands in for `instance`, if one does. + pub fn bound_module(&self, instance: Instance<'tcx>) -> Option<&ModPath> { + let InstanceKind::Item(def_id) = instance.def else { + return None; + }; + if let Some(binding) = self.bindings.items.get(&def_id) { + return Some(&binding.module); + } + let ty::Adt(adt, _) = self.self_ty(instance)?.peel_refs().kind() else { + return None; + }; + // One its own crate defines: std's blanket impls and its traits' provided functions, + // such as `ToOwned` and `ne`, are std's, as for any other type. + if def_id.krate != adt.did().krate { + return None; + } + self.bound_drops(adt.did())?; + Some(&self.bindings.items[&adt.did()].module) + } + + /// A symbol of a binding's file, named as the file exports it, which reaches the file. + fn bound_sym(&mut self, module: &ModPath, name: String, kind: SymKind, span: Span) -> SymId { + if !self.program.bound.contains_key(module) { + let file = &self.bindings.files[module]; + // A census writes nothing, so it reads no file. + let text = match &self.refusals { + Some(_) => Ok(String::new()), + None => std::fs::read_to_string(self.config.dir.join(&file.path)), + }; + let namespace = file.namespace.clone(); + let text = match text { + Ok(text) => text, + Err(err) => { + let msg = format!("reading {}: {err}", file.path.display()); + self.err(span, msg); + String::new() + } + }; + self.program.bound.insert(module.clone(), Bound { text, namespace }); + } + self.program.sym(Symbol { + module: module.clone(), + base: name.clone(), + suffix: String::new(), + owner: None, + kind, + order: 0, + instance: String::new(), + name, + alias: None, + }) + } + + /// A function of the check module, which type-checks only if the binding it reads is + /// what translated code takes it for. + fn check(&mut self, params: Vec, ret: Type, body: Vec, span: Span, what: String) { + let sym = self.program.sym(Symbol { + module: check_module(), + base: "check".into(), + suffix: self.program.checks.len().to_string(), + owner: None, + kind: SymKind::Function, + order: self.program.checks.len() as u32, + instance: String::new(), + name: String::new(), + alias: None, + }); + self.program.checks.push(Check { + item: Item::Function(Function { + sym, + params, + ret, + body, + loc: None, + doc: None, + }), + span, + what, + }); + } +} + +/// The project `tsc` checks the check module in, beside it: the runtime's own settings, as +/// `runtime/tsconfig.json` has them, but for the unused names a binding may leave, which +/// are its author's to lint. A project of its own, since `tsc` refuses to check a file it +/// names where a caller's `tsconfig.json` is above it. +const TSCONFIG: &str = r#"{ + "compilerOptions": { + "strict": true, + "noEmit": true, + "pretty": false, + "target": "es2022", + "lib": ["es2022", "esnext.disposable", "dom"], + "module": "esnext", + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "erasableSyntaxOnly": true + }, + "files": ["bind.ts"] +} +"#; + +/// Type-check the bindings the translation in `out` reaches against what it takes each +/// for, reporting each that fails at its Rust item, and then removing the translation. +pub fn typecheck(tcx: TyCtxt<'_>, program: &Program, out: &Path) { + if program.checks.is_empty() { + return; + } + let module = check_module(); + let path = out.join(module_file(&module)); + let failed = |msg: String| { + tcx.dcx().err(format!("rust2-ts: {msg}")); + _ = crate::program::clean(out); + }; + let items: Vec<&Item> = program.checks.iter().map(|check| &check.item).collect(); + let printed = match crate::print::module(program, &module, &items, &path) { + Ok(printed) => printed, + Err(err) => return failed(err), + }; + let project = path.with_extension("json"); + if let Err(err) = crate::program::write_file(&path, &printed.code) + .and_then(|()| crate::program::write_file(&project, TSCONFIG)) + { + return failed(err); + } + let name = path.file_name().expect("a file name").to_string_lossy().into_owned(); + let output = std::process::Command::new("tsc") + .arg("-p") + .arg(&project) + .current_dir(path.parent().expect("the runtime's directory")) + .output(); + _ = std::fs::remove_file(&path); + _ = std::fs::remove_file(&project); + let output = match output { + Ok(output) => output, + Err(err) => return failed(format!("running tsc, which checks the bindings: {err}")), + }; + if output.status.success() { + return; + } + // The check each line of the module is in, by the function it starts at. + let mut starts = Vec::new(); + for (line, text) in printed.code.lines().enumerate() { + if let Some(rest) = text.split("function check$").nth(1) + && let Some(index) = rest.split('(').next().and_then(|n| n.parse::().ok()) + { + starts.push((line + 1, index)); + } + } + let text = String::from_utf8_lossy(&output.stdout); + let prefix = format!("{name}("); + // Where the output is, which a message naming a module spells out. + let dir = format!("{}/", out.display()); + // Each item's instances of a generic function fail alike, so each failure is reported once. + let mut reported = std::collections::HashSet::new(); + for diagnostic in diagnostics(&text) { + let diagnostic = diagnostic.replace(&dir, ""); + let check = diagnostic + .strip_prefix(&prefix) + .and_then(|rest| rest.split([',', ')']).next()) + .and_then(|line| line.parse::().ok()) + .and_then(|line| starts.iter().rev().find(|&&(start, _)| start <= line)) + .map(|&(_, index)| &program.checks[index]); + let msg = diagnostic.split_once(": ").map_or(diagnostic.as_str(), |(_, msg)| msg); + if !reported.insert((check.map(|check| check.span), msg.to_string())) { + continue; + } + match check { + Some(check) => _ = tcx.dcx().span_err(check.span, format!("rust2-ts: {} doesn't type-check: {msg}", check.what)), + None => _ = tcx.dcx().err(format!("rust2-ts: tsc: {diagnostic}")), + } + } + if reported.is_empty() { + tcx.dcx().err(format!("rust2-ts: tsc failed: {text}{}", String::from_utf8_lossy(&output.stderr))); + } + _ = crate::program::clean(out); +} + +/// Each of `tsc`'s diagnostics, with the lines that go on with it. +fn diagnostics(text: &str) -> Vec { + let mut all: Vec = Vec::new(); + for line in text.lines() { + match all.last_mut() { + Some(last) if line.starts_with(char::is_whitespace) => { + last.push('\n'); + last.push_str(line); + } + _ if line.trim().is_empty() => {} + _ => all.push(line.to_string()), + } + } + all +} + +/// The module `tsc` checks the bindings in, which no Rust module or binding can be: the +/// runtime's directory holds only the runtime. +pub fn check_module() -> ModPath { + vec!["rt".into(), "bind".into()] +} + +/// The export standing in for `instance`'s function: its name, then the trait it +/// implements with the trait's type arguments as the impl names them, as a translated +/// function's suffix has them, but not the type arguments of the type or the function, +/// which a generic TS function takes. `Bytes::slice` is `slice`, and `Bytes`'s +/// `PartialEq<[u8]>` is `eq$PartialEq_u8s`. +fn export_name<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> String { + let def_id = instance.def_id(); + let base = camel(tcx.item_name(def_id).as_str()); + let trait_ref = match tcx.trait_impl_of_assoc(def_id) { + Some(imp) => Some(tcx.impl_trait_ref(imp).instantiate_identity().skip_norm_wip()), + // A trait's own method, which the impl did not override. + None => tcx + .trait_of_assoc(def_id) + .map(|trait_id| ty::TraitRef::from_assoc(tcx, trait_id, instance.args)), + }; + let Some(trait_ref) = trait_ref else { + return base; + }; + let mut parts = vec![tcx.item_name(trait_ref.def_id).to_string()]; + let args = tcx + .generics_of(trait_ref.def_id) + .own_args_no_defaults(tcx, trait_ref.args); + parts.extend(args.iter().filter_map(|arg| arg.as_type()).map(|t| short_name(tcx, t))); + format!("{base}${}", parts.join("_")) +} + +/// The item `segments` name under `module`, through modules, and then a type's inherent +/// functions. +pub fn resolve_in(tcx: TyCtxt<'_>, mut item: DefId, segments: &[&str]) -> Option { + for (i, segment) in segments.iter().enumerate() { + item = match tcx.def_kind(item) { + DefKind::Mod => { + let children = match item.as_local() { + Some(local) => tcx.module_children_local(local), + None => tcx.module_children(item), + }; + let named = children.iter().filter_map(|child| match child.res { + // A tuple struct's constructor shares its name. + Res::Def(kind, def_id) if child.ident.as_str() == *segment && !matches!(kind, DefKind::Ctor(..)) => { + Some((kind, def_id)) + } + _ => None, + }); + // A function may share its name with a module, as `rand::rng` does: the path + // goes on through the module, or ends at the function. + let last = i + 1 == segments.len(); + let (first, rest): (Vec<_>, Vec<_>) = named.partition(|&(kind, _)| (kind == DefKind::Mod) != last); + first.into_iter().chain(rest).next()?.1 + } + DefKind::Struct | DefKind::Enum | DefKind::Union => tcx.inherent_impls(item).iter().find_map(|&imp| { + tcx.associated_items(imp) + .in_definition_order() + .find(|assoc| assoc.is_fn() && assoc.name().as_str() == *segment) + .map(|assoc| assoc.def_id) + })?, + _ => return None, + }; + } + (!segments.is_empty()).then_some(item) +} + +/// The module of a binding's file: its path in the output, which must stay inside it, +/// apart from the runtime. +fn module_of(file: &Path) -> Result { + if file.extension().is_none_or(|ext| ext != "ts") { + return Err(format!("{} is no TypeScript file", file.display())); + } + let mut module = Vec::new(); + for part in file.with_extension("").components() { + match part { + Component::Normal(name) => module.push(name.to_string_lossy().into_owned()), + _ => return Err(format!("{} must be relative to ts.toml, inside its directory", file.display())), + } + } + if module.first().is_some_and(|first| first == "rt") { + return Err(format!("{} would replace the runtime, rt.ts and rt/", file.display())); + } + Ok(module) +} + +/// `name` as a JS identifier. +fn ident(name: &str) -> String { + let name: String = name + .chars() + .map(|c| if c.is_alphanumeric() || c == '_' || c == '$' { c } else { '_' }) + .collect(); + match name.starts_with(|c: char| c.is_ascii_digit()) { + true => format!("_{name}"), + false => name, + } +} diff --git a/ts/src/lower/body.rs b/ts/src/lower/body.rs index 51b121031..3079648a7 100644 --- a/ts/src/lower/body.rs +++ b/ts/src/lower/body.rs @@ -96,6 +96,9 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { Ok(None) if self.t.polymorphic() && gargs.has_param() => return Expr::undefined(), _ => return self.err(span, format!("`{}` does not resolve", tcx.def_path_str(def_id))), }; + if let Some(sym) = self.t.bound_fn(instance, span) { + return Expr::Sym(sym).call(args.into_iter().map(Expr::typed).collect()); + } // An `async` body's `Future::poll`, which its future has itself. if let ty::InstanceKind::Item(did) = instance.def && tcx.is_coroutine(did) @@ -171,7 +174,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { _ => self.err(span, format!("constructing `{}`", tcx.def_path_str(adt_id))), }; } - if !adt_id.is_local() { + if !self.t.own(adt_id) { return self.err( span, format!("constructing `{}` from another crate", tcx.def_path_str(adt_id)), @@ -211,13 +214,18 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { let sig = tcx.instantiate_bound_regions_with_erased(tcx.fn_sig(def_id).instantiate(tcx, gargs).skip_norm_wip()); let names: Vec = (0..sig.inputs().len()).map(|i| self.fresh(&format!("a{i}"))).collect(); let values: Vec = names.iter().map(Expr::ident).collect(); - // A local function is named directly. + // A local function or a binding's is named directly. if !matches!(tcx.def_kind(def_id), rustc_hir::def::DefKind::Ctor(..)) && let Ok(Some(instance)) = Instance::try_resolve(tcx, self.t.typing_env_pub(), def_id, gargs) - && let ty::InstanceKind::Item(did) = instance.def - && did.is_local() { - return Expr::Sym(self.t.instance(instance)); + if let Some(sym) = self.t.bound_fn(instance, span) { + return Expr::Sym(sym); + } + if let ty::InstanceKind::Item(did) = instance.def + && did.is_local() + { + return Expr::Sym(self.t.instance(instance)); + } } let mut body = Vec::new(); let call = self.call_def(def_id, gargs, values, span, &mut body); diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index 0c311c7f8..9af3736b8 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -402,7 +402,7 @@ fn hashing<'tcx>(b: &mut Body<'_, '_, 'tcx>, coll: Ty<'tcx>, key: Ty<'tcx>, span ty::Bool | ty::Char | ty::Str => true, ty::Int(_) | ty::Uint(_) => !Int::of(key)?.big(), ty::Adt(..) if library::string(b.t.tcx, key) => true, - ty::Adt(adt, _) if adt.did().is_local() && fieldless(*adt) => derived_eq(b, key), + ty::Adt(adt, _) if b.t.own(adt.did()) && fieldless(*adt) => derived_eq(b, key), _ => false, }; if primitive { @@ -432,7 +432,7 @@ fn own_ne<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { match ty.kind() { ty::Tuple(tys) => tys.iter().any(|t| own_ne(b, t)), ty::Adt(..) if let Some(inner) = arc_of(b, ty) => own_ne(b, inner), - ty::Adt(adt, _) if adt.did().is_local() => { + ty::Adt(adt, _) if b.t.own_or_bound(adt.did()) => { let ne = trait_fn(tcx, "PartialEq", "ne"); Instance::try_resolve(tcx, b.t.typing_env_pub(), ne, tcx.mk_args(&[ty.into(), ty.into()])) .ok() @@ -759,7 +759,7 @@ fn discriminant<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, value: Expr, spa let ty::Adt(adt, _) = ty.kind() else { return b.err(span, format!("the discriminant of `{ty}`")); }; - if !adt.is_enum() || !adt.did().is_local() { + if !adt.is_enum() || !b.t.own(adt.did()) { return b.err(span, format!("the discriminant of `{ty}`")); } let info = b.t.adt(*adt, span); @@ -788,7 +788,7 @@ fn std_key<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { // What a std type holds may be the crate's, which compares and hashes by its own impls. let held = |t: Ty<'tcx>| { let t = peel(b, t); - std_key(b, t) || b.t.deferred(t) || matches!(t.kind(), ty::Adt(adt, _) if adt.did().is_local()) + std_key(b, t) || b.t.deferred(t) || matches!(t.kind(), ty::Adt(adt, _) if b.t.own_or_bound(adt.did())) }; match ty.kind() { ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Str => true, @@ -827,11 +827,11 @@ fn cmp_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, partial: bool, span: S Expr::rt("cmpOption", vec![cmp_fn(b, inner, partial, span)]) } ty::Adt(..) if let Some(inner) = arc_of(b, ty) => Expr::rt("cmpArc", vec![cmp_fn(b, inner, partial, span)]), - ty::Adt(adt, _) if adt.did().is_local() && partial => { + ty::Adt(adt, _) if b.t.own_or_bound(adt.did()) && partial => { let cmp = trait_fn(tcx, "PartialOrd", "partial_cmp"); b.fn_value(cmp, tcx.mk_args(&[ty.into(), ty.into()]), span) } - ty::Adt(adt, _) if adt.did().is_local() => { + ty::Adt(adt, _) if b.t.own_or_bound(adt.did()) => { let cmp = trait_fn(tcx, "Ord", "cmp"); b.fn_value(cmp, tcx.mk_args(&[ty.into()]), span) } @@ -871,7 +871,7 @@ fn eq_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) -> Expr { } Expr::rt("eqArc", values) } - ty::Adt(adt, _) if adt.did().is_local() => { + ty::Adt(adt, _) if b.t.own_or_bound(adt.did()) => { let eq = trait_fn(tcx, "PartialEq", "eq"); b.fn_value(eq, tcx.mk_args(&[ty.into(), ty.into()]), span) } @@ -898,7 +898,7 @@ fn hash_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, hasher: Ty<'tcx>, spa Expr::rt("hashOption", vec![hash_fn(b, inner, hasher, span)]) } ty::Adt(..) if let Some(inner) = arc_of(b, ty) => Expr::rt("hashArc", vec![hash_fn(b, inner, hasher, span)]), - ty::Adt(adt, _) if adt.did().is_local() => { + ty::Adt(adt, _) if b.t.own_or_bound(adt.did()) => { let hash = trait_fn(tcx, "Hash", "hash"); b.fn_value(hash, tcx.mk_args(&[ty.into(), hasher.into()]), span) } diff --git a/ts/src/lower/consts.rs b/ts/src/lower/consts.rs index ae0ebab73..c11e0784b 100644 --- a/ts/src/lower/consts.rs +++ b/ts/src/lower/consts.rs @@ -124,7 +124,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { _ => self.err(span, format!("a constant of type `{ty}`")), }; } - if !adt.did().is_local() { + if !self.t.own(adt.did()) { return self.err(span, format!("a constant of type `{ty}` from another crate")); } let info = self.t.adt(*adt, span); diff --git a/ts/src/lower/copy.rs b/ts/src/lower/copy.rs index e7743ff72..b94cfe061 100644 --- a/ts/src/lower/copy.rs +++ b/ts/src/lower/copy.rs @@ -38,7 +38,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { // replaces the result. Some(Std::Option | Std::Poll | Std::Result | Std::ControlFlow) => (false, args.types().collect()), Some(_) => return false, - None if adt.did().is_local() && adt.all_fields().next().is_some() => { + None if self.own(adt.did()) && adt.all_fields().next().is_some() => { (true, adt.all_fields().map(|f| self.field_ty(f.did, args)).collect()) } None => return false, diff --git a/ts/src/lower/drop.rs b/ts/src/lower/drop.rs index 707cc0340..1e3a7e6e8 100644 --- a/ts/src/lower/drop.rs +++ b/ts/src/lower/drop.rs @@ -28,6 +28,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { let outer_cycle = std::mem::replace(&mut self.drops_cycle, false); let tcx = self.tcx; let drops = match ty.kind() { + ty::Adt(adt, _) if let Some(drops) = self.bound_drops(adt.did()) => drops, ty::Adt(adt, args) => match Std::of(tcx, adt.did()) { Some(Std::Arc | Std::Waker) => true, Some( @@ -92,6 +93,8 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { return self.inert(ty); } match ty.kind() { + // Its binding's `[Symbol.dispose]`. + ty::Adt(adt, _) if self.bound_drops(adt.did()).is_some() => true, ty::Adt(adt, args) => match Std::of(self.tcx, adt.did()) { Some(Std::Waker) => true, Some(Std::Arc | Std::Mutex | Std::Option | Std::Box | Std::Pin) => { @@ -123,6 +126,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { } let tcx = self.tcx; match ty.kind() { + ty::Adt(adt, _) if let Some(drops) = self.bound_drops(adt.did()) => drops, ty::Adt(adt, args) => match Std::of(tcx, adt.did()) { Some(Std::Waker) => false, Some(_) => args.types().any(|t| self.drop_panics(t, seen)), @@ -164,6 +168,8 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { } match ty.kind() { ty::Ref(_, inner, _) => self.inert(*inner), + // A binding's value that drops nothing has no `[Symbol.dispose]` to call. + ty::Adt(adt, _) if self.bound_drops(adt.did()).is_some() => true, ty::Adt(adt, args) => match Std::of(self.tcx, adt.did()) { // Each is its contents in JS. Some(Std::Option | Std::Box | Std::Mutex | Std::MutexGuard | Std::Poll | Std::Pin) => { diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 7146eed13..5a990ca99 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -49,7 +49,7 @@ fn own_next<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> Option let tcx = b.t.tcx; let ty = ty.peel_refs(); // Over parameters, only a census's instance, which never runs, would call it. - if !matches!(ty.kind(), ty::Adt(adt, _) if adt.did().is_local()) || ty.has_param() || ty.has_aliases() { + if !matches!(ty.kind(), ty::Adt(adt, _) if b.t.own(adt.did())) || ty.has_param() || ty.has_aliases() { return None; } let (next, _) = trait_items(tcx)?; diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs index cde8f76db..aac739845 100644 --- a/ts/src/lower/mir/call.rs +++ b/ts/src/lower/mir/call.rs @@ -32,6 +32,11 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { let result = dest.ty(&self.mir.local_decls, tcx).ty; let mut pre = Vec::new(); let (value, effect) = match *fn_ty.kind() { + // A binding's export, which takes what JS passes, as std's mappings do not. + ty::FnDef(def_id, gargs) if self.b.t.binds(def_id, gargs.skip_binder()) => ( + self.call_def(def_id, gargs.skip_binder(), args, result, span, &mut pre), + true, + ), ty::FnDef(def_id, gargs) if self.bytes_across(def_id, gargs.skip_binder(), fn_ty) => { let path = tcx.def_path_str(def_id); let msg = format!( @@ -519,7 +524,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { // The crate's own iterator is its own `IntoIterator`; std's adaptors convert it // where they take it. ("IntoIterator", "into_iter") - if matches!(targ(0)?.kind(), ty::Adt(adt, _) if adt.did().is_local()) + if matches!(targ(0)?.kind(), ty::Adt(adt, _) if self.b.t.own(adt.did())) && self.b.t.iter_item(targ(0)?).is_some() => { let value = self.operand(&args[0].node, span, out); diff --git a/ts/src/lower/mir/mod.rs b/ts/src/lower/mir/mod.rs index 542533661..19635d5f6 100644 --- a/ts/src/lower/mir/mod.rs +++ b/ts/src/lower/mir/mod.rs @@ -514,7 +514,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { let owned = match mir.local_decls[local].ty.kind() { ty::Tuple(_) | ty::Array(..) => true, ty::Adt(adt, _) => { - adt.did().is_local() + self.b.t.own(adt.did()) || matches!( crate::lower::library::Std::of(self.tcx, adt.did()), Some(crate::lower::library::Std::Option | crate::lower::library::Std::Result) @@ -748,7 +748,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { let ty::Adt(adt, _) = ty.kind() else { return None; }; - if !adt.is_struct() || !adt.did().is_local() || adt.all_fields().next().is_some() { + if !adt.is_struct() || !self.b.t.own(adt.did()) || adt.all_fields().next().is_some() { return None; } let info = self.b.t.adt(*adt, self.local_span(local)); @@ -2675,7 +2675,7 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { match ty.kind() { ty::Tuple(tys) => stack.extend(tys.iter()), ty::Array(elem, _) | ty::Slice(elem) => stack.push(*elem), - ty::Adt(adt, args) if adt.did().is_local() => { + ty::Adt(adt, args) if self.b.t.own(adt.did()) => { stack.extend(adt.all_fields().map(|field| self.b.t.field_ty(field.did, args))) } ty::Adt(_, args) => stack.extend(args.types()), @@ -2706,7 +2706,7 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { ty::Tuple(tys) => tys.iter().any(|t| self.holds_behind(t, part)), ty::Array(elem, _) => self.holds_behind(*elem, part), ty::Adt(adt, args) - if adt.did().is_local() + if self.b.t.own(adt.did()) || matches!( self.b.t.std_of(ty), Some(Std::Option | Std::Result | Std::ControlFlow | Std::Poll) @@ -2747,7 +2747,7 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { return true; } if let ty::Adt(adt, args) = t.kind() - && adt.did().is_local() + && self.b.t.own(adt.did()) && seen.insert(t) { stack.extend(adt.all_fields().map(|field| self.b.t.field_ty(field.did, args))); diff --git a/ts/src/lower/mir/place.rs b/ts/src/lower/mir/place.rs index 437add783..159a2432d 100644 --- a/ts/src/lower/mir/place.rs +++ b/ts/src/lower/mir/place.rs @@ -264,7 +264,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { let ty::Adt(adt, args) = ty.kind() else { return base; }; - if !adt.did().is_local() || !matches!(self.b.t.adt(*adt, span).repr, AdtRepr::Union) { + if !self.b.t.own(adt.did()) || !matches!(self.b.t.adt(*adt, span).repr, AdtRepr::Union) { return base; } let def = adt.variant(variant); @@ -326,7 +326,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { Some(Std::Cow) => self.err(span, crate::lower::library::cow_variant(ty)), Some(Std::ControlFlow) => super::call::control_flow_field(self, base, ty, variant), _ if transparent(self.b.t, ty) => base, - None if adt.did().is_local() => { + None if self.b.t.own(adt.did()) => { let info = self.b.t.adt(*adt, span); match info.repr { AdtRepr::Struct { fields, .. } => base.field(fields[field.index()]), @@ -576,7 +576,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { 0 => scrut.clone().member("ok"), _ => scrut.clone().member("ok").not(), }, - None if adt.did().is_local() => { + None if self.b.t.own(adt.did()) => { let info = self.b.t.adt(*adt, span); let name = Expr::Str(adt.variant(index).name.to_string()); match info.repr { @@ -631,7 +631,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { // An aggregate, field by field. if let ty::Adt(..) | ty::Tuple(_) | ty::Array(..) = ty.kind() && !matches!(ty.kind(), ty::Tuple(tys) if tys.is_empty()) - && !matches!(ty.kind(), ty::Adt(adt, _) if adt.is_struct() && adt.did().is_local() && adt.all_fields().next().is_none()) + && !matches!(ty.kind(), ty::Adt(adt, _) if adt.is_struct() && self.b.t.own(adt.did()) && adt.all_fields().next().is_none()) { let Some(d) = tcx.try_destructure_mir_constant_for_user_output(val, ty) else { return self.err(span, format!("a constant of type `{ty}` that can't be taken apart")); @@ -661,7 +661,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { ConstValue::ZeroSized => match *ty.kind() { ty::FnDef(def_id, args) => self.b.fn_value(def_id, args.skip_binder(), span), ty::Tuple(tys) if tys.is_empty() => Expr::undefined(), - ty::Adt(adt, _) if adt.is_struct() && adt.did().is_local() => { + ty::Adt(adt, _) if adt.is_struct() && self.b.t.own(adt.did()) => { let info = self.b.t.adt(adt, span); self.b.t.construct(info.sym, &crate::ts::Ctor::Unit, &[], Vec::new()) } diff --git a/ts/src/lower/mir/rvalue.rs b/ts/src/lower/mir/rvalue.rs index 1b9e441ae..18b73f85f 100644 --- a/ts/src/lower/mir/rvalue.rs +++ b/ts/src/lower/mir/rvalue.rs @@ -251,7 +251,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { // The value is its discriminant. Some(Std::Ordering) => scrut, Some(Std::Cow) => self.err(span, crate::lower::library::cow_variant(ty)), - None if adt.did().is_local() => { + None if self.b.t.own(adt.did()) => { let info = self.b.t.adt(*adt, span); let table = discrs .iter() @@ -386,7 +386,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { ["iter", "sources", "empty", "Empty"] => return Expr::rt("iterEmpty", Vec::new()), _ => {} } - if !def_id.is_local() { + if !self.b.t.own(def_id) { return self.err(span, format!("constructing `{ty}` from another crate")); } let info = self.b.t.adt(adt, span); diff --git a/ts/src/lower/mod.rs b/ts/src/lower/mod.rs index dcdc450f9..c79ff6a7c 100644 --- a/ts/src/lower/mod.rs +++ b/ts/src/lower/mod.rs @@ -5,6 +5,8 @@ //! call is always a direct call. Types are translated once, generically, and a function of //! a type is a module-level one taking `self` first, so a bundler drops what goes unused. +mod bind; +pub use bind::typecheck; mod body; mod collections; mod consts; @@ -19,7 +21,7 @@ mod task; use std::collections::VecDeque; -use rustc_hir::def::{DefKind, Res}; +use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_middle::ty::{ self, AdtDef, EarlyBinder, GenericArgsRef, Instance, InstanceKind, Ty, TyCtxt, TypeVisitableExt, TypingEnv, @@ -34,6 +36,7 @@ use crate::ts::{self, Ctor, FieldId, Item, Loc, SymId, Type}; /// Translate what the config's roots reach, or `None` after reporting why not. pub fn translate<'tcx>(tcx: TyCtxt<'tcx>, config: &Config) -> Option { let mut t = Translator::new(tcx, config, None)?; + t.bind(); for root in &config.roots { t.root(root); } @@ -52,8 +55,10 @@ pub fn translate<'tcx>(tcx: TyCtxt<'tcx>, config: &Config) -> Option { /// as if each were a root. pub fn census<'tcx>(tcx: TyCtxt<'tcx>, config: &Config) -> Option { let mut t = Translator::new(tcx, config, Some(Vec::new()))?; + t.bind(); for def_id in tcx.hir_crate_items(()).definitions() { - if matches!(tcx.def_kind(def_id), DefKind::Struct | DefKind::Enum | DefKind::Union) { + if matches!(tcx.def_kind(def_id), DefKind::Struct | DefKind::Enum | DefKind::Union) && !t.bound(def_id.to_def_id()) + { t.adt(tcx.adt_def(def_id), tcx.def_span(def_id)); } } @@ -70,6 +75,7 @@ pub fn census<'tcx>(tcx: TyCtxt<'tcx>, config: &Config) -> Option { .filter(|&def_id| { !tcx.impl_of_assoc(def_id) .is_some_and(|imp| tcx.is_automatically_derived(imp)) + && !t.bound(def_id) }) .collect(); let generic = |def_id: DefId| tcx.generics_of(def_id).requires_monomorphization(tcx); @@ -154,6 +160,8 @@ pub struct Translator<'a, 'tcx> { pub coroutines: rustc_data_structures::fx::FxHashMap, mir::coroutine::Info<'tcx>>, /// Whether [`Self::ty`] is typing a variable: see [`Self::held_ty`]. held: bool, + /// The hand-written TypeScript the config binds items to. + bindings: bind::Bindings<'tcx>, } /// A local struct or enum, and how it is represented. @@ -219,6 +227,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { copy_shims: Default::default(), writes: copy::Writes::of_crate(tcx), held: false, + bindings: Default::default(), }) } @@ -389,12 +398,19 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { /// The item a config path names, or `None` after reporting that it names none. fn config_item(&mut self, path: &str) -> Option { - let def_id = self.resolve_path(path); - if def_id.is_none() { + let segments: Vec<&str> = path.split("::").collect(); + let def_id = bind::resolve_in(self.tcx, rustc_hir::def_id::CRATE_DEF_ID.to_def_id(), &segments) + .filter(|def_id| def_id.is_local()); + let Some(def_id) = def_id else { self.tcx.dcx().err(format!("rust2-ts: no item `{path}` in the crate")); self.failed = true; + return None; + }; + if self.bound(def_id) { + self.err(self.tcx.def_span(def_id), "a root or printable item can't be bound"); + return None; } - def_id + Some(def_id) } /// Translate a type the config names, which must not be generic. @@ -454,36 +470,17 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { } } - fn resolve_path(&self, path: &str) -> Option { - let mut module = rustc_hir::def_id::CRATE_DEF_ID; - let mut segments = path.split("::").peekable(); - while let Some(segment) = segments.next() { - let child = self - .tcx - .module_children_local(module) - .iter() - .find(|child| child.ident.as_str() == segment)?; - let Res::Def(kind, def_id) = child.res else { - return None; - }; - if segments.peek().is_none() { - return Some(def_id); - } - if kind != DefKind::Mod { - return None; - } - module = def_id.as_local()?; - } - None - } - - /// The symbol of a function instance, queueing its translation the first time. + /// The symbol of a function instance, queueing its translation the first time, or the + /// export of a binding that stands in for it. pub fn instance(&mut self, instance: Instance<'tcx>) -> SymId { if let Some(&sym) = self.instances.get(&instance) { return sym; } let tcx = self.tcx; let def_id = instance.def_id(); + if let Some(sym) = self.bound_fn(instance, tcx.def_span(def_id)) { + return sym; + } let base = camel(tcx.item_name(def_id).as_str()); // A function of a local type sits in the type's own module, escaped only where it is // declared, so callers call it by its own name: `Decoder.new`. @@ -655,7 +652,17 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { /// translates, rather than std's. pub fn translated(&self, def_id: DefId, args: GenericArgsRef<'tcx>) -> bool { match Instance::try_resolve(self.tcx, self.typing_env(), def_id, args) { - Ok(Some(instance)) => matches!(instance.def, InstanceKind::Item(did) if did.is_local()), + Ok(Some(instance)) => { + matches!(instance.def, InstanceKind::Item(did) if did.is_local()) && self.bound_module(instance).is_none() + } + _ => false, + } + } + + /// Whether a call of `def_id` with `args` calls a binding's export. + pub fn binds(&self, def_id: DefId, args: GenericArgsRef<'tcx>) -> bool { + match Instance::try_resolve(self.tcx, self.typing_env(), def_id, args) { + Ok(Some(instance)) => self.bound_module(instance).is_some(), _ => false, } } @@ -837,6 +844,8 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { ty::Uint(ty::UintTy::U8) => Some(ObjectKind::Bytes), _ => Some(ObjectKind::Array), }, + // A binding's value: an object, unless Rust copies it, which JS can't. + ty::Adt(adt, _) if self.bound_drops(adt.did()).is_some() => (!self.is_copy(ty)).then_some(ObjectKind::Class), ty::Adt(adt, _) => match library::Std::of(self.tcx, adt.did()) { Some(library::Std::Vec) => Some(ObjectKind::Vec), Some( @@ -1015,6 +1024,10 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { fn adt_ty(&mut self, adt: AdtDef<'tcx>, args: GenericArgsRef<'tcx>, span: Span) -> Type { let tcx = self.tcx; + if let Some(sym) = self.bound_type(adt.did(), span) { + let args = args.types().map(|t| self.ty(t, span)).collect(); + return Type::Sym(sym, args); + } if let Some(std) = library::Std::of(tcx, adt.did()) { return match std { library::Std::Option => { diff --git a/ts/src/print.rs b/ts/src/print.rs index cda3e9204..e62841e22 100644 --- a/ts/src/print.rs +++ b/ts/src/print.rs @@ -6,7 +6,7 @@ use std::path::{Component, Path, PathBuf}; use oxc_sourcemap::SourceMapBuilder; -use crate::program::{HEADER, ModPath, Program, SymKind, is_type_module, module_file, unique}; +use crate::program::{HEADER, ModPath, Program, SymKind, module_file, unique}; use crate::ts::{Ctor, Expr, FieldInit, Function, Item, Key, Loc, Param, Stmt, StmtKind, SymId, Type}; pub struct Printed { @@ -182,10 +182,8 @@ impl Bindings { foreign.sort_by(|a, b| key(a.0).cmp(&key(b.0))); for (id, value) in foreign { let sym = &program.syms[id]; - if is_type_module(&sym.module) { + if let Some((module, ty)) = program.namespace(&sym.module) { let (binding, used) = namespaces.entry(sym.module.clone()).or_insert_with(|| { - // `decode.Decoder` binds `Decoder`, or `decode_Decoder` if that is taken. - let (module, ty) = sym.module.last().unwrap().split_once('.').unwrap(); let mut binding = ty.to_string(); let mut n = 2; while taken.contains(&binding) { diff --git a/ts/src/program.rs b/ts/src/program.rs index ce7b32077..71341c418 100644 --- a/ts/src/program.rs +++ b/ts/src/program.rs @@ -91,6 +91,27 @@ pub struct Program { /// The structs whose values are plain objects; the others are classes. pub plain: HashSet, pub sources: Vec, + /// The hand-written files that bindings reach, copied into the output, by module. + pub bound: BTreeMap, + /// What `tsc` checks each binding against: a function of the check module each. + pub checks: Vec, +} + +/// A binding's file, which callers import whole, as they do a type's module. +#[derive(Debug)] +pub struct Bound { + pub text: String, + /// What callers import it as: the type bound to it, or else its file's name. + pub namespace: String, +} + +/// A function that type-checks only if a binding is what translated code takes it for. +#[derive(Debug)] +pub struct Check { + pub item: Item, + /// The Rust item, which a failed check reports. + pub span: rustc_span::Span, + pub what: String, } impl Program { @@ -131,6 +152,10 @@ impl Program { } for (scope, mut ids) in scopes { + // A binding's names are its file's exports, which lowering named already. + if matches!(&scope, Scope::Module(module) if self.bound.contains_key(module)) { + continue; + } ids.sort_by(|&a, &b| self.syms[a].key().cmp(&self.syms[b].key())); let mut taken = HashSet::new(); match &scope { @@ -192,10 +217,16 @@ impl Program { // Two types whose names differ only in case would share a file where file names // ignore case, as on macOS and Windows. let mut files = HashMap::new(); - for module in modules.keys() { + for module in modules.keys().copied().chain(self.bound.keys()) { let file = module_file(module); let folded = file.to_string_lossy().to_lowercase(); if let Some(other) = files.insert(folded, file.clone()) { + if other == file { + return Err(format!( + "{} is a binding's file, which would replace a translated module's", + file.display() + )); + } return Err(format!( "{} and {} differ only in case, which a case-insensitive file system can't tell apart", other.display(), @@ -209,8 +240,21 @@ impl Program { write_file(&path, &printed.code)?; write_file(&path.with_extension("ts.map"), &printed.map)?; } + for (module, bound) in &self.bound { + write_file(&out.join(module_file(module)), &format!("{HEADER}\n{}", bound.text))?; + } Ok(()) } + + /// How a module's importers bind it whole, if they do: a type's module, or a binding's + /// file. The name its importers give it, and what they prefix that with where it is taken. + pub fn namespace<'p>(&'p self, module: &'p ModPath) -> Option<(&'p str, &'p str)> { + if let Some(bound) = self.bound.get(module) { + return Some((module.last().map_or("lib", String::as_str), &bound.namespace)); + } + // `decode.Decoder` binds `Decoder`, or `decode_Decoder` if that is taken. + module.last()?.split_once('.') + } } #[derive(PartialEq, Eq, PartialOrd, Ord)] @@ -251,11 +295,6 @@ pub fn type_module(module: &[String], ty: &str) -> ModPath { path } -/// Whether `module` is a type's, from [`type_module`]. -pub fn is_type_module(module: &[String]) -> bool { - module.last().is_some_and(|last| last.contains('.')) -} - /// The file a module is written to, relative to the output directory. pub fn module_file(module: &[String]) -> PathBuf { match module { @@ -265,7 +304,7 @@ pub fn module_file(module: &[String]) -> PathBuf { } /// Remove what an earlier run generated, and nothing else. -fn clean(out: &Path) -> Result<(), String> { +pub fn clean(out: &Path) -> Result<(), String> { let Ok(entries) = std::fs::read_dir(out) else { return Ok(()); }; diff --git a/ts/tests/fixtures.rs b/ts/tests/fixtures.rs index d30e57711..9cdfc595d 100644 --- a/ts/tests/fixtures.rs +++ b/ts/tests/fixtures.rs @@ -476,32 +476,62 @@ fn unwinding(out: &Path, entry: &str, scripts: u32) { #[test] fn reject() { let (out, output) = translate("reject"); - rejected(&out, &output); + rejected(&out, &output, &fixture("reject").join("expected.txt")); } -/// The errors of a failed translation, each with where it points, against `expected.txt`. -fn rejected(out: &Path, output: &Output) { - assert!(!output.status.success(), "translating reject succeeded"); +/// Bindings stand in for what translation refuses, `bytes::Bytes` and the crate's own +/// function and type over raw pointers, and run as native Rust does. `tsc` refuses each +/// binding that doesn't match the Rust signature at its item, and the config refuses each +/// binding it can't make. +#[test] +fn bind() { + let out = translated("bind"); + snapshot("bind", &out); + typecheck(&out); + differential("bind-fixture", &[], &out, "harness.ts", 1_000, &[]); + for config in ["wrong", "refused"] { + let out = scratch().join("bind").join(config); + let toml = fixture("bind").join(format!("{config}.toml")); + let output = driver("bind", &toml, &["--out".as_ref(), out.as_ref()], "build"); + assert!( + !out.join("bind/bytes.ts").exists(), + "a failed translation left a binding's file" + ); + rejected(&out, &output, &fixture("bind").join(format!("{config}.txt"))); + } +} + +/// The errors of a failed translation, each with where it points, against `expected`. +fn rejected(out: &Path, output: &Output, expected: &Path) { + assert!(!output.status.success(), "translating {} succeeded", expected.display()); assert!(!out.join("lib.ts").exists(), "a failed translation wrote output"); let stderr = String::from_utf8_lossy(&output.stderr); - // Each error and where it points, without rustc's rendering of the source. + // Each error and where it points, without rustc's rendering of the source, and with a + // dependency's source from wherever cargo keeps it. let mut got = String::new(); let mut lines = stderr.lines().peekable(); while let Some(line) = lines.next() { if let Some(error) = line.strip_prefix("error: rust2-ts: ") { got.push_str(error); + // What goes on in the lines after, as `tsc`'s messages do. + while let Some(more) = lines.peek().filter(|l| l.starts_with(" ") && !l.trim_start().starts_with("--> ")) { + got.push_str(&format!("\n{more}")); + lines.next(); + } if let Some(at) = lines.peek().and_then(|l| l.trim_start().strip_prefix("--> ")) { + let at = at.split_once("/registry/src/").map_or(at, |(_, rest)| { + rest.split_once('/').map_or(rest, |(_, path)| path) + }); got.push_str(&format!(" at {at}")); } got.push('\n'); } } - let path = fixture("reject").join("expected.txt"); if std::env::var_os("UPDATE_EXPECT").is_some() { - std::fs::write(&path, &got).unwrap(); + std::fs::write(expected, &got).unwrap(); return; } - let want = std::fs::read_to_string(&path).unwrap_or_default(); + let want = std::fs::read_to_string(expected).unwrap_or_default(); assert!( got == want, "the errors differ; rerun with UPDATE_EXPECT=1\n{}", From ec63eff536ae64988097b0cd6931660aca923a02 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 27 Oct 2026 14:10:18 -0700 Subject: [PATCH 3/8] fix(ts): refuse bound types as collection keys Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3 --- ts/README.md | 2 +- ts/src/driver.rs | 4 ++- ts/src/lower/bind.rs | 60 ++++++++++++++++++++++++++----------- ts/src/lower/collections.rs | 12 ++++---- ts/src/lower/mod.rs | 10 +++++-- ts/tests/fixtures.rs | 11 ++++--- 6 files changed, 67 insertions(+), 32 deletions(-) diff --git a/ts/README.md b/ts/README.md index ff79614ae..63896a99a 100644 --- a/ts/README.md +++ b/ts/README.md @@ -225,7 +225,7 @@ export serves every instance and a generic one is a generic TS function. `Bytes: `>>::from` is `Bytes.from$From_Vec_u8`. std's blanket impls and its traits' provided functions, such as `ToOwned` and `ne`, stay std's, as for any other type. Translation can't see into a bound type, so a field, variant, or constructor of one is -refused, as is iterating one. +refused, as are iterating one and keying a collection by one. Once the output is written, `tsc`, from `PATH`, checks each export against the translated signature of every instance translation calls, and each bound type against the Rust type's diff --git a/ts/src/driver.rs b/ts/src/driver.rs index 25017a190..d29621493 100644 --- a/ts/src/driver.rs +++ b/ts/src/driver.rs @@ -78,7 +78,9 @@ impl Callbacks for Translate { // Only a program rustc accepts is translated. let written = match &self.output { Output::Dir(out) => match lower::translate(tcx, &self.config) { - Some(program) if tcx.dcx().has_errors().is_none() => program.write(out).map(|()| lower::typecheck(tcx, &program, out)), + Some(program) if tcx.dcx().has_errors().is_none() => { + program.write(out).map(|()| lower::typecheck(tcx, &program, out)) + } _ => Ok(()), }, Output::Census(path) => match lower::census(tcx, &self.config) { diff --git a/ts/src/lower/bind.rs b/ts/src/lower/bind.rs index 35c36adc3..93f1e7409 100644 --- a/ts/src/lower/bind.rs +++ b/ts/src/lower/bind.rs @@ -29,7 +29,9 @@ struct Binding { enum Kind { Function, /// A type, and whether its value drops through its `[Symbol.dispose]`. - Type { drop: bool }, + Type { + drop: bool, + }, } /// A binding's file. @@ -68,7 +70,9 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { }; let kind = match tcx.def_kind(def_id) { DefKind::Fn => Kind::Function, - DefKind::AssocFn if tcx.trait_impl_of_assoc(def_id).is_none() && tcx.impl_of_assoc(def_id).is_some() => { + DefKind::AssocFn + if tcx.trait_impl_of_assoc(def_id).is_none() && tcx.impl_of_assoc(def_id).is_some() => + { Kind::Function } DefKind::Struct | DefKind::Enum | DefKind::Union => { @@ -168,12 +172,6 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { def_id.is_local() && !self.bindings.items.contains_key(&def_id) } - /// Whether the trait impls of `def_id`, a type, are called as they are: the crate's - /// own, or a binding's exports. - pub fn own_or_bound(&self, def_id: DefId) -> bool { - def_id.is_local() || self.bindings.items.contains_key(&def_id) - } - /// Whether a bound type's value drops through its `[Symbol.dispose]`, if it is one. pub fn bound_drops(&self, def_id: DefId) -> Option { match self.bindings.items.get(&def_id)?.kind { @@ -205,7 +203,11 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { ), _ => (Type::Builtin("void"), Vec::new()), }; - let what = format!("the binding of `{}` in {}", self.bindings.items[&def_id].path, module_file(&module).display()); + let what = format!( + "the binding of `{}` in {}", + self.bindings.items[&def_id].path, + module_file(&module).display() + ); self.check(vec![value], ret, body, self.tcx.def_span(def_id), what); Some(sym) } @@ -247,7 +249,10 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { let sig = tcx.normalize_erasing_regions(self.typing_env_pub(), ty::Unnormalized::new_wip(sig)); let ty = self.fn_ty(sig.inputs(), sig.output(), span); let body = vec![StmtKind::Return(Some(Expr::Sym(sym))).at(None)]; - let what = ty::print::with_no_trimmed_paths!(format!("the binding of `{instance}` in {}", module_file(&module).display())); + let what = ty::print::with_no_trimmed_paths!(format!( + "the binding of `{instance}` in {}", + module_file(&module).display() + )); self.check(Vec::new(), ty, body, tcx.def_span(def_id), what); } Some(sym) @@ -374,8 +379,8 @@ pub fn typecheck(tcx: TyCtxt<'_>, program: &Program, out: &Path) { Err(err) => return failed(err), }; let project = path.with_extension("json"); - if let Err(err) = crate::program::write_file(&path, &printed.code) - .and_then(|()| crate::program::write_file(&project, TSCONFIG)) + if let Err(err) = + crate::program::write_file(&path, &printed.code).and_then(|()| crate::program::write_file(&project, TSCONFIG)) { return failed(err); } @@ -422,12 +427,20 @@ pub fn typecheck(tcx: TyCtxt<'_>, program: &Program, out: &Path) { continue; } match check { - Some(check) => _ = tcx.dcx().span_err(check.span, format!("rust2-ts: {} doesn't type-check: {msg}", check.what)), + Some(check) => { + _ = tcx.dcx().span_err( + check.span, + format!("rust2-ts: {} doesn't type-check: {msg}", check.what), + ) + } None => _ = tcx.dcx().err(format!("rust2-ts: tsc: {diagnostic}")), } } if reported.is_empty() { - tcx.dcx().err(format!("rust2-ts: tsc failed: {text}{}", String::from_utf8_lossy(&output.stderr))); + tcx.dcx().err(format!( + "rust2-ts: tsc failed: {text}{}", + String::from_utf8_lossy(&output.stderr) + )); } _ = crate::program::clean(out); } @@ -492,7 +505,9 @@ pub fn resolve_in(tcx: TyCtxt<'_>, mut item: DefId, segments: &[&str]) -> Option }; let named = children.iter().filter_map(|child| match child.res { // A tuple struct's constructor shares its name. - Res::Def(kind, def_id) if child.ident.as_str() == *segment && !matches!(kind, DefKind::Ctor(..)) => { + Res::Def(kind, def_id) + if child.ident.as_str() == *segment && !matches!(kind, DefKind::Ctor(..)) => + { Some((kind, def_id)) } _ => None, @@ -525,7 +540,12 @@ fn module_of(file: &Path) -> Result { for part in file.with_extension("").components() { match part { Component::Normal(name) => module.push(name.to_string_lossy().into_owned()), - _ => return Err(format!("{} must be relative to ts.toml, inside its directory", file.display())), + _ => { + return Err(format!( + "{} must be relative to ts.toml, inside its directory", + file.display() + )); + } } } if module.first().is_some_and(|first| first == "rt") { @@ -538,7 +558,13 @@ fn module_of(file: &Path) -> Result { fn ident(name: &str) -> String { let name: String = name .chars() - .map(|c| if c.is_alphanumeric() || c == '_' || c == '$' { c } else { '_' }) + .map(|c| { + if c.is_alphanumeric() || c == '_' || c == '$' { + c + } else { + '_' + } + }) .collect(); match name.starts_with(|c: char| c.is_ascii_digit()) { true => format!("_{name}"), diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index 9af3736b8..acb35d2d4 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -432,7 +432,7 @@ fn own_ne<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { match ty.kind() { ty::Tuple(tys) => tys.iter().any(|t| own_ne(b, t)), ty::Adt(..) if let Some(inner) = arc_of(b, ty) => own_ne(b, inner), - ty::Adt(adt, _) if b.t.own_or_bound(adt.did()) => { + ty::Adt(adt, _) if b.t.own(adt.did()) => { let ne = trait_fn(tcx, "PartialEq", "ne"); Instance::try_resolve(tcx, b.t.typing_env_pub(), ne, tcx.mk_args(&[ty.into(), ty.into()])) .ok() @@ -788,7 +788,7 @@ fn std_key<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { // What a std type holds may be the crate's, which compares and hashes by its own impls. let held = |t: Ty<'tcx>| { let t = peel(b, t); - std_key(b, t) || b.t.deferred(t) || matches!(t.kind(), ty::Adt(adt, _) if b.t.own_or_bound(adt.did())) + std_key(b, t) || b.t.deferred(t) || matches!(t.kind(), ty::Adt(adt, _) if b.t.own(adt.did())) }; match ty.kind() { ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Str => true, @@ -827,11 +827,11 @@ fn cmp_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, partial: bool, span: S Expr::rt("cmpOption", vec![cmp_fn(b, inner, partial, span)]) } ty::Adt(..) if let Some(inner) = arc_of(b, ty) => Expr::rt("cmpArc", vec![cmp_fn(b, inner, partial, span)]), - ty::Adt(adt, _) if b.t.own_or_bound(adt.did()) && partial => { + ty::Adt(adt, _) if b.t.own(adt.did()) && partial => { let cmp = trait_fn(tcx, "PartialOrd", "partial_cmp"); b.fn_value(cmp, tcx.mk_args(&[ty.into(), ty.into()]), span) } - ty::Adt(adt, _) if b.t.own_or_bound(adt.did()) => { + ty::Adt(adt, _) if b.t.own(adt.did()) => { let cmp = trait_fn(tcx, "Ord", "cmp"); b.fn_value(cmp, tcx.mk_args(&[ty.into()]), span) } @@ -871,7 +871,7 @@ fn eq_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) -> Expr { } Expr::rt("eqArc", values) } - ty::Adt(adt, _) if b.t.own_or_bound(adt.did()) => { + ty::Adt(adt, _) if b.t.own(adt.did()) => { let eq = trait_fn(tcx, "PartialEq", "eq"); b.fn_value(eq, tcx.mk_args(&[ty.into(), ty.into()]), span) } @@ -898,7 +898,7 @@ fn hash_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, hasher: Ty<'tcx>, spa Expr::rt("hashOption", vec![hash_fn(b, inner, hasher, span)]) } ty::Adt(..) if let Some(inner) = arc_of(b, ty) => Expr::rt("hashArc", vec![hash_fn(b, inner, hasher, span)]), - ty::Adt(adt, _) if b.t.own_or_bound(adt.did()) => { + ty::Adt(adt, _) if b.t.own(adt.did()) => { let hash = trait_fn(tcx, "Hash", "hash"); b.fn_value(hash, tcx.mk_args(&[ty.into(), hasher.into()]), span) } diff --git a/ts/src/lower/mod.rs b/ts/src/lower/mod.rs index c79ff6a7c..95130add9 100644 --- a/ts/src/lower/mod.rs +++ b/ts/src/lower/mod.rs @@ -57,7 +57,8 @@ pub fn census<'tcx>(tcx: TyCtxt<'tcx>, config: &Config) -> Option { let mut t = Translator::new(tcx, config, Some(Vec::new()))?; t.bind(); for def_id in tcx.hir_crate_items(()).definitions() { - if matches!(tcx.def_kind(def_id), DefKind::Struct | DefKind::Enum | DefKind::Union) && !t.bound(def_id.to_def_id()) + if matches!(tcx.def_kind(def_id), DefKind::Struct | DefKind::Enum | DefKind::Union) + && !t.bound(def_id.to_def_id()) { t.adt(tcx.adt_def(def_id), tcx.def_span(def_id)); } @@ -653,7 +654,8 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { pub fn translated(&self, def_id: DefId, args: GenericArgsRef<'tcx>) -> bool { match Instance::try_resolve(self.tcx, self.typing_env(), def_id, args) { Ok(Some(instance)) => { - matches!(instance.def, InstanceKind::Item(did) if did.is_local()) && self.bound_module(instance).is_none() + matches!(instance.def, InstanceKind::Item(did) if did.is_local()) + && self.bound_module(instance).is_none() } _ => false, } @@ -845,7 +847,9 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { _ => Some(ObjectKind::Array), }, // A binding's value: an object, unless Rust copies it, which JS can't. - ty::Adt(adt, _) if self.bound_drops(adt.did()).is_some() => (!self.is_copy(ty)).then_some(ObjectKind::Class), + ty::Adt(adt, _) if self.bound_drops(adt.did()).is_some() => { + (!self.is_copy(ty)).then_some(ObjectKind::Class) + } ty::Adt(adt, _) => match library::Std::of(self.tcx, adt.did()) { Some(library::Std::Vec) => Some(ObjectKind::Vec), Some( diff --git a/ts/tests/fixtures.rs b/ts/tests/fixtures.rs index 9cdfc595d..b7e4499a0 100644 --- a/ts/tests/fixtures.rs +++ b/ts/tests/fixtures.rs @@ -514,14 +514,17 @@ fn rejected(out: &Path, output: &Output, expected: &Path) { if let Some(error) = line.strip_prefix("error: rust2-ts: ") { got.push_str(error); // What goes on in the lines after, as `tsc`'s messages do. - while let Some(more) = lines.peek().filter(|l| l.starts_with(" ") && !l.trim_start().starts_with("--> ")) { + while let Some(more) = lines + .peek() + .filter(|l| l.starts_with(" ") && !l.trim_start().starts_with("--> ")) + { got.push_str(&format!("\n{more}")); lines.next(); } if let Some(at) = lines.peek().and_then(|l| l.trim_start().strip_prefix("--> ")) { - let at = at.split_once("/registry/src/").map_or(at, |(_, rest)| { - rest.split_once('/').map_or(rest, |(_, path)| path) - }); + let at = at + .split_once("/registry/src/") + .map_or(at, |(_, rest)| rest.split_once('/').map_or(rest, |(_, path)| path)); got.push_str(&format!(" at {at}")); } got.push('\n'); From 2abb1f6400823831455002d438dd7516f8345731 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 27 Oct 2026 14:32:29 -0700 Subject: [PATCH 4/8] fix(ts): check a binding's value and refuse what it can't stand in for tsc now checks a bound type's value against how translated code uses it. It refuses: - a binding inside the output - an item bound twice - a phantom bounded type parameter - a type argument that drops under a binding without drop - whole-value stores through a &mut - iterator adaptors on a bound type Routing follows the impl's declared self type in translation and census alike. The write analysis skips bound bodies, and MIR inlining is off so a call stays a call. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3 --- ts/README.md | 38 +++--- ts/fixtures/bind/refused.toml | 11 +- ts/fixtures/bind/refused.txt | 8 +- ts/fixtures/bind/src/harness.rs | 13 ++ ts/fixtures/bind/src/lib.rs | 2 + ts/fixtures/bind/src/raw.rs | 14 +++ ts/fixtures/bind/wrong.txt | 2 +- ts/runtime/rt.ts | 27 ++++ ts/src/driver.rs | 5 +- ts/src/lower/bind.rs | 213 ++++++++++++++++++++++---------- ts/src/lower/body.rs | 2 +- ts/src/lower/consts.rs | 3 +- ts/src/lower/copy.rs | 20 +-- ts/src/lower/iter.rs | 13 ++ ts/src/lower/library.rs | 2 + ts/src/lower/mir/place.rs | 4 +- ts/src/lower/mir/rvalue.rs | 2 +- ts/src/lower/mod.rs | 5 +- ts/src/program.rs | 20 ++- 19 files changed, 300 insertions(+), 104 deletions(-) diff --git a/ts/README.md b/ts/README.md index 63896a99a..92e110f28 100644 --- a/ts/README.md +++ b/ts/README.md @@ -217,27 +217,37 @@ and may import the translated modules and npm packages too. Callers import it wh type's module, `import * as Bytes from "./bind/bytes.ts"`. A bound type is the file's export of its name, `Bytes.Bytes`, with the Rust type's type -parameters. A call of a bound function, or of a function of a bound type that its own -crate defines, trait impls included, calls the file's export. The export is named as the +parameters. A call of a bound function, or of a function that an impl for a bound type in +the type's own crate defines, calls the file's export. The export is named as the translated function would be, without the type's or the function's type arguments, so one export serves every instance and a generic one is a generic TS function. `Bytes::slice` is `Bytes.slice`, `::eq` is `Bytes.eq$PartialEq`, and -`>>::from` is `Bytes.from$From_Vec_u8`. std's blanket impls and its -traits' provided functions, such as `ToOwned` and `ne`, stay std's, as for any other type. -Translation can't see into a bound type, so a field, variant, or constructor of one is -refused, as are iterating one and keying a collection by one. +`>>::from` is `Bytes.from$From_Vec_u8`. A trait's provided function +that the impl doesn't override, such as `ne`, and a blanket impl's, such as `ToOwned`'s, +stay the trait's, as for any other type. A generic export sees a type argument only in the +values it takes, so a function with a bounded type parameter that no argument holds, such +as `fn zero() -> T`, can't be bound. -Once the output is written, `tsc`, from `PATH`, checks each export against the translated -signature of every instance translation calls, and each bound type against the Rust type's -type parameters. A missing or mismatched binding fails the translation at the Rust item's -span, and the output is removed. +Translation can't see into a bound type, so a field, variant, or constructor of one is +refused, as are iterating one, keying a collection by one, and assigning a whole one +through a `&mut`. Its value is never `undefined` or `rt.Pending`, which `None` and +`Poll::Pending` are, and is an object unless the Rust type is `Copy`. A `&mut` to one +passes that object, which the binding's functions change in place. Where the type is +`Copy`, such a `&mut` is refused, since JS shares the value where Rust copies it, and a +binding never changes a `Copy` value in place. Nor does a binding change in place what it +takes by value or by `&`, or share with a clone what a `&mut` changes, and an integer it +returns is in its Rust type's range. + +Once the output is written, `tsc` 5.8 or later, from `PATH`, checks each export against the +translated signature of every instance translation calls, and each bound type's value. A +missing or mismatched binding fails the translation at the Rust item's span, and the +output is removed. Only the binding files translation reaches are copied, so one imports +another only where translation reaches both. A binding inside the output is refused, since +a translation replaces it. A bound type with `drop = true` drops where Rust drops it, through its `[Symbol.dispose]`, which `tsc` checks it has. Without it, dropping one does nothing JS -sees, and its value has no `[Symbol.dispose]`. A `&mut` to a bound type passes its JS -object, which the binding changes in place. Where the type is `Copy`, such a `&mut` is -refused, since JS shares the value where Rust copies it, and a binding never changes a -`Copy` value in place. +sees, `tsc` checks it has no `[Symbol.dispose]`, and a type argument that drops is refused. ## Design diff --git a/ts/fixtures/bind/refused.toml b/ts/fixtures/bind/refused.toml index e348ce5f0..af038e9b1 100644 --- a/ts/fixtures/bind/refused.toml +++ b/ts/fixtures/bind/refused.toml @@ -1,6 +1,7 @@ -# Bindings `ts.toml` can't make: `refused.txt` holds the errors. +# Bindings `ts.toml` can't make, and what bound items can't do: `refused.txt` holds the +# errors. package = "bind-fixture" -roots = ["harness::run"] +roots = ["harness::run", "harness::refused"] [bind] "nowhere::Thing" = "bind/thing.ts" @@ -9,7 +10,11 @@ roots = ["harness::run"] "bytes::Buf" = "bind/buf.ts" "raw::Tally::get" = { file = "bind/get.ts", drop = true } "raw::Tally::new" = "rt/tally.ts" +"raw::Held::new" = "RT/held.ts" "bytes::BytesMut" = "../bytes.ts" "bytes::Bytes" = "bind/bytes.ts" "raw::Tally" = "bind/bytes.ts" -"raw::checksum" = "bind/missing.ts" +"checksum" = "bind/missing.ts" +"raw::checksum" = "bind/checksum.ts" +"raw::Held" = "bind/tally.ts" +"raw::zero" = "bind/checksum.ts" diff --git a/ts/fixtures/bind/refused.txt b/ts/fixtures/bind/refused.txt index 534ad0614..972bfe452 100644 --- a/ts/fixtures/bind/refused.txt +++ b/ts/fixtures/bind/refused.txt @@ -1,13 +1,19 @@ binding `bytes::Buf`: a binding is of a function or a type, not Trait binding `bytes::BytesMut`: ../bytes.ts must be relative to ts.toml, inside its directory -binding `nowhere::Thing`: no such item in the crate or a crate it depends on +binding `nowhere::Thing`: no such item, or more than one, in the crate or a crate it depends on binding `raw`: a binding is of a function or a type, not Mod +binding `raw::Held::new`: RT/held.ts would replace the runtime, rt.ts and rt/ binding `raw::Tally`: bind/bytes.ts binds another type binding `raw::Tally::get`: only a type's value drops binding `raw::Tally::new`: rt/tally.ts would replace the runtime, rt.ts and rt/ +binding `raw::checksum`: `checksum` binds the same item binding `std::vec::Vec`: std's type has a representation of its own reading bind/missing.ts: No such file or directory (os error 2) at fixtures/bind/src/harness.rs:25:12 unsupported type `*const u32` at fixtures/bind/src/raw.rs:27:2 +`raw::Held` binds without drop, so it would never drop its `raw::Tally` at fixtures/bind/src/harness.rs:52:13 +a binding of `raw::zero`, whose `T` no argument holds: its export can't call `T`'s impls at fixtures/bind/src/raw.rs:51:1 +`raw::Held` binds without drop, so it would never drop its `raw::Tally` at fixtures/bind/src/harness.rs:52:6 unsupported call to `std::sync::Arc::::from_raw` at fixtures/bind/src/raw.rs:46:17 unsupported call to `std::sync::Arc::::into_raw` at fixtures/bind/src/raw.rs:33:11 a raw pointer dereferenced at fixtures/bind/src/raw.rs:39:12 +an assignment through a reference to `bytes::Bytes` at fixtures/bind/src/harness.rs:58:2 diff --git a/ts/fixtures/bind/src/harness.rs b/ts/fixtures/bind/src/harness.rs index 6548a448a..805745ddf 100644 --- a/ts/fixtures/bind/src/harness.rs +++ b/ts/fixtures/bind/src/harness.rs @@ -44,3 +44,16 @@ pub fn run(input: &[u8]) -> Vec { out.push(Arc::strong_count(&count) as u8); out } + +/// What `refused.toml`'s bindings can't stand in for, which `run` never reaches. +pub fn refused(count: &Arc) -> u8 { + let mut bytes = Bytes::new(); + replace(&mut bytes, Bytes::from_static(b"replaced")); + let held = crate::raw::Held::new(Tally::new(count)); + drop(held); + crate::raw::zero::() + bytes.len() as u8 +} + +fn replace(target: &mut Bytes, value: Bytes) { + *target = value; +} diff --git a/ts/fixtures/bind/src/lib.rs b/ts/fixtures/bind/src/lib.rs index cec347048..5a50e860e 100644 --- a/ts/fixtures/bind/src/lib.rs +++ b/ts/fixtures/bind/src/lib.rs @@ -6,6 +6,8 @@ pub mod harness; pub mod raw; +/// A second path to the function, which a binding can't also name. +pub use raw::checksum; #[cfg(target_arch = "wasm32")] #[path = "../../wasm.rs"] mod wasm; diff --git a/ts/fixtures/bind/src/raw.rs b/ts/fixtures/bind/src/raw.rs index 0a7f309ea..1f09e66fe 100644 --- a/ts/fixtures/bind/src/raw.rs +++ b/ts/fixtures/bind/src/raw.rs @@ -46,3 +46,17 @@ impl Drop for Tally { drop(unsafe { Arc::from_raw(self.count) }); } } + +/// A default made from its type alone, which a generic TS function can't make. +pub fn zero() -> T { + T::default() +} + +/// A value it holds, and drops where Rust drops it. +pub struct Held(T); + +impl Held { + pub fn new(value: T) -> Self { + Self(value) + } +} diff --git a/ts/fixtures/bind/wrong.txt b/ts/fixtures/bind/wrong.txt index 63a11d4ac..7763e00c8 100644 --- a/ts/fixtures/bind/wrong.txt +++ b/ts/fixtures/bind/wrong.txt @@ -5,4 +5,4 @@ the binding of `bytes::Bytes::slice::>` in wrong/bytes. the binding of `::eq` in wrong/bytes.ts doesn't type-check: error TS2322: Type '(self: ByteSlice, other: string) => boolean' is not assignable to type '(a0: ByteSlice, a1: ByteSlice) => boolean'. Types of parameters 'other' and 'a1' are incompatible. Type 'ByteSlice' is not assignable to type 'string'. at bytes-1.12.1/src/bytes.rs:769:5 -the binding of `raw::Tally` in wrong/tally.ts doesn't type-check: error TS2741: Property '[Symbol.dispose]' is missing in type 'Tally' but required in type 'Disposable'. at fixtures/bind/src/raw.rs:26:1 +the binding of `raw::Tally` in wrong/tally.ts doesn't type-check: error TS2322: Type 'Tally' is not assignable to type '"a bound value that drops must have a [Symbol.dispose]"'. at fixtures/bind/src/raw.rs:26:1 diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index 158fa9ad2..6f2139c83 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -6,6 +6,7 @@ // `writeStr`, a future's `poll`, a waker's, and `[Symbol.dispose]`. import { type char, type TryFromIntError, type u32, u8Display, usizeDisplay } from "./rt/int.ts"; +import type { Pending } from "./rt/task.ts"; export * from "./rt/collections.ts"; export * from "./rt/int.ts"; @@ -44,6 +45,32 @@ export function assertFailed(op: string, left: Fmt, right: Fmt, args?: Arguments panicFmt(["assertion `left ", op, " right`", ...failed, "\n left: ", left, "\n right: ", right]); } +// Bindings + +/** + * `T` if it can be a bound Rust type's value, or else why not: never `undefined` or + * `Pending`, which `None` and `Poll::Pending` are, an object unless Rust copies it, and + * disposable exactly when it drops. + */ +export type Bound = [undefined] extends [T] + ? "a bound value can't be undefined, which None is" + : [typeof Pending] extends [T] + ? "a bound value can't be Pending, which Poll::Pending is" + : [Copy, T] extends [false, object] + ? Disposes + : Copy extends true + ? Disposes + : "a bound value Rust doesn't copy must be an object, which a reference changes in place"; + +/** `T` if it has a `[Symbol.dispose]` exactly when `Drop`, or else why not. */ +type Disposes = Drop extends true + ? [T] extends [Disposable] + ? T + : "a bound value that drops must have a [Symbol.dispose]" + : [T] extends [{ [Symbol.dispose]: unknown }] + ? "a bound value with a [Symbol.dispose] must bind with drop = true" + : T; + // Results /** `Result`. */ diff --git a/ts/src/driver.rs b/ts/src/driver.rs index d29621493..db8e5e2f6 100644 --- a/ts/src/driver.rs +++ b/ts/src/driver.rs @@ -18,10 +18,13 @@ pub fn run(args: Vec) -> ExitCode { // MIR without the checks of undefined behavior that debug builds add around raw // pointers, such as a box's: JS has no pointers to check. And with the cleanups a // panic unwinds through, which wasm32's default of aborting leaves out: a JS caller - // can catch a panic and go on. + // can catch a panic and go on. And with every call where the source has it, as a + // release build's inlining would not keep it, since a call is what a binding or a + // std function's translation stands in for. if job.is_some() { args.push("-Zub-checks=no".into()); args.push("-Cpanic=unwind".into()); + args.push("-Zinline-mir=no".into()); } Ok(job.map(|job| Box::new(job) as rust2_driver::rustc::Job)) }) diff --git a/ts/src/lower/bind.rs b/ts/src/lower/bind.rs index 93f1e7409..0472a4a0d 100644 --- a/ts/src/lower/bind.rs +++ b/ts/src/lower/bind.rs @@ -9,7 +9,7 @@ use std::path::{Component, Path, PathBuf}; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{CRATE_DEF_ID, DefId}; -use rustc_middle::ty::{self, Instance, InstanceKind, TyCtxt}; +use rustc_middle::ty::{self, Instance, InstanceKind, TyCtxt, TypeVisitableExt}; use rustc_span::Span; use super::{Translator, camel, short_name}; @@ -40,6 +40,8 @@ struct File { path: PathBuf, /// What its importers call it: the type bound to it, or its file's name. namespace: String, + /// Its file's name as an identifier. + prefix: String, } /// What the translation knows of the config's bindings. @@ -65,7 +67,10 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { t.failed = true; }; let Some(def_id) = self.resolve_bound(path) else { - fail(self, "no such item in the crate or a crate it depends on".into()); + fail( + self, + "no such item, or more than one, in the crate or a crate it depends on".into(), + ); continue; }; let kind = match tcx.def_kind(def_id) { @@ -98,10 +103,19 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { continue; } }; + if let Some(other) = self.bindings.items.get(&def_id) { + let msg = format!("`{}` binds the same item", other.path); + fail(self, msg); + continue; + } let name = tcx.item_name(def_id).to_string(); - let file = self.bindings.files.entry(module.clone()).or_insert_with(|| File { - path: bind.file.clone(), - namespace: ident(module.last().expect("a file name")), + let file = self.bindings.files.entry(module.clone()).or_insert_with(|| { + let prefix = ident(module.last().expect("a file name")); + File { + path: bind.file.clone(), + namespace: prefix.clone(), + prefix, + } }); if let Kind::Type { .. } = kind { let types = self @@ -114,7 +128,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { fail(self, format!("{} binds another type", bind.file.display())); continue; } - file.namespace = name; + file.namespace = super::escape(&name); } self.bindings.items.insert( def_id, @@ -125,6 +139,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { }, ); } + self.writes = super::copy::Writes::of_crate(tcx, &|def_id| self.bound(def_id)); } /// The item a binding's path names: from the crate root, or else from a crate the crate @@ -155,16 +170,28 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { /// Whether `def_id` is a bound type or function, or a function of a bound type in an /// impl of the type's own crate: translation never reads it. pub fn bound(&self, def_id: DefId) -> bool { + self.binder(def_id).is_some() + } + + /// The bound item whose binding stands in for `def_id`: itself, or the bound type an + /// impl of the type's own crate is of. A trait's provided function the impl doesn't + /// override, and a blanket impl's, are the trait's, as for any other type. + fn binder(&self, def_id: DefId) -> Option { let tcx = self.tcx; if self.bindings.items.contains_key(&def_id) { - return true; + return Some(def_id); } - let Some(imp) = tcx.impl_of_assoc(def_id) else { - return false; + let imp = tcx.impl_of_assoc(def_id)?; + let ty::Adt(adt, _) = tcx + .type_of(imp) + .instantiate_identity() + .skip_norm_wip() + .peel_refs() + .kind() + else { + return None; }; - let self_ty = tcx.type_of(imp).instantiate_identity().skip_norm_wip(); - matches!(self_ty.peel_refs().kind(), ty::Adt(adt, _) - if adt.did().krate == def_id.krate && self.bound_drops(adt.did()).is_some()) + (adt.did().krate == def_id.krate && self.bound_drops(adt.did()).is_some()).then_some(adt.did()) } /// Whether `def_id` is the crate's own, which it translates, rather than bound. @@ -172,6 +199,14 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { def_id.is_local() && !self.bindings.items.contains_key(&def_id) } + /// Why `def_id`, which isn't [`own`](Self::own), isn't the crate's to translate. + pub fn foreign(&self, def_id: DefId) -> &'static str { + match self.bindings.items.contains_key(&def_id) { + true => "bound to TypeScript", + false => "from another crate", + } + } + /// Whether a bound type's value drops through its `[Symbol.dispose]`, if it is one. pub fn bound_drops(&self, def_id: DefId) -> Option { match self.bindings.items.get(&def_id)?.kind { @@ -180,9 +215,16 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { } } - /// The symbol of a bound type, from its binding's file. - pub fn bound_type(&mut self, def_id: DefId, span: Span) -> Option { - self.bound_drops(def_id)?; + /// The symbol of a bound type, from its binding's file. `span` is where `args` reach it. + pub fn bound_type(&mut self, adt: ty::AdtDef<'tcx>, args: ty::GenericArgsRef<'tcx>, span: Span) -> Option { + let def_id = adt.did(); + let drop = self.bound_drops(def_id)?; + // What it holds drops only where it does. + if !drop && let Some(arg) = args.types().find(|&t| !t.has_param() && self.drops(t)) { + let path = &self.bindings.items[&def_id].path; + let msg = format!("`{path}` binds without drop, so it would never drop its `{arg}`"); + self.err(span, msg); + } if let Some(&sym) = self.bindings.types.get(&def_id) { return Some(sym); } @@ -190,19 +232,18 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { let name = self.tcx.item_name(def_id).to_string(); let sym = self.bound_sym(&module, name, SymKind::Struct, span); self.bindings.types.insert(def_id, sym); - // It takes as many type parameters as the Rust type. + // It takes as many type parameters as the Rust type, any of which meets the + // binding's constraints on them. let params = self.tcx.generics_of(def_id).own_counts().types; + let value = Type::Sym(sym, vec![Type::Builtin("any"); params]); + let copy = self.is_copy(self.tcx.type_of(def_id).instantiate_identity().skip_norm_wip()); + let flag = |b: bool| Type::Builtin(if b { "true" } else { "false" }); + let ret = Type::Rt("Bound", vec![value.clone(), flag(copy), flag(drop)]); let value = Param { name: "value".into(), - ty: Some(Type::Sym(sym, vec![Type::Builtin("unknown"); params])), - }; - let (ret, body) = match self.bound_drops(def_id) { - Some(true) => ( - Type::Builtin("Disposable"), - vec![StmtKind::Return(Some(Expr::ident("value"))).at(None)], - ), - _ => (Type::Builtin("void"), Vec::new()), + ty: Some(value), }; + let body = vec![StmtKind::Return(Some(Expr::ident("value"))).at(None)]; let what = format!( "the binding of `{}` in {}", self.bindings.items[&def_id].path, @@ -218,7 +259,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { let tcx = self.tcx; let module = self.bound_module(instance)?.clone(); let def_id = instance.def_id(); - let name = export_name(tcx, instance); + let name = export_name(tcx, def_id); let sym = match self.bindings.exports.get(&(module.clone(), name.clone())) { Some(&(other, sym)) if other == def_id => sym, Some(&(other, _)) => { @@ -234,6 +275,13 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { return None; } None => { + if let Some(param) = self.phantom(def_id) { + let msg = format!( + "a binding of `{}`, whose `{param}` no argument holds: its export can't call `{param}`'s impls", + tcx.def_path_str(def_id) + ); + self.err(tcx.def_span(def_id), msg); + } let sym = self.bound_sym(&module, name.clone(), SymKind::Function, span); self.bindings.exports.insert((module.clone(), name), (def_id, sym)); sym @@ -263,19 +311,31 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { let InstanceKind::Item(def_id) = instance.def else { return None; }; - if let Some(binding) = self.bindings.items.get(&def_id) { - return Some(&binding.module); - } - let ty::Adt(adt, _) = self.self_ty(instance)?.peel_refs().kind() else { - return None; - }; - // One its own crate defines: std's blanket impls and its traits' provided functions, - // such as `ToOwned` and `ne`, are std's, as for any other type. - if def_id.krate != adt.did().krate { - return None; - } - self.bound_drops(adt.did())?; - Some(&self.bindings.items[&adt.did()].module) + Some(&self.bindings.items[&self.binder(def_id)?].module) + } + + /// A type parameter of `def_id` with a trait bound that no argument's type holds: a + /// generic export sees each type argument only in the values it takes, so it has no + /// way to call that one's impls. + fn phantom(&self, def_id: DefId) -> Option { + let tcx = self.tcx; + let sig = tcx.fn_sig(def_id).instantiate_identity().skip_norm_wip().skip_binder(); + let held: Vec = sig + .inputs() + .iter() + .flat_map(|t| t.walk()) + .filter_map(|arg| match arg.as_type()?.kind() { + ty::Param(p) => Some(p.index), + _ => None, + }) + .collect(); + tcx.param_env(def_id).caller_bounds().find_map(|clause| { + let pred = clause.as_trait_clause()?.skip_binder(); + match pred.self_ty().kind() { + ty::Param(p) if !held.contains(&p.index) && !tcx.is_sizedness_trait(pred.def_id()) => Some(p.name), + _ => None, + } + }) } /// A symbol of a binding's file, named as the file exports it, which reaches the file. @@ -287,7 +347,8 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { Some(_) => Ok(String::new()), None => std::fs::read_to_string(self.config.dir.join(&file.path)), }; - let namespace = file.namespace.clone(); + let (namespace, prefix) = (file.namespace.clone(), file.prefix.clone()); + let source = self.config.dir.join(&file.path); let text = match text { Ok(text) => text, Err(err) => { @@ -296,7 +357,13 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { String::new() } }; - self.program.bound.insert(module.clone(), Bound { text, namespace }); + let bound = Bound { + source, + text, + namespace, + prefix, + }; + self.program.bound.insert(module.clone(), bound); } self.program.sym(Symbol { module: module.clone(), @@ -342,8 +409,9 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { /// The project `tsc` checks the check module in, beside it: the runtime's own settings, as /// `runtime/tsconfig.json` has them, but for the unused names a binding may leave, which -/// are its author's to lint. A project of its own, since `tsc` refuses to check a file it -/// names where a caller's `tsconfig.json` is above it. +/// are its author's to lint, and the ambient types a caller's `node_modules` may hold. A +/// project of its own, since `tsc` refuses to check a file it names where a caller's +/// `tsconfig.json` is above it. const TSCONFIG: &str = r#"{ "compilerOptions": { "strict": true, @@ -355,7 +423,9 @@ const TSCONFIG: &str = r#"{ "moduleResolution": "bundler", "allowImportingTsExtensions": true, "verbatimModuleSyntax": true, - "erasableSyntaxOnly": true + "erasableSyntaxOnly": true, + "skipLibCheck": true, + "types": [] }, "files": ["bind.ts"] } @@ -410,12 +480,19 @@ pub fn typecheck(tcx: TyCtxt<'_>, program: &Program, out: &Path) { } let text = String::from_utf8_lossy(&output.stdout); let prefix = format!("{name}("); - // Where the output is, which a message naming a module spells out. - let dir = format!("{}/", out.display()); + // Where the output is, which a message naming a module spells out, as given or as the + // path it resolves to. + let dirs: Vec = [Some(out.to_path_buf()), out.canonicalize().ok()] + .into_iter() + .flatten() + .map(|dir| format!("{}/", dir.display())) + .collect(); // Each item's instances of a generic function fail alike, so each failure is reported once. let mut reported = std::collections::HashSet::new(); for diagnostic in diagnostics(&text) { - let diagnostic = diagnostic.replace(&dir, ""); + // One in a binding's file names it from the runtime's directory. + let diagnostic = diagnostic.strip_prefix("../").unwrap_or(&diagnostic); + let diagnostic = dirs.iter().fold(diagnostic.to_string(), |d, dir| d.replace(dir, "")); let check = diagnostic .strip_prefix(&prefix) .and_then(|rest| rest.split([',', ')']).next()) @@ -467,24 +544,17 @@ pub fn check_module() -> ModPath { vec!["rt".into(), "bind".into()] } -/// The export standing in for `instance`'s function: its name, then the trait it +/// The export standing in for the function `def_id`: its name, then the trait it /// implements with the trait's type arguments as the impl names them, as a translated /// function's suffix has them, but not the type arguments of the type or the function, /// which a generic TS function takes. `Bytes::slice` is `slice`, and `Bytes`'s /// `PartialEq<[u8]>` is `eq$PartialEq_u8s`. -fn export_name<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> String { - let def_id = instance.def_id(); +fn export_name(tcx: TyCtxt<'_>, def_id: DefId) -> String { let base = camel(tcx.item_name(def_id).as_str()); - let trait_ref = match tcx.trait_impl_of_assoc(def_id) { - Some(imp) => Some(tcx.impl_trait_ref(imp).instantiate_identity().skip_norm_wip()), - // A trait's own method, which the impl did not override. - None => tcx - .trait_of_assoc(def_id) - .map(|trait_id| ty::TraitRef::from_assoc(tcx, trait_id, instance.args)), - }; - let Some(trait_ref) = trait_ref else { + let Some(imp) = tcx.trait_impl_of_assoc(def_id) else { return base; }; + let trait_ref = tcx.impl_trait_ref(imp).instantiate_identity().skip_norm_wip(); let mut parts = vec![tcx.item_name(trait_ref.def_id).to_string()]; let args = tcx .generics_of(trait_ref.def_id) @@ -504,9 +574,10 @@ pub fn resolve_in(tcx: TyCtxt<'_>, mut item: DefId, segments: &[&str]) -> Option None => tcx.module_children(item), }; let named = children.iter().filter_map(|child| match child.res { - // A tuple struct's constructor shares its name. + // A tuple struct's constructor shares its name, as a macro may. Res::Def(kind, def_id) - if child.ident.as_str() == *segment && !matches!(kind, DefKind::Ctor(..)) => + if child.ident.as_str() == *segment + && !matches!(kind, DefKind::Ctor(..) | DefKind::Macro(_)) => { Some((kind, def_id)) } @@ -518,12 +589,20 @@ pub fn resolve_in(tcx: TyCtxt<'_>, mut item: DefId, segments: &[&str]) -> Option let (first, rest): (Vec<_>, Vec<_>) = named.partition(|&(kind, _)| (kind == DefKind::Mod) != last); first.into_iter().chain(rest).next()?.1 } - DefKind::Struct | DefKind::Enum | DefKind::Union => tcx.inherent_impls(item).iter().find_map(|&imp| { - tcx.associated_items(imp) - .in_definition_order() - .find(|assoc| assoc.is_fn() && assoc.name().as_str() == *segment) - .map(|assoc| assoc.def_id) - })?, + // The one function of the name, which impls for different type arguments can + // each define. + DefKind::Struct | DefKind::Enum | DefKind::Union => { + let mut fns = tcx.inherent_impls(item).iter().flat_map(|&imp| { + tcx.associated_items(imp) + .in_definition_order() + .filter(|assoc| assoc.is_fn() && assoc.name().as_str() == *segment) + .map(|assoc| assoc.def_id) + }); + match (fns.next(), fns.next()) { + (Some(def_id), None) => def_id, + _ => return None, + } + } _ => return None, }; } @@ -548,7 +627,7 @@ fn module_of(file: &Path) -> Result { } } } - if module.first().is_some_and(|first| first == "rt") { + if module.first().is_some_and(|first| first.eq_ignore_ascii_case("rt")) { return Err(format!("{} would replace the runtime, rt.ts and rt/", file.display())); } Ok(module) @@ -568,6 +647,6 @@ fn ident(name: &str) -> String { .collect(); match name.starts_with(|c: char| c.is_ascii_digit()) { true => format!("_{name}"), - false => name, + false => super::escape(&name), } } diff --git a/ts/src/lower/body.rs b/ts/src/lower/body.rs index 3079648a7..845933f8a 100644 --- a/ts/src/lower/body.rs +++ b/ts/src/lower/body.rs @@ -177,7 +177,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { if !self.t.own(adt_id) { return self.err( span, - format!("constructing `{}` from another crate", tcx.def_path_str(adt_id)), + format!("constructing `{}` {}", tcx.def_path_str(adt_id), self.t.foreign(adt_id)), ); } let info = self.t.adt(adt, span); diff --git a/ts/src/lower/consts.rs b/ts/src/lower/consts.rs index c11e0784b..21268a308 100644 --- a/ts/src/lower/consts.rs +++ b/ts/src/lower/consts.rs @@ -125,7 +125,8 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { }; } if !self.t.own(adt.did()) { - return self.err(span, format!("a constant of type `{ty}` from another crate")); + let foreign = self.t.foreign(adt.did()); + return self.err(span, format!("a constant of type `{ty}` {foreign}")); } let info = self.t.adt(*adt, span); let def = adt.variant(variant); diff --git a/ts/src/lower/copy.rs b/ts/src/lower/copy.rs index b94cfe061..747f33372 100644 --- a/ts/src/lower/copy.rs +++ b/ts/src/lower/copy.rs @@ -213,6 +213,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { /// or one kept anywhere but in a local, whose uses tell. Bodies are read as written, over /// their own type parameters, so a write into a type parameter stands for every type its /// bounds let it be. +#[derive(Default)] pub struct Writes<'tcx> { /// Each type written without type parameters, an array as a slice of its element, /// since the two share storage in JS. @@ -224,24 +225,22 @@ pub struct Writes<'tcx> { } impl<'tcx> Writes<'tcx> { - /// The writes of every function, closure, and coroutine body in the crate. - pub fn of_crate(tcx: TyCtxt<'tcx>) -> Self { - let mut writes = Writes { - concrete: FxHashSet::default(), - generic: FxIndexSet::default(), - found: RefCell::default(), - }; + /// The writes of every function, closure, and coroutine body in the crate but those + /// `bound` says a binding stands in for, which count as code it doesn't read. + pub fn of_crate(tcx: TyCtxt<'tcx>, bound: &dyn Fn(DefId) -> bool) -> Self { + let mut writes = Writes::default(); for &def_id in tcx.mir_keys(()) { - if !tcx.def_kind(def_id).is_fn_like() { + let def_id = def_id.to_def_id(); + if !tcx.def_kind(def_id).is_fn_like() || bound(def_id) { continue; } - let def_id = def_id.to_def_id(); let body = tcx.optimized_mir(def_id); BodyWrites { tcx, body, def_id, writes: &mut writes, + bound, } .visit_body(body); } @@ -365,6 +364,7 @@ struct BodyWrites<'w, 'm, 'tcx> { body: &'m mir::Body<'tcx>, def_id: DefId, writes: &'w mut Writes<'tcx>, + bound: &'w dyn Fn(DefId) -> bool, } impl<'tcx> BodyWrites<'_, '_, 'tcx> { @@ -400,7 +400,7 @@ impl<'tcx> BodyWrites<'_, '_, 'tcx> { let env = ty::TypingEnv::post_analysis(self.tcx, self.def_id); matches!( ty::Instance::try_resolve(self.tcx, env, def_id, args), - Ok(Some(instance)) if matches!(instance.def, ty::InstanceKind::Item(did) if did.is_local()) + Ok(Some(instance)) if matches!(instance.def, ty::InstanceKind::Item(did) if did.is_local() && !(self.bound)(did)) ) } } diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 5a990ca99..eb6134879 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -116,6 +116,7 @@ pub fn into_iter<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, value: Expr, sp let tcx = b.t.tcx; no_drops(b, ty, span); if b.t.iter_item(ty).is_some() { + not_bound(b, ty, span); return Some(match own_next(b, ty) { Some(instance) => { let sym = b.t.instance(instance); @@ -167,6 +168,7 @@ fn by_ref(iter: Expr, elem: Ty<'_>) -> Expr { /// A function doing `into_iter` on each `ty`, or `None` where it is the value itself. fn into_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) -> Option> { if b.t.iter_item(ty).is_some() && own_next(b, ty).is_none() { + not_bound(b, ty, span); return Some(None); } let param = Param { @@ -188,6 +190,16 @@ fn no_drops<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) { } } +/// Refuse iterating `ty`, an iterator, if a binding stands in for it: it is no `rt.Iter`, +/// which std's adaptors and `for` take every iterator but the crate's own for. +fn not_bound<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) { + if let ty::Adt(adt, _) = ty.peel_refs().kind() + && b.t.bound_drops(adt.did()).is_some() + { + b.t.err(span, "iterating a bound type, which is not supported yet"); + } +} + /// Refuse an `Option` of `ty` that `T | undefined` can't hold, as the type would be. pub fn option_ok<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) { if b.t.erases_to(ty).undefined { @@ -283,6 +295,7 @@ pub fn call<'tcx>( _ => return None, }; let item = b.t.iter_item(self_ty.peel_refs())?; + not_bound(b, self_ty, span); // The crate's own `next` hands its item over; every other call may leave items behind. if name != "by_ref" && !(name == "next" && own_next(b, self_ty).is_some()) { no_drops(b, item, span); diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index e6242ba6a..4fdfe32fb 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -361,6 +361,8 @@ pub fn clone_copies<'tcx>(t: &super::Translator<'_, 'tcx>, ty: Ty<'tcx>) -> bool ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Ref(..) | ty::FnPtr(..) | ty::Never => true, ty::Tuple(fields) => fields.iter().all(|field| clone_copies(t, field)), ty::Array(elem, _) => clone_copies(t, *elem), + // A binding's `Clone` is its own. + ty::Adt(adt, _) if t.bound_drops(adt.did()).is_some() => false, ty::Adt(adt, args) if !adt.did().is_local() => args.types().all(|arg| clone_copies(t, arg)), ty::Adt(adt, args) => { let Some(clone) = tcx.lang_items().clone_fn() else { diff --git a/ts/src/lower/mir/place.rs b/ts/src/lower/mir/place.rs index 159a2432d..c7cdd3c40 100644 --- a/ts/src/lower/mir/place.rs +++ b/ts/src/lower/mir/place.rs @@ -434,8 +434,10 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { let pointee = place.ty(&self.mir.local_decls, self.tcx).ty; let alias = matches!(self.slots[place.local], Slot::Alias(_)); let target = self.local_expr(place.local, span); + // A binding's value may keep what it holds where `Object.assign` can't reach. + let bound = matches!(pointee.kind(), ty::Adt(adt, _) if self.b.t.bound_drops(adt.did()).is_some()); let e = match self.b.t.object_kind(pointee) { - Some(ObjectKind::Class) if !shared(self, pointee) => { + Some(ObjectKind::Class) if !shared(self, pointee) && !bound => { Some(Expr::ident("Object").member("assign").call(vec![target, value.clone()])) } Some(ObjectKind::Bytes) => Some(target.member("set").call(vec![value.clone()])), diff --git a/ts/src/lower/mir/rvalue.rs b/ts/src/lower/mir/rvalue.rs index 18b73f85f..8af99128e 100644 --- a/ts/src/lower/mir/rvalue.rs +++ b/ts/src/lower/mir/rvalue.rs @@ -387,7 +387,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { _ => {} } if !self.b.t.own(def_id) { - return self.err(span, format!("constructing `{ty}` from another crate")); + return self.err(span, format!("constructing `{ty}` {}", self.b.t.foreign(def_id))); } let info = self.b.t.adt(adt, span); match info.repr { diff --git a/ts/src/lower/mod.rs b/ts/src/lower/mod.rs index 95130add9..115375937 100644 --- a/ts/src/lower/mod.rs +++ b/ts/src/lower/mod.rs @@ -226,7 +226,8 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { coroutines: Default::default(), drop_shims: Default::default(), copy_shims: Default::default(), - writes: copy::Writes::of_crate(tcx), + // Once the bindings are known. + writes: Default::default(), held: false, bindings: Default::default(), }) @@ -1028,7 +1029,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { fn adt_ty(&mut self, adt: AdtDef<'tcx>, args: GenericArgsRef<'tcx>, span: Span) -> Type { let tcx = self.tcx; - if let Some(sym) = self.bound_type(adt.did(), span) { + if let Some(sym) = self.bound_type(adt, args, span) { let args = args.types().map(|t| self.ty(t, span)).collect(); return Type::Sym(sym, args); } diff --git a/ts/src/program.rs b/ts/src/program.rs index 71341c418..76bd4ad77 100644 --- a/ts/src/program.rs +++ b/ts/src/program.rs @@ -100,9 +100,14 @@ pub struct Program { /// A binding's file, which callers import whole, as they do a type's module. #[derive(Debug)] pub struct Bound { + /// The file it is copied from. + pub source: PathBuf, pub text: String, /// What callers import it as: the type bound to it, or else its file's name. pub namespace: String, + /// Its file's name as an identifier, which callers prefix the namespace with where it + /// is taken. + pub prefix: String, } /// A function that type-checks only if a binding is what translated code takes it for. @@ -199,6 +204,19 @@ impl Program { /// Write every module, the runtime, and their source maps to `out`. pub fn write(&self, out: &Path) -> Result<(), String> { + // A copy, or a clean, would replace or remove the source. + if let Ok(dir) = out.canonicalize() + && let Some(bound) = self + .bound + .values() + .find(|b| b.source.canonicalize().is_ok_and(|s| s.starts_with(&dir))) + { + return Err(format!( + "the binding {} is inside the output, {}, which a translation replaces", + bound.source.display(), + out.display() + )); + } clean(out)?; let mut modules: BTreeMap<&ModPath, Vec<&Item>> = BTreeMap::new(); for item in &self.items { @@ -250,7 +268,7 @@ impl Program { /// file. The name its importers give it, and what they prefix that with where it is taken. pub fn namespace<'p>(&'p self, module: &'p ModPath) -> Option<(&'p str, &'p str)> { if let Some(bound) = self.bound.get(module) { - return Some((module.last().map_or("lib", String::as_str), &bound.namespace)); + return Some((&bound.prefix, &bound.namespace)); } // `decode.Decoder` binds `Decoder`, or `decode_Decoder` if that is taken. module.last()?.split_once('.') From d339b3e3f22183a002ed08c79e31422f100e04e8 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 27 Oct 2026 14:59:16 -0700 Subject: [PATCH 5/8] feat(ts): census the model from its roots, and require tsc 7 A census now starts from the config's roots, as translation does, and makes every item a root only for a config that names none. ts/moq/model.toml roots the census at the model's public types with their boundaries bound. moq/net.toml keeps the whole-crate census, and nightly reports both. A rooted census lists its roots. Checking bindings now requires tsc 7.0 or later, the flake's, and refuses an older one up front. A bound type can't key an ordered collection through its fields. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3 --- .github/workflows/nightly.yml | 2 +- quest/m2/ts-model/README.md | 4 +- ts/README.md | 24 +++---- ts/fixtures/reject/census.toml | 4 ++ ts/justfile | 5 +- ts/moq/model.toml | 39 ++++++++++++ ts/moq/net.toml | 4 ++ ts/src/census.rs | 10 ++- ts/src/driver.rs | 2 +- ts/src/lower/bind.rs | 29 +++++++++ ts/src/lower/collections.rs | 2 +- ts/src/lower/mod.rs | 112 ++++++++++++++++++++------------- ts/tests/fixtures.rs | 21 ++++++- 13 files changed, 190 insertions(+), 68 deletions(-) create mode 100644 ts/fixtures/reject/census.toml create mode 100644 ts/moq/model.toml create mode 100644 ts/moq/net.toml diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index f3327cc05..0eb6b6383 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -210,7 +210,7 @@ jobs: # The ranked tables: the spans would pass the summary's size limit. - name: Summarize - run: awk 'FNR == 1 { spans = 0 } /^## Spans/ { spans = 1 } !spans' ts/target/census/moq-net.md ts/target/census/kio.md >> "$GITHUB_STEP_SUMMARY" + run: awk 'FNR == 1 { spans = 0 } /^## Spans/ { spans = 1 } !spans' ts/target/census/moq-net.md ts/target/census/model.md ts/target/census/kio.md >> "$GITHUB_STEP_SUMMARY" - name: Upload uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 diff --git a/quest/m2/ts-model/README.md b/quest/m2/ts-model/README.md index fd393c0ff..39ee019c8 100644 --- a/quest/m2/ts-model/README.md +++ b/quest/m2/ts-model/README.md @@ -35,8 +35,8 @@ Decided with the maintainer on 2026-10-27, from a census of the model's 22 publi it; only true boundaries (`Box`, clocks, `rand`, DEFLATE) bind to hand-written TS. A dependency containing `unsafe` is bound by the consumer, until [unsafe code the translator can model](/quest/m3/ts-unsafe.md) translates. -- Check in `ts/moq/model.toml`, rooted at those 22 types, and have nightly's census - report it beside the whole crate, so each child measures this target. +- Each child measures this target by the census of `ts/moq/model.toml`, rooted at those + types with the boundaries bound, which nightly reports beside the whole crate. - The end-to-end work stays here: the model's roots, its differential harness, and the `frames.ts` workload. diff --git a/ts/README.md b/ts/README.md index 2d59d861e..aa683014e 100644 --- a/ts/README.md +++ b/ts/README.md @@ -39,16 +39,18 @@ moq consumes it as the `ts` package of this repository's flake. In this reposito ## Census -`--census file.md` translates nothing. It treats every item in the library as a root, -except derived impls, which come along only when called or printable, and writes a Markdown report of -every construct translation refuses, with counts and spans. Refusals are ranked by -construct, by the type or module they name, and by each type or path. A generic function -that no instance reaches is lowered over its own type parameters, refusing nothing that -hinges on what they are, since each instance decides that. A census succeeds whatever -it finds; translation still fails on any refusal. - -`just ts census` runs it on moq-net and kio at the pinned moq revision, into -`target/census`, and the nightly job publishes their tables. +`--census file.md` translates nothing. It writes a Markdown report of every construct +translation refuses, with counts and spans, ranked by construct, by the type or module +they name, and by each type or path. It starts from the config's roots, as translation +does. A config that names none makes every item in the library a root, except derived +impls, which come along only when called or printable, and lowers a generic function +that no instance reaches over its own type parameters, refusing nothing that hinges on +what they are, since each instance decides that. A census succeeds whatever it finds; +translation still fails on any refusal. + +`just ts census` runs it at the pinned moq revision, into `target/census`, on moq-net +whole (`moq/net.toml`), on its model's public types with their boundaries bound +(`moq/model.toml`), and on kio, and the nightly job publishes their tables. ## Output @@ -242,7 +244,7 @@ binding never changes a `Copy` value in place. Nor does a binding change in plac takes by value or by `&`, or share with a clone what a `&mut` changes, and an integer it returns is in its Rust type's range. -Once the output is written, `tsc` 5.8 or later, from `PATH`, checks each export against the +Once the output is written, `tsc` 7.0 or later, from `PATH`, checks each export against the translated signature of every instance translation calls, and each bound type's value. A missing or mismatched binding fails the translation at the Rust item's span, and the output is removed. Only the binding files translation reaches are copied, so one imports diff --git a/ts/fixtures/reject/census.toml b/ts/fixtures/reject/census.toml new file mode 100644 index 000000000..53df8b231 --- /dev/null +++ b/ts/fixtures/reject/census.toml @@ -0,0 +1,4 @@ +# The census's: no roots, so every item is one. +package = "reject-fixture" +roots = [] +printable = ["Meters"] diff --git a/ts/justfile b/ts/justfile index 99a50c109..c678a7b24 100644 --- a/ts/justfile +++ b/ts/justfile @@ -52,7 +52,7 @@ moq checkout="": printf '{ "extends": "%s/runtime/tsconfig.json", "files": [], "include": ["**/*.ts"] }\n' "$PWD" > "$out/tsconfig.json" tsc -p "$out" -# Census what translation refuses in moq-net and kio into target/census, from the pinned moq revision or a checkout. +# Census what translation refuses in moq-net, its model, and kio into target/census, from the pinned moq revision or a checkout. census checkout="": #!/usr/bin/env bash set -euo pipefail @@ -61,7 +61,8 @@ census checkout="": just moq-pin src=target/moq fi - cargo run --locked --quiet -- --config moq/ts.toml --manifest-path "$src/rs/moq-net/Cargo.toml" --census target/census/moq-net.md -- --package moq-wasm + cargo run --locked --quiet -- --config moq/net.toml --manifest-path "$src/rs/moq-net/Cargo.toml" --census target/census/moq-net.md -- --package moq-wasm + cargo run --locked --quiet -- --config moq/model.toml --manifest-path "$src/rs/moq-net/Cargo.toml" --census target/census/model.md -- --package moq-wasm cargo run --locked --quiet -- --config moq/kio.toml --manifest-path "$src/rs/kio/Cargo.toml" --census target/census/kio.md # Check out the pinned moq revision in target/moq. diff --git a/ts/moq/model.toml b/ts/moq/model.toml new file mode 100644 index 000000000..042528810 --- /dev/null +++ b/ts/moq/model.toml @@ -0,0 +1,39 @@ +# moq-net's model, for the census: its public types, with what the subset refuses for good +# bound. moq writes the binding files; a census reads none. +package = "moq-net" +roots = [ + "model::origin::Producer", + "model::origin::Consumer", + "model::broadcast::Producer", + "model::broadcast::Consumer", + "model::track::Producer", + "model::track::Consumer", + "model::group::Producer", + "model::group::Consumer", + "model::frame::Producer", + "model::frame::Consumer", + "model::bandwidth::Producer", + "model::bandwidth::Consumer", + "model::cache::Pool", + "model::broadcast::listing::Tracks", + "model::time::Timestamp", +] +ignore-macros = ["tracing::trace", "tracing::debug", "tracing::info", "tracing::warn", "tracing::error"] + +[bind] +# `unsafe` inside. +"bytes::Bytes" = "bind/bytes.ts" +"bytes::BytesMut" = "bind/bytes-mut.ts" +# Randomness, DEFLATE, and the clock, which JS has its own of. +"rand::rng" = "bind/rand.ts" +"rand::rngs::ThreadRng" = "bind/rand.ts" +"model::flate::Encoder" = "bind/encoder.ts" +"model::flate::Decoder" = "bind/decoder.ts" +"model::clock::now" = "bind/clock.ts" +"model::clock::wall" = "bind/clock.ts" +"model::clock::anchor" = "bind/clock.ts" +# Boxed futures, which JS runs as promises. +"util::Tasks" = { file = "bind/tasks.ts", drop = true } +"util::TasksWeak" = "bind/tasks-weak.ts" +"util::TaskSet" = { file = "bind/task-set.ts", drop = true } +"kio::Tasks" = { file = "bind/kio-tasks.ts", drop = true } diff --git a/ts/moq/net.toml b/ts/moq/net.toml new file mode 100644 index 000000000..e81cece7d --- /dev/null +++ b/ts/moq/net.toml @@ -0,0 +1,4 @@ +# moq-net whole, for the census: it names no roots, so every item is one. +package = "moq-net" +roots = [] +ignore-macros = ["tracing::trace", "tracing::debug", "tracing::info", "tracing::warn", "tracing::error"] diff --git a/ts/src/census.rs b/ts/src/census.rs index efc4a8c9e..579ed8af0 100644 --- a/ts/src/census.rs +++ b/ts/src/census.rs @@ -41,9 +41,9 @@ impl Refusal { type Groups<'a, K> = BTreeMap>; impl Census { - /// The census of `krate` as Markdown: refusals ranked by construct, by API, and by - /// type or path, then every span. - pub fn render(&self, krate: &str) -> String { + /// The census of `krate` from `roots`, or every item where there are none, as Markdown: + /// refusals ranked by construct, by API, and by type or path, then every span. + pub fn render(&self, krate: &str, roots: &[String]) -> String { let mut refusals = self.refusals.clone(); // The crate's own code first, so a group's first span is in it when it can be. refusals.sort_by(|a, b| (Path::new(&a.file).is_absolute(), a).cmp(&(Path::new(&b.file).is_absolute(), b))); @@ -68,6 +68,10 @@ impl Census { let mut out = String::new(); let _ = writeln!(out, "# Census of `{krate}`\n"); + if !roots.is_empty() { + let roots: Vec = roots.iter().map(|root| format!("`{root}`")).collect(); + let _ = writeln!(out, "- Roots: {}", roots.join(", ")); + } let _ = writeln!( out, "- Refusals: {}\n- Function instances: {}\n- Generic functions no instance reached, lowered over their type parameters: {}", diff --git a/ts/src/driver.rs b/ts/src/driver.rs index db8e5e2f6..bf146bd9f 100644 --- a/ts/src/driver.rs +++ b/ts/src/driver.rs @@ -87,7 +87,7 @@ impl Callbacks for Translate { _ => Ok(()), }, Output::Census(path) => match lower::census(tcx, &self.config) { - Some(census) => program::write_file(path, &census.render(&self.config.package)), + Some(census) => program::write_file(path, &census.render(&self.config.package, &self.config.roots)), None => Ok(()), }, }; diff --git a/ts/src/lower/bind.rs b/ts/src/lower/bind.rs index 0472a4a0d..eb1172fe6 100644 --- a/ts/src/lower/bind.rs +++ b/ts/src/lower/bind.rs @@ -443,6 +443,9 @@ pub fn typecheck(tcx: TyCtxt<'_>, program: &Program, out: &Path) { tcx.dcx().err(format!("rust2-ts: {msg}")); _ = crate::program::clean(out); }; + if let Err(err) = tsc_version() { + return failed(err); + } let items: Vec<&Item> = program.checks.iter().map(|check| &check.item).collect(); let printed = match crate::print::module(program, &module, &items, &path) { Ok(printed) => printed, @@ -522,6 +525,32 @@ pub fn typecheck(tcx: TyCtxt<'_>, program: &Program, out: &Path) { _ = crate::program::clean(out); } +/// The oldest `tsc` the check project is written for. +const TSC_MAJOR: u32 = 7; + +/// Refuse a `tsc` on `PATH` older than [`TSC_MAJOR`], which would fail the check project's +/// options rather than the bindings. +fn tsc_version() -> Result<(), String> { + let output = std::process::Command::new("tsc") + .arg("--version") + .output() + .map_err(|err| format!("running tsc, which checks the bindings: {err}"))?; + let text = String::from_utf8_lossy(&output.stdout); + // `Version 7.0.2`. + let major = text + .split_whitespace() + .last() + .and_then(|version| version.split('.').next()) + .and_then(|major| major.parse::().ok()); + match major { + Some(major) if major >= TSC_MAJOR => Ok(()), + _ => Err(format!( + "tsc {}, which checks the bindings, must be {TSC_MAJOR}.0 or later", + text.trim() + )), + } +} + /// Each of `tsc`'s diagnostics, with the lines that go on with it. fn diagnostics(text: &str) -> Vec { let mut all: Vec = Vec::new(); diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index 600d9057a..f566e8394 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -658,7 +658,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(adt, args) if adt.did().is_local() => { + ty::Adt(adt, args) if b.t.own(adt.did()) => { let derived = |tr: &str, item: &str, args: &[ty::GenericArg<'tcx>]| { let f = trait_fn(tcx, tr, item); Instance::try_resolve(tcx, b.t.typing_env_pub(), f, tcx.mk_args(args)) diff --git a/ts/src/lower/mod.rs b/ts/src/lower/mod.rs index 599655dbe..eae55bafd 100644 --- a/ts/src/lower/mod.rs +++ b/ts/src/lower/mod.rs @@ -52,55 +52,25 @@ pub fn translate<'tcx>(tcx: TyCtxt<'tcx>, config: &Config) -> Option { Some(t.program) } -/// Every construct translation refuses in the crate, reached from each of its items -/// as if each were a root. +/// Every construct translation refuses in the crate: in what the config's roots reach, as +/// translation does, or where it names none, in what each item reaches as if each were a +/// root. pub fn census<'tcx>(tcx: TyCtxt<'tcx>, config: &Config) -> Option { let mut t = Translator::new(tcx, config, Some(Vec::new()))?; t.bind(); - for def_id in tcx.hir_crate_items(()).definitions() { - if matches!(tcx.def_kind(def_id), DefKind::Struct | DefKind::Enum | DefKind::Union) - && !t.bound(def_id.to_def_id()) - { - t.adt(tcx.adt_def(def_id), tcx.def_span(def_id)); + let polymorphic = match config.roots.is_empty() { + true => t.every_item(), + false => { + for root in &config.roots { + t.root(root); + } + for path in &config.printable { + t.printable(path); + } + t.drain(); + 0 } - } - let functions = |tcx: TyCtxt<'tcx>| { - tcx.hir_body_owners() - .filter(move |&def_id| matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn)) }; - // A derived impl comes along only when called or printable, as in translation. - for path in &config.printable { - t.printable(path); - } - let roots: Vec = functions(tcx) - .map(LocalDefId::to_def_id) - .filter(|&def_id| { - !tcx.impl_of_assoc(def_id) - .is_some_and(|imp| tcx.is_automatically_derived(imp)) - && !t.bound(def_id) - }) - .collect(); - let generic = |def_id: DefId| tcx.generics_of(def_id).requires_monomorphization(tcx); - for &def_id in roots.iter().filter(|&&def_id| !generic(def_id)) { - t.instance(Instance::mono(tcx, def_id)); - } - t.drain(); - // A generic function no instance reached is lowered over its own type parameters, so - // what it refuses whatever they are is counted too. - let mut polymorphic = 0; - for &def_id in roots.iter().filter(|&&def_id| generic(def_id)) { - let lowered = |instance: &Instance<'tcx>| instance.def_id() == def_id && !instance.args.has_param(); - if t.instances.keys().any(lowered) { - continue; - } - polymorphic += 1; - t.env = TypingEnv::post_analysis(tcx, def_id); - let instance = Instance::new_raw(def_id, ty::GenericArgs::identity_for_item(tcx, def_id)); - let sym = t.instance(instance); - t.queue.retain(|&(queued, _)| queued != instance); - t.function(instance, sym); - t.drain(); - } let sm = tcx.sess.source_map(); let mut seen = rustc_data_structures::fx::FxHashSet::default(); let mut refusals = Vec::new(); @@ -130,6 +100,60 @@ pub fn census<'tcx>(tcx: TyCtxt<'tcx>, config: &Config) -> Option { }) } +impl<'tcx> Translator<'_, 'tcx> { + /// Lower what each of the crate's items reaches as if each were a root, returning how + /// many generic functions no instance reached, lowered over their type parameters. + fn every_item(&mut self) -> usize { + let tcx = self.tcx; + let t = self; + for def_id in tcx.hir_crate_items(()).definitions() { + if matches!(tcx.def_kind(def_id), DefKind::Struct | DefKind::Enum | DefKind::Union) + && !t.bound(def_id.to_def_id()) + { + t.adt(tcx.adt_def(def_id), tcx.def_span(def_id)); + } + } + let functions = |tcx: TyCtxt<'tcx>| { + tcx.hir_body_owners() + .filter(move |&def_id| matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn)) + }; + // A derived impl comes along only when called or printable, as in translation. + for path in &t.config.printable { + t.printable(path); + } + let roots: Vec = functions(tcx) + .map(LocalDefId::to_def_id) + .filter(|&def_id| { + !tcx.impl_of_assoc(def_id) + .is_some_and(|imp| tcx.is_automatically_derived(imp)) + && !t.bound(def_id) + }) + .collect(); + let generic = |def_id: DefId| tcx.generics_of(def_id).requires_monomorphization(tcx); + for &def_id in roots.iter().filter(|&&def_id| !generic(def_id)) { + t.instance(Instance::mono(tcx, def_id)); + } + t.drain(); + // A generic function no instance reached is lowered over its own type parameters, so + // what it refuses whatever they are is counted too. + let mut polymorphic = 0; + for &def_id in roots.iter().filter(|&&def_id| generic(def_id)) { + let lowered = |instance: &Instance<'tcx>| instance.def_id() == def_id && !instance.args.has_param(); + if t.instances.keys().any(lowered) { + continue; + } + polymorphic += 1; + t.env = TypingEnv::post_analysis(tcx, def_id); + let instance = Instance::new_raw(def_id, ty::GenericArgs::identity_for_item(tcx, def_id)); + let sym = t.instance(instance); + t.queue.retain(|&(queued, _)| queued != instance); + t.function(instance, sym); + t.drain(); + } + polymorphic + } +} + pub struct Translator<'a, 'tcx> { pub tcx: TyCtxt<'tcx>, config: &'a Config, diff --git a/ts/tests/fixtures.rs b/ts/tests/fixtures.rs index b7e4499a0..04a6bff75 100644 --- a/ts/tests/fixtures.rs +++ b/ts/tests/fixtures.rs @@ -499,6 +499,21 @@ fn bind() { ); rejected(&out, &output, &fixture("bind").join(format!("{config}.txt"))); } + // A census starts from the roots, as translation does: `harness::refused`, which no + // root reaches, refuses nothing in it. + let census = scratch().join("bind").join("census.md"); + let config = fixture("bind").join("ts.toml"); + let output = driver("bind", &config, &["--census".as_ref(), census.as_ref()], "census"); + assert!( + output.status.success(), + "the census of bind failed:\n{}", + String::from_utf8_lossy(&output.stderr) + ); + let got = std::fs::read_to_string(&census).unwrap(); + assert!( + got.contains("\n- Refusals: 0\n"), + "the census of bind's roots refused:\n{got}" + ); } /// The errors of a failed translation, each with where it points, against `expected`. @@ -542,12 +557,12 @@ fn rejected(out: &Path, output: &Output, expected: &Path) { ); } -/// A census ranks every refusal in the crate, reached from each of its items, and -/// succeeds where translation fails. +/// A census of a config without roots ranks every refusal in the crate, reached from each +/// of its items, and succeeds where translation fails. #[test] fn census() { let census = scratch().join("reject").join("census.md"); - let config = fixture("reject").join("ts.toml"); + let config = fixture("reject").join("census.toml"); let output = driver("reject", &config, &["--census".as_ref(), census.as_ref()], "census"); assert!( output.status.success(), From 8520951e7dfbca70cad160ed74dedb3d7b6909dd Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 27 Oct 2026 14:59:38 -0700 Subject: [PATCH 6/8] docs(ts): migrate a census of roots Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3 --- ts/MIGRATE.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ts/MIGRATE.md b/ts/MIGRATE.md index ce202373c..729622694 100644 --- a/ts/MIGRATE.md +++ b/ts/MIGRATE.md @@ -2,6 +2,8 @@ ## Unreleased +- `--census` starts from the config's roots, as translation does. For the census of every + item, census with a config that names no roots: `roots = []`. - A struct is a plain object typed by an interface, unless it may drop: build one as a literal, `{ subscribe, sequence, frameStart: 0 }` rather than `new Group({ subscribe, sequence, frameStart: 0 })`. From 343b98dd9a8cdf7a04a6261222e2646bc443b81e Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 27 Oct 2026 15:09:15 -0700 Subject: [PATCH 7/8] fix(ts): refuse a bound function's const parameter no argument holds One export serves every instance, so `value::<1>()` and `value::<2>()` would call the same export, which has no way to tell them apart. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3 --- ts/fixtures/bind/refused.toml | 1 + ts/fixtures/bind/refused.txt | 5 +++-- ts/fixtures/bind/src/harness.rs | 3 ++- ts/fixtures/bind/src/raw.rs | 5 +++++ ts/src/lower/bind.rs | 29 +++++++++++++++++++++-------- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/ts/fixtures/bind/refused.toml b/ts/fixtures/bind/refused.toml index af038e9b1..2d7142859 100644 --- a/ts/fixtures/bind/refused.toml +++ b/ts/fixtures/bind/refused.toml @@ -18,3 +18,4 @@ roots = ["harness::run", "harness::refused"] "raw::checksum" = "bind/checksum.ts" "raw::Held" = "bind/tally.ts" "raw::zero" = "bind/checksum.ts" +"raw::value" = "bind/checksum.ts" diff --git a/ts/fixtures/bind/refused.txt b/ts/fixtures/bind/refused.txt index 972bfe452..42facb127 100644 --- a/ts/fixtures/bind/refused.txt +++ b/ts/fixtures/bind/refused.txt @@ -11,9 +11,10 @@ binding `std::vec::Vec`: std's type has a representation of its own reading bind/missing.ts: No such file or directory (os error 2) at fixtures/bind/src/harness.rs:25:12 unsupported type `*const u32` at fixtures/bind/src/raw.rs:27:2 `raw::Held` binds without drop, so it would never drop its `raw::Tally` at fixtures/bind/src/harness.rs:52:13 -a binding of `raw::zero`, whose `T` no argument holds: its export can't call `T`'s impls at fixtures/bind/src/raw.rs:51:1 +a binding of `raw::value`, whose `N` no argument holds: one export for every instance can't recover it at fixtures/bind/src/raw.rs:56:1 +a binding of `raw::zero`, whose `T` no argument holds: one export for every instance can't recover it at fixtures/bind/src/raw.rs:51:1 `raw::Held` binds without drop, so it would never drop its `raw::Tally` at fixtures/bind/src/harness.rs:52:6 unsupported call to `std::sync::Arc::::from_raw` at fixtures/bind/src/raw.rs:46:17 unsupported call to `std::sync::Arc::::into_raw` at fixtures/bind/src/raw.rs:33:11 a raw pointer dereferenced at fixtures/bind/src/raw.rs:39:12 -an assignment through a reference to `bytes::Bytes` at fixtures/bind/src/harness.rs:58:2 +an assignment through a reference to `bytes::Bytes` at fixtures/bind/src/harness.rs:59:2 diff --git a/ts/fixtures/bind/src/harness.rs b/ts/fixtures/bind/src/harness.rs index 805745ddf..78ca1c637 100644 --- a/ts/fixtures/bind/src/harness.rs +++ b/ts/fixtures/bind/src/harness.rs @@ -51,7 +51,8 @@ pub fn refused(count: &Arc) -> u8 { replace(&mut bytes, Bytes::from_static(b"replaced")); let held = crate::raw::Held::new(Tally::new(count)); drop(held); - crate::raw::zero::() + bytes.len() as u8 + let values = crate::raw::value::<1>() + crate::raw::value::<2>(); + crate::raw::zero::() + bytes.len() as u8 + values as u8 } fn replace(target: &mut Bytes, value: Bytes) { diff --git a/ts/fixtures/bind/src/raw.rs b/ts/fixtures/bind/src/raw.rs index 1f09e66fe..e3810bd4e 100644 --- a/ts/fixtures/bind/src/raw.rs +++ b/ts/fixtures/bind/src/raw.rs @@ -52,6 +52,11 @@ pub fn zero() -> T { T::default() } +/// A value of its const argument alone, which a generic TS function can't read. +pub fn value() -> u32 { + N +} + /// A value it holds, and drops where Rust drops it. pub struct Held(T); diff --git a/ts/src/lower/bind.rs b/ts/src/lower/bind.rs index eb1172fe6..ecfe78016 100644 --- a/ts/src/lower/bind.rs +++ b/ts/src/lower/bind.rs @@ -277,7 +277,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { None => { if let Some(param) = self.phantom(def_id) { let msg = format!( - "a binding of `{}`, whose `{param}` no argument holds: its export can't call `{param}`'s impls", + "a binding of `{}`, whose `{param}` no argument holds: one export for every instance can't recover it", tcx.def_path_str(def_id) ); self.err(tcx.def_span(def_id), msg); @@ -314,9 +314,10 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { Some(&self.bindings.items[&self.binder(def_id)?].module) } - /// A type parameter of `def_id` with a trait bound that no argument's type holds: a - /// generic export sees each type argument only in the values it takes, so it has no - /// way to call that one's impls. + /// A parameter of `def_id` that no argument's type holds, and that one export serving + /// every instance would need, seeing each generic argument only in the values it takes: + /// a const parameter, whose value it can't read, or a type parameter with a trait bound, + /// whose impls it can't call. fn phantom(&self, def_id: DefId) -> Option { let tcx = self.tcx; let sig = tcx.fn_sig(def_id).instantiate_identity().skip_norm_wip().skip_binder(); @@ -324,11 +325,23 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { .inputs() .iter() .flat_map(|t| t.walk()) - .filter_map(|arg| match arg.as_type()?.kind() { - ty::Param(p) => Some(p.index), - _ => None, - }) + .filter_map( + |arg| match (arg.as_type().map(|t| t.kind()), arg.as_const().map(|c| c.kind())) { + (Some(ty::Param(p)), _) => Some(p.index), + (_, Some(ty::ConstKind::Param(p))) => Some(p.index), + _ => None, + }, + ) .collect(); + let mut generics = Some(tcx.generics_of(def_id)); + while let Some(g) = generics { + if let Some(param) = g.own_params.iter().find(|param| { + matches!(param.kind, ty::GenericParamDefKind::Const { .. }) && !held.contains(¶m.index) + }) { + return Some(param.name); + } + generics = g.parent.map(|parent| tcx.generics_of(parent)); + } tcx.param_env(def_id).caller_bounds().find_map(|clause| { let pred = clause.as_trait_clause()?.skip_binder(); match pred.self_ty().kind() { From 0a1ee039ec7ab922ca4d675f41dc6da57f23bde8 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 27 Oct 2026 15:19:23 -0700 Subject: [PATCH 8/8] fix(ts): check each bound instantiation, and refuse every bound const parameter A generic binding's value was checked once over `any`, which a binding such as `type Held = T` passes even where `Held>` is `undefined`, as `None` is. Each instantiation translation reaches is now checked with its own type arguments and `Copy`. A const parameter is refused even where an argument's type holds it, since a value such as `None` of an `Option<[u8; N]>` carries no length. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3 --- ts/README.md | 3 +- ts/fixtures/bind/refused.txt | 10 ++-- ts/fixtures/bind/src/harness.rs | 6 +++ ts/fixtures/bind/wrong.toml | 3 +- ts/fixtures/bind/wrong.txt | 2 + ts/fixtures/bind/wrong/held.ts | 9 ++++ ts/src/lower/bind.rs | 95 ++++++++++++++++++--------------- 7 files changed, 77 insertions(+), 51 deletions(-) create mode 100644 ts/fixtures/bind/wrong/held.ts diff --git a/ts/README.md b/ts/README.md index aa683014e..275677293 100644 --- a/ts/README.md +++ b/ts/README.md @@ -232,7 +232,8 @@ export serves every instance and a generic one is a generic TS function. `Bytes: that the impl doesn't override, such as `ne`, and a blanket impl's, such as `ToOwned`'s, stay the trait's, as for any other type. A generic export sees a type argument only in the values it takes, so a function with a bounded type parameter that no argument holds, such -as `fn zero() -> T`, can't be bound. +as `fn zero() -> T`, or with a const parameter, can't be bound. `tsc` checks a +generic bound type's value at each of its instantiations translation reaches. Translation can't see into a bound type, so a field, variant, or constructor of one is refused, as are iterating one, keying a collection by one, and assigning a whole one diff --git a/ts/fixtures/bind/refused.txt b/ts/fixtures/bind/refused.txt index 42facb127..282435c04 100644 --- a/ts/fixtures/bind/refused.txt +++ b/ts/fixtures/bind/refused.txt @@ -10,11 +10,11 @@ binding `raw::checksum`: `checksum` binds the same item binding `std::vec::Vec`: std's type has a representation of its own reading bind/missing.ts: No such file or directory (os error 2) at fixtures/bind/src/harness.rs:25:12 unsupported type `*const u32` at fixtures/bind/src/raw.rs:27:2 -`raw::Held` binds without drop, so it would never drop its `raw::Tally` at fixtures/bind/src/harness.rs:52:13 -a binding of `raw::value`, whose `N` no argument holds: one export for every instance can't recover it at fixtures/bind/src/raw.rs:56:1 -a binding of `raw::zero`, whose `T` no argument holds: one export for every instance can't recover it at fixtures/bind/src/raw.rs:51:1 -`raw::Held` binds without drop, so it would never drop its `raw::Tally` at fixtures/bind/src/harness.rs:52:6 +`raw::Held` binds without drop, so it would never drop its `raw::Tally` at fixtures/bind/src/harness.rs:58:13 +a binding of `raw::value`, whose `N` one export for every instance can't recover from its arguments at fixtures/bind/src/raw.rs:56:1 +a binding of `raw::zero`, whose `T` one export for every instance can't recover from its arguments at fixtures/bind/src/raw.rs:51:1 +`raw::Held` binds without drop, so it would never drop its `raw::Tally` at fixtures/bind/src/harness.rs:58:6 unsupported call to `std::sync::Arc::::from_raw` at fixtures/bind/src/raw.rs:46:17 unsupported call to `std::sync::Arc::::into_raw` at fixtures/bind/src/raw.rs:33:11 a raw pointer dereferenced at fixtures/bind/src/raw.rs:39:12 -an assignment through a reference to `bytes::Bytes` at fixtures/bind/src/harness.rs:59:2 +an assignment through a reference to `bytes::Bytes` at fixtures/bind/src/harness.rs:65:2 diff --git a/ts/fixtures/bind/src/harness.rs b/ts/fixtures/bind/src/harness.rs index 78ca1c637..5a6e6050a 100644 --- a/ts/fixtures/bind/src/harness.rs +++ b/ts/fixtures/bind/src/harness.rs @@ -45,6 +45,12 @@ pub fn run(input: &[u8]) -> Vec { out } +/// A `Held` of `None`, which `wrong.toml`'s binding would make `undefined`, as the outer +/// `None` is. +pub fn held() -> bool { + Some(crate::raw::Held::new(None::)).is_some() +} + /// What `refused.toml`'s bindings can't stand in for, which `run` never reaches. pub fn refused(count: &Arc) -> u8 { let mut bytes = Bytes::new(); diff --git a/ts/fixtures/bind/wrong.toml b/ts/fixtures/bind/wrong.toml index 99460b41e..a11a36e21 100644 --- a/ts/fixtures/bind/wrong.toml +++ b/ts/fixtures/bind/wrong.toml @@ -1,9 +1,10 @@ # Bindings that don't match what translated code takes them for, each refused at its Rust # item: `wrong.txt` holds the errors. package = "bind-fixture" -roots = ["harness::run"] +roots = ["harness::run", "harness::held"] [bind] "bytes::Bytes" = "wrong/bytes.ts" "raw::checksum" = "bind/checksum.ts" "raw::Tally" = { file = "wrong/tally.ts", drop = true } +"raw::Held" = "wrong/held.ts" diff --git a/ts/fixtures/bind/wrong.txt b/ts/fixtures/bind/wrong.txt index 7763e00c8..7b03b86f0 100644 --- a/ts/fixtures/bind/wrong.txt +++ b/ts/fixtures/bind/wrong.txt @@ -6,3 +6,5 @@ the binding of `::eq` in wrong/bytes.ts doe Types of parameters 'other' and 'a1' are incompatible. Type 'ByteSlice' is not assignable to type 'string'. at bytes-1.12.1/src/bytes.rs:769:5 the binding of `raw::Tally` in wrong/tally.ts doesn't type-check: error TS2322: Type 'Tally' is not assignable to type '"a bound value that drops must have a [Symbol.dispose]"'. at fixtures/bind/src/raw.rs:26:1 +the binding of `raw::Held>` in wrong/held.ts doesn't type-check: error TS2322: Type 'u8 | undefined' is not assignable to type '"a bound value can't be undefined, which None is"'. + Type 'undefined' is not assignable to type '"a bound value can't be undefined, which None is"'. at fixtures/bind/src/raw.rs:61:1 diff --git a/ts/fixtures/bind/wrong/held.ts b/ts/fixtures/bind/wrong/held.ts new file mode 100644 index 000000000..a380bd4b9 --- /dev/null +++ b/ts/fixtures/bind/wrong/held.ts @@ -0,0 +1,9 @@ +// `raw::Held`, bound wrong: it is what it holds, so a `Held>` may be `undefined`, +// as `None` is. + +export type Held = T; + +function make(value: T): Held { + return value; +} +export { make as new }; diff --git a/ts/src/lower/bind.rs b/ts/src/lower/bind.rs index ecfe78016..d0400dd4d 100644 --- a/ts/src/lower/bind.rs +++ b/ts/src/lower/bind.rs @@ -54,6 +54,8 @@ pub struct Bindings<'tcx> { exports: rustc_data_structures::fx::FxHashMap<(ModPath, String), (DefId, SymId)>, /// The instances checked against their export. checked: rustc_data_structures::fx::FxHashSet>, + /// The instantiations of bound types whose value is checked. + values: rustc_data_structures::fx::FxHashSet>, } impl<'a, 'tcx> Translator<'a, 'tcx> { @@ -225,31 +227,36 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { let msg = format!("`{path}` binds without drop, so it would never drop its `{arg}`"); self.err(span, msg); } - if let Some(&sym) = self.bindings.types.get(&def_id) { - return Some(sym); - } let module = self.bindings.items[&def_id].module.clone(); - let name = self.tcx.item_name(def_id).to_string(); - let sym = self.bound_sym(&module, name, SymKind::Struct, span); - self.bindings.types.insert(def_id, sym); - // It takes as many type parameters as the Rust type, any of which meets the - // binding's constraints on them. - let params = self.tcx.generics_of(def_id).own_counts().types; - let value = Type::Sym(sym, vec![Type::Builtin("any"); params]); - let copy = self.is_copy(self.tcx.type_of(def_id).instantiate_identity().skip_norm_wip()); - let flag = |b: bool| Type::Builtin(if b { "true" } else { "false" }); - let ret = Type::Rt("Bound", vec![value.clone(), flag(copy), flag(drop)]); - let value = Param { - name: "value".into(), - ty: Some(value), + let sym = match self.bindings.types.get(&def_id) { + Some(&sym) => sym, + None => { + let name = self.tcx.item_name(def_id).to_string(); + let sym = self.bound_sym(&module, name, SymKind::Struct, span); + self.bindings.types.insert(def_id, sym); + sym + } }; - let body = vec![StmtKind::Return(Some(Expr::ident("value"))).at(None)]; - let what = format!( - "the binding of `{}` in {}", - self.bindings.items[&def_id].path, - module_file(&module).display() - ); - self.check(vec![value], ret, body, self.tcx.def_span(def_id), what); + // Each instantiation translation reaches, with its own type arguments: a generic + // binding may be one value for one and another for the next, as `Held = T` is + // `undefined` for `Held<()>`. Over type parameters, only a census's, which checks + // nothing, reaches one. + let ty = ty::Ty::new_adt(self.tcx, adt, args); + if !ty.has_param() && self.bindings.values.insert(ty) { + let value = Type::Sym(sym, args.types().map(|t| self.ty(t, span)).collect()); + let flag = |b: bool| Type::Builtin(if b { "true" } else { "false" }); + let ret = Type::Rt("Bound", vec![value.clone(), flag(self.is_copy(ty)), flag(drop)]); + let value = Param { + name: "value".into(), + ty: Some(value), + }; + let body = vec![StmtKind::Return(Some(Expr::ident("value"))).at(None)]; + let what = ty::print::with_no_trimmed_paths!(format!( + "the binding of `{ty}` in {}", + module_file(&module).display() + )); + self.check(vec![value], ret, body, self.tcx.def_span(def_id), what); + } Some(sym) } @@ -277,7 +284,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { None => { if let Some(param) = self.phantom(def_id) { let msg = format!( - "a binding of `{}`, whose `{param}` no argument holds: one export for every instance can't recover it", + "a binding of `{}`, whose `{param}` one export for every instance can't recover from its arguments", tcx.def_path_str(def_id) ); self.err(tcx.def_span(def_id), msg); @@ -314,34 +321,34 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { Some(&self.bindings.items[&self.binder(def_id)?].module) } - /// A parameter of `def_id` that no argument's type holds, and that one export serving - /// every instance would need, seeing each generic argument only in the values it takes: - /// a const parameter, whose value it can't read, or a type parameter with a trait bound, - /// whose impls it can't call. + /// A parameter of `def_id` that one export serving every instance, seeing each generic + /// argument only in the values it takes, can't recover: a const parameter, whose value + /// even an argument whose type holds it may not carry, as `None` of an + /// `Option<[u8; N]>` doesn't, or a type parameter with a trait bound that no argument's + /// type holds, whose impls it can't call. fn phantom(&self, def_id: DefId) -> Option { let tcx = self.tcx; - let sig = tcx.fn_sig(def_id).instantiate_identity().skip_norm_wip().skip_binder(); - let held: Vec = sig - .inputs() - .iter() - .flat_map(|t| t.walk()) - .filter_map( - |arg| match (arg.as_type().map(|t| t.kind()), arg.as_const().map(|c| c.kind())) { - (Some(ty::Param(p)), _) => Some(p.index), - (_, Some(ty::ConstKind::Param(p))) => Some(p.index), - _ => None, - }, - ) - .collect(); let mut generics = Some(tcx.generics_of(def_id)); while let Some(g) = generics { - if let Some(param) = g.own_params.iter().find(|param| { - matches!(param.kind, ty::GenericParamDefKind::Const { .. }) && !held.contains(¶m.index) - }) { + if let Some(param) = g + .own_params + .iter() + .find(|param| matches!(param.kind, ty::GenericParamDefKind::Const { .. })) + { return Some(param.name); } generics = g.parent.map(|parent| tcx.generics_of(parent)); } + let sig = tcx.fn_sig(def_id).instantiate_identity().skip_norm_wip().skip_binder(); + let held: Vec = sig + .inputs() + .iter() + .flat_map(|t| t.walk()) + .filter_map(|arg| match arg.as_type()?.kind() { + ty::Param(p) => Some(p.index), + _ => None, + }) + .collect(); tcx.param_env(def_id).caller_bounds().find_map(|clause| { let pred = clause.as_trait_clause()?.skip_binder(); match pred.self_ty().kind() {