Commit Graph
637 Commits
Author SHA1 Message Date
Sandeep Vijayarao 1f4913eb96 [BitCollections] Don't trap on BitSet[members:] closed ranges ending at Int.max
`BitSet[members:]` resolved range expressions against
`Int.min ..< Int.max`. A closed range ending at `Int.max`
(`bits[members: ...Int.max]`, `bits[members: 0 ... Int.max]`) needs the
index after `Int.max`, so it trapped with "Index out of bounds", while
`..<Int.max` and `0...` worked.

Resolve against a small internal collection of all `Int` values whose
`index(after:)` saturates at `Int.max`. `Int.max` can never be a bit set
member, so `lower ... Int.max` resolving to `lower ..< Int.max` doesn't
change which members are selected. `BitSet.Counted[members:]` forwards
to `BitSet`, so it's fixed too.
2026-11-11 00:19:39 -08:00
Karoy Lorentey 88754449ab Merge pull request #845 from aashish254/codex/812-container-iterator-boundary
Fix ContainerIterator skip bounds
2026-11-10 11:16:20 -08:00
Karoy Lorentey 827fef0660 Merge pull request #817 from Joseph-Cursio/drainable-container-check
Add checkDrainableContainer and fix RigidArray's consumeLast recursion
2026-11-10 11:01:51 -08:00
Karoy Lorentey 612865a2d0 Merge pull request #823 from Joseph-Cursio/fix-816-permutable-noncopyable
Add checkPermutableContainer
2026-11-10 11:01:28 -08:00
Aashish fc11462e69 Fix ContainerIterator skip bounds 2026-11-09 21:42:36 +05:45
Karoy Lorentey 9e74a3f71f Merge pull request #839 from sandeepvijayarao09/sorted-collections-fuzz-tests
[SortedCollections] Add differential fuzz tests for SortedDictionary and SortedSet
2026-11-06 14:33:31 -08:00
Karoy Lorentey de714531cf Merge pull request #837 from sandeepvijayarao09/hashed-containers-fuzz-tests-v2
[BasicContainers] Add differential fuzz tests for the hashed containers
2026-11-06 14:33:11 -08:00
Sandeep Vijayarao ebc8757bd3 Guard the memoizedValue fuzz steps with #available
`memoizedValue(forKey:_:)` and `Ref.value` need SwiftStdlib 6.4, so the
fuzz tests failed to compile on macOS with the UnstableHashedContainers
trait (CI only enables that trait on Linux). Skip just that operation
on older platforms instead of marking the whole tests unavailable.
2026-11-06 03:35:03 -08:00
Swastik TripathiandClaude Opus 5.5 d3f3684195 [BasicContainers] Mark the new memoizedValue tests as SwiftStdlib 6.4
test_memoizedValue_returnsNewlyInsertedValue in RigidDictionaryTests and
UniqueDictionaryTests calls memoizedValue(forKey:_:), which needs
SwiftStdlib 6.4, but the tests had no availability attribute. With the
UnstableHashedContainers trait on macOS, BasicContainersTests failed to
build ("'memoizedValue(forKey:_:)' is only available in macOS 27.0 or
newer"). Add the same @available attribute test_memoizedValue_one uses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 13:40:07 +05:30
Sandeep Vijayarao 1aa0323dd7 [SortedCollections] Add differential fuzz tests for SortedDictionary and SortedSet
Run random sequences of operations on SortedDictionary and SortedSet and
compare them against a standard Dictionary/Set model after each step:
count, contents in sorted order (forward and backward), offset-based
indexing, lookups for every key in the domain, and value semantics of
earlier copies.

SortedDictionary operations include subscript assignment, the
`_modify` accessor (`take()`, optional chaining, inout), updateValue,
removeValue, modifyValue(forKey:default:), the default subscript,
remove(at:), removeFirst/removeLast(_:), popFirst/popLast, filter,
mapValues, and rebuilding from a sequence.

SortedSet operations include insert/update/remove with members that are
equal but distinguishable (checking that insert keeps and update
replaces the stored member), remove(at:), removeFirst/removeLast(_:),
the form* set algebra operations, and the element range subscripts.

In debug builds, nodes hold at most 4-5 keys, so even small key domains
produce multi-level trees. Building with COLLECTIONS_INTERNAL_CHECKS
also validates the B-tree after every mutation; with that, these tests
catch the bugs fixed in 93745231 (remove(at:) not rebalancing) and
3a80514a (empty root after removal through `_modify`).
2026-11-05 19:05:49 -08:00
Sandeep Vijayarao b2f5535d27 [BasicContainers] Add differential fuzz tests for the hashed containers
Run random sequences of operations on RigidSet, UniqueSet,
RigidDictionary and UniqueDictionary, and after each step compare them
against a standard Set/Dictionary model: count, contents (via index
iteration), positive and negative lookups, and `_checkInvariants()`.

The operations cover insert/update/remove, the closure form of
`updateValue(forKey:with:)`, `memoizedValue(forKey:_:)`, `reallocate`/
`reserveCapacity`, `removeAll` (with and without keeping capacity),
Rigid <-> Unique conversions via `init(consuming:)`, and `_consumeAll`.
Keys use a configurable number of distinct hash values, from no
collisions down to a single collision chain, and all elements are
lifetime-tracked to catch leaks and double destroys.

These tests found the bugs fixed in #835 and #836.
2026-11-05 18:51:15 -08:00
Karoy Lorentey 3f5e6e427b Merge branch 'main' into fix-unique-dictionary-closure-removal 2026-11-05 18:32:45 -08:00
Sandeep Vijayarao 1735b98380 [BasicContainers] Fix double destroy in UniqueDictionary.updateValue(forKey:with:)
When the updater closure removes an existing entry (sets the value to
nil), `UniqueDictionary.updateValue(forKey:with:)` called
`_removeValue(at:)`. But the value had already been moved out of its
slot before calling the updater, and `_removeValue(at:)` moves it out
again and discards the result, destroying uninitialized memory. This
leads to crashes or memory corruption.

`RigidDictionary.updateValue(forKey:with:)` already handles this case
correctly, by deinitializing just the key and resolving the hole.

Add `UniqueDictionary._removeEntry(withMovedValueAt:)`, which does the
same and then shrinks storage under the same condition as
`_removeValue(at:)`, and use it for the removal path.
2026-11-05 18:17:48 -08:00
Sandeep VijayaraoandClaude Opus 5.5 c8cc9a7d4c [BasicContainers] Fix memoizedValue returning another key's value
`RigidDictionary._insertNew` (and `RigidSet._insertNew`) returned the
bucket reported by the hash table's `insertNew_Small`/`insertNew_Large`.
That is where the *last displaced* item ends up, not the new item: small
tables swap the new item into the middle and move the old middle item to
the end, and hashed tables do Robin Hood swaps along the probe sequence.
Whenever a swap happened, the returned bucket held some other entry.

`memoizedValue(forKey:_:)` on `RigidDictionary` and `UniqueDictionary`
returns a reference to the value at that bucket, so inserting a new key
often returned the value of a different key.

Track the first bucket the swapper writes the new item into, and return
that (or the final bucket if nothing was swapped). The hash table's own
return value is still used to initialize the displaced item, and its
other user (rehashing in `migrateItems_Large`) is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 17:51:08 -08:00
Joseph CursioandClaude Opus 5.5 476fdaff1b Check OutputSpan with checkPermutableContainer
Also run the permutable container checker against an OutputSpan, over the
same layouts as RigidArray.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 16:48:47 -06:00
Joseph CursioandClaude Opus 5.5 71d10cd2f4 Check InputSpan with checkPermutableContainer
Also run the permutable container checker against an InputSpan, over the
same layouts as RigidArray. InputSpan.swapAt(_:_:) is the only span
implementation of swapAt in this package, and a no-op version of it
passes every other test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 16:48:47 -06:00
Joseph CursioandClaude Opus 5.5 7460d35e2b Check UniqueDeque with checkPermutableContainer
Run the permutable container checker against UniqueDeque over the same
storage layouts as its mutable container test, including wrapped ones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 16:48:47 -06:00
Joseph CursioandClaude Opus 5.5 68be4ea868 Check RigidDeque with checkPermutableContainer
Run the permutable container checker against RigidDeque over the same
storage layouts as its mutable container test, including wrapped ones,
where swapped pairs can span both ends of the storage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 16:48:47 -06:00
Joseph CursioandClaude Opus 5.5 8275b719e3 Check UniqueArray with checkPermutableContainer
Run the permutable container checker against UniqueArray, over arrays of
up to ten items, full and with spare capacity.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 16:48:47 -06:00
Joseph CursioandClaude Opus 5.5 a9967e32eb Check MutableSpan with checkPermutableContainer
Also run the permutable container checker against the mutable span of a
RigidArray, over the same layouts. MutableSpan is nonescapable, so this
also runs the checker on a nonescapable container.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 16:48:47 -06:00
Joseph CursioandClaude Opus 5.5 69a2d15395 Check RigidArray with checkPermutableContainer
Run the permutable container checker against RigidArray, over arrays of
up to ten items, full and with spare capacity.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 16:48:47 -06:00
Joseph CursioandClaude Opus 5.5 afa8580b15 Add checkPermutableContainer
Check swapAt(_:_:), the one requirement PermutableContainer adds to
Container, against an Array model. The checker swaps every pair of
indices, including an index with itself, and then repeats the same swaps
in reverse order, comparing the whole container after each swap. That
leaves the container holding the expected contents in their original
order. It first runs validateContainer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 16:48:47 -06:00
Karoy Lorentey f7952f9e34 Merge pull request #830 from unaltolga/fix-unique-deque-iterable
[DequeModule] Make UniqueDeque Iterable without ContainersPreview
2026-11-05 14:13:21 -08:00
Karoy Lorentey eaab6cbf06 Merge pull request #831 from sandeepvijayarao09/fix-test-support-810
Fix six small bugs in _CollectionsTestSupport
2026-11-05 14:13:01 -08:00
Karoy Lorentey 54086ee4f2 Merge pull request #829 from sandeepvijayarao09/fix-sorted-dictionary-modify-root
[SortedCollections] Collapse an empty root after removing a root key through _modify
2026-11-05 13:57:07 -08:00
Sandeep Vijayarao 2c246541db Fix six small bugs in _CollectionsTestSupport
- withEveryDeque/withEveryFullDeque now visit capacity 0.
- checkEquatable reports transitivity violations instead of trapping
  when pairs arrive out of order (non-transitive oracles, maxSamples).
- Minimal range-replaceable collections no longer trap in removeLast()
  and popLast().
- LifetimeTracked's "Double deinit" precondition can now fire.
- withEvery, withEveryRange and withSomeArrayLayouts include the
  failing value in their "Throwing trace".
- CustomCountedProducer drops its unused chunkSize parameter, and the
  two loops that reran identical cases over it are removed.

Adds regression tests for the first three.

Fixes #810
2026-11-05 13:52:56 -08:00
Karoy Lorentey 63132036cf Adjust tests
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
2026-11-05 13:46:03 -08:00
Karoy Lorentey 13045ef84c Merge pull request #825 from sainadth/fix-heap-check-invariants-access
[Heap] Make _checkInvariants() public in internal checks builds
2026-11-05 13:40:49 -08:00
Karoy Lorentey 24b919a5ba Merge pull request #828 from lorentey/fix-permutable-container-algorithms
Fix some container algorithms
2026-11-05 13:40:23 -08:00
Tolga Unal 68fd4f2cc8 [DequeModule] Make UniqueDeque Iterable without ContainersPreview
842f125c removed the trait conditions around the Iterable APIs, but
UniqueDeque only got its conformance through Container in ContainersPreview.
RigidDeque had no underestimatedCount, so it was 0 through Iterable.
2026-11-05 09:09:48 -05:00
Sandeep Vijayarao 3a80514ab6 [SortedCollections] Collapse an empty root after removing a root key through _modify
UnsafeCursor.removeElement(hasValueHole:) only collapsed an empty root
inside the loop that walks up the cursor's path, which never runs when
the removed key is in the root. Removing such a key with d[key].take()
could leave the root as an internal node with no keys and one child.
Collapse the root after the loop so every path depth is covered.

Fixes #811
2026-11-04 19:17:52 -08:00
Karoy Lorentey 7b578e5bfb Review and fix filter implementations
- Define a separate `filter` result on Iterable vs BorrowingIteratorProtocol
- Fix handling of delimited iterators in Container.filter
- Add fixmes for throwing
- Inlinability and other small fixes
2026-11-04 18:28:38 -08:00
Karoy Lorentey 401b60a2b6 Refactor assertions about iterable contents
- Add iterable-based assertions
- Have a single actual implementation; let everything else forward to it
- Delete obsolete assertion variants
- Stop requiring UnstableContainersPreview for Iterable assertions
2026-11-04 18:28:34 -08:00
Karoy Lorentey f4966f7685 Move ArrayLayout to _CollectionsTestSupport 2026-11-04 18:28:30 -08:00
Tolga Unal 9927f05e42 [BasicContainers] Make UniqueDictionary Equatable and Hashable with noncopyable contents
73ec3d341 made the other containers conform to Equatable/Hashable with
noncopyable contents, but UniqueDictionary was missed. The Hashable
extensions of RigidDictionary were also missing `Key: ~Copyable`.
2026-11-04 19:29:14 -05:00
sainadth c9286ec384 [Heap] Make _checkInvariants() public in internal checks builds
`Heap._checkInvariants()` was `internal` when COLLECTIONS_INTERNAL_CHECKS
is defined but `public` otherwise, so code outside `HeapModule` compiled
normally and then failed in an internal checks build.

Make both declarations `public`, matching `OrderedSet`,
`OrderedDictionary`, `BitSet` and `BitArray`, and add a test that calls it
through a plain `import HeapModule`.

Fixes #813
2026-11-04 13:10:43 -06:00
Joseph CursioandClaude Opus 5.5 ffd6fe4a3f Check UniqueDeque with checkDrainableContainer
Run the drainable container checker against UniqueDeque over every
storage layout, including wrapped ones. UniqueDeque's own remove(at:),
removeSubrange(_:) and consumeSubrange(_:consumingWith:) don't match
the protocol's requirements, since the first takes its index by value
and the others return nothing, so the checker's generic calls reach the
protocol's default implementations of these, over a two-part storage.
It passes with #814's fix reverted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 19:16:49 -06:00
Joseph CursioandClaude Opus 5.5 b0a4a44eeb Check RigidDeque with checkDrainableContainer
Run the drainable container checker against RigidDeque over every
storage layout, including wrapped ones, where a removed subrange can
span both ends of the storage and its drain hands it out in two parts.
RigidDeque doesn't override the _customConsumeLast(_:) hook, so it
passes with #814's fix reverted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 19:16:44 -06:00
Joseph CursioandClaude Opus 5.5 fe90f11c44 Check UniqueArray with checkDrainableContainer
Run the drainable container checker against UniqueArray, over arrays of
up to six items, full and with spare capacity. UniqueArray doesn't
override the _customConsumeLast(_:) hook, so it passes with #814's fix
reverted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 19:16:39 -06:00
Joseph CursioandClaude Opus 5.5 204d1ae40c Check RigidArray with checkDrainableContainer
Run the drainable container checker against RigidArray, over arrays of
up to six items, full and with spare capacity. With #814's fix
reverted, the test crashes on its first consumeLast(_:) call, with
DrainableContainer.consumeLast(_:) and RigidArray._customConsumeLast(_:)
calling each other until the stack overflows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 19:16:33 -06:00
Joseph CursioandClaude Opus 5.5 d60ceae062 Add checkDrainableContainer
Check the DrainableContainer requirements and extensions against
Array's removeSubrange(_:), for every subrange: that a subrange drain
hands out the removed items in order, in spans no longer than maxCount,
then only empty spans, with an accurate count; that it removes the
whole subrange whether it is finalized or dropped partway, and
finalize() returns the index following the removed items; that
consumeSubrange(_:consumingWith:) passes the removed items to its
consumer and discards those it leaves; the items and indices returned
by remove(at:), removeFirst() and the index-returning removals,
including the RangeExpression2 and UnboundedRange overloads; and that
no item is leaked or destroyed but the removed items not handed out.
For a BidirectionalContainer, consumeLast(_:), removeLast(),
removeLast(_:) and popLast() are checked too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 19:16:00 -06:00
Karoy Lorentey b14ab924a5 Fix PermutableContainer algorithms 2026-11-03 16:04:44 -08:00
Karoy Lorentey 1233c97689 Merge pull request #790 from Joseph-Cursio/fix-764-span-navigation
Add checkBidirectionalContainer and checkMutableContainer
2026-11-03 14:59:13 -08:00
Karoy Lorentey 9d026ae383 Merge pull request #808 from HuzaifaChaudary/fix/bitarray-wide-signed-conversion
[BitCollections] sign extend BitArray conversions to integers wider than a word
2026-11-03 14:22:18 -08:00
Swastik TripathiandClaude Opus 5.5 93745231f1 [SortedCollections] Balance the child after remove(at:)
remove(at:) recursed into the child holding the offset and returned
without balancing it, so repeated removals could empty a leaf. Iterating
then read a stale slot, which returned already-removed elements and read
released references in release builds, and trapped in debug builds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 09:43:22 +05:30
HuzaifaChaudary ed61a95cc8 [BitCollections] sign extend BitArray conversions to integers wider than a word
converting a negative bit array to `Int128` lost the sign above the first
64 bits , so `Int128(truncatingIfNeeded: "1" as BitArray)` gave
18446744073709551615 , not -1 , `Int128(exactly:)` gave nil , and
`Int128(_:)` trapped . a negative value now starts out as all ones , so the
words shifted in keep the sign .

the trap message also named `BitArray` as the type it could not convert
to , it names the integer type now .
2026-10-30 20:22:20 +05:00
Joseph CursioandClaude Opus 5.5 bdddf6dba8 Check UniqueDeque with checkMutableContainer
Run the mutable container checker, which also runs the bidirectional
one, against UniqueDeque over every storage layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 04:51:50 -05:00
Joseph CursioandClaude Opus 5.5 6ed61375a6 Check RigidDeque with checkMutableContainer
Run the mutable container checker, which also runs the bidirectional
one, against RigidDeque over every storage layout. With #793's
spanBoundary fix reverted, it fails with a negated distance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 04:51:50 -05:00
Joseph CursioandClaude Opus 5.5 d1f377cb2d Check UniqueArray with checkMutableContainer
Run the mutable container checker, which also runs the bidirectional
one, against UniqueArray.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 04:51:50 -05:00
Joseph CursioandClaude Opus 5.5 bfc228d7b7 Check RigidArray with checkMutableContainer
Run the mutable container checker, which also runs the bidirectional
one, against RigidArray. With #793's nextMutableSpan fix reverted, it
fails with the index not advancing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 04:51:50 -05:00