1267 Commits
Author SHA1 Message Date
Karoy Lorentey 1b936eda0b Disable strict memory safety on older toolchains
It would take too much effort to try to mitigate stdlib differences.

Fix low-hanging warnings, but turn off `.strictMemorySafety` on older toolchains by splitting the package manifest.
2026-10-01 13:40:27 -07:00
Karoy Lorentey 9e7949017a Tests: add unsafe keywords where needed 2026-10-01 11:44:47 -07:00
Karoy Lorentey d07dc32dfa Sources: Add unsafe keywords where needed 2026-10-01 11:14:29 -07:00
Karoy Lorentey a5b094974f Merge pull request #745 from lorentey/migrate-from-spi-to-package
Stop using `@_spi` attribute and `@testable import`
2026-09-29 18:51:34 -07:00
Karoy Lorentey c991c8543a Update SortedCollections for un-@testable testing 2026-09-29 18:32:58 -07:00
Karoy Lorentey 0ec72a9532 Stop using @_spi attribute and @testable import 2026-09-29 18:32:55 -07:00
Karoy Lorentey c410b3cd3a Remove obsolete/disabled APIs 2026-09-29 15:41:06 -07:00
Karoy Lorentey 44b674265c Update import statements 2026-09-29 15:41:05 -07:00
Karoy Lorentey 1be474f795 Xcode, CMake: Spin off ContainersPreview into its own target 2026-09-29 15:41:03 -07:00
Karoy Lorentey 2ba0f6e59f Merge pull request #739 from lorentey/drop-unused-code
Avoid using `Ref` in Swift 6.4
2026-09-25 15:18:19 -07:00
Karoy Lorentey f1e675acd2 Turn off BorrowingIteratorProtocol.next()
The reference to `Ref` appears to trigger a broken strong linkage to `_swift_initBorrow`, despite the code being only available on macOS/iOS/etc 27.
2026-09-25 15:01:04 -07:00
Karoy Lorentey feef32596a Merge pull request #736 from Denimworld12/fix-bitset-counted-update-count
[BitCollections] Fix BitSet.Counted.update(with:) not maintaining the cached count
2026-09-25 14:07:45 -07:00
Karoy Lorentey 5448b9cdb1 Delete distracting comments
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
2026-09-25 13:47:08 -07:00
Nikhil Gupta ab29aa793d [HashTreeCollections] Fix union misplacing items pushed into a new child
`copyNodeAndPushItemIntoNewChild` takes an existing item out of `self` and
pushes it into `newChild`, which is then installed as a child of `self` at
`bucket`. `newChild` therefore sits one level below `self`, but the item was
inserted into it using `self`'s level:

    let r = newChild.inserting(level, item, hash)

Navigating with the parent's level picks the wrong hash digit, so the item —
and the subtree it is merged with — end up filed under a path that doesn't
match their hash.

`TreeSet.union` reaches this when it merges an incoming collision node against
an item already occupying that bucket. The result is a set that still reports
the right `count` and still yields every element when iterated, but whose
misplaced members cannot be found by `contains`, since lookup navigates by
hash:

    let a1 = RawCollider(1, "A")
    let a2 = RawCollider(2, "B")
    let c1 = RawCollider(5, "ACAD")
    let c2 = RawCollider(6, "ACAD")

    let u = TreeSet([a1, a2]).union(TreeSet([c1, c2]))
    u.count          // 4
    Array(u)         // [2#B, 1#A, 5#ACAD, 6#ACAD]
    u.contains(c1)   // false
    u.contains(a1)   // false

Building the same set directly with `TreeSet([a1, a2, c1, c2])` is unaffected;
only the structural merge paths are.

Insert at `level.descend()` instead.

Enumerating every pair of subsets of the shared `testItems` fixture, 171 of
4096 unions contained at least one member that `contains` could not find.
With this change all 4096 are clean.

Also fix the full invariant check, which could not run on any tree containing
a collision node. Collision nodes store their item count in `itemMap` and
`childMap` rather than a set of occupied buckets, so treating those bitmaps as
buckets walks items that aren't there — a node holding 5 items has
`itemMap._value == 5`, whose bits nominate buckets 0 and 2. The bucket loop now
only runs for regular nodes. Before this, `swift test
-Xswiftc -DCOLLECTIONS_INTERNAL_CHECKS` trapped immediately in
HashTreeCollections; that trap was also what kept the invariant check from
reporting the misplaced nodes above.

Add a regression test alongside the existing `test_union_collision_node_ordering`.
2026-09-25 14:11:23 +05:30
Nikhil Gupta 84e65e8b5a [BitCollections] Fix BitSet.Counted.update(with:) not maintaining the cached count
BitSet.Counted caches its size in `_count` so that `count` is O(1).
Every mutating operation is responsible for keeping that cache in sync
with the underlying bit set.

