Skip to content

[stdlib] Generalize Array(unsafeUninitializedCapacity:initializingWith:) for typed throws #83160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions stdlib/public/SwiftOnoneSupport/SwiftOnoneSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import Swift
// actual generic function name. The "prespecialize" annotation forces
// the actual generic function to be specialized based on the argument
// types passed to the proxy function.
//
// The list of symbols generated by prespecialization can be found at the path
// "lib/SILOptimizer/Utils/OnonePrespecializations.def"
// =============================================================================

extension Collection {
Expand Down
33 changes: 27 additions & 6 deletions stdlib/public/core/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1509,15 +1509,34 @@ extension Array {
}

extension Array {
/// Implementation for Array(unsafeUninitializedCapacity:initializingWith:)
/// Implementation preserved (for ABI reasons) for:
/// Array(unsafeUninitializedCapacity:initializingWith:)
/// and ContiguousArray(unsafeUninitializedCapacity:initializingWith:)
@inlinable
/*@_spi(SwiftStdlibLegacyABI)*/ @available(swift, obsoleted: 1)
@inlinable // inlinable is necessary for prespecialization
internal init(
_unsafeUninitializedCapacity: Int,
initializingWith initializer: (
_ buffer: inout UnsafeMutableBufferPointer<Element>,
_ initializedCount: inout Int) throws -> Void
) rethrows {
try unsafe self.init(
_unsafeUninitializedCapacity: _unsafeUninitializedCapacity,
initializingWithTypedThrowsInitializer: initializer
)
}

/// Implementation for:
/// Array(unsafeUninitializedCapacity:initializingWith:)
/// and ContiguousArray(unsafeUninitializedCapacity:initializingWith:)
@_alwaysEmitIntoClient
internal init<E: Error>(
_unsafeUninitializedCapacity: Int,
initializingWithTypedThrowsInitializer initializer: (
_ buffer: inout UnsafeMutableBufferPointer<Element>,
_ initializedCount: inout Int
) throws(E) -> Void
) throws(E) {
var firstElementAddress: UnsafeMutablePointer<Element>
unsafe (self, firstElementAddress) =
unsafe Array._allocateUninitialized(_unsafeUninitializedCapacity)
Expand Down Expand Up @@ -1567,15 +1586,17 @@ extension Array {
/// which begins as zero. Set `initializedCount` to the number of
/// elements you initialize.
@_alwaysEmitIntoClient @inlinable
public init(
public init<E: Error>(
unsafeUninitializedCapacity: Int,
initializingWith initializer: (
_ buffer: inout UnsafeMutableBufferPointer<Element>,
_ initializedCount: inout Int) throws -> Void
) rethrows {
_ initializedCount: inout Int
) throws(E) -> Void
) throws(E) {
self = try unsafe Array(
_unsafeUninitializedCapacity: unsafeUninitializedCapacity,
initializingWith: initializer)
initializingWithTypedThrowsInitializer: initializer
)
}

// Superseded by the typed-throws version of this function, but retained
Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/core/ContiguousArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1105,15 +1105,15 @@ extension ContiguousArray {
/// which begins as zero. Set `initializedCount` to the number of
/// elements you initialize.
@_alwaysEmitIntoClient @inlinable
public init(
public init<E>(
unsafeUninitializedCapacity: Int,
initializingWith initializer: (
_ buffer: inout UnsafeMutableBufferPointer<Element>,
_ initializedCount: inout Int) throws -> Void
) rethrows {
_ initializedCount: inout Int) throws(E) -> Void
) throws(E) {
self = try unsafe ContiguousArray(Array(
_unsafeUninitializedCapacity: unsafeUninitializedCapacity,
initializingWith: initializer))
initializingWithTypedThrowsInitializer: initializer))
}

// Superseded by the typed-throws version of this function, but retained
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/Sort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ extension UnsafeMutableBufferPointer {
//
// There's no need to set the initialized count within the initializing
// closure, since the buffer is guaranteed to be uninitialized at exit.
_ = try unsafe Array<Element>(_unsafeUninitializedCapacity: count / 2) {
_ = try unsafe Array<Element>(unsafeUninitializedCapacity: count / 2) {
buffer, _ in
var runs: [Range<Index>] = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ Func withUnsafeBytes(of:_:) is now without rethrows
Func withUnsafeMutableBytes(of:_:) is now without rethrows
Func withoutActuallyEscaping(_:do:) has generic signature change from <ClosureType, ResultType> to <ClosureType, ResultType, Failure where Failure : Swift.Error>
Func withoutActuallyEscaping(_:do:) is now without rethrows
Constructor Array.init(unsafeUninitializedCapacity:initializingWith:) has generic signature change from <Element> to <Element, E where E : Swift.Error>
Constructor Array.init(unsafeUninitializedCapacity:initializingWith:) has parameter 1 type change from (inout Swift.UnsafeMutableBufferPointer<Element>, inout Swift.Int) throws -> Swift.Void to (inout Swift.UnsafeMutableBufferPointer<Element>, inout Swift.Int) throws(E) -> Swift.Void
Constructor Array.init(unsafeUninitializedCapacity:initializingWith:) is now without rethrows
Constructor ContiguousArray.init(unsafeUninitializedCapacity:initializingWith:) has generic signature change from <Element> to <Element, E where E : Swift.Error>
Constructor ContiguousArray.init(unsafeUninitializedCapacity:initializingWith:) has parameter 1 type change from (inout Swift.UnsafeMutableBufferPointer<Element>, inout Swift.Int) throws -> Swift.Void to (inout Swift.UnsafeMutableBufferPointer<Element>, inout Swift.Int) throws(E) -> Swift.Void
Constructor ContiguousArray.init(unsafeUninitializedCapacity:initializingWith:) is now without rethrows

Protocol SIMDScalar has generic signature change from <Self == Self.SIMD16Storage.Scalar, Self.SIMD16Storage : Swift.SIMDStorage, Self.SIMD2Storage : Swift.SIMDStorage, Self.SIMD32Storage : Swift.SIMDStorage, Self.SIMD4Storage : Swift.SIMDStorage, Self.SIMD64Storage : Swift.SIMDStorage, Self.SIMD8Storage : Swift.SIMDStorage, Self.SIMDMaskScalar : Swift.FixedWidthInteger, Self.SIMDMaskScalar : Swift.SIMDScalar, Self.SIMDMaskScalar : Swift.SignedInteger, Self.SIMD16Storage.Scalar == Self.SIMD2Storage.Scalar, Self.SIMD2Storage.Scalar == Self.SIMD32Storage.Scalar, Self.SIMD32Storage.Scalar == Self.SIMD4Storage.Scalar, Self.SIMD4Storage.Scalar == Self.SIMD64Storage.Scalar, Self.SIMD64Storage.Scalar == Self.SIMD8Storage.Scalar> to <Self : Swift.BitwiseCopyable, Self == Self.SIMD16Storage.Scalar, Self.SIMD16Storage : Swift.SIMDStorage, Self.SIMD2Storage : Swift.SIMDStorage, Self.SIMD32Storage : Swift.SIMDStorage, Self.SIMD4Storage : Swift.SIMDStorage, Self.SIMD64Storage : Swift.SIMDStorage, Self.SIMD8Storage : Swift.SIMDStorage, Self.SIMDMaskScalar : Swift.FixedWidthInteger, Self.SIMDMaskScalar : Swift.SIMDScalar, Self.SIMDMaskScalar : Swift.SignedInteger, Self.SIMDMaskScalar == Self.SIMDMaskScalar.SIMDMaskScalar, Self.SIMD16Storage.Scalar == Self.SIMD2Storage.Scalar, Self.SIMD2Storage.Scalar == Self.SIMD32Storage.Scalar, Self.SIMD32Storage.Scalar == Self.SIMD4Storage.Scalar, Self.SIMD4Storage.Scalar == Self.SIMD64Storage.Scalar, Self.SIMD64Storage.Scalar == Self.SIMD8Storage.Scalar>

Expand Down
3 changes: 3 additions & 0 deletions test/api-digester/stability-stdlib-abi-without-asserts.test
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,9 @@ Func _SliceBuffer.withUnsafeBufferPointer(_:) has mangled name changing from 'Sw
Func _SliceBuffer.withUnsafeMutableBufferPointer(_:) has been renamed to Func __abi_withUnsafeMutableBufferPointer(_:)
Func _SliceBuffer.withUnsafeMutableBufferPointer(_:) has mangled name changing from 'Swift._SliceBuffer.withUnsafeMutableBufferPointer<A>((Swift.UnsafeMutableBufferPointer<A>) throws -> A1) throws -> A1' to 'Swift._SliceBuffer.__abi_withUnsafeMutableBufferPointer<A>((Swift.UnsafeMutableBufferPointer<A>) throws -> A1) throws -> A1'

// Functions changed for typed throws
Constructor Array.init(_unsafeUninitializedCapacity:initializingWith:) has been removed

Struct String.Index has added a conformance to an existing protocol CustomDebugStringConvertible

Struct CollectionOfOne is now with @_addressableForDependencies
Expand Down