Skip to content

feat(ts): drop what closures own - #115

Merged
kixelated merged 39 commits into
mainfrom
ts-drop-closures
Oct 28, 2026
Merged

kixelated merged 39 commits into
mainfrom
ts-drop-closures

Conversation

@kixelated

@kixelated kixelated commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Builds on #101, now in main, and rewrites quest/m2/ts-drop.md to what remains.

Problem

A closure was an arrow function, whose captures nothing outside could drop. So rust2-ts refused every closure that owns a value that drops wherever Rust drops it uncalled: at the end of its scope, as a panic unwinds, in an FnOnce body that drops its closure whole, and where std discards it (Option::map on None, Result::map_err on Ok). That was 17 refusals in moq-net, plus 2 for the whole-environment drop, after #101 made map, map_err, and and_then refuse rather than leak.

Separately, a closure that owns a value that drops, given to a std function whose mapping calls it but never drops it (an iterator's map or any, a collection's retain or or_insert_with), leaked what it owned silently.

Consumer: moq-net's model, per the quest.

Approach

  • A closure that owns a value that drops is rt.closure(f, dispose): the arrow function with [Symbol.dispose], which drops each capture in order, the rest still dropping after one panics, as rustc's glue does (closure_glue in drop.rs). It is then disposable, so rt.drop drops it wherever MIR does, in a class, an Arc, or a coroutine too.
  • An FnOnce body whose MIR drops its closure whole drops each capture there.
  • FnOnce::call_once of a closure that Fn or FnMut call drops it after the call, a panic included, as rustc's ClosureOnceShim does: rt.callOnce(f, ...args). Given to a std mapping that calls it through FnOnce, such as bool::then, it is rt.once(f), which does the same.
  • Option's and Result's combinators lower from core's MIR (#129), whose bodies drop a closure they discard and call one through FnOnce as any MIR does, so the two above serve them. bool::then takes what drops the closure it discards (discards in library.rs).
  • Any other std mapping given a closure it calls that owns a value that drops refuses it with a span (called_closures), where it leaked before. Checked after the mapping exists, so an unmapped call still reads as unsupported.

Proof: fixtures/task/src/drops/closures.rs adds three scenarios (scopes and calls, std's discards including bool::then, and Drop panics inside a closure's captures), each run at every panic point, through the differential test against wasm32 and the unwinding test against the host (24 new cases). Each fix was broken on purpose and the differential or unwinding test caught it: no dispose, the whole-environment drop, callOnce (dropping, and dropping on a panic), once, capture order, glue unwinding, and the dropper of boolThen. Rerun on #129's lowering: no dispose fails the differential test (417 differences), and so does once not dropping after its call or on its discard (43 each), which a named closure that Fn calls, given to bool::then, covers now that core's bodies reach callOnce instead. Removing the refusal fails the reject snapshot.

Impact

Census at the pinned moq revision, #101's head (1b072d4) then this branch:

moq-net kio
refusals 11310 → 11291 342 → 342
dropping a closure that owns a value that drops 17 → 0 0 → 0
a closure's environment read whole 2 → 0 0 → 0

No refusal about drops is left in either census. Not rerun since merging #129. Rerun at 0a95736 (on origin/main 5f93b21 and #101's db28893): moq-net 11055 refusals, kio 339, still none about drops. Translating moq-net's model (roots model::origin_impl::Producer and Consumer, not a census), drop refusals go from #101's 1, a closure that owns a value that drops, to 0.

Bench and bundle: the codec's translation is main's (its snapshot is unchanged), and the runtime only adds closure, callOnce, and once, which the codec never calls, so neither can differ from main. Earlier heads, whose runtime droppers #129 replaced, measured within A/A in just ts bench and 51 / 5 bytes minified / gzip.

Public API:

  • The translator's output for closures that own values that drop: they translate instead of refusing, as rt.closure(...).
  • New runtime exports closure, callOnce, and once. The runtime ships with each translation, so no consumer calls these by hand.
  • Newly refused: a closure that owns a value that drops given to std's iterators or a collection's methods, which leaked what it owned.
  • bool::then of a closure that owns a value that drops translates, where origin/main refused it.

