Skip to content

feat(ts): drop what std's iterators own - #101

Merged
kixelated merged 25 commits into
mainfrom
quest/m2/ts-drop
Oct 27, 2026
Merged

kixelated merged 25 commits into
mainfrom
quest/m2/ts-drop

Conversation

@kixelated

@kixelated kixelated commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

An rt.Iter dropped nothing it skipped, consumed, or still held, so rust2-ts refused every iterator that owns a value that drops: a for over a Vec or map, collect of cloned Paths, 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

  • Sources that own items take what drops each and drop the rest by [Symbol.dispose]: iterSlice (a Vec or array by value), iterOption, a crate's own iterator through iterFrom, and the hash, B-tree, and deque iterators, drains included. A Vec iterator stepped in place drops through rt.dropAll(s, f, i).
  • Adaptors drop what std's drop, in std's order: 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, and skip, take, and step_by's next follow std's per-type overrides (advance_by of a Vec moves past then drops, Skip::count is nth then count, and so on).
  • Those overrides are module-private dispatchers on the iterator's kind, not methods: the translation calls them through rt.ownedCount, ownedLast, ownedNth, ownedNthBack, ownedSkip, ownedTake, and ownedStepBy only where an item or the iterator drops, and the plain rt.iterCount and the rest otherwise, so a bundle that never drops carries none of them. Without an item that drops, every kind's own nth reads the same items, so the plain paths keep main's methods. [Symbol.dispose] stays a method only where rt.drop calls 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 (one Adaptor base for the single-iterator ones; Chain, Zip, Fuse, Flatten, and Peekable drop their own parts).
  • A consumer that takes an iterator by value is given rt.drop for it and drops it once done or on a panic: count, last, fold, for_each, sum, max, collect, extend.
  • collect into a Vec follows std's in-place path where std takes it (InPlaceIterable and SourceIter down to a vec::IntoIter, MERGE_BY and EXPAND_BY const-evaluated, wasm32 layouts compared as std's in_place_collectible does): a panic drops what it collected before the rest of the source, and the generic path the other way. Result and Option collects drop the values before an Err or None. A B-tree collect sorts, dedups keeping the last, and on a panicking drop drops and leaks as std's does.
  • An iterator borrowed by &mut is wrapped in iterBorrow, which forwards what std's impl Iterator for &mut I forwards, so a consumer of a borrow reads through it rather than by the by-value overrides, and never drops it.
  • std_iter in drop.rs classifies std's iterator adaptors: one drops if a type argument (with a Drop impl) or a field does.
  • A generic class's glue drops each field at its declared type. One generic over lifetimes alone (moq-net's 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, on main too; a census never showed them, since it drops a type with parameters by rt.drop. fixtures/task's Named, Text, and Entries cover them, Entries with a panicking first half.
  • Where std's order is one the runtime can't follow yet, translation refuses with a span: listed in the quest, none in the census.
  • Option::map on None, and Result's map, map_err, and and_then on 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's discarded_closure).
  • from_fn and successors (from feat(ts): translate bool::then, iter::successors, and From<bool> #113) still refuse a closure that owns a value that drops, which rt.iterFromFn never drops, and successors refuses items that drop, as rt.iterSuccessors leaves the one it holds next undropped (fixtures/reject's owning_lazy).

Proof: fixtures/task/src/drops/iters.rs adds six scenarios (loops and adaptors, collect, draining, extend, Drop panics 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 and advance_by order, filter rejects, zip unmatched, skip, Result collect, max_by_key losers, Peekable and Flatten dispose, hash, B-tree, and deque dispose, B-tree collect dedup and its panic path, extend dropping its source on a panic, the stepped Vec index, and in-place detection.

Impact

Census at the pinned moq revision, origin/main (53506aa) then this branch:

moq-net kio
refusals 11416 → 11310 344 → 342
an iterator that owns _, which drops 101 → 0 2 → 0
_ of _, which may drop what it holds 13 → 0 0 → 0
dropping a closure that owns a value that drops 8 → 17 0 → 0

Translating moq-net's model (roots model::origin_impl::Producer and Consumer, not a census), drop refusals on origin/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 bench against origin/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. The iter/ workloads are within 1% of main but iter/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:

codec handoff
origin/main (9fc2c22) 30242 / 9466 14341 / 3423
this PR's previous head, with the drop paths as methods (main at 53506aa: 30722 / 9819) 34043 / 10686 14350 / 3433
this PR 31452 / 9881 14350 / 3434

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_keys and into_values, and its dispose), iterSlice's dispose, filter's and find's rejects, fold's and iterToByteVec's drop of the iterator on a panic, rt.drop, and the adaptors' shared dispose.

Public API:

  • The translator's output for values that drop: iterators that own them translate instead of refusing.
  • The runtime's iterator and collection functions (rt/iter.ts, rt/collections.ts) take droppers after their own arguments; hashKeys and hashValues take only the yielded half's. New: ownedCount, ownedLast, ownedNth, ownedNthBack, ownedSkip, ownedTake, ownedStepBy; HashIter and BTreeIter are exported, for rt/iter.ts's dispatch. iterCount, iterLast, iterNth, iterSkip, iterTake, and iterStepBy take no droppers. The runtime ships with each translation, so no consumer calls these by hand.

Alternatives

  • Closures in this PR too: they need [Symbol.dispose] on arrow functions and on every adaptor holding one, a separate design. Left in the quest.
  • Per-type count, last, nth, and advance_by methods (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.
  • The remaining 415 gzip: an owned twin 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.
  • Refuse in-place collect instead of modelling it: it is the common into_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's nth and nth_back of what drops nothing read other items than std's under Fuse, Chain, and others.

(written by Opus 5.5)

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

kixelated and others added 5 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
@kixelated kixelated changed the title Drop in the TypeScript translator feat(ts): drop what std's iterators own Oct 27, 2026
@kixelated
kixelated marked this pull request as ready for review October 27, 2026 04:07
@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-27T21:36:08.152165Z d698cb7 New commits
🔒 Security Review ✅ Completed 2026-10-27T21:34:35.040190Z d698cb7 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: 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".

Comment thread ts/runtime/rt/collections.ts Outdated
Comment on lines +623 to +624
const n = this.left;
this[Symbol.dispose]();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

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 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 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: 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 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: 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

@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: 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".

Comment thread ts/runtime/rt/iter.ts Outdated
Comment on lines +71 to +72
nthBackItem(n: number, drop?: Drop<T>): T | Done {
return this.advanceBackBy(n, drop) === 0 ? this.nextBack() : DONE;

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 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 👍 / 👎.

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 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 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: 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)

Comment thread ts/runtime/rt/iter.ts Outdated

/** 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);

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.

[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.

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 114b9a4 and tested in 1672244: nth_back saturates its count at wasm32's usize::MAX, as std's saturating_mul and saturating_add do, and the task fixture checks (0i32..10).step_by(usize::MAX).nth_back(usize::MAX) against native Rust; without the saturation it fails.

(written by Opus 5.5)

@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: 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".

Comment thread ts/runtime/rt/iter.ts Outdated
Comment on lines +1146 to +1147
override advanceBy(n: number, drop?: Drop<T>): number {
return this.inner.advanceBackBy(n, 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 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 👍 / 👎.

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.

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)

Comment thread ts/runtime/rt/iter.ts Outdated
Comment on lines +66 to +67
nthItem(n: number, drop?: Drop<T>): T | Done {
return this.advanceBy(n, drop) === 0 ? this.next() : DONE;

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 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 👍 / 👎.

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, 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)

kixelated and others added 2 commits October 27, 2026 00:07
`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 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: 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)

@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: 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".

Comment thread ts/runtime/rt/collections.ts Outdated
Comment on lines +621 to +625
override countItems(drop?: (item: T) => void): number {
if (this.drain) return super.countItems(drop);
const n = this.left;
this[Symbol.dispose]();
return n;

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 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 👍 / 👎.

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, 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 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: 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)

@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: 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".

Comment thread ts/src/lower/iter.rs Outdated
Comment on lines 138 to 139
None if !owned && b.t.drops(ty.peel_refs()) => Expr::rt("iterBorrow", vec![value]),
None => value,

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 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 👍 / 👎.

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, 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 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: 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 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: 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)

kixelated and others added 3 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

@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: 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)

Comment thread ts/runtime/rt/iter.ts
if (item === DONE) this.inner = undefined;
return item;
return this.inner === undefined ? DONE : this.clear(this.inner.nextBack());
}

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.

[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.

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 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)

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 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: 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)

kixelated and others added 2 commits October 27, 2026 09:33
`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 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: 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)

kixelated and others added 2 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

@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: 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
kixelated added a commit that referenced this pull request Oct 27, 2026
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
@kixelated

Copy link
Copy Markdown
Collaborator Author

Merged origin/main (through #125) in d698cb7, resolving conflicts by hand:

  • ts/src/lower/iter.rs: feat(ts): translate bool::then, iter::successors, and From<bool> #113 refused owning closures in from_fn and successors through no_drops, which this PR deletes. Kept an equivalent refusal so no drop is lost: a closure that owns a value that drops is refused with the PR's closure message, and successors of items that drop with "an iterator that owns T, which drops", since rt.iterSuccessors leaves its pending item undropped. feat(ts): drop what closures own #115 lifts the closure half.
  • ts/runtime/rt/iter.ts: kept main's iterSuccessors and this PR's iterFrom with its dropper.
  • Fixtures: kept both sides; owning_lazy in fixtures/reject also covers successors of a Noisy. Snapshots regenerated.

Checks: just check, just ts check, just ts test pass locally; CI passed; Codex reviewed d698cb7 with no findings. The only open thread, the no-drop Fuse nth_back mismatch, exists on main too and is tracked in quest/m2/ts-iter-nth.md.

(written by Opus 5.5)

@kixelated
kixelated merged commit 74b98d0 into main Oct 27, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-drop branch October 27, 2026 21:43
kixelated added a commit that referenced this pull request Oct 27, 2026
#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
kixelated added a commit that referenced this pull request Oct 27, 2026
#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.
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