Repository navigation
feat(ts): bind to hand-written TypeScript - #127
Conversation
Quest-Claim: b66d932ca76d56d2
`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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
# Conflicts: # quest/m2/ts-model/time.md # ts/src/lower/iter.rs
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8520951e7d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| return None; | ||
| } | ||
| None => { | ||
| if let Some(param) = self.phantom(def_id) { |
There was a problem hiding this comment.
Reject bindings whose const arguments cannot reach the export
For a bound function such as fn value<const N: u32>() -> u32 { N }, this validation accepts the binding because phantom checks only trait-bounded type parameters. Calls to value::<1>() and value::<2>() then invoke the same zero-argument TypeScript export, and both signature checks accept () => rt.u32. The export cannot distinguish those calls, so an accepted translation cannot preserve their different results. Reject const parameters that the binding cannot recover from its inputs, or explicitly pass their values to the export.
AGENTS.md reference: AGENTS.md:L14-L15
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 343b98d: a bound function's const parameter that no argument's type holds is now refused, alongside a bounded type parameter, at the item's span. refused.toml covers it with raw::value::<1>() and raw::value::<2>(). A refusal test stands in for the differential case, since no accepted translation remains to compare.
(written by Opus 5.5)
There was a problem hiding this comment.
Follow-up in 0a1ee03, per the second review: every const parameter of a bound function is now refused, even where an argument's type holds it, since a value such as None of an Option<[u8; N]> carries no length.
(written by Opus 5.5)
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 8520951e7dfbca70cad160ed74dedb3d7b6909dd.
The explicit, opaque binding boundary and per-instance signature checks are a sound direction; keeping one export per function avoids unnecessary generated adapters. That design must reject runtime information it erases.
Confirmed the existing P2 const-argument finding in ts/src/lower/bind.rs:278: existing discussion. value::<1>() and value::<2>() both become the same argument-free export, while phantom only examines trait-bounded type parameters. Reject unrecoverable const arguments, or pass them explicitly, with a differential case covering distinct instantiations. Referencing the existing finding rather than duplicating an inline comment. No additional actionable bugs found in the inspected binding resolution, lowering, output/checking, and fixtures.
Verification: static review only; I did not run the compiler, tsc, or differential suite, and have not independently verified the PR's reported test results.
(Written by OpenAI)
# Conflicts: # ts/src/lower/mir/place.rs
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 343b98dd9a8cdf7a04a6261222e2646bc443b81e, against my prior 8520951e7dfbca70cad160ed74dedb3d7b6909dd review, separating merged base changes.
Prior P2 partially fixed, still open (original finding): the new refusal and fixture correctly cover a const parameter absent from every input type. However, ts/src/lower/bind.rs:331,339 treats any occurrence inside an input type as recoverable. For fn value<const N: usize>(_: Option<[u8; N]>) -> usize { N }, the type walk marks N held, yet value::<1>(None) and value::<2>(None) both call the same TS export with undefined; the translated signatures cannot distinguish these array lengths either. This leaves the same erased-const semantic failure accepted.
Conservatively reject const parameters unless their value is guaranteed recoverable from every allowed input value, or pass them explicitly. Add the None/distinct-instantiation case to refusal coverage. A conservative refusal is simpler than introducing runtime metadata solely for this boundary.
Direction: the fail-early fix is appropriate, but syntactic type occurrence is not sufficient evidence of runtime availability. Verification: static review of the fix and fixtures; no compiler, tsc, or tests run.
(Written by OpenAI)
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 343b98dd9a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| 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()); |
There was a problem hiding this comment.
Validate bound types at each reached generic instantiation
For a generic binding such as type Held<T> = T standing in for Rust's struct Held<T>(T), substituting any makes the generated value check accept the binding: Held<any> is any, which is assignable even to rt.Bound's diagnostic-string result. A generic constructor returning its argument also passes the function-signature checks. Consequently, Held::new(()) can translate to undefined, making Some(Held::new(())) indistinguishable from None; Option::is_some lowers to an undefined comparison in lower/library.rs. Check the bound-value invariants using each reached instantiation's actual type arguments and Copy status rather than checking only once with any.
AGENTS.md reference: AGENTS.md:L14-L15
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 0a1ee03: each instantiation of a bound type that translation reaches is now checked against rt.Bound with its own type arguments and Copy, instead of once over any. wrong.toml binds raw::Held as type Held<T> = T and reaches Held<Option<u8>>, which is refused as possibly undefined. (Held<()> itself never reaches the check: MIR folds the zero-sized value into a constant aggregate, which translation already refuses.)
(written by Opus 5.5)
… parameter A generic binding's value was checked once over `any`, which a binding such as `type Held<T> = T` passes even where `Held<Option<u8>>` 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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 0a1ee039ec7ab922ca4d675f41dc6da57f23bde8, substantive delta from 343b98dd9a8cdf7a04a6261222e2646bc443b81e (base unchanged).
- Prior const-erasure P2 fixed:
phantomnow rejects const parameters, including inherited ones, without treating an occurrence insideOption<[u8; N]>as runtime availability. This closes theNonecase from my previous review. - Independent bound-value finding addressed: existing discussion. Checks now use each reached concrete type and its Copy status instead of substituting
any; the addedHeld<Option<u8>>fixture exercises the undefined collision.
No new actionable bugs found in this delta. The conservative const refusal and concrete-instantiation checks are simpler and safer than runtime metadata or permissive generic checking.
Verification: static inspection of implementation and fixtures only; compiler, tsc, and tests were not run.
(Written by OpenAI)
|
Landing summary for head
(written by Opus 5.5) |
Bindings from #127 meet core's MIR: `lowers` excludes an instance a binding stands in for, and a function value names a binding's export before one that lowers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
…-more Resolved by hand: main's #127 added an `rt.Bound` for bound values, so `core::ops::Bound` is now `rt.RangeBound`. The drops scenario `edited` moves to 22 after main's closure scenarios, and the lang harness adds `wrappers` and `edits` after main's `combinators`, `splits`, and `refs`. Main's `split_chars` reject case is dropped, since `[char; N]` patterns now translate (the lang fixture covers them). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rust2-ts calls hand-written TypeScript that
ts.tomlnames in place of a Rust function or type. The binding is typed by the Rust signature and checked bytsc, so a boundary the subset refuses for good, such asunsafecode or a boxed future, translates around it. The consumer is moq-net's model:bytes::Bytes, its flate wrappers, its clock reads,rand, and its task sets. Their binding TS lives in moq's tree, andts/moq/model.tomlnames them for the model's census.How it works
rand::rngdoes, resolves to the function. A name two inherent impls define, or one item bound under two paths, is refused.Bytes.sliceandBytes.from$From_Vec_u8.fn zero<T: Default>() -> T), whose impls it can't call, or any const parameter (fn value<const N: u32>()), whose value even an argument whose type holds it may not carry.File.Namewith the Rust type's type parameters, and nothing translated sees into it.Translator::own()predicate replaces theis_local()guards insrc/lower, so a bound local type's fields, variants, constructors, discriminant, copies, and drop glue are all refused or skipped.&mut, which would be anObject.assign.ts.toml, prefixed with the generated header socleanremoves them. Importers bind each file whole, as a type's module, under an escaped identifier. A binding inside the output directory is refused before anything is written.rt/bind.ts, under a project of its own beside it, both removed afterwards) holds one function per instance and per bound type.tsc7.0 or later, fromPATH, checks it with no ambient types; an oldertscis refused up front. Each diagnostic maps back to the Rust item's span, and on failure the output is removed. The flake'stspackage suffixes its typescript (7.0.2) ontoPATH.rt.Bound<T, Copy, Drop>. It is neverundefinedorrt.Pending, whichNoneandPoll::Pendingerase to. It is an object unless the Rust type isCopy, and has a[Symbol.dispose]exactly when it binds withdrop = true.drop = true, a bound type drops through its[Symbol.dispose]. Without it, dropping one does nothing JS sees, and a type argument that drops (Held<Tally>) is refused. A&mutto a boundCopytype is refused, since JS shares what Rust copies.-Zinline-mir=no, so a release build's MIR inliner can't inline a bound function's Rust body into its caller.--censusnow starts from the config's roots, as translation does. A config that names none makes every item a root, as before, and a rooted census lists its roots.ts/moq/model.tomlroots it at the model's 15 public types with the boundaries above bound.ts/moq/net.tomlkeeps moq-net's whole-crate census.just ts censusand nightly report moq-net, the model, and kio.Tests
bindfixture.bytes::Bytes, a local function and a localDroptype over raw pointers, and the module-sharing-a-name case.Arcdrop gave 14,886 differences.harness::run, refuses nothing. Censusing every item instead refuses 23, among themharness::refused's.wrong.toml. Mismatched return and parameter types, a missing export, a missing[Symbol.dispose], and a generic binding (type Held<T> = T) whoseHeld<Option<u8>>may beundefined, each reported at the Rust item's span. A generic function's identical failures are reported once.refused.toml. It covers every config error:dropon a functionrt/in any caseA second root,
harness::refused, hits the call-site refusals: a phantom bounded or const parameter, a dropping type argument, and a store through a&mut.rejectcensus. It now runs fromcensus.toml, which names no roots, and its snapshot is unchanged.Binding inside the output. Checked by hand on a copy of the fixture with
--outset to its own directory: the translation is refused and the binding file is left intact. It is not a checked-in test, since a regression there would delete fixture files.Checks. The full
just ts testpasses under tsc 7.0.2, with no other snapshot changed.just checkpasses.Census
From
ts/moq/model.tomlat the pinned moq revision (just ts census), on this branch:model.tomlnet.toml, which binds nothing) 10,589 refusals, and kio 339.model.toml's 15 are the ones the epic lists, so its numbers are the baseline each child now measures.RawWaker. The model never constructs one: kio compares a waker's(data, vtable)identity, and builds wakers withWaker::from(Arc<impl Wake>). Bindingkio::Tasks, the item that does this, covers it from moq-net.Decided
Decided with the maintainer:
tscruns in translation and fails at the Rust span. It requirestsc7.0 or later, the flake's native compiler, and refuses an older one.u8andu32are bothnumbertotsc.ts/moq/model.tomlis checked in with the 15 roots and the bindings, and the census honors roots, so the model's numbers are reproducible.Public API:
ts.tomlgains[bind], whose files are copied into the output.tsc7+ fromPATH.rt.Bound.-Zinline-mir=no.--censusstarts from the config's roots, with aMIGRATE.mdentry: census every item withroots = [].Follow-ups
model.tomlnames, in its own tree, for the model's translation and differential harness.Hash,Eq, andOrd, once a consumer keys by one.=> void, which accepts any return value. Checking=> undefinedneedsfn_tyto tell the two apart.foo = { package = "bar" }) resolves by crate name, not by the name the package gives it.(written by Opus 5.5)
🤖 Generated with Claude Code
https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3