Merge pull request #848 from sandeepvijayarao09/fix-trailing-array-throwing-init

[TrailingElements] Don't deinitialize elements when the initializer throws
This commit is contained in:
Doug Gregor authored and GitHub committed 2026-11-21 14:50:19 -08:00
commit 3b10d15a65
4 files changed
+129 -15

No files matched your search

+1 -1
View File
@@ -344,7 +344,7 @@ let targets: [CustomTarget] = [
.target(
kind: .test,
name: "TrailingElementsTests",
dependencies: ["TrailingElementsModule"]),
dependencies: ["TrailingElementsModule", "_CollectionsTestSupport"]),
.target(
kind: .exported,
+1 -1
View File
@@ -342,7 +342,7 @@ let targets: [CustomTarget] = [
.target(
kind: .test,
name: "TrailingElementsTests",
dependencies: ["TrailingElementsModule"]),
dependencies: ["TrailingElementsModule", "_CollectionsTestSupport"]),
.target(
kind: .exported,
@@ -58,15 +58,23 @@ where Header: ~Copyable
unsafe _pointer.initialize(to: header)
}
/// Initialize the trailing elements that follow the header at `pointer`.
///
/// If `initializer` throws, the output span deinitializes the elements it
/// has initialized so far, so the caller only needs to clean up the header.
@available(SwiftStdlib 5.1, *)
@_alwaysEmitIntoClient
mutating func _initializeTrailingElements<E>(
@unsafe
static func _initializeTrailingElements<E>(
after pointer: UnsafeMutablePointer<Header>,
initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) {
var output = unsafe OutputSpan(buffer: rawElements, initializedCount: 0)
let elements = unsafe _elements(after: pointer)
var output = unsafe OutputSpan(buffer: elements, initializedCount: 0)
try initializer(&output)
let initialized = unsafe output.finalize(for: rawElements)
precondition(count == initialized, "TrailingArray initialization underflow")
let initialized = unsafe output.finalize(for: elements)
precondition(
elements.count == initialized, "TrailingArray initialization underflow")
}
/// Allocate an intrusive managed buffer with the given header and calling
@@ -77,8 +85,22 @@ where Header: ~Copyable
header: consuming Header,
initializingTrailingElementsWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) {
self.init(_headerOnly: header)
try _initializeTrailingElements(initializer: initializer)
let (bytes, alignment) = Self.allocationSize(header: header)
let storage = unsafe UnsafeMutableRawPointer.allocate(
byteCount: bytes,
alignment: alignment)
let pointer = unsafe Self.headerPointer(fromStorage: storage)
unsafe pointer.initialize(to: header)
do throws(E) {
try unsafe Self._initializeTrailingElements(
after: pointer, initializer: initializer)
} catch {
// The trailing elements aren't initialized, so don't let `deinit` run.
unsafe pointer.deinitialize(count: 1)
unsafe storage.deallocate()
throw error
}
unsafe self._storage = storage
}
/// Allocate an intrusive managed buffer with the given header and
@@ -184,10 +206,18 @@ where Header: ~Copyable
@_alwaysEmitIntoClient
@unsafe
var rawElements: UnsafeMutableBufferPointer<Element> {
unsafe Self._elements(after: _pointer)
}
@_alwaysEmitIntoClient
@unsafe
static func _elements(
after pointer: UnsafeMutablePointer<Header>
) -> UnsafeMutableBufferPointer<Element> {
unsafe UnsafeMutableBufferPointer(
start: UnsafeMutableRawPointer(_pointer.advanced(by: 1))
start: UnsafeMutableRawPointer(pointer.advanced(by: 1))
.assumingMemoryBound(to: Element.self),
count: header.trailingCount
count: pointer.pointee.trailingCount
)
}
@@ -334,11 +364,13 @@ extension TrailingArray where Header: Copyable {
body: (inout TrailingArray<Header>) throws(E) -> R
) throws(E) -> R {
return unsafe try _withTemporaryValue(
header: header, uninitializedTrailingElements: ()
) { (value) throws(E) in
try value._initializeTrailingElements(initializer: initializer)
return try body(&value)
}
header: header,
uninitializedTrailingElements: (),
initializingTrailingElements: { (pointer) throws(E) in
try unsafe _initializeTrailingElements(
after: pointer, initializer: initializer)
},
body: body)
}
}
@@ -353,6 +385,9 @@ extension TrailingArray {
static func _withTemporaryValue<R: ~Copyable, E>(
header: Header,
uninitializedTrailingElements: (),
initializingTrailingElements: (
UnsafeMutablePointer<Header>
) throws(E) -> Void = { _ in },
body: (inout TrailingArray<Header>) throws(E) -> R
) throws(E) -> R {
let (numBytes, alignment) = allocationSize(header: header)
@@ -367,6 +402,16 @@ extension TrailingArray {
let headerPointer = unsafe TrailingArray.headerPointer(fromStorage: storagePointer)
unsafe headerPointer.initialize(to: header)
// Initialize the trailing elements, if requested, before creating the
// trailing array, so a throwing initializer doesn't leave behind
// a value whose elements aren't initialized.
do throws(E) {
try initializingTrailingElements(headerPointer)
} catch {
unsafe headerPointer.deinitialize(count: 1)
throw error
}
/// Create a trailing array over that temporary storage.
var managedBuffer: Optional = unsafe TrailingArray(
consuming: headerPointer,
@@ -16,6 +16,7 @@ import Testing
#if COLLECTIONS_SINGLE_MODULE
import Collections
#else
import _CollectionsTestSupport
import TrailingElementsModule
#endif
@@ -59,6 +60,13 @@ struct ManyBytesWithPointers: @unsafe TrailingElements {
var trailingCount: Int { Int(bytes.0) }
}
struct TrackedList: TrailingElements {
typealias Element = LifetimeTracked<Int>
var trailingCount: Int
}
struct InitializationError: Error {}
@Suite("Trailing elements tests")
struct TrailingElementsTests {
@Test func simpleCoordinates() {
@@ -183,4 +191,65 @@ struct TrailingElementsTests {
}
}
}
@Test(arguments: 0 ... 3)
func throwingInitializerOnHeap(initializedCount: Int) {
let tracker = LifetimeTracker()
#expect(throws: InitializationError.self) {
_ = try TrailingArray(
header: TrackedList(trailingCount: 3)
) { (outputSpan) throws(InitializationError) in
for _ in 0 ..< initializedCount {
outputSpan.append(tracker.instance(for: 0))
}
throw InitializationError()
}
}
#expect(tracker.instances == 0)
}
@Test(arguments: 0 ... 3)
func throwingInitializerOnStack(initializedCount: Int) {
let tracker = LifetimeTracker()
let initializer:
(inout OutputSpan<LifetimeTracked<Int>>) throws(InitializationError) -> Void = {
outputSpan throws(InitializationError) in
for _ in 0 ..< initializedCount {
outputSpan.append(tracker.instance(for: 0))
}
throw InitializationError()
}
let body: (inout TrailingArray<TrackedList>) throws(InitializationError) -> Void = { _ in
Issue.record("body shouldn't be called")
}
#expect(throws: InitializationError.self) {
try TrailingArray<TrackedList>.withTemporaryValue(
header: TrackedList(trailingCount: 3),
initializingTrailingElementsWith: initializer,
body: body)
}
#expect(tracker.instances == 0)
}
@Test func trackedElementsAreReleased() {
let tracker = LifetimeTracker()
do {
let tracked = TrailingArray(header: TrackedList(trailingCount: 3)) { outputSpan in
for _ in 0 ..< 3 {
outputSpan.append(tracker.instance(for: 0))
}
}
#expect(tracker.instances == 3)
#expect(tracked.count == 3)
}
#expect(tracker.instances == 0)
TrailingArray.withTemporaryValue(header: TrackedList(trailingCount: 2)) { outputSpan in
outputSpan.append(tracker.instance(for: 0))
outputSpan.append(tracker.instance(for: 0))
} body: { tracked in
#expect(tracker.instances == 2)
}
#expect(tracker.instances == 0)
}
}