Repository navigation
feat(ts): drop values inside std's iterators and formatting where std's do - #307
Merged
Merged
Conversation
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
marked this pull request as ready for review
October 30, 2026 22:58
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
quest/m3/ts-drop-order.mdlists values that drop which rust2-ts refuses inside std's iterators and formatting. Reproducing them turned up two silent miscompiles beside the refusals:DebugList::entriesonly checked whether the iterator drops. Items that drop from an iterator that doesn't, such asmap(|n| Shown(guard(n))), were never dropped.drops()called std's ranges never-dropping, soArc::new(a)..Arc::new(b)leaked bothArcs.lang'sfmt.tssnapshot shows the fix:rt.dropAll([held.start, held.end]).Approach
Each now drops where std's does, built on each
rt.Iter'sfoldand itsrelease, not onrt.drop:DebugList::entries: each item drops once written, then the iterator, as std'sfordoes.Range,RangeInclusive,RangeFrom,RangeTo,RangeToInclusive) drop their bounds in field order.RangeInclusive::newof bounds that drop is no longer refused.range(alsoslice::copy_withinandString'sdrain,replace_range, andextend_from_within):collections::boundsreturns aRanged.Ranged::afterwraps the call inrt.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::withinchecksslice::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.into_keys/into_valueswhose other half drops:HashIter.nextdrops the other half before it returns, and a newHashIter.folddrops it afterfhas the item, as hashbrown's are.for_eachandfoldown:rt.iterFold/rt.iterForEachtakeownsand drop the closure as the fold ends, before the iterator (release).iter::releasestellscalled_closuresthe mapping drops it.({ start: g }.start[Symbol.dispose]())), which dropping aRangeFrommade.Tests: a new task-fixture step,
inside(ts/fixtures/task/src/drops/inside.rs, 10 scenarios), runs differentially against native wasm32. It covers:nextvsfold(into_keys().next()vsfor_each/fold/last);find,take(0), atakewhose source holds leftovers);Vec, empty drain, empty B-tree);for_each(&mut f));for_each, a drain past the end, and a range whose drop panics given to each ofString's three. A panic aborts, so nothing drops after one.Mutation checks: swapping the
into_keysfold's order gave 516 differences, dropping thefor_eachclosure 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:
flattenof inner iterators that hold items once ended. std'stry_foldkeeps an ended inner iterator until the next one replaces it, so every adaptor whosetry_foldis its source's (map,filter'snext,revviatry_rfold) would needfolded()to pass through it. That is a hot-path change across most adaptors.zipof twoTrustedRandomAccessiterators of droppers. It needs std's by-index side effects innextandnext_back, and overlapsts-zip-fold, which fix(ts): fold an inclusive range through std's adaptors as core does #301 just planned.collectunder the crate's ownOrd. AnOrdthat disagrees with itself decides which duplicates drop by std's driftsort order.for_each/fold(reduce,max_by_key,partition), kept closures, andFnOncecalls such asor_insert_with.Each is still refused with its span.
Impact
Range<T>of aTthat drops now drops its bounds, where it leaked before.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?).MIGRATE.mdentry: no caller-visible signature breaks.Bench
just ts benchwith the merge-base's translations as an extrabaseroute. Medians in ns per item, translated / A/A / base: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
rt.dropAfteraround the call is equivalent and touches none of them.Locatedexpression changed ~400 unrelated snapshot lines, so I left it.Follow-ups
quest/m3/ts-drop-order.md.Choices made without asking
inside.orderedwas taken by an existing scenario.keysandspent_range, which now translate and are covered differentially.called_closurekeeps onlyor_insert_with.Review
Local
quest review: Codex, gpt-6.1-sol, medium effort. Latest round 4 on headcc00eec81f29bc2c727d6b3535e6d509fc27272a: no findings.f6377d7c), medium:String'sdrain,replace_range, andextend_from_withindropped a range that drops after their char-boundary asserts, where std drops it before. Fixed in26e8afc0(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.26e8afc0): no findings.26f1b411, merging feat(ts): translate an array's map, each_ref, and each_mut #310 with two hand-resolved conflicts inlibrary.rs): no findings.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 cipasses locally oncc00eec8.(written by Claude Opus 5.5)
🤖 Generated with Claude Code