Skip to content

feat(ts): drop values inside std's iterators and formatting where std's do - #307

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

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

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

quest/m3/ts-drop-order.md lists values that drop which rust2-ts refuses inside std's iterators and formatting. Reproducing them turned up two silent miscompiles beside the refusals:

  • DebugList::entries only checked whether the iterator drops. Items that drop from an iterator that doesn't, such as map(|n| Shown(guard(n))), were never dropped.
  • drops() called std's ranges never-dropping, so Arc::new(a)..Arc::new(b) leaked both Arcs. lang's fmt.ts snapshot shows the fix: rt.dropAll([held.start, held.end]).

Approach

Each now drops where std's does, built on each rt.Iter's fold and its release, not on rt.drop:

  • DebugList::entries: each item drops once written, then the iterator, as std's for does.
  • Ranges (Range, RangeInclusive, RangeFrom, RangeTo, RangeToInclusive) drop their bounds in field order. RangeInclusive::new of bounds that drop is no longer refused.
  • A range that drops given to a drain or a B-tree's range (also slice::copy_within and String's drain, replace_range, and extend_from_within): collections::bounds returns a Ranged.
    • Ranged::after wraps the call in rt.dropAfter(range, (range) => call, drop), so the range is read once and drops as the call returns. After std drops it (once it has read the bounds and searched, or at once for an empty B-tree), it does nothing JS can see before returning, so the two orders can't be told apart.
    • String's three still assert char boundaries after std drops the range. For them, Ranged::within checks slice::range (rt.boundsChecked) and drops the range before the call. A range whose drop panics proves the order: before the fix it gave 513 differences.
  • A hash map's into_keys/into_values whose other half drops: HashIter.next drops the other half before it returns, and a new HashIter.fold drops it after f has the item, as hashbrown's are.
  • The closure for_each and fold own: rt.iterFold/rt.iterForEach take owns and drop the closure as the fold ends, before the iterator (release). iter::releases tells called_closures the mapping drops it.
  • The printer parenthesizes an expression statement that starts with an object literal (({ start: g }.start[Symbol.dispose]())), which dropping a RangeFrom made.

Tests: a new task-fixture step, inside (ts/fixtures/task/src/drops/inside.rs, 10 scenarios), runs differentially against native wasm32. It covers:

  • next vs fold (into_keys().next() vs for_each/fold/last);
  • early exit (find, take(0), a take whose source holds leftovers);
  • empty inputs (empty map, empty Vec, empty drain, empty B-tree);
  • lent closures (for_each(&mut f));
  • an accumulator that drops;
  • panics, whose messages must match: mid-iteration in for_each, a drain past the end, and a range whose drop panics given to each of String's three. A panic aborts, so nothing drops after one.

Mutation checks: swapping the into_keys fold's order gave 516 differences, dropping the for_each closure after the fold gave 478, and dropping a drained range before the call gave 2411. Each fix now runs at 0 differences over 35,382 cases.

Split

