Skip to content

Commit c9a3fdf

Browse files
authored
Merge pull request swiftlang#83771 from glessard/rdar158440246
2 parents 16fbfec + 4764913 commit c9a3fdf

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

stdlib/public/core/Span/OutputSpan.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ extension OutputSpan where Element: ~Copyable {
315315
@_alwaysEmitIntoClient
316316
@lifetime(self: copy self)
317317
public mutating func removeAll() {
318+
guard count > 0 else { return }
318319
_ = unsafe _start().withMemoryRebound(to: Element.self, capacity: _count) {
319320
unsafe $0.deinitialize(count: _count)
320321
}

test/stdlib/Span/OutputSpanTests.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ struct Allocation<T>: ~Copyable {
3535
_ body: (inout OutputSpan<T>) throws(E) -> Void
3636
) throws(E) {
3737
if count != nil { fatalError() }
38-
var outputBuffer = OutputSpan<T>(buffer: allocation, initializedCount: 0)
38+
var allocation = allocation
39+
if allocation.count == 0 { allocation = .init(start: nil, count: 0) }
40+
var outputBuffer = OutputSpan(buffer: allocation, initializedCount: 0)
3941
do {
4042
try body(&outputBuffer)
4143
let initialized = outputBuffer.finalize(for: allocation)
@@ -188,6 +190,20 @@ suite.test("deinitialize buffer")
188190
catch {
189191
expectTrue(false)
190192
}
193+
194+
a = Allocation(of: 0, Int.self)
195+
do {
196+
try a.initialize {
197+
expectEqual($0.freeCapacity, 0)
198+
throw MyTestError.error
199+
}
200+
}
201+
catch MyTestError.error {
202+
expectEqual(a.isEmpty, true)
203+
}
204+
catch {
205+
expectTrue(false)
206+
}
191207
}
192208

193209
suite.test("InlineArray initialization")

0 commit comments

Comments
 (0)