Skip to content

feat(ts): fold std's adaptors as std's drop them - #172

Merged
kixelated merged 6 commits into
mainfrom
quest/m3/ts-drop-order
Oct 28, 2026
Merged

kixelated merged 6 commits into
mainfrom
quest/m3/ts-drop-order

Conversation

@kixelated

@kixelated kixelated commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Claims quest/m3/ts-drop-order.md.

Problem

std's by-value consumers (fold, for_each, sum, count, last, max_by_key, TrustedLen collect and extend, ...) go through each adaptor's own fold, which drops that adaptor's closure when its fold ends, before what it wraps. The runtime walked every iterator through next and dropped the whole chain afterwards, in struct order. So the translator refused a fold over a closure that owns something that drops (owning_closure) and a by-value flatten fold (flattened). It also needed iterFold and the other consumers to take an rt.drop for the whole chain.

Approach

  • rt.Iter gains fold and rfold, defaulting to a next loop that disposes the iterator afterwards. Each adaptor std overrides overrides it too, dropping what it holds where std's does, panic paths included: Map, Filter, FilterMap, Inspect, Enumerate, Chain, Rev, Skip, Take (rfold), SkipWhile, StepBy, Fuse, Peekable, Flatten. The pieces std builds on try_fold (Take, TakeWhile, MapWhile, in-place collect) keep struct order, which matches std's.
  • By-value consumers are told whether anything drops (drops) and whether std reads by next or fold (rt.Take). The translator picks "fold" where std's impl goes through for_each/fold (TrustedLen Vec/VecDeque collect and extend, HashMap/BTreeMap extend) and "next" otherwise.
  • count and last of an iterator that drops now consume it through the same folds (rt.ownedCount, rt.ownedLast).
  • Where nothing drops, every consumer keeps its plain next loop. The hot paths are untouched, so no benchmark was added or run.

Dropped: the translator's folds()/held() refusals and iter_dropper, the runtime's flattenEach/flattenLast, and two reject fixture cases that now translate.

Impact

  • Differential tests: new folded (256 cases: 32 adaptor chains, each panicking at every point) and rfolded (72 cases over the reverse folds, step_by, fuse, flatten) task steps, compared against native Rust drop logs. Locally: 35875 cases, 15946 panics, 0 differences; 5602 scripts, 0 differences.
  • Mutation-checked: disabling or reordering each override, one at a time, makes the task test fail.
  • Public API: the runtime consumers' signatures changed (entry in ts/MIGRATE.md). iterFold, iterForEach, iterSum, iterProduct, iterMax/Min(ByKey), and durationSum take a drops boolean instead of an rt.drop. ownedCount/ownedLast take only the item dropper. The collect/extend consumers take an rt.Take. iterToArray takes "place" instead of inPlace. Translated output changes only where something drops.

Alternatives

  • Keep next and reorder drops after the fact. This can't put a closure's drop before the items its fold still has to drop on a panic.
  • A generic fold in the translator over std's MIR. That would mean translating std's iterator internals, which is a much larger change.

Follow-ups

Still in the quest: the closures any/or_insert_with/Pattern call but never drop, zip of TrustedRandomAccess, a flatten whose inner iterator holds items once ended, B-tree collect of an own Ord, and HashMap::into_keys/into_values folds (now reachable through a HashIter.fold).

(Written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 5 commits October 28, 2026 05:31
Quest-Claim: b6a8b6541f57103e
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/MIGRATE.md
#	ts/fixtures/reject/census.md
#	ts/fixtures/reject/expected.txt
#	ts/fixtures/task/src/harness.rs
#	ts/tests/task.ts
@kixelated kixelated changed the title Drop order in std's folds and iterators feat(ts): fold std's adaptors as std's drop them Oct 28, 2026
@kixelated
kixelated marked this pull request as ready for review October 28, 2026 13:26
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T14:39:47.763699Z 267e82a Manual request
🔒 Security Review ✅ Completed 2026-10-28T14:40:08.493184Z 267e82a Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Automated review by review (OpenAI)

Reviewed commit: 6fb5461

No actionable bugs found in the reviewed changes. Adaptor-specific fold/rfold is the right direction: it models closure/accumulator/source ownership at the point of consumption rather than trying to reorder final disposal. Keeping the no-drop loops and centralizing cleanup helpers limits the added complexity; the forward/reverse differential fixtures cover meaningful panic and composition cases.

Checked the runtime consumers and adaptor overrides, collection lowering, retained refusals, migration notes, and test scenarios, including selected implementations from the pinned Rust 1.98.1 source. Verification was static via GitHub; I did not run builds, differential tests, mutation checks, or benchmarks.

# Conflicts:
#	ts/fixtures/reject/census.md
#	ts/fixtures/reject/expected.txt
#	ts/fixtures/task/expected/core/option.ts
#	ts/fixtures/task/src/harness.rs
#	ts/fixtures/task/ts.toml
#	ts/runtime/rt/collections.ts
#	ts/tests/task.ts
@kixelated

Copy link
Copy Markdown
Collaborator Author

@codex review

(Written by Claude Opus 5.5)

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 267e82ab2b

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

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: 267e82ab2b

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@kixelated

Copy link
Copy Markdown
Collaborator Author

Landing summary: each rt.Iter adaptor std overrides now has its own fold/rfold, which drops closures and wrapped iterators where std's do, panic paths included. By-value consumers take drops and rt.Take (see ts/MIGRATE.md), and paths where nothing drops are unchanged. The folded/rfolded task steps show 0 differences against native Rust, and every override is mutation-checked. main is merged through #177. CI passed on 267e82a, and Codex found no major issues there. The maintainer authorized a squash merge. quest check against main passes, and the quest file keeps the bullets this PR leaves.

(Written by Claude Opus 5.5)

@kixelated
kixelated merged commit c194c29 into main Oct 28, 2026
1 check passed
@kixelated
kixelated deleted the quest/m3/ts-drop-order branch October 28, 2026 14:52
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