diff --git a/quest/m3/ts-bind-gaps.md b/quest/m3/ts-bind-gaps.md index 0c6506f3d..5666c5b9d 100644 --- a/quest/m3/ts-bind-gaps.md +++ b/quest/m3/ts-bind-gaps.md @@ -3,15 +3,12 @@ ## Goal rust2-ts's [bindings](/ts/README.md) cover the cases -[#127](https://github.com/kixelated/rust2/pull/127) refuses or checks loosely: a bound type -as a collection key through its binding's `Hash`, `Eq`, and `Ord`; a binding that returns -unit is checked against `=> undefined` rather than `=> void`, which accepts any return -value; and a path through a renamed dependency (`foo = { package = "bar" }`) resolves by -the name the package gives it. +[#127](https://github.com/kixelated/rust2/pull/127) refuses: a bound type as a collection +key through its binding's `Hash`, `Eq`, and `Ord`, and a path through a renamed dependency +(`foo = { package = "bar" }`) resolving by the name the package gives it. ## Plan -Found by #127 on 2026-10-27. Each fails with the Rust item's span today, apart from the -unit return, which `tsc` accepts unchecked. +Found by #127 on 2026-10-27. Each fails with the Rust item's span today. Public API: `ts.toml`'s bindings. diff --git a/ts/MIGRATE.md b/ts/MIGRATE.md index f7e8d12e9..19b33fc93 100644 --- a/ts/MIGRATE.md +++ b/ts/MIGRATE.md @@ -2,6 +2,8 @@ ## Unreleased +- `tsc` checks a binding of a function returning `()` against `=> undefined`, not + `=> void`: type its export `: undefined`. - `rt.ordMax`, `rt.ordMin`, and `rt.sliceSortByKey` take a `PartialOrd::lt` rather than a `partial_cmp`, and `rt.ordClamp` takes `le`, `lt`, and `gt`; `rt.cmpLt` makes one from a `partial_cmp`. `rt.sliceSortBy` takes only an `Ord::cmp`-like comparator: sort by an diff --git a/ts/README.md b/ts/README.md index b9983e40b..523149f39 100644 --- a/ts/README.md +++ b/ts/README.md @@ -301,9 +301,10 @@ takes by value or by `&`, or share with a clone what a `&mut` changes, and an in returns is in its Rust type's range. 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 +translated signature of every instance translation calls, and each bound type's value. One +returning `()` is checked against `=> undefined`, not `=> void`, which accepts any value, +so its export is typed `: undefined`. 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. diff --git a/ts/fixtures/bind/bind/skip.ts b/ts/fixtures/bind/bind/skip.ts index a9170fa0f..90676bfaf 100644 --- a/ts/fixtures/bind/bind/skip.ts +++ b/ts/fixtures/bind/bind/skip.ts @@ -2,6 +2,6 @@ import type * as rt from "../rt.ts"; -export function skip(bytes: rt.ByteSlice, at: rt.Place): void { +export function skip(bytes: rt.ByteSlice, at: rt.Place): undefined { while (at.v < bytes.length && bytes.data[bytes.start + at.v] === 0) at.v++; } diff --git a/ts/fixtures/bind/expected/bind/skip.ts b/ts/fixtures/bind/expected/bind/skip.ts index 182c90215..3921bfb9e 100644 --- a/ts/fixtures/bind/expected/bind/skip.ts +++ b/ts/fixtures/bind/expected/bind/skip.ts @@ -3,6 +3,6 @@ import type * as rt from "../rt.ts"; -export function skip(bytes: rt.ByteSlice, at: rt.Place): void { +export function skip(bytes: rt.ByteSlice, at: rt.Place): undefined { while (at.v < bytes.length && bytes.data[bytes.start + at.v] === 0) at.v++; } diff --git a/ts/fixtures/bind/wrong.toml b/ts/fixtures/bind/wrong.toml index 181413f09..c1d1f212d 100644 --- a/ts/fixtures/bind/wrong.toml +++ b/ts/fixtures/bind/wrong.toml @@ -6,6 +6,6 @@ roots = ["harness::run", "harness::held"] [bind] "bytes::Bytes" = "wrong/bytes.ts" "raw::checksum" = "bind/checksum.ts" -"raw::skip" = "bind/skip.ts" +"raw::skip" = "wrong/skip.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 d4a0892c9..921a1dec5 100644 --- a/ts/fixtures/bind/wrong.txt +++ b/ts/fixtures/bind/wrong.txt @@ -5,6 +5,8 @@ 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::skip` in wrong/skip.ts doesn't type-check: error TS2322: Type '(bytes: ByteSlice, at: Place) => usize' is not assignable to type '(a0: ByteSlice, a1: Place) => undefined'. + Type 'usize' is not assignable to type 'undefined'. 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:35: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:70:1 diff --git a/ts/fixtures/bind/wrong/skip.ts b/ts/fixtures/bind/wrong/skip.ts new file mode 100644 index 000000000..00e53a15d --- /dev/null +++ b/ts/fixtures/bind/wrong/skip.ts @@ -0,0 +1,8 @@ +// `raw::skip`, bound wrong: it returns the new position, where Rust returns `()`. + +import type * as rt from "../rt.ts"; + +export function skip(bytes: rt.ByteSlice, at: rt.Place): rt.usize { + while (at.v < bytes.length && bytes.data[bytes.start + at.v] === 0) at.v++; + return at.v; +} diff --git a/ts/src/lower/bind.rs b/ts/src/lower/bind.rs index d0400dd4d..4e2224544 100644 --- a/ts/src/lower/bind.rs +++ b/ts/src/lower/bind.rs @@ -302,7 +302,9 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { 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); + // `=> undefined` for `()`, not `fn_ty`'s `=> void`, which accepts any return value. + let params = sig.inputs().iter().map(|t| self.ty(*t, span)).collect(); + let ty = Type::Func(params, Box::new(self.ty(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 {}",