From 3d5d6b0d10551abf266003c5e1e61678dc454c44 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 08:24:09 -0700 Subject: [PATCH 1/2] chore(quest): claim quest/m3/ts-bind-gaps.md Quest-Claim: c9a29eabe67bf368 From fdf02b6b29c673fe2929b0ca63d20aefbddda3be Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:44:29 -0700 Subject: [PATCH 2/2] fix(ts): resolve a binding through a renamed dependency by the crate's name for it A binding's path named a dependency by its crate name, so `foo = { package = "bar" }` could only be bound as `bar::Item`, a path the package's source can't write. Resolve the first segment through the `--extern` name rustc loaded the crate by, and a crate the package reaches only through another by its own name. Co-Authored-By: Claude Opus 5.5 --- quest/m3/ts-bind-gaps.md | 10 +++++----- ts/MIGRATE.md | 2 ++ ts/README.md | 5 ++++- ts/fixtures/bind/Cargo.toml | 3 ++- ts/fixtures/bind/refused.toml | 6 ++++-- ts/fixtures/bind/refused.txt | 5 +++-- ts/fixtures/bind/src/harness.rs | 2 +- ts/fixtures/bind/src/lib.rs | 6 +++--- ts/fixtures/bind/ts.toml | 2 +- ts/fixtures/bind/wrong.toml | 2 +- ts/src/config.rs | 2 +- ts/src/lower/bind.rs | 29 ++++++++++++++++------------- ts/src/main.rs | 1 + 13 files changed, 44 insertions(+), 31 deletions(-) diff --git a/quest/m3/ts-bind-gaps.md b/quest/m3/ts-bind-gaps.md index 5666c5b9d..e7a967601 100644 --- a/quest/m3/ts-bind-gaps.md +++ b/quest/m3/ts-bind-gaps.md @@ -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. diff --git a/ts/MIGRATE.md b/ts/MIGRATE.md index f972794f2..5a752aa68 100644 --- a/ts/MIGRATE.md +++ b/ts/MIGRATE.md @@ -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. diff --git a/ts/README.md b/ts/README.md index 6873d3791..363538c87 100644 --- a/ts/README.md +++ b/ts/README.md @@ -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 @@ -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`. diff --git a/ts/fixtures/bind/Cargo.toml b/ts/fixtures/bind/Cargo.toml index 77e75fb8f..e388fd091 100644 --- a/ts/fixtures/bind/Cargo.toml +++ b/ts/fixtures/bind/Cargo.toml @@ -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 diff --git a/ts/fixtures/bind/refused.toml b/ts/fixtures/bind/refused.toml index 5f73489d9..bdfab714c 100644 --- a/ts/fixtures/bind/refused.toml +++ b/ts/fixtures/bind/refused.toml @@ -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" diff --git a/ts/fixtures/bind/refused.txt b/ts/fixtures/bind/refused.txt index 55941426a..0f97b1f49 100644 --- a/ts/fixtures/bind/refused.txt +++ b/ts/fixtures/bind/refused.txt @@ -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/ diff --git a/ts/fixtures/bind/src/harness.rs b/ts/fixtures/bind/src/harness.rs index fb7cb3216..85fef507a 100644 --- a/ts/fixtures/bind/src/harness.rs +++ b/ts/fixtures/bind/src/harness.rs @@ -2,7 +2,7 @@ use std::sync::Arc; -use blob::Blob; +use buf::Blob; use crate::raw::{Tally, checksum, skip}; diff --git a/ts/fixtures/bind/src/lib.rs b/ts/fixtures/bind/src/lib.rs index d94cf4b46..2214b1ed3 100644 --- a/ts/fixtures/bind/src/lib.rs +++ b/ts/fixtures/bind/src/lib.rs @@ -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. diff --git a/ts/fixtures/bind/ts.toml b/ts/fixtures/bind/ts.toml index ab6840c05..fe86d3bd9 100644 --- a/ts/fixtures/bind/ts.toml +++ b/ts/fixtures/bind/ts.toml @@ -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 } diff --git a/ts/fixtures/bind/wrong.toml b/ts/fixtures/bind/wrong.toml index c30bd0031..74795f1a3 100644 --- a/ts/fixtures/bind/wrong.toml +++ b/ts/fixtures/bind/wrong.toml @@ -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 } diff --git a/ts/src/config.rs b/ts/src/config.rs index 7301019c1..a61296b91 100644 --- a/ts/src/config.rs +++ b/ts/src/config.rs @@ -19,7 +19,7 @@ pub struct Config { #[serde(default)] pub printable: 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. + /// the crate root, or else from a crate it depends on, by the name the crate gives it. #[serde(default)] pub bind: BTreeMap, /// The directory `ts.toml` is in, which binding files are relative to. diff --git a/ts/src/lower/bind.rs b/ts/src/lower/bind.rs index 50f472458..18836406b 100644 --- a/ts/src/lower/bind.rs +++ b/ts/src/lower/bind.rs @@ -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; @@ -153,7 +154,9 @@ 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 { let segments: Vec<&str> = path.split("::").collect(); if let Some(def_id) = resolve_in(self.tcx, CRATE_DEF_ID.to_def_id(), &segments) { @@ -161,18 +164,18 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { } 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) } diff --git a/ts/src/main.rs b/ts/src/main.rs index 7cbf30f85..546052f16 100644 --- a/ts/src/main.rs +++ b/ts/src/main.rs @@ -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;