This lands five of the quest's eight refusals plus the two miscompiles. The quest now holds the rest, each with an exact reproduction:

  • flatten of inner iterators that hold items once ended. std's try_fold keeps an ended inner iterator until the next one replaces it, so every adaptor whose try_fold is its source's (map, filter's next, rev via try_rfold) would need folded() to pass through it. That is a hot-path change across most adaptors.
  • zip of two TrustedRandomAccess iterators of droppers. It needs std's by-index side effects in next and next_back, and overlaps ts-zip-fold, which fix(ts): fold an inclusive range through std's adaptors as core does #301 just planned.
  • B-tree collect under the crate's own Ord. An Ord that disagrees with itself decides which duplicates drop by std's driftsort order.
  • Consumers other than for_each/fold (reduce, max_by_key, partition), kept closures, and FnOnce calls such as or_insert_with.

Each is still refused with its span.

Impact

  • Translator output: values listed above now translate and drop as std's do. Range<T> of a T that drops now drops its bounds, where it leaked before.
  • Runtime (additive, optional trailing parameters, not breaking): new rt.boundsChecked(r, n); rt.debugListEntries(..., drop?), rt.hashKeys(m, k?, v?), rt.hashValues(m, v?, k?), rt.iterFold(..., drops, owns?), rt.iterForEach(..., drops, owns?), rt.dropAfter(f, call: (f) => R, by?).
  • No MIGRATE.md entry: no caller-visible signature breaks.

Bench

just ts bench with the merge-base's translations as an extra base route. Medians in ns per item, translated / A/A / base:

workload Node Chromium Firefox
iter/skip-take-fold 4.71 / 4.54 / 4.67 5.04 / 4.98 / 4.46 7.14 / 7.30 / 7.05
iter/sum 13.0 / 13.2 / 17.3 12.3 / 12.1 / 13.2 23.9 / 24.0 / 32.9
iter/map-filter-count 16.2 / 16.8 / 16.8 13.9 / 17.7 / 15.2 27.9 / 27.0 / 28.2
btree/4096 346 / 312 / 326 306 / 350 / 338 641 / 646 / 631
handoff depth 16 983 / 901 / 1010 518 / 567 / 481 1060 / 1050 / 1060

Every difference is within A/A noise (the report's A/A spread: 6.4% at the median workload, 24% at most). Bun was noisy throughout.

Alternatives

  • Dropping a drained range inside the runtime right after it reads the bounds. That needs a new trailing parameter on every drain and range function. rt.dropAfter around the call is equivalent and touches none of them.
  • Fixing the printer's double parentheses around a Located expression changed ~400 unrelated snapshot lines, so I left it.

Follow-ups

Choices made without asking

  • Split as described above, rather than one PR for all eight.
  • Fixed the two silent miscompiles here, since they are the same drop-order problem.
  • Named the new task-fixture step inside. ordered was taken by an existing scenario.
  • Deleted the reject fixture's keys and spent_range, which now translate and are covered differentially. called_closure keeps only or_insert_with.
  • Left the printer's cosmetic double parentheses as they are.

Review

Local quest review: Codex, gpt-6.1-sol, medium effort. Latest round 4 on head cc00eec81f29bc2c727d6b3535e6d509fc27272a: no findings.

  • Round 1 (f6377d7c), medium: String's drain, replace_range, and extend_from_within dropped a range that drops after their char-boundary asserts, where std drops it before. Fixed in 26e8afc0 (Ranged::within, rt.boundsChecked), with a differential case: a range whose drop panics, given to each of the three. Before the fix it gave 513 differences; now it gives 0.
  • Round 2 (26e8afc0): no findings.
  • Round 3 (26f1b411, merging feat(ts): translate an array's map, each_ref, and each_mut #310 with two hand-resolved conflicts in library.rs): no findings.
  • Round 4 (cc00eec8, merging feat(ts): translate mul_add, rounding once as std's fma does #309, whose only conflicts were regenerated reject snapshots): no findings.

Checks: just ci passes locally on cc00eec8.

(written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 9 commits October 30, 2026 14:12
Quest-Claim: ff8c5614791fced1
…'s do

DebugList::entries drops each item once written and the iterator after, std's
ranges drop their bounds, a drain or a B-tree's range drops a range that drops
once the call returns, a hash map's into_keys and into_values drop the other
half before next returns and after fold's closure, and for_each and fold drop
the closure they own as their fold ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s closure

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/reject/census.md
#	ts/fixtures/reject/expected.txt
#	ts/src/lower/iter.rs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ries

std's String::drain, replace_range, and extend_from_within drop the range
once slice::range has checked it, before they assert char boundaries, so a
range whose drop panics panics first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/src/lower/library.rs
# Conflicts:
#	ts/fixtures/reject/census.md
#	ts/fixtures/reject/expected.txt
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Drop order in std's folds and iterators feat(ts): drop values inside std's iterators and formatting where std's do Oct 30, 2026
@kixelated
kixelated marked this pull request as ready for review October 30, 2026 22:58
@kixelated
kixelated merged commit fba78a4 into main Oct 30, 2026
2 checks passed
@kixelated
kixelated deleted the quest/m3/ts-drop-order branch October 30, 2026 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant