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
10 changes: 5 additions & 5 deletions quest/m3/ts-bind-gaps.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

## Goal

rust2-ts's [bindings](/ts/README.md) cover the cases
[#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.
rust2-ts's [bindings](/ts/README.md) cover a bound type as a collection key, through its
binding's `Hash`, `Eq`, and `Ord`, which [#127](https://github.com/kixelated/rust2/pull/127)
refuses.

## Plan

Found by #127 on 2026-10-27. Each fails with the Rust item's span today.
Found by #127 on 2026-10-27. Keying a collection by a bound type 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

- A binding's path names a dependency the package renames (`foo = { package = "bar" }`)
as the package does, `foo::Item`, no longer by its crate name, `bar::Item`.
- A panic aborts, as on wasm32: the output throws `rt.Panic` without running any cleanup,
so nothing drops as it unwinds, and what the translation held is undefined after one.
A caller that catches `rt.Panic` must not use the translation's values again.
Expand Down
5 changes: 4 additions & 1 deletion ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ of them:
## Bindings

A binding stands in for what the subset refuses, such as `unsafe` code or a boxed future.
Its path goes from the crate root, or else from a crate it depends on by the name the crate
gives it, as `buf::Blob` for `buf = { package = "blob" }`. A crate it reaches only through
another goes by its own name, where one crate of that name is built.
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/blob.ts`,
and may import the translated modules and npm packages too. Callers import it whole, as a
Expand Down Expand Up @@ -576,7 +579,7 @@ built natively for wasm32 and run beside its translation on the same inputs by
`rand::rng()` reads the fixture's own getrandom backend on wasm32. It checks `Bytes`'s
slices and panics, how each is made and printed, that no event evaluates a field, and
that each random value is in its range, every value of a small range drawn.
- `bind` binds `blob::Blob`, a dependency's type with `unsafe` inside, and a function and a type of its own that read through raw
- `bind` binds `buf::Blob`, a renamed dependency's type with `unsafe` inside, 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`.
Expand Down
3 changes: 2 additions & 1 deletion ts/fixtures/bind/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ publish = false
crate-type = ["lib", "cdylib"]

[dependencies]
blob = { path = "blob" }
# Renamed, so a binding names it as the crate does.
buf = { package = "blob", path = "blob" }

[lints]
workspace = true
6 changes: 4 additions & 2 deletions ts/fixtures/bind/refused.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ roots = ["harness::run", "harness::refused"]
"nowhere::Thing" = "bind/thing.ts"
"std::vec::Vec" = "bind/vec.ts"
"raw" = "bind/raw.ts"
"blob::Source" = "bind/source.ts"
"buf::Source" = "bind/source.ts"
"raw::Tally::get" = { file = "bind/get.ts", drop = true }
"raw::Tally::new" = "rt/tally.ts"
"raw::Held::new" = "RT/held.ts"
"blob::empty" = "../blob.ts"
"buf::empty" = "../blob.ts"
"buf::Blob" = "bind/blob.ts"
# The crate renames the dependency, so its own name names nothing.
"blob::Blob" = "bind/blob.ts"
"raw::Tally" = "bind/blob.ts"
"checksum" = "bind/missing.ts"
Expand Down
5 changes: 3 additions & 2 deletions ts/fixtures/bind/refused.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
binding `blob::Source`: a binding is of a function or a type, not Trait
binding `blob::empty`: ../blob.ts must be relative to ts.toml, inside its directory
binding `blob::Blob`: no such item, or more than one, in the crate or a crate it depends on
binding `buf::Source`: a binding is of a function or a type, not Trait
binding `buf::empty`: ../blob.ts must be relative to ts.toml, inside its directory
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/
Expand Down
2 changes: 1 addition & 1 deletion ts/fixtures/bind/src/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::sync::Arc;

use blob::Blob;
use buf::Blob;

use crate::raw::{Tally, checksum, skip};

Expand Down
6 changes: 3 additions & 3 deletions ts/fixtures/bind/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Items outside the subset, bound to hand-written TypeScript: `blob::Blob`, which
//! contains `unsafe`, and the crate's own function and type that read through raw
//! pointers, one of them dropping where Rust drops it.
//! Items outside the subset, bound to hand-written TypeScript: `buf::Blob`, a renamed
//! dependency's type that 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.

Expand Down
2 changes: 1 addition & 1 deletion ts/fixtures/bind/ts.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package = "bind-fixture"
roots = ["harness::run"]

[bind]
"blob::Blob" = "bind/blob.ts"
"buf::Blob" = "bind/blob.ts"
"raw::checksum" = "bind/checksum.ts"
"raw::skip" = "bind/skip.ts"
"raw::Tally" = { file = "bind/tally.ts", drop = true }
2 changes: 1 addition & 1 deletion ts/fixtures/bind/wrong.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package = "bind-fixture"
roots = ["harness::run", "harness::held"]

[bind]
"blob::Blob" = "wrong/blob.ts"
"buf::Blob" = "wrong/blob.ts"
"raw::checksum" = "bind/checksum.ts"
"raw::skip" = "wrong/skip.ts"
"raw::Tally" = { file = "wrong/tally.ts", drop = true }
Expand Down
2 changes: 1 addition & 1 deletion ts/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct Config {
#[serde(default)]
pub printable: Vec<String>,
/// 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.
/// the crate root, or else from a crate it depends on, by the name the crate gives it.
#[serde(default)]
pub bind: BTreeMap<String, Bind>,
/// The directory `ts.toml` is in, which binding files are relative to.
Expand Down
29 changes: 16 additions & 13 deletions ts/src/lower/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +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_metadata::creader::CStore;
use rustc_middle::ty::{self, Instance, InstanceKind, TyCtxt, TypeVisitableExt};
use rustc_span::Span;

Expand Down Expand Up @@ -153,26 +154,28 @@ impl<'a, 'tcx> Translator<'a, 'tcx> {
}

/// 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.
/// depends on, through modules and then a type's inherent functions. A dependency goes
/// by the name the crate's source gives it, which a renamed one's isn't its own; a crate
/// reached only through another goes by its own name, where one crate of it is built.
fn resolve_bound(&self, path: &str) -> Option<DefId> {
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()))?,
let named = CStore::from_tcx(tcx).resolved_extern_crate(rustc_span::Symbol::intern(name));
let cnum = match named {
Some(cnum) => cnum,
None => {
let mut own = tcx.crates(()).iter().copied().filter(|&cnum| {
tcx.crate_name(cnum).as_str() == *name && !tcx.extern_crate(cnum).is_some_and(|e| e.is_direct())
});
match (own.next(), own.next()) {
(Some(cnum), None) => cnum,
_ => return None,
}
}
};
resolve_in(tcx, cnum.as_def_id(), rest)
}
Expand Down
1 change: 1 addition & 0 deletions ts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extern crate rustc_hir;
extern crate rustc_index;
extern crate rustc_infer;
extern crate rustc_interface;
extern crate rustc_metadata;
extern crate rustc_middle;
extern crate rustc_span;
extern crate rustc_target;
Expand Down