Skip to content

feat(ts): stable names for generic instances and trait impls - #47

Merged
kixelated merged 5 commits into
mainfrom
quest/m2/ts-stable-names
Oct 25, 2026
Merged

kixelated merged 5 commits into
mainfrom
quest/m2/ts-stable-names

Conversation

@kixelated

@kixelated kixelated commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A translated function's name depended on what else was reached. A generic function with one instance was foo until a root reached a second, and a type with only Debug got fmt until it also had Display; either renamed calls in code the new root never reached. Instances whose suffixes matched, such as two closures, were numbered $2 by their full type arguments, so a new instance could shift them too.

Approach

  • Program::name always prints a non-empty suffix; the "shared base" pass is gone.
  • Translator::suffix is a pure function of the instance: the trait (impl or provided method) with its non-default type arguments, then the self type's arguments (or the whole self type when it is not a local type), then the function's own type and const arguments. dedup is dropped.
  • short_name names closures, coroutines, and function items by def path (methods_closure1 for methods::{closure#1}), not by span. Closures used to be dropped from the suffix, which is what made them collide and get numbered.
  • A THIR async body (fooBody) and a MIR coroutine class (fooFuture) take their parent instance's suffix, so two instances of a generic async fn no longer tie on the sort key. Both lowerings share the same naming.
  • New stable_names test: translates lang with its roots, then with fmt::added added first, which reaches a second into_inner, a second closure instance of map, and Padded's Debug beside its Display. Every line of the first output must stay, in order, in the second. It fails on main (fmt.Padded.ts). source_order now shares the helper.
  • The snapshots churn by design. In the fixtures, the only numbered names left are an async fn's own blocks (blocksBody$2), which follow that function's source. moq-net's translation has none.

Impact

  • Breaking: output names. Trait impl functions always name their trait (Log.clone$Clone, Pair.fmt$Debug, Version.encode$Encode_Version). Functions of generic types and generic instances always name their type arguments (Wrap.intoInner$Point, Halving.next$Iterator_u64, Wrap.fmt$Debug_Pair). Closure types appear in suffixes (decodeWith$Group_decode_closure0). ts/MIGRATE.md has the entry, and the earlier Unreleased Group.encode example is corrected.
  • Consumer: moq's translated moq-net, whose names now stay put as roots grow. just ts moq type-checks.

Alternatives

  • A hash suffix: rejected by the maintainer on 2026-10-24 in favor of today's Rust-like format.
  • Keep closures out of the suffix: shorter names, but two closure instances of one function then fall back to $2, which shifts.
  • Make short_name injective (refs, array lengths, same-named types from different modules): those still fall back to $2. That is rare and nothing reaches it today.

Follow-ups

  • Same-named types from different modules, &T vs T, and arrays of different lengths can still collide and be numbered. If moq ever hits this, qualify short_name further.

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

(written by Opus 5.5)

kixelated and others added 3 commits October 24, 2026 22:43
A generic instance always prints its type arguments, and a trait impl's
function its trait, whether or not another is reached. Closure types in a
suffix are named by where they are defined, so they stop being numbered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
@kixelated kixelated changed the title Stable names in the TypeScript translator feat(ts): stable names for generic instances and trait impls Oct 25, 2026
@kixelated
kixelated marked this pull request as ready for review October 25, 2026 06:08
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-25T15:43:16.371258Z a90f179 New commits
🔒 Security Review ✅ Completed 2026-10-25T15:46:38.712450Z a90f179 New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

A closure in an anonymous constant has no named typeck root, and a tuple
struct's constructor goes by the struct.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 71b439f

No actionable bugs found. Deriving suffixes from each instance rather than the reached symbol set is a sound simplification; carrying that rule through generated async symbols and adding the root-expansion regression addresses the intended instability. The latest definition-path fix avoids assuming every type-checking root has a name. The documented short-name collision fallback remains a limitation, so this is not a guarantee of globally unique stable names.

Verification: static inspection of naming/lowering changes, surrounding code, migration notes, tests and snapshots, including the latest incremental commit. I did not run the compiler, TypeScript checks or tests.

Regenerates the snapshots for the cases #48 and #49 added, and names `!`
and `()` in a suffix (`pass$never`), which printed as an empty part.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: a90f179

No actionable bugs found since 71b439f. After separating merged main changes, explicitly naming unit and never types is a sensible extension of instance-based naming: these arguments no longer disappear from suffixes. Regenerated fixtures consistently use suffixed names, including pass$never and unwrapOrBail$never. The documented short-name collision limitation is unchanged.

Verification: static inspection of incremental naming change, surrounding code and merged fixture snapshots. No compiler runs, TypeScript checks or tests.

@kixelated

Copy link
Copy Markdown
Collaborator Author

Merging after #48 and #49, per the maintainer's merge order.

  • Merged origin/main in (no rebase) and regenerated every snapshot. The new cases follow the stable naming (Moved.fmt$Debug, Form.from$From_Version).
  • fix(ts): diverging calls through closures and generics at ! pass tsc #48's generic_never showed that ! and () printed as an empty suffix part, so pass::<!> was a bare pass. These types are now named never and unit (pass$never, unwrapOrBail$never).
  • The maintainer accepted all four open decisions: keep closures in suffixes, keep the trait-then-types order, carry the trait on provided methods, and leave the remaining short-name collisions numbered.
  • just check, just test, just ts moq, and quest check (run from outside the checkout) pass, and CI is green on a90f179.

(written by Opus 5.5)

@kixelated
kixelated merged commit 04722b1 into main Oct 25, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-stable-names branch October 27, 2026 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant