Merge pull request #782 from aashish254/fix-774-rigidset-insert-hang

[BasicContainers] Fix RigidSet/UniqueSet batch insertion never terminating without the preview trait
This commit is contained in:
Karoy Lorentey authored and GitHub committed 2026-10-21 16:33:58 -07:00
commit 83e5f26fba
2 files changed
+44

No files matched your search

@@ -155,6 +155,11 @@ extension RigidSet where Element: ~Copyable {
}
#else
output.withUnsafeMutableBufferPointer { buffer, count in
if count < c {
remainder = 0
} else {
remainder &-= count
}
for i in 0 ..< count {
self.insert(buffer.moveElement(from: i))
}
@@ -110,6 +110,45 @@ class RigidSetTests: CollectionTestCase {
}
}
func test_init_capacity_initializingWith() {
// Regression test for https://github.com/apple/swift-collections/issues/774:
// without the `UnstableContainersPreview` trait, `insert(addingCount:initializingWith:)`
// never terminated because the batch loop didn't update its `remainder` bookkeeping.
var calls = 0
let fillTwo: (inout OutputSpan<Int>) -> Void = { span in
calls += 1
span.append(1)
span.append(2)
}
// Exact fill: the batch fills its whole span.
let a = RigidSet<Int>(capacity: 2, initializingWith: fillTwo)
expectEqual(a.count, 2)
expectEqual(calls, 1)
expectTrue(a.contains(1))
expectTrue(a.contains(2))
expectConsistentSet(a)
// Short fill: the initializer stops before the span is full.
calls = 0
let b = RigidSet<Int>(capacity: 4, initializingWith: fillTwo)
expectEqual(b.count, 2)
expectEqual(calls, 1)
expectConsistentSet(b)
calls = 0
var c = RigidSet<Int>(capacity: 8)
c.insert(addingCount: 5, initializingWith: fillTwo)
expectEqual(c.count, 2)
expectEqual(calls, 1)
expectConsistentSet(c)
calls = 0
let d = UniqueSet<Int>(minimumCapacity: 4, initializingWith: fillTwo)
expectEqual(d.count, 2)
expectEqual(calls, 1)
}
func test_insert_one_large() {
withLifetimeTracking { tracker in
var s = RigidSet<LifetimeTracked<Int>>(capacity: 20)