mirror of
https://github.com/apple/swift-collections
synced 2026-11-22 04:20:40 +00:00
`TrailingArray.init(header:initializingTrailingElementsWith:)` created the value with just its header, then ran the initializer. If the initializer threw, the half-built value was destroyed, so `deinit` deinitialized all `trailingCount` elements, even though the output span had already deinitialized the ones it had initialized and the rest were never initialized. `withTemporaryValue(header:initializingTrailingElementsWith:body:)` had the same problem through the `defer` in `_withTemporaryValue`. With non-trivial elements (e.g. class references) both crashed. Initialize the trailing elements before creating the `TrailingArray`, and if the initializer throws, only deinitialize the header (and free the heap storage) before rethrowing.