Choices made

  • bool::then, from #113, drops a closure that owns a value that drops on false through rt.boolThen's dropper, where origin/main refused it. It joins discarded without a panic point of its own: all eight are taken, and a panic inside its closure is the path map's point 1 covers. Breaking its dropper fails the differential test (43 differences).
  • main refuses a closure that owns a value that drops given to from_fn or successors through closure_no_drops in iter.rs, from feat(ts): drop what std's iterators own #101's merge of feat(ts): translate bool::then, iter::successors, and From<bool> #113. This deletes it: such a closure refuses through called_closures, as any std mapping that calls it does, and successors of items that drop refuses inline, since rt.iterSuccessors would leave the item it holds next undropped. Giving it a dropper is left to the quest, where it is recorded. The reject snapshot's dropping Take<Successors<..>> and Take<FromFn<..>> stay, as on main: count takes each by value, and no std iterator holding such a closure can drop it yet.
  • Closures given to std's iterators and collections stay refused, with none in the census: an adaptor drops its closure after its iterator, but each consumer's fold path drops it in an order of its own relative to the source, as feat(ts): drop what std's iterators own #101 found for items. Recorded in the quest.
  • Every closure that owns a value that drops gets its dispose, even one called at once, whose dispose never runs. Telling them apart needs to know where the value flows; none is in a benchmarked path.

Alternatives

  • Two droppers for each FnOnce consumer, one for a discard and one for after a call, in place of rt.once: every such runtime function would grow, to serve the rare closure that Fn calls but std consumes.
  • A flag an FnOnce body sets as it starts, so a dispose after the call does nothing: a write on every call, and a mutable function object.
  • Dropping a closure through rustc's drop_in_place shim, as enums do: its body reads the closure's fields through a pointer, which JS has no form of; the captures are variables of the scope that made it.

Follow-ups

In the quest: closures that own values that drop given to std's iterators and collections, and the refusals without a census instance.

(written by Opus 5.5)

🤖 Generated with Claude Code

https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3

kixelated and others added 15 commits October 26, 2026 19:11
Quest-Claim: 662797e1badcb803
A `for` over a `Vec`, `collect`, `extend`, `drain`, `into_keys`, and
`into_values` of values that drop, and the adaptors that discard or hold
them, drop each where std's do, a panic included. A field of an aggregate
read again through a reference is re-read only if reading it runs nothing.

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

# Conflicts:
#	ts/fixtures/lang/expected/fmt.ts
#	ts/fixtures/lang/expected/ints.ts
#	ts/fixtures/lang/expected/iters.ts
#	ts/fixtures/reject/census.md
#	ts/src/lower/iter.rs
…he quest

`count` and a byte `collect` loop without a dropper check where nothing
drops. The drop quest now holds what remains: closures that own values
that drop, and the refusals without a census instance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
A hash iterator's dispose resumed where a panicking drop left it, so a
consumer that disposed it again after `count` dropped the entries std's
table leaks. It now takes its count first. The task fixture's `spent`
covers each consumer that drops an iterator it already disposed.

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

# Conflicts:
#	ts/fixtures/lang/expected/ints.ts
`Skip`'s `nth_back` and `advance_back_by`, `Take`'s `advance_back_by`, and
`StepBy`'s `nth_back` read their iterator from the back in one step, so
what they pass drops front to back, as a `Vec`'s slice does, where the
generic loop dropped it one item at a time from the back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
`StepBy`'s `nth` drops the first item, then reads every step in one
`nth`, so a reversed source drops what it passes front to back, as std's
does. Its `nth_back` saturates at wasm32's `usize::MAX`, as std's does,
rather than passing on a count past it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
A borrowed one is read again after `by_ref().count()`, so its count must end it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
`Option::map` drops its closure on `None`, and `Result`'s `map`, `map_err`, and
`and_then` on the variant they skip, which an arrow function can't. They now list
the closure among what they discard, so one that owns a value that drops is refused
where it was leaked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
A consumer of a borrow took the iterator's by-value overrides, so
`it.by_ref().count()` of a `Chain` ended both halves where std's reads on. Every
`&mut` iterator is now `iterBorrow`'s, not only one that drops, which also covers
the borrowed hash iterator's count that e732b4b special-cased.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
A closure that owns a value that drops carries [Symbol.dispose], which drops
its captures in order, the rest still dropping after one panics. An FnOnce
body that drops its closure whole drops each capture. One that Fn or FnMut
call drops after a call through FnOnce, as rustc's shim does. Option::map,
map_or, and Result's map, map_err, and_then, and inspect_err drop the closure
they discard. A closure that owns a value that drops, given to any other std
function that calls it, is refused rather than leaked.

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

chatgpt-codex-connector Bot commented Oct 27, 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 ✅ Completed 2026-10-27T23:55:25.139676Z a929f07 New commits
🔒 Security Review ✅ Completed 2026-10-27T23:56:09.628005Z a929f07 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.

@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: 37cccfa

No actionable bugs found in the closure-drop delta above #101's reviewed head (1b072d4). I checked capture glue and unwind ordering, whole-environment FnOnce drops, the Fn/FnMut-to-FnOnce shim, std discard paths, and the new differential/rejection fixtures. The inherited iterator changes are not new findings here.

Direction looks sound: reuse the existing drop glue and centralize consuming-call cleanup in callOnce/once, rather than multiplying per-consumer cleanup paths. Explicit refusal for iterator/collection mappings without correct closure-drop ordering is preferable to silently accepting leaks.

Verification limits: static source and generated-fixture inspection only; I did not run the translator, differential/unwinding tests, census, or benchmarks.

kixelated and others added 4 commits October 27, 2026 08:26
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
std's `nth`, `advance_by`, `count` and `last` drop items in an order each
kind of iterator has its own. These were methods on every adaptor, which
a bundler can't drop, so every bundle carried them. They are now
module-private dispatchers on the kind, reached only through `rt.owned*`
functions the translation calls where an item or the iterator drops; the
plain `rt.iterCount`, `iterLast`, `iterNth`, `iterSkip`, `iterTake` and
`iterStepBy` take no droppers.

`[Symbol.dispose]` stays a method where `rt.drop` calls it without knowing
the kind: an iterator that owns items, and an adaptor, which drops what it
wraps.

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

@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: 072f7aee2b

ℹ️ 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/iter.ts Outdated
Comment on lines +503 to +504
if (inPlace) both(() => dropAll(out, drop), () => dropIt?.(it));
else both(() => dropIt?.(it), () => dropAll(out, drop));

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 Skip collected-item cleanup when no item dropper exists

When the iterator needs dropping but its output items do not, drop is undefined; passing that to dropAll selects its default rt.drop instead of doing nothing. For example, collecting Vec<Guard>::into_iter().map(...) into Vec<&Guard>, with the mapping returning an external reference before a later iteration panics, prematurely disposes the referenced external guard during cleanup and disposes it again when its real owner drops. I reproduced this against native Rust and the runtime: only the runtime drops the external guard before catch_unwind returns. Guard these cleanup calls, including the later iterator-disposal failure path, on an actual item dropper being present.

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

Useful? React with 👍 / 👎.

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.

Agreed, and reproduced: a panicking collect of &Guards from a Vec<Guard> dropped the referenced guard once more under the unwinding test. The bug is #101's iterToArray, so it's fixed there in ff5f56c, with that case added to collected's point 5. Both cleanup paths now drop what was collected only when there is an item dropper. This head merges it in (547cd3f).

(written by Opus 5.5)

kixelated and others added 3 commits October 27, 2026 09:19
Of items that drop nothing, `iterToArray` passed `undefined` to
`dropAll`, whose default is `rt.drop`: a collect of references from a
source that drops disposed what they pointed to as a panic unwound.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
kixelated and others added 7 commits October 27, 2026 10:50
…ters

A generic class's glue drops each field at its declared type. One generic
over lifetimes alone, as moq-net's `path::Repr<'a>` is, found no glue,
since rustc's is per instance; lifetimes don't change a drop, so it now
drops by the glue of its erased type. A tuple of type parameters, as a
generic map's `(K, V)` entries are, now drops each element in order, the
rest still dropping after one panics, as rustc's glue for a tuple does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
# Conflicts:
#	ts/README.md
#	ts/fixtures/reject/census.md
#	ts/fixtures/reject/expected.txt
Resolves #113 against this PR: `bool::then` now drops an owning closure on
`false` through `rt.boolThen`'s dropper (checked in `drops/closures.rs`), and
`from_fn` and `successors` refuse an owning closure through `called_closures`.
#101 removed `no_drops`, so `successors` of items that drop refuses inline.

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

@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: 0a95736

No new actionable bugs found in the integration delta after accounting for main and #101. bool::then now participates in the existing closure-discard/consuming-call machinery: false drops the closure, while true calls it with the appropriate FnOnce cleanup. The added closure fixture exercises both branches. from_fn/successors reject owning closures through the shared guard, and successors separately rejects an owned item requiring cleanup.

Direction looks sound: reuse the existing closure ownership checks rather than add another cleanup mechanism; retain explicit refusal where iterator drop ordering is unsupported. The previously reported reference-collection and Skip::nth overflow fixes remain present; their discussion threads remain unresolved, not new findings.

Verification limits: static source and generated/rejection-fixture inspection only; I did not run tests, census, or benchmarks.

#101 squash-merged. Its own merge of #113 refused owning closures in
from_fn and successors through closure_no_drops; this branch's
called_closures refusal replaces it, keeping successors' refusal of
items that drop.

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

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

kixelated and others added 3 commits October 27, 2026 16:12
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
#129 lowers Option's and Result's combinators from core's MIR and
deletes the runtime functions this branch gave closure droppers. Core's
bodies drop a discarded closure through MIR, which rt.closure's dispose
serves, so those droppers and their discards entries go, and main's
refusal of an owning closure in core's bodies no longer fires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
Core's MIR now calls a closure std's Option and Result consume through
rt.callOnce, which left rt.once, still bool::then's, untested. A named
closure that Fn calls, given to then on true and on false, covers its
call and its discard.

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

Copy link
Copy Markdown
Collaborator Author

Design change: merging #129, which lowers Option's and Result's combinators from core's MIR and deletes rt.optionMap, optionMapOr, and result*, drops this PR's droppers on those functions and their discards entries. Core's bodies now drop a closure they discard through MIR, which rt.closure's dispose serves, and call it through rt.callOnce, so main's refusal of an owning closure in those bodies no longer fires. rt.once now serves only bool::then, which discarded covers with a named Fn closure (1572755). Breaking the dispose fails the differential test with 417 differences, and breaking once's call or discard fails it with 43 each.

(written by 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: 1572755

No new actionable bugs found in the closure-specific integration with core MIR. Option/Result's generated functions retain explicit uncalled-closure drops and use callOnce when consuming a closure callable through Fn/FnMut. Removing the superseded runtime mappings avoids maintaining a second ownership implementation. bool::then still uses once, and the new named-closure fixture covers its call and discard paths.

The direction remains sound: let MIR determine cleanup order and retain the existing consuming-call shim. Earlier collection-reference and Skip::nth findings were fixed previously; this update does not change those paths, and their unresolved threads are not new findings.

Verification limits: static review of the integration, generated Option/Result functions and closure fixtures only. I did not run tests, census, or benchmarks.

#128 makes a closure's captures `Capture`s, which mark a `&mut` it reads
and writes in place. closure_glue drops each capture's value; a `&mut`
never drops.

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

Copy link
Copy Markdown
Collaborator Author

Landing summary for a929f07:

(written by Opus 5.5)

@kixelated
kixelated merged commit a229672 into main Oct 28, 2026
1 check passed
@kixelated
kixelated deleted the ts-drop-closures branch October 28, 2026 00:11
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