`update(with:)` forwarded straight to `BitSet.update(with:)` without
adjusting `_count`. That method inserts the member when it isn't already
present (it is defined as `insert(newMember).inserted ? nil : newMember`),
so inserting a new member this way left the cached count one too low.

The stale count is observable in a few ways:

- `count` under-reports the number of members.
- `isEmpty` is `_count == 0`, so a set that still contains members can
  report itself as empty.
- The `Collection` conformance promises `count` elements but iteration
  yields more, which breaks the standard library. `Array(set)` traps with
  "Fatal error: invalid Collection: more than 'count' elements in
  collection".

In builds with COLLECTIONS_INTERNAL_CHECKS this also violates
`_checkInvariants()`, which asserts `_count == _bits.count`.

Mirror what `insert(_:)` already does and bump `_count` when a new member
was inserted. `update(with:)` returns nil exactly in that case.

`BitSet` has had a `test_update` for this all along; `BitSet.Counted` did
not, which is how this went unnoticed. Add the matching test, following
the same combinatorial style and using `Set<Int>` as the oracle.
2026-09-25 13:02:10 +05:30
Karoy Lorentey 1a1818df8f Drop unused Optional._borrow, ._mutate and _insert definitions 2026-09-23 15:45:26 -07:00
Karoy Lorentey e2c040bcaa BorrowingIteratorProtocol.map: Hide map2 and map3 behind underscores
`map2` and `map3` are merely illustrating problems with the throwing Iterable in higher-order functions in Swift 6.4; they aren’t really suitable for exposing as API, even behind a package trait.
2026-09-22 19:04:12 -07:00
Karoy Lorentey 2dcded505e Stop shipping retroactive Iterable conformances on standard types 2026-09-22 19:04:09 -07:00
Karoy Lorentey 9bf8a9763b Merge pull request #729 from lorentey/release-prep
1.7.0 release preparations
2026-09-22 12:42:05 -07:00
Karoy Lorentey 537bf9cbfb Drop support for Swift 6.0 and 6.1
This allows the use of Span types without `#if compiler` guards.
2026-09-21 13:34:39 -07:00
Karoy Lorentey 8e4f078441 Producer.generate(into:): Switch to saturating semantics
This noticeable simplifies client code, as clients invariably want to fully populate their buffer (when possible). It also is consistent with the semantics of `Iterable.skip(by:)`.
2026-09-21 13:07:01 -07:00
Karoy Lorentey a70b97e2bf Merge pull request #721 from hxperl/docc-parameter-name-backticks
Unbacktick parameter names so DocC binds the parameter docs
2026-09-21 12:54:44 -07:00
Karoy Lorentey 45e7f63387 Merge pull request #725 from eliorpom-cmd/fix-typos-20260918
Fix typos in documentation and comments
2026-09-21 12:54:16 -07:00
Karoy Lorentey 561d10ed84 Merge pull request #727 from lorentey/container-updates
Container: Reinstate requirement for Comparable indices
2026-09-21 11:01:37 -07:00
Karoy Lorentey b2424210b4 Container: Reinstate requirement for Comparable indices
I have a linked list prototype that can support comparable indices (and an O(1) `count`); accordingly, reinstate the requirement for index comparability. This eliminates a potential performance footgun for custom containers that define their own Index, as an incomparable index would make `func distance` 2x slower.
2026-09-21 10:41:46 -07:00
Karoy Lorentey f5db7175b3 Merge pull request #724 from lorentey/container-updates
MutableContainer: Add some bulk update operations
2026-09-21 10:10:34 -07:00
Karoy Lorentey d392f0cbff MutableContainer: Add tests; resolve issues uncovered 2026-09-21 09:54:29 -07:00
Elior Pommier cabf7390ce Fix typos in documentation and comments
Corrects 25 misspellings across DocC articles, design documentation and doc
comments. Prose only: no identifiers, no public API, no behaviour change.

| Typo | Correction | Count |
|---|---|---|
| `offseting` | `offsetting` | 7 |
| `valud` | `value` | 4 |
| `necessarilly` | `necessarily` | 3 |
| `recommmend` | `recommend` | 2 |
| `Mutablility` | `Mutability` | 1 |
| `childs` | `child's` | 1 |
| `constaints` | `constraints` | 1 |
| `destintion` | `destination` | 1 |
| `explan` | `explain` | 1 |
| `forcuses` | `focuses` | 1 |
| `ot` | `or` | 1 |
| `succcess` | `success` | 1 |
| `unqiue` | `unique` | 1 |

Two of these are worth a second look, since they are not pure spelling:

- `_HTable+Removal.swift:18` reads "without marking it as unoccupied ot
  restoring the hash table's other invariants". Read as `or`, matching the
  sentence, and deliberately not as a prefix of `other` later on the line.
- `_Node.UnsafeHandle+Deletion.swift:369` reads "reuse the left childs node".
  Corrected to the possessive `child's` rather than `children`, which is what
  a plain spellchecker suggests but does not fit the sentence.

`valud` is corrected to `value` to match the wording of the equivalent
parameter documentation on `swapAt(_:_:)`.

One change is outside documentation: `CheckContainer.swift:476` fixes
`succcess` inside a test assertion message. It is a human-readable string, not
an expected value, so no test behaviour changes.

Deliberately left alone:

- `isDisjoint(with:)-54iy6` in `OrderedSet.md` is a DocC disambiguation hash,
  not a misspelling.
- `Peristent` in the 2022 ShareableHashedCollections review document, which is
  an archived record of a past review rather than living documentation.

Found with `typos-cli`, then reviewed by hand; the tool's raw output also
flagged identifiers and symbol hashes, which are not included here.
2026-09-18 14:31:47 +02:00
Karoy Lorentey 4ea0e4443d MutableContainer: Also add copying variants of bulk updates 2026-09-16 20:44:13 -07:00
Karoy Lorentey c031de73d0 MutableContainer: Add some bulk update operations 2026-09-16 19:58:28 -07:00
Karoy Lorentey 3ceaf1ac39 Merge pull request #723 from lorentey/container-updates
Refactor DrainableContainer/RangeReplaceableContainer/DynamicContainer
2026-09-16 13:40:56 -07:00
Karoy Lorentey 5cebba6f29 Refactor DrainableContainer/RangeReplaceableContainer/DynamicContainer
Range replacement operations generally need to invalidate all indices, and that means that we lose our position in the container after each insertion/removal, unless we happen to be using a container with looser index invalidation (such as arrays or deques).

A linked list is an example for a container type where insertions/removals need to invalidate all indices — as we need to track index validation using a single version counter that is stored in the list’s head. However, it would not be acceptable to lose our place whenever we insert a new item to the list; therefore, to mitigate the validation issue, this commit changes all range-replacement operations to return a valid index range to the affected items in the container.

This is a radical break from `RangeReplaceableCollection`. It fixes a long-standing defect with it, but it also makes it tricky to conform the same type to both protocol families, as the two require the “same” methods to return different types (`Void` vs `Range<Index>`).

Given that the LSG has intentionally diverged `UniqueArray.removeAll()` from RangeReplaceableCollection semantics, I expect this isn’t going to be an issue. For example, types with existing collection conformances can expose container views, mitigating such semantic differences.

To allow SubrangeConsumers to work without losing position, add a new protocol, `ContainerDrain` with a consuming `finalize` operation that returns a valid index in the resulting container state.

While we’re here, apply the API tweaks from SE-0527:
- Revert the `replace(removing:…:)` names, rolling back to `replaceSubrange(_:…:)` .
- Similarly, rename `consume(_:)` → `consumeSubrange(_:)`
- Rename `reallocate(capacity:)` → `setCapacity(_:)`.
2026-09-14 20:37:12 -07:00
hxperl 80216ab026 Unbacktick parameter names so DocC binds the parameter docs
Six `- Parameter` entries in `Deque` and `BitSet` write the parameter
name inside backticks (``- Parameter `i`:``). DocC binds parameter
documentation by bare identifier, so these never attach: no Parameters
section is rendered at all, and the entry instead falls into the
Discussion as a literal "Parameter `i`: ..." bullet, which also swallows
the `- Complexity:` callout that follows it.

Dropping the backticks around the name restores the Parameters section on
all six pages. Backticks inside the description text are left alone.
2026-09-11 14:07:13 +09:00
Karoy Lorentey 774d6ba631 Merge pull request #718 from dylanpulver/bitset-isequalset-empty-range
[BitSet] Fix isEqualSet(to: Range<Int>) for empty ranges
2026-09-04 10:48:55 -07:00
Karoy Lorentey 396fee354f Merge pull request #717 from Azoy/rigiddictionary-layout-update
[RigidDictionary] Store a sentinel value for values ptr if null
2026-09-02 17:12:29 -07:00
Dylan Pulver 85342a6a49 [BitSet] isEqualSet(to: Range<Int>) is wrong for an empty range
`_toUInt()` returns nil whenever a bound is negative, and the guard maps that
straight to `false`. That is right for a nonempty range -- a bit set can never
hold a negative value -- but an empty range equals the empty set regardless of
where its bounds sit, so `BitSet().isEqualSet(to: -5 ..< -5)` answered false.

Five of the six `Range<Int>` predicates already handle this: isSubset and
isDisjoint clamp, isStrictSubset composes those two, and isSuperset and
isStrictSuperset test `other.isEmpty` before converting. isEqualSet was the
one that did not, which let a bit set be both a subset and a superset of a
range while reporting that it was not equal to it.
2026-09-03 02:59:42 +03:00
Alejandro Alonso b2110d3563 Use single module conditional 2026-09-02 14:52:44 -07:00
Alejandro Alonso e773108091 Make RigidDictionary store a sentinel value for values ptr if null 2026-09-02 14:25:00 -07:00
Karoy Lorentey 08bbdd3def Reinstate exported imports in Collections module 2026-09-01 13:46:05 -07:00
Karoy Lorentey 26f299bd49 Merge pull request #715 from lorentey/no-dynamic-conformance-checks-in-embedded
Disable `_UniqueCollection` fast paths in embedded Swift
2026-09-01 10:50:17 -07:00
Karoy Lorentey e611f62e67 Disable _UniqueCollection fast paths in embedded Swift
As of 2026-09-01, conformance checks are now diagnosed as errors:

```
 70 |     // Fast paths for when we know elements are all unique
 71 |     if elements is _UniqueCollection {
    |                 `- error: cannot perform a dynamic cast to a type involving protocol '_UniqueCollection' in Embedded Swift
```
2026-09-01 10:31:54 -07:00
Karoy Lorentey 225bd07e56 RigidSet, UniqueSet: Add Container conformance 2026-09-01 09:34:05 -07:00
Karoy Lorentey 842f125c47 Invert dependencies to ContainersPreview, moving all container APIs there
- Move `InputSpan` into its own module, `SpanPreview`. Add dependencies to it.
- Invert dependencies to `ContainersPreview`, so that no production target imports it, but it now depends on its previous dependents.
- Move all generic APIs and conformances using `Producer`/`Drain`/`Container` to the `ContainersPreview` module.
- Remove `UnstableContainersPreview` conditions around `Iterable` APIs

In summary, importing `BasicContainers` and `DequeModule` continue to get us the basic definitions for `RigidArray`, `UniqueSet`, `RigidDeque` etc., without most of their unstable APIs. (With the exception of core APIs that require `InputSpan`.)

To get any API that uses an unstable protocol like `Container` or `Drain`, we’ll now need to import `ContainersPreview` in addition to the type-defining module.

This will provide a slightly less horrible migration path when (if) the container/producer protocol family gets adopted/adapted into the stdlib.
2026-08-25 18:29:58 -07:00
Karoy Lorentey 1574bed0dc [ContainersPreview] Spin off removal/consumption operations from RangeReplaceableContainer into a new DrainableContainer protocol
We have types that can usefully conform to `DrainableContainer` but not the full `RangeReplaceableContainer`; `OrderedSet` is one obvious example.
2026-08-25 14:10:06 -07:00
Karoy Lorentey 7e2a4bc761 Merge pull request #705 from karpovantonme/doc/sorted-collections
[SortedCollections] Name the documented arguments the way the declarations name them
2026-08-25 14:02:59 -07:00
Karoy Lorentey fcfa3b00cb Merge pull request #703 from inju2403/cleanup/replaceelement-exchange-same-key
[OrderedCollections] Use exchange(_:with:) in replaceElement's equal-key path
2026-08-25 14:02:39 -07:00
Karoy Lorentey 99b8eca1c2 Merge pull request #701 from karpovantonme/doc/parameter-names
Name the documented arguments the way the declarations name them
2026-08-25 13:50:46 -07:00
Karoy Lorentey f425dffd5b Merge pull request #702 from apple/dn/bit-set-count
Improve `BitSet.count` performance by ~3x
2026-08-18 13:54:33 -07:00
Anton Karpov ed41e49a16 [SortedCollections] Name the documented arguments the way the declarations name them
Same species as #701, in the module behind the UnstableSortedCollections
trait. 16 doc comments name an argument the declaration does not have, and
one block documents a function that is no longer there.

The Keys and Values views carry the same four each: - Parameter i where the
argument is index, and - Parameter index on subscript(position:).

In the B-tree internals the descriptions usually name the right thing while
the key does not: - node on toNode(leftChild:) says the argument becomes the
left child, and - newElement on exchangeChild(atSlot:with newChild:) says it
is the new child.

_BTree.swift keeps a doc block for a function that was removed: it documents
an offset parameter and sits above an unrelated declaration.
2026-08-16 21:52:31 +03:00
David Nadoba 962d21d92d Mark _Word.allBits as @inlinable 2026-08-14 16:32:50 -07:00