Skip to content

feat(ts): translate std's future helpers and wakers from Arc<impl Wake> - #166

Merged
kixelated merged 9 commits into
mainfrom
quest/m2/ts-async
Oct 28, 2026
Merged

kixelated merged 9 commits into
mainfrom
quest/m2/ts-async

Conversation

@kixelated

@kixelated kixelated commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

moq-net's census refuses std's future helpers 592 times (the model 45, kio 5): Poll's is_ready, is_pending, map, map_ok, and map_err, pin!, poll_fn, future::pending, Waker::from(Arc<impl Wake>), and Waker::noop. The model's census needs only Poll's methods and Waker::from among async constructs, so this is the first piece of ts-async that lands on its own.

Approach

  • Poll's methods lower from core's MIR, as Option's and Result's combinators do (library::from_mir).
  • pin! builds a PinMacroHelper { value } and calls pin_new_unchecked_in_helper(&mut it). PinMacroHelper maps to Std::Pin, so it is its value, and the helper call is its argument. A pin! of the future is the future itself.
  • poll_fn(f) is new rt.PollFn(f, drop?), a future class whose poll calls f and whose [Symbol.dispose] drops f through drop, which is passed only when F itself drops. A poll_fn(&mut f) drops nothing. future::pending() is rt.NEVER, one shared future that is never ready. Future::poll on either calls its poll, and their TS type is rt.Future<Output>.
  • Waker::from(arc) is new rt.ArcWaker(arc, wake, drop, wakeByRef?). wake and wakeByRef are the instances of the crate's Wake impl, with wakeByRef passed only when the impl overrides it, as std's default otherwise wakes a clone. drop drops the Arc as drop_value would. clone takes another count, and willWake compares the Arc. Waker::noop() is rt.NOOP_WAKER. Since a waker's drop can now run the crate's Drop, drop_panics counts a Waker as able to panic, so glue around one unwinds.
  • A closure that assigns or mutably borrows a by-value capture whole (here relay = Some(...) inside a poll_fn) declared that capture const, which tsc rejected. Captures that the closure's MIR mutates whole, not counting drops, are now let. No other snapshot changed.
  • Test: the task fixture's new futures::helpers future, spawned as step 14, uses every helper. It polls a pinned recv by hand in a poll_fn that owns a guard, and a pinned pending() with Waker::noop. recv wakes the task through a relay made from an Arc of a Wake impl, one type with its own wake_by_ref and one without. Each relay logs its drop, so every Arc count shows in the log. An even value also wakes it by value, and 99 panics inside the poll_fn. Its prologue drops an Arc of a poll_fn(&mut f) before f, and for one guard in eight, a struct of a waker whose last drop panics, then a guard. Results: 35,547 differential scripts and 5,274 unwinding scripts, 0 differences. Breaking rt.PollFn's dispose, ArcWaker's default wakeByRef clone, its willWake, or its dispose gives 1,779, 43, 14, and 1,779 differences. Dropping a borrowed function gives 847, and treating a waker's drop as unable to panic gives 7 unwinding differences.

Census at the pinned moq revision: the targeted refusals go from 592 to 0 in moq-net, 45 to 0 in the model, and 5 to 0 in kio. Those Poll instances now name kio's types, which moq-net refuses as from another crate, pending ts-model/crates.

