mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
SetAPIChecker verifies that a set-like type has the right shape: it is a protocol with no assertions, so conforming makes the compiler check that the API surface exists. This adds the other half — that the operations behave as documented. Constrained on SetAPIChecker rather than SetAlgebra, because OrderedSet deliberately does not conform to SetAlgebra (only its UnorderedView does, for documented reasons about order-sensitive equality). Constraining on the stdlib protocol would exclude the type that needs this most. Follows the house pattern of validating the specification before the implementation. A Set<Element> model is only as sound as Element's Hashable conformance, so _checkSetAlgebraModels runs _checkHashable over the model elements and verifies each model survives a round trip through Array before any instance is judged; it returns false and the caller stops rather than reporting defects in the wrong artefact. A broken model and a broken implementation can agree with each other, which fails quietly in a way a wrong implementation does not. Per-instance: contents and count against the model, isEmpty against count independently of it, the empty set's identity and absorption cases, the reflexive containment predicates, and filter at both extremes and in between. Per-pair: the four operations against the model and again through the Sequence-taking overloads, which are separate entry points and can diverge from the Self-taking ones; commutativity, both absorption laws, and symmetricDifference against its definition in terms of the other three; and the containment predicates against the model and against each other. A type can implement each predicate plausibly and still have them disagree. Contents and count are asserted separately throughout: a type that maintains a stored count can return the right elements alongside a stale one. Call sites for BitSet, OrderedSet and TreeSet. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>