Repository navigation
feat(ts): drop what std's iterators own - #101
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
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: 0a01766e8c
ℹ️ 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".
| const n = this.left; | ||
| this[Symbol.dispose](); |
There was a problem hiding this comment.
Avoid disposing hash iterators twice during count
When a key or value destructor panics during HashMap::into_iter().count() or HashSet::into_iter().count(), this disposal throws with left still pointing to the remaining entries. iterCount then invokes the iterator's disposal again in its finally block, dropping those remaining entries even though native Rust leaves them undropped. I reproduced this with three entries and a destructor that panics only on its first invocation: native Rust runs one destructor, while the runtime runs all three. Leave disposal to the consuming wrapper, or ensure a failed disposal cannot resume dropping entries.
AGENTS.md reference: AGENTS.md:L15-L15
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in eb9d876: the hash iterator's dispose now takes its count before dropping, so a second dispose after a panicking drop drops nothing, as std's table leaks the rest. The other iterators' disposes already take what they hold first. A new task scenario, spent, panics in the first drop inside count of a hash map and set, skip, peekable, chain, a B-tree, a deque, and a hash drain, and checks how many drop against native Rust; it failed for the map and set before the fix.
(written by Opus 5.5)
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 0a01766e8c82544bfe562157f6bf2ab2fbcd7ee0.
One actionable issue, already independently reported: hash iterator disposal is retried after a destructor panics (ts/runtime/rt/collections.ts:623-624, called by iterCount's finally). I independently traced the same remaining-entry drops; no duplicate inline comment. Make failed disposal non-resumable or give consumption a single disposal owner, including the SkipIter/Peekable consumer paths, and add a multi-entry panicking-drop regression. No other actionable bugs found in the reviewed areas.
Direction: explicit droppers and per-iterator overrides are justified by observable std drop order; the pure-expression guard also fixes the underlying repeated-evaluation problem. Keeping unsupported closure ownership and differing flatten/zip orders as explicit refusals is preferable to silently approximating them. Centralizing disposal ownership would simplify the remaining panic-sensitive paths.
Verification: inspected lowering, runtime changes, generated output and differential-test additions, with an independent runtime pass. Reproduced the reported double-disposal control flow in a minimal JS model, not the full translator. Did not run the repository's Rust/TypeScript tests or benchmarks.
(Written by OpenAI)
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
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: eb9d8765b1d8b0877eb0c05f779eb5a3070577ac, compared with my prior review at 0a01766e8c82544bfe562157f6bf2ab2fbcd7ee0.
The previous hash-iterator double-disposal finding is fixed: HashIter consumes its remaining count before invoking a destructor, so a panic cannot make later consumer/adaptor cleanup resume disposal. The local count preserves normal traversal. No new actionable bugs found in this update.
Direction: this small ownership-state fix is simpler than special-casing each consumer. The eight new spent scenarios add useful coverage for first-drop panics across collections, adaptors and draining without depending on hash iteration order.
Verification: reviewed the one-commit delta, surrounding disposal paths and generated fixtures, with an independent runtime check. A minimal JS control-flow model now runs only the first destructor. Repository differential tests and benchmarks were not run by me.
(Written by OpenAI)
…0cccd38efd6 # Conflicts: # ts/fixtures/lang/expected/ints.ts
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0ccae57570
ℹ️ 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".
| nthBackItem(n: number, drop?: Drop<T>): T | Done { | ||
| return this.advanceBackBy(n, drop) === 0 ? this.nextBack() : DONE; |
There was a problem hiding this comment.
Preserve specialized reverse skipping for owning adaptors
For owning Skip and StepBy iterators, this fallback drops items through repeated nextBack() calls instead of preserving std's specialized nth_back behavior. Differential checks with five values whose destructors log their IDs show that .into_iter().skip(2).nth_back(3) drops 4,5,3,1,2 natively but 5,4,3,1,2 in this runtime; .step_by(2).nth_back(3) drops 1,2,3,4,5 natively but 5,4,3,2,1 here. A panicking destructor also changes which remaining destructors run. Add the corresponding reverse-skipping specializations rather than routing these adaptors through the generic loop.
AGENTS.md reference: AGENTS.md:L15-L15
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in c06f73a: Skip now has std's nth_back and advance_back_by, Take its advance_back_by, and StepBy its nth_back, each reading the iterator from the back in one step. The task fixture now checks skip(2).nth_back(3), step_by(2).nth_back(1), and advance_back_by of take and skip (through rev().chain(..).nth(..)) against native Rust; each override, removed, fails it. StepBy's forward nth stays on the generic path: it drops the same items in the same order, front to back, so no test can tell it from std's.
(written by Opus 5.5)
`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
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: c06f73a0cfc8a2bb41b002046ea36443d5fa40c1, against my last reviewed eb9d8765b1d8b0877eb0c05f779eb5a3070577ac, excluding the equivalent main merge.
One new P2 below: the combined StepBy reverse index needs bounded arithmetic. The independent reverse-skipping drop-order finding is addressed by the specializations and targeted tests; the earlier hash double-disposal fix remains intact.
Direction: delegating reverse skipping to the inner iterator is the right, compact way to retain its drop ordering. Preserve std's saturating index calculation rather than forwarding an unbounded JS product.
Verification: inspected the delta, generated fixtures, range lowering and runtime, with an independent pass. A minimal JS calculation reproduces the unsafe-integer exception below. I did not run the translator, native differential tests or benchmarks.
(Written by OpenAI)
|
|
||
| /** std's: every step from the back in one. */ | ||
| override nthBackItem(n: number, drop?: Drop<T>): T | Done { | ||
| return this.inner.nthBackItem(n * (this.step + 1) + this.backIndex(), drop); |
There was a problem hiding this comment.
[P2] Saturate the combined reverse index before forwarding it
(0i32..10).step_by(usize::MAX).nth_back(usize::MAX) should return None, but on the wasm32 target this multiplication produces about 1.84e19. Narrow ranges are lowered without an integer step helper (mir/rvalue.rs), and Range.nthBackItem passes that offset into step, which throws rust2-ts: stepping a wide range without its type when the sum is not a safe JS integer. This is introduced by the new combined-index path; the previous repeated-next path exhausted this one-element stepped range. Use std's saturating multiply/add semantics for the target usize width and add a large step/index range regression.
There was a problem hiding this comment.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c06f73a0cf
ℹ️ 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".
| override advanceBy(n: number, drop?: Drop<T>): number { | ||
| return this.inner.advanceBackBy(n, drop); |
There was a problem hiding this comment.
Preserve sequential dropping when advancing a reversed iterator
For an owning reversed iterator, forwarding advanceBy to the inner iterator's specialized advanceBackBy changes the order in which skipped values drop. A differential check with five values whose destructors log their IDs shows that .into_iter().rev().take(3).count() drops 5,4,3,1,2 in native Rust but 3,4,5,1,2 in this runtime. A panicking destructor consequently also changes the unwinding path. Preserve std's sequential reverse advancement here rather than using the inner iterator's bulk slice drop.
AGENTS.md reference: AGENTS.md:L15-L15
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing this one: on the pinned nightly (1.101.0-nightly 2026-10-15), std's Rev::advance_by is self.iter.advance_back_by(n) and Take::count is self.iter.advance_by(self.n), so vec::IntoIter::advance_back_by drops the slice 3,4,5 front to back. The task fixture now checks .into_iter().rev().take(3).count() against native Rust built with that toolchain: it passes as is, and fails if Rev advances one item at a time. The 5,4,3,1,2 order is that of a std whose Take::count goes through fold.
(written by Opus 5.5)
| nthItem(n: number, drop?: Drop<T>): T | Done { | ||
| return this.advanceBy(n, drop) === 0 ? this.next() : DONE; |
There was a problem hiding this comment.
Specialize forward nth for owning StepBy iterators
When StepByIter inherits this implementation, nth skips through repeated next() calls instead of std's specialized forward skipping. With five values whose destructors log their IDs, .into_iter().rev().step_by(2).nth(2) followed by dropping the result and iterator produces 5,2,3,4,1 in native Rust but 5,4,3,2,1 in this runtime. This also changes which destructors run before a panic. Add a forward nthItem specialization for StepByIter, independently of its existing nthBackItem specialization.
AGENTS.md reference: AGENTS.md:L15-L15
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, fixed in 114b9a4: I had left forward nth on the generic path thinking the order matched, which holds only for a source read front to back. StepBy now has std's spec_nth: the first item dropped, then every step in one nth, split as std splits a count past usize::MAX. The fixture checks rev().step_by(2).nth(2) against native Rust, which fails without the override.
(written by Opus 5.5)
`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
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 1672244203e1c4f81d9638f1ba18fe3f91acd3d4, compared with my last published review at c06f73a0cfc8a2bb41b002046ea36443d5fa40c1.
Fixed: the StepBy reverse-index overflow now saturates at wasm32 usize::MAX, with the exact large-step/large-index regression added. The independent forward-nth finding is also addressed: first-item handling and overflow splitting preserve the inner iterator's bulk-drop behavior. Prior hash-disposal and reverse-skipping fixes remain intact. No new actionable bugs found in these updates.
Direction: the extra forward-nth logic is justified by observable drop order; bounded indices avoid imposing unsafe JS numbers on narrow ranges. The separate reverse-count discussion is toolchain-sensitive; the author explains the pinned nightly's specialization and adds a differential case, so I am not re-raising it from older std behavior.
Verification: inspected both commits, surrounding runtime and generated fixtures, with an independent arithmetic/ownership pass. Checked the saturated calculation stays a safe integer. I did not run the translator, pinned-nightly differential tests or benchmarks; the reported test outcomes are the author's.
(Written by OpenAI)
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1672244203
ℹ️ 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".
| override countItems(drop?: (item: T) => void): number { | ||
| if (this.drain) return super.countItems(drop); | ||
| const n = this.left; | ||
| this[Symbol.dispose](); | ||
| return n; |
There was a problem hiding this comment.
Exhaust non-owning hash iterators when counting them
For a hash iterator without key/value droppers, countItems() returns its length without consuming any entries because [Symbol.dispose]() immediately returns. The lowering also leaves borrowed iterators unwrapped when their underlying type does not drop, so let mut it = map.iter(); it.by_ref().count(); it.next() incorrectly yields another entry. A differential check with two integer entries produces 2, None in native Rust but 2, Some(...) in this runtime. Use the consuming loop for non-owning/no-drop iterators, or otherwise advance their cursor when counting.
AGENTS.md reference: AGENTS.md:L15-L15
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, fixed in e732b4b: a hash iterator with no droppers now counts by reading each entry, so a borrowed map.iter() ends after by_ref().count(). Only one that owns droppable entries takes the length-then-drop path, and that one is wrapped when borrowed. The lang fixture checks it.by_ref().count() then it.next().is_none() against native Rust, which failed before the fix.
(written by Opus 5.5)
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
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: e732b4b509ada689c73c897d931350650124986f, compared with my last published review at 1672244203e1c4f81d9638f1ba18fe3f91acd3d4.
Fixed: the independent non-owning hash-iterator count finding. The no-drop path now consumes entries, so by_ref().count() leaves subsequent next() exhausted. Owning disposal and drain behavior are unchanged; earlier fixes remain intact. No new actionable bugs found.
Direction: reusing the existing consuming loop is a small, clear fix, with a targeted borrowed-iterator regression. A future no-callback fast path could consume the remaining count directly, but is not needed for correctness.
Verification: inspected the one-commit delta, surrounding iterator/consumer code and generated fixture, with an independent runtime pass. Generated changes beyond the new checks are identifier renumbering. I did not run repository tests or benchmarks.
(Written by OpenAI)
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e732b4b509
ℹ️ 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".
| None if !owned && b.t.drops(ty.peel_refs()) => Expr::rt("iterBorrow", vec![value]), | ||
| None => value, |
There was a problem hiding this comment.
Wrap borrowed iterators even when they have no droppable values
When a non-dropping iterator is borrowed through by_ref(), this condition leaves it unwrapped, so consumers invoke its by-value specializations instead of consuming through the borrow. For example, chain an empty iterator with a from_fn source that returns Some(1), None, Some(3), then call it.by_ref().count(); it.next(). A differential check produces 1, Some(3) in native Rust but 1, None in this commit's runtime because ChainIter.countItems() clears both stored iterators. Apply the borrowed wrapper regardless of whether the underlying type drops, so consuming a borrow preserves the original iterator's remaining state.
AGENTS.md reference: AGENTS.md:L15-L15
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, fixed in 1b072d4: every iterator borrowed by &mut is now wrapped in iterBorrow, which forwards only what std's impl Iterator for &mut I forwards (next, advance_by, nth, and their back versions), so count and last read through the borrow. The lang fixture checks your empty().chain(from_fn(..)) case against native Rust (1, Some(3)), which failed before. This also covers the borrowed hash count, so e732b4b's special case is reverted; its test still passes. The codec translation the bench runs is unchanged.
(written by Opus 5.5)
`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
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 9f7a821bca92915bc8c41729a731eb4f0b25f655, compared with my last published review at e732b4b509ada689c73c897d931350650124986f.
The new closure checks correctly route Option::map and Result::{map,map_err,and_then} through the existing refusal for owned droppable captures, rather than silently leaking captures on an uncalled branch. No new actionable bugs found in this delta. Reject fixtures cover Option::map and Result::map_err; prior hash and StepBy fixes remain intact.
Still open, not introduced by this update: the independently reported borrowed-iterator finding at ts/src/lower/iter.rs:138-139. A no-drop borrowed chain still reaches the by-value count specialization, which clears its resumable source. Apply the borrowed wrapper regardless of drop requirements and retain the non-fused-source regression described there; no duplicate inline comment.
Direction: conservative refusal reuses the existing ownership checks and is preferable to adding incomplete closure disposal support. Verification: inspected the one-commit delta, generic-type indices, drop/refusal plumbing and fixtures, plus an independent check of the still-open iterator finding. I did not run translator tests or benchmarks.
(Written by OpenAI)
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
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 1b072d4878a57005f5f47decd600dff4e3df60a1, compared with my last published review at 9f7a821bca92915bc8c41729a731eb4f0b25f655.
Fixed: the borrowed-chain count finding previously noted as open. Borrowed iterators now use the wrapper regardless of drop requirements, so counting the gappy chain preserves its resumable source. The hash-count workaround can safely be removed: its existing regression now counts through the same wrapper. Earlier disposal, StepBy and closure-refusal fixes remain intact. No new actionable bugs found.
Direction: fixing borrow semantics centrally is simpler and more general than adding per-iterator exceptions. The exact non-fused-chain regression and updated borrowed-adaptor fixtures match that approach.
Verification: inspected the one-commit delta, lowering paths, wrapper forwarding, count/disposal behavior and generated fixtures, with an independent runtime pass. I did not run repository tests or benchmarks.
(Written by OpenAI)
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
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 771e3e6e53d09e20b9fef583b6279ba10d2336ad, against my last published 1b072d4878a57005f5f47decd600dff4e3df60a1, accounting for the main merges. The aggregate/formatting and bundle changes from #114, #117 and #119 are inherited, not new findings here.
One new P2 below: removing Fuse's drop-free reverse-nth forwarding changes observable borrowed-iterator state. Previously addressed hash-disposal, borrowed-chain, StepBy owned-path and closure-refusal issues remain fixed.
Direction: separating owned operations into tree-shakeable functions is reasonable, but iterator specializations can matter without destructors. Keep the small semantic forwarding methods rather than treating every no-drop operation as repeated next.
Verification: inspected the refactor, lowering/collection integration, generated fixtures and merge overlap, with an independent runtime pass. A minimal JS model reproduces the old/new state difference below. I did not run the translator, pinned-nightly differential tests or bundle benchmarks.
(Written by OpenAI)
| if (item === DONE) this.inner = undefined; | ||
| return item; | ||
| return this.inner === undefined ? DONE : this.clear(this.inner.nextBack()); | ||
| } |
There was a problem hiding this comment.
[P2] Preserve Fuse's drop-free nth_back forwarding
After removing FuseIter.nthBackItem, a no-drop nth_back(0) now uses the generic implementation and calls nextBack() instead of the inner iterator's nthBackItem(0). These are observably different: let mut it = 0..3; { let mut f = it.by_ref().take(0).fuse(); f.nth_back(0); } should leave it.next() as None, because Take::nth_back drains its inner iterator even at a zero limit. The new path reaches Take.nextBack, which immediately returns DONE at limit zero, leaving the original range to yield Some(0). Restore the no-drop forwarding override (and audit other removed specializations for state effects), with a borrowed-source regression.
There was a problem hiding this comment.
Agreed that this differs from std, but not that it's this PR's: main has no FuseIter.nthBackItem either, so main already leaves the range at Some(0) here. This head's no-drop paths are exactly main's methods. The previous head fixed it only as a side effect of putting every override on the classes, which is the bundle cost the maintainer asked to drop.
A faithful no-drop fix isn't only Fuse's forwarding. Chain::nth_back goes through advance_back_by, Rev::nth through nth_back, and Take::advance_back_by reads past its limit too, so restoring a few forwarding methods would be a partial fix. Doing the whole thing means choosing between methods on every class and calling the owned dispatchers, which pull in every class they name. I recorded that as quest/m2/ts-iter-nth.md (405151c) with this case. Where an item drops, the dispatchers already follow std, Fuse included.
(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
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 17aba733e6d0b0e917e2c95c6546d0a578bbfd3d, compared with my last published 771e3e6e53d09e20b9fef583b6279ba10d2336ad, accounting for inherited #120 changes.
The collect cleanup fix correctly avoids dropAll's default destructor when collected items have no dropper. This prevents a panic from disposing borrowed referents, preserves both cleanup orders, and adds a targeted Vec<&Guard> regression. No new actionable bugs found in this update.
Correction to my prior finding's scope: I verified that the Fuse mismatch also exists in the base. It was a regression relative to an intermediate reviewed head, not a newly introduced base-relative defect. It remains unfixed and explicitly tracked in quest/m2/ts-iter-nth.md; I am not re-raising it as new. Earlier ownership fixes remain intact.
Direction: the explicit-dropper guard is the right small fix; documenting the separate no-drop specialization work keeps its bundle-size tradeoff visible. Verification: inspected the fix, cleanup helpers, fixture output and base comparison, with an independent pass. I did not run repository tests or benchmarks.
(Written by OpenAI)
`Skip::nth` of items that drop passed what it skips plus `n` in one step even past `usize::MAX`, where std reads what it skips in one `nth` and `n` in another: a panicking drop of the first left nothing in the iterator here, and the rest in std's. 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
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: eebf34d49f84b2d26bb1830ca1ae073b93c932d4, compared with my last published 17aba733e6d0b0e917e2c95c6546d0a578bbfd3d, excluding inherited benchmark changes from #116.
The owned Skip::nth overflow fix correctly separates the skipped prefix from the requested index, preserving the iterator's remaining ownership when a destructor panics. Clearing the pending skip before either call is appropriate, and overflow guarantees skip - 1 cannot underflow. No new actionable bugs found. Previous ownership fixes remain intact; the pre-existing no-drop Fuse limitation remains separately tracked, not newly raised here.
Direction: the small overflow branch is justified by observable cleanup order. The added intervening _after destructor makes the regression test discriminate that order, rather than merely checking that everything eventually drops.
Verification: inspected the runtime delta, surrounding dispatch and generated panic cleanup, with an independent pass. A minimal JS model changes the observed order from 1,2,3,4 to 1,4,2,3 as intended. I did not run repository tests or benchmarks.
(Written by OpenAI)
…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
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 44e7b25710b2a0a00d41225757cafa9762ecb6c6, compared with my last published eebf34d49f84b2d26bb1830ca1ae073b93c932d4.
No new actionable bugs found. Erasing lifetimes before drop-glue lookup correctly permits lifetime-only generic fields to use concrete glue. The generic-tuple fallback descends through elements and reuses the existing unwinding sequence, preserving later-element cleanup after an earlier destructor panics. Generated Named and Entries glue matches those cases, with targeted normal/unwind fixture coverage.
Direction: canonicalizing lifetime-only types and reusing the existing cleanup builder is simpler than introducing separate per-lifetime glue or a second unwinding mechanism. Earlier ownership fixes are unchanged; the pre-existing no-drop Fuse limitation remains separately tracked.
Verification: inspected the one-commit delta, glue resolution, tuple recursion, cleanup builder and generated fixtures, with an independent tuple/unwinding pass. I did not run repository tests or benchmarks.
(Written by OpenAI)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
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
Keeps #113's refusal of what `from_fn` and `successors` would never drop: a closure that owns a value that drops, and `successors` of items that drop, whose pending item `rt.iterSuccessors` leaves undropped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
|
Merged origin/main (through #125) in d698cb7, resolving conflicts by hand:
Checks: (written by Opus 5.5) |
#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
#101's discards for Option's and Result's combinators go with their mappings, and reject's case for the closures they drop gains and_then in place of this branch's duplicate.
Problem
An
rt.Iterdropped nothing it skipped, consumed, or still held, so rust2-ts refused every iterator that owns a value that drops: aforover aVecor map,collectof clonedPaths,drain,into_keys,into_values, and the adaptors over them. At the pinned moq revision that was 101 refusals in moq-net and 2 in kio, plus 13 for the collection methods that make such an iterator.Consumer: moq-net's model and kio, per quest/m2/ts-drop.md.
Approach
[Symbol.dispose]:iterSlice(aVecor array by value),iterOption, a crate's own iterator throughiterFrom, and the hash, B-tree, and deque iterators,drains included. AVeciterator stepped in place drops throughrt.dropAll(s, f, i).filter's rejects,skip,step_by,nth,skip_while,take_while's ender,zip's unmatched item,Peekable's item,Flatten's front and back,max_by_key's losers.count,last,nth,nth_back, andskip,take, andstep_by'snextfollow std's per-type overrides (advance_byof aVecmoves past then drops,Skip::countisnththencount, and so on).rt.ownedCount,ownedLast,ownedNth,ownedNthBack,ownedSkip,ownedTake, andownedStepByonly where an item or the iterator drops, and the plainrt.iterCountand the rest otherwise, so a bundle that never drops carries none of them. Without an item that drops, every kind's ownnthreads the same items, so the plain paths keep main's methods.[Symbol.dispose]stays a method only wherert.dropcalls it without knowing the kind: on an iterator that owns items (slice,Option,iterFrom's, hash, B-tree, deque) and on an adaptor, which drops what it wraps (oneAdaptorbase for the single-iterator ones;Chain,Zip,Fuse,Flatten, andPeekabledrop their own parts).rt.dropfor it and drops it once done or on a panic:count,last,fold,for_each,sum,max,collect,extend.collectinto aVecfollows std's in-place path where std takes it (InPlaceIterableandSourceIterdown to avec::IntoIter,MERGE_BYandEXPAND_BYconst-evaluated, wasm32 layouts compared as std'sin_place_collectibledoes): a panic drops what it collected before the rest of the source, and the generic path the other way.ResultandOptioncollects drop the values before anErrorNone. A B-treecollectsorts, dedups keeping the last, and on a panicking drop drops and leaks as std's does.&mutis wrapped initerBorrow, which forwards what std'simpl Iterator for &mut Iforwards, so a consumer of a borrow reads through it rather than by the by-value overrides, and never drops it.std_iterindrop.rsclassifies std's iterator adaptors: one drops if a type argument (with aDropimpl) or a field does.path::Repr<'a>) now drops by its erased type's glue, and a tuple of type parameters (a(K, V)entry) drops each element in order, the rest still dropping after one panics. Both were refused before, onmaintoo; a census never showed them, since it drops a type with parameters byrt.drop.fixtures/task'sNamed,Text, andEntriescover them,Entrieswith a panicking first half.Option::maponNone, andResult'smap,map_err, andand_thenon the variant they skip, dropped nothing of their closure, so one that owns a value that drops leaked it silently. They now list the closure among what they discard, which refuses such a closure until closures can drop (fixtures/reject'sdiscarded_closure).from_fnandsuccessors(from feat(ts): translate bool::then, iter::successors, and From<bool> #113) still refuse a closure that owns a value that drops, whichrt.iterFromFnnever drops, andsuccessorsrefuses items that drop, asrt.iterSuccessorsleaves the one it holds next undropped (fixtures/reject'sowning_lazy).Proof:
fixtures/task/src/drops/iters.rsadds six scenarios (loops and adaptors,collect, draining,extend,Droppanics inside iterators, and inside consumers that drop the iterator again), each run at every panic point, through the differential test against wasm32 and the unwinding test against the host (48 new cases). Each fix was broken on purpose and a test caught it: in-place order, slice dispose andadvance_byorder, filter rejects, zip unmatched, skip,Resultcollect,max_by_keylosers,PeekableandFlattendispose, hash, B-tree, and deque dispose, B-treecollectdedup and its panic path,extenddropping its source on a panic, the steppedVecindex, and in-place detection.Impact
Census at the pinned moq revision,
origin/main(53506aa) then this branch:_, which drops_of_, which may drop what it holdsTranslating moq-net's model (roots
model::origin_impl::ProducerandConsumer, not a census), drop refusals onorigin/main(9fc2c22) then this branch: 27 → 0 for an iterator that owns a value that drops, 4 → 0 for a collection that may drop what it holds, 2 → 0 for glue of a type with parameters, and 0 → 1 for a closure that owns a value that drops, which #115 takes to 0.just ts benchagainstorigin/main(9655e37)'s translations as--route main, run on #115's head, whose iterator runtime is this PR's. Chrome: A/A differed from translated by 0.8% at the median workload and 18% at most; translated over main is 1.005 at the median. Theiter/workloads are within 1% of main butiter/filter-collect, 4% behind in both copies;btree/is within 2%. The async handoff is within 1% of main in Chrome.Bundle, codec then async handoff, minified / gzip:
origin/main(9fc2c22)What the codec still carries over main, 1210 / 415, is the drop paths of the entry points it calls without a dropper: each is an optional trailing parameter, main's convention for droppers, which a bundler keeps with its function. The largest are the B-tree iterator's (dropping the other half of
into_keysandinto_values, and its dispose),iterSlice's dispose,filter's andfind's rejects,fold's anditerToByteVec's drop of the iterator on a panic,rt.drop, and the adaptors' shared dispose.Public API:
rt/iter.ts,rt/collections.ts) take droppers after their own arguments;hashKeysandhashValuestake only the yielded half's. New:ownedCount,ownedLast,ownedNth,ownedNthBack,ownedSkip,ownedTake,ownedStepBy;HashIterandBTreeIterare exported, forrt/iter.ts's dispatch.iterCount,iterLast,iterNth,iterSkip,iterTake, anditerStepBytake no droppers. The runtime ships with each translation, so no consumer calls these by hand.Alternatives
[Symbol.dispose]on arrow functions and on every adaptor holding one, a separate design. Left in the quest.count,last,nth, andadvance_bymethods (this PR's first shape): the plainest faithful port, but a bundler can't drop a class's methods, so every bundle carried them: the codec's growth over main was 3321 / 867, now 1210 / 415.ownedtwin for every entry point that takes a dropper (ownedSlice,ownedFilter,ownedFold, the B-tree iterators, and so on) would bring the codec to about main's size, for about a dozen more exports. Left for the maintainer's call on that shape.collectinstead of modelling it: it is the commoninto_iter().map().collect(), so refusing would keep most of the census.Follow-ups
In quest/m2/ts-drop.md: closures that own values that drop (17 in moq-net, 9 of them leaked silently before this PR), and the refusals without a census instance. New in quest/m2/ts-iter-nth.md, from review:
main'snthandnth_backof what drops nothing read other items than std's underFuse,Chain, and others.(written by Opus 5.5)
🤖 Generated with Claude Code
https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn