From 1f4913eb968837faf241ec1f150c987b51156e93 Mon Sep 17 00:00:00 2001 From: Sandeep Vijayarao Date: Wed, 11 Nov 2026 00:19:39 -0800 Subject: [PATCH] [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 `..) -> Slice { - let bounds = bounds.relative(to: Int.min ..< Int.max) + let bounds = bounds.relative(to: _AllIntegers()) return self[members: bounds] } } +/// All `Int` values as a collection, for resolving range expressions in +/// `BitSet[members:]`. Unlike `Int.min ..< Int.max`, stepping past `Int.max` +/// doesn't trap, so closed ranges like `0 ... Int.max` resolve to +/// `0 ..< Int.max`. `Int.max` can never be a member, so that loses nothing. +internal struct _AllIntegers: Collection { + internal var startIndex: Int { Int.min } + internal var endIndex: Int { Int.max } + + internal subscript(position: Int) -> Int { position } + + internal func index(after i: Int) -> Int { + i == Int.max ? Int.max : i + 1 + } +} + extension BitSet { /// Removes and returns the element at the specified position. /// diff --git a/Tests/BitCollectionsTests/BitSet.Counted Tests.swift b/Tests/BitCollectionsTests/BitSet.Counted Tests.swift index ff287ee7..1c68b5a6 100644 --- a/Tests/BitCollectionsTests/BitSet.Counted Tests.swift +++ b/Tests/BitCollectionsTests/BitSet.Counted Tests.swift @@ -29,6 +29,13 @@ extension BitSet.Counted: SetAPIExtras { extension BitSet.Counted: SortedCollectionAPIChecker {} final class BitSetCountedTests: CollectionTestCase { + func test_member_range_subscript() { + let bits: BitSet.Counted = [2, 5, 6, 8, 9] + expectEqualElements(bits[members: 3 ..< 7], [5, 6]) + expectEqualElements(bits[members: ...Int.max], [2, 5, 6, 8, 9]) + expectEqualElements(bits[members: 6 ... Int.max], [6, 8, 9]) + } + func test_collection_strides() { withEvery("stride", in: [1, 2, 3, 5, 7, 8, 11, 13, 63, 79, 300]) { stride in withEvery("count", in: [0, 1, 2, 5, 10, 20, 25]) { count in diff --git a/Tests/BitCollectionsTests/BitSetTests.swift b/Tests/BitCollectionsTests/BitSetTests.swift index 9991e0a5..704d4997 100644 --- a/Tests/BitCollectionsTests/BitSetTests.swift +++ b/Tests/BitCollectionsTests/BitSetTests.swift @@ -446,6 +446,24 @@ final class BitSetTest: CollectionTestCase { expectEqualElements(h, []) expectEqual(h.startIndex, bits.endIndex) expectEqual(h.endIndex, bits.endIndex) + + // Closed ranges ending at `Int.max` + let i = bits[members: ...Int.max] + expectEqualElements(i, [2, 5, 6, 8, 9]) + expectEqual(i.startIndex, bits.startIndex) + expectEqual(i.endIndex, bits.endIndex) + + let j = bits[members: 6 ... Int.max] + expectEqualElements(j, [6, 8, 9]) + expectEqual(j.endIndex, bits.endIndex) + + let k = bits[members: Int.min ... Int.max] + expectEqualElements(k, [2, 5, 6, 8, 9]) + + let l = bits[members: Int.max ... Int.max] + expectEqualElements(l, []) + expectEqual(l.startIndex, bits.endIndex) + expectEqual(l.endIndex, bits.endIndex) } func test_member_range_subscript_exhaustive() {