Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions quest/m3/ts-bind-gaps.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 2 additions & 0 deletions ts/MIGRATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion ts/fixtures/bind/bind/skip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import type * as rt from "../rt.ts";

export function skip(bytes: rt.ByteSlice, at: rt.Place<rt.usize>): void {
export function skip(bytes: rt.ByteSlice, at: rt.Place<rt.usize>): undefined {
while (at.v < bytes.length && bytes.data[bytes.start + at.v] === 0) at.v++;
}
2 changes: 1 addition & 1 deletion ts/fixtures/bind/expected/bind/skip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

import type * as rt from "../rt.ts";

export function skip(bytes: rt.ByteSlice, at: rt.Place<rt.usize>): void {
export function skip(bytes: rt.ByteSlice, at: rt.Place<rt.usize>): undefined {
while (at.v < bytes.length && bytes.data[bytes.start + at.v] === 0) at.v++;
}
2 changes: 1 addition & 1 deletion ts/fixtures/bind/wrong.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 2 additions & 0 deletions ts/fixtures/bind/wrong.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ the binding of `bytes::Bytes::slice::<std::ops::RangeTo<usize>>` in wrong/bytes.
the binding of `<bytes::Bytes as std::cmp::PartialEq>::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>) => usize' is not assignable to type '(a0: ByteSlice, a1: Place<usize>) => 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<std::option::Option<u8>>` 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
8 changes: 8 additions & 0 deletions ts/fixtures/bind/wrong/skip.ts
Original file line number Diff line number Diff line change
@@ -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>): rt.usize {
while (at.v < bytes.length && bytes.data[bytes.start + at.v] === 0) at.v++;
return at.v;
}
4 changes: 3 additions & 1 deletion ts/src/lower/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}",
Expand Down