Choices made without asking:

  • Runtime names: rt.PollFn, rt.ArcWaker (classes), and rt.NEVER, rt.NOOP_WAKER (shared /* @__PURE__ */ instances, which bundlers shake; the codec's shake test checks it).
  • pin! of a value JS copies, such as a number, stays refused, through the existing check on std calls that return a &mut to one. moq pins only futures.
  • The new task step is numbered 14, after #124's 13. #164 replaces these numbers with named steps; whichever merges second appends futures::helpers to the list.

Impact

  • Translator output: std::task::Poll's methods, pin!, poll_fn, future::pending, Waker::from(Arc<W>), and Waker::noop now translate instead of being refused. Non-breaking.
  • rt.ts gains PollFn, NEVER, ArcWaker, and NOOP_WAKER.

Alternatives

  • PollFn as its closure alone, with poll as a call: every future would no longer have a poll method, which rt.spawn and boxed futures rely on.
  • A separate Std::PinMacroHelper variant: it would repeat every Pin arm, since both are a one-field wrapper that is its contents.
  • rt.NEVER as an object literal: Bun keeps it, since [Symbol.dispose] is a computed key.

Follow-ups

  • ts-async is rewritten to what remains: Box::pin and Pin<Box<dyn Future>>, async closures, kio's API across crates, and rt.spawn's edge.

(Written by Claude Opus 5.5)

🤖 Generated with Claude Code

@kixelated kixelated changed the title Async in the TypeScript translator feat(ts): translate std's future helpers and wakers from Arc<impl Wake> Oct 28, 2026
@kixelated
kixelated marked this pull request as ready for review October 28, 2026 09:11
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 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-28T09:51:46.129196Z 8640e7b New commits
🔒 Security Review ✅ Completed 2026-10-28T09:54:36.955354Z 8640e7b 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9922ad1f1b

ℹ️ 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".

Comment thread ts/runtime/rt/task.ts Outdated
Comment on lines +238 to +239
[Symbol.dispose](): void {
drop(this.#f);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve borrowed closure ownership when dropping PollFn

When poll_fn receives &mut f, where f owns a value that drops, the stored JS function is still f itself, but dropping the future must not drop that borrowed closure. For example, drop(Arc::new(poll_fn(&mut f))) reaches this disposer through Arc's runtime drop and destroys f's captures prematurely; dropping f later destroys them again. The inert check in lower/drop.rs currently treats this non-dropping PollFn as safe for runtime disposal. Pass a type-aware closure dropper, or otherwise distinguish borrowed functions, instead of unconditionally calling drop(this.#f).

AGENTS.md reference: AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

@kixelated kixelated Oct 28, 2026 •

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.

Fixed in 7d69c00: rt.PollFn now takes a dropper for its function, which the translation passes only when F itself drops (drop_fn(F)), so poll_fn(&mut f) drops nothing and stays inert. The task fixture drops Arc::new(poll_fn(&mut lend)) before lend; reverting the fix gives 847 differences.

(Written by Claude Opus 5.5)

Comment thread ts/runtime/rt/task.ts
Comment on lines +193 to +194
[Symbol.dispose](): void {
this.#drop(this.#arc);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Account for panicking Arc-backed waker destructors

When the last waker owns an Arc<W> whose Drop implementation panics, this callback can now propagate that panic. However, Translator::drop_panics in lower/drop.rs still classifies every Std::Waker as non-panicking, so generated class and closure drop glue omits cleanup protection around waker fields. For a struct containing a waker followed by another dropping field, the latter is skipped when the waker's destructor panics, whereas native Rust still drops it. Update the waker panic classification alongside this new source of user-defined destruction.

AGENTS.md reference: AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

@kixelated kixelated Oct 28, 2026 •

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.

Fixed in 7d69c00: drop_panics now counts a Waker as able to panic, since one from an Arc runs the crate's Drop and the type doesn't say which. The task fixture drops a struct of a waker whose last drop panics, then a guard; reverting the fix gives 7 unwinding differences. No other snapshot changed.

(Written by Claude Opus 5.5)

@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: 9922ad1f1b77387e5523e148e5f608bdcb255aba.

Two P2 correctness issues remain; existing independent findings cover them, so no duplicate inline comments:

  • ts/runtime/rt/task.ts:238–239: PollFn<&mut F> must not dispose its borrowed closure. Arc<PollFn<&mut F>> currently reaches the unconditional disposer because inert treats it as safe, prematurely dropping captures and later double-dropping them. Preserve ownership with a type-aware dropper or equivalent distinction.
  • ts/runtime/rt/task.ts:193–194: Arc-backed waker destruction can now panic in the crate's Drop. drop_panics still returns false for Waker, so enclosing class/closure glue can skip subsequent fields. Account for this possibility and cover a panicking waker followed by another dropping field.

Direction: reusing core MIR for Poll and the existing Future/Waker interfaces is appropriately scoped; no broader abstraction is needed. Ownership and unwind edge cases need the above corrections.

Verification: static review of changed lowering/runtime, generated snapshots and fixtures via GitHub. I did not run the differential/unwind suites or independently verify the PR's reported test counts.

(Written by OpenAI)

@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: 9833ab00fac5966203e1a6d9dc5c1e0ca66d86c8, compared with my previous review of 9922ad1f1b77387e5523e148e5f608bdcb255aba (same base).

Both prior P2 findings are fixed: PollFn now receives a type-aware dropper, leaving borrowed closures alone; Waker drops are conservatively classified as panicking, preserving subsequent field/capture cleanup. The new borrowed-closure and panicking-waker fixtures and generated glue cover the reported paths.

No new actionable bugs found in the update. Direction is sound: these fixes reuse existing drop analysis without adding unnecessary abstractions.

Verification: static GitHub review of the two-commit delta, relevant runtime/lowering and generated fixtures. I did not run tests or independently verify the reported differential results.

(Written by OpenAI)

@kixelated

Copy link
Copy Markdown
Collaborator Author

Merging. Both Codex findings on 9922ad1 were fixed in 7d69c00, and Codex's review of 9833ab0 found nothing. 8640e7b only merges main (#159) with no conflicts. just ts check and just ts test pass on it locally, and CI Check passes. quest check --base origin/main passes. #164 is still open, so it will pick up futures::helpers when it renames the task steps.

(Written by Claude Opus 5.5)

@kixelated
kixelated merged commit 4e6231b into main Oct 28, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-async branch October 28, 2026 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant