Repository navigation
feat(ts): drop what closures own - #115
Conversation
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
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. |
kixelated
left a comment
There was a problem hiding this comment.
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.
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
There was a problem hiding this comment.
💡 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".
| if (inPlace) both(() => dropAll(out, drop), () => dropIt?.(it)); | ||
| else both(() => dropIt?.(it), () => dropAll(out, drop)); |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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)
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
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
…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
left a comment
There was a problem hiding this comment.
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
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
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
|
Design change: merging #129, which lowers (written by Opus 5.5) |
kixelated
left a comment
There was a problem hiding this comment.
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
|
Landing summary for a929f07:
(written by Opus 5.5) |
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
FnOncebody that drops its closure whole, and where std discards it (Option::maponNone,Result::map_erronOk). That was 17 refusals in moq-net, plus 2 for the whole-environment drop, after #101 mademap,map_err, andand_thenrefuse 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
maporany, a collection'sretainoror_insert_with), leaked what it owned silently.Consumer: moq-net's model, per the quest.
Approach
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_glueindrop.rs). It is thendisposable, sort.dropdrops it wherever MIR does, in a class, anArc, or a coroutine too.FnOncebody whose MIR drops its closure whole drops each capture there.FnOnce::call_onceof a closure thatFnorFnMutcall drops it after the call, a panic included, as rustc'sClosureOnceShimdoes:rt.callOnce(f, ...args). Given to a std mapping that calls it throughFnOnce, such asbool::then, it isrt.once(f), which does the same.Option's andResult's combinators lower from core's MIR (#129), whose bodies drop a closure they discard and call one throughFnOnceas any MIR does, so the two above serve them.bool::thentakes what drops the closure it discards (discardsinlibrary.rs).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.rsadds three scenarios (scopes and calls, std's discards includingbool::then, andDroppanics 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 ofboolThen. Rerun on #129's lowering: no dispose fails the differential test (417 differences), and so doesoncenot dropping after its call or on its discard (43 each), which a named closure thatFncalls, given tobool::then, covers now that core's bodies reachcallOnceinstead. Removing the refusal fails the reject snapshot.Impact
Census at the pinned moq revision, #101's head (1b072d4) then this branch:
No refusal about drops is left in either census. Not rerun since merging #129. Rerun at 0a95736 (on
origin/main5f93b21 and #101's db28893): moq-net 11055 refusals, kio 339, still none about drops. Translating moq-net's model (rootsmodel::origin_impl::ProducerandConsumer, 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 addsclosure,callOnce, andonce, which the codec never calls, so neither can differ frommain. Earlier heads, whose runtime droppers #129 replaced, measured within A/A injust ts benchand 51 / 5 bytes minified / gzip.Public API:
rt.closure(...).closure,callOnce, andonce. The runtime ships with each translation, so no consumer calls these by hand.bool::thenof a closure that owns a value that drops translates, whereorigin/mainrefused it.Choices made
bool::then, from #113, drops a closure that owns a value that drops onfalsethroughrt.boolThen's dropper, whereorigin/mainrefused it. It joinsdiscardedwithout a panic point of its own: all eight are taken, and a panic inside its closure is the pathmap's point 1 covers. Breaking its dropper fails the differential test (43 differences).mainrefuses a closure that owns a value that drops given tofrom_fnorsuccessorsthroughclosure_no_dropsiniter.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 throughcalled_closures, as any std mapping that calls it does, andsuccessorsof items that drop refuses inline, sincert.iterSuccessorswould leave the item it holds next undropped. Giving it a dropper is left to the quest, where it is recorded. The reject snapshot'sdropping Take<Successors<..>>andTake<FromFn<..>>stay, as onmain:counttakes each by value, and no std iterator holding such a closure can drop it yet.foldpath 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.Alternatives
FnOnceconsumer, one for a discard and one for after a call, in place ofrt.once: every such runtime function would grow, to serve the rare closure thatFncalls but std consumes.FnOncebody sets as it starts, so a dispose after the call does nothing: a write on every call, and a mutable function object.drop_in_placeshim, 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