Skip to content

Commit 5c07fd9

Browse files
committed
Adopt typed throws in withUnsafeBufferPointer.
Part of rdar://131405937.
1 parent 03a0019 commit 5c07fd9

File tree

9 files changed

+114
-22
lines changed

9 files changed

+114
-22
lines changed

stdlib/public/core/Array.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,16 @@ extension Array {
15651565
initializingWith: initializer)
15661566
}
15671567

1568+
// Superseded by the typed-throws version of this function, but retained
1569+
// for ABI reasons.
1570+
@usableFromInline
1571+
@_disfavoredOverload
1572+
func withUnsafeBufferPointer<R>(
1573+
_ body: (UnsafeBufferPointer<Element>) throws -> R
1574+
) rethrows -> R {
1575+
return try _buffer.withUnsafeBufferPointer(body)
1576+
}
1577+
15681578
/// Calls a closure with a pointer to the array's contiguous storage.
15691579
///
15701580
/// Often, the optimizer can eliminate bounds checks within an array
@@ -1595,9 +1605,10 @@ extension Array {
15951605
/// valid only for the duration of the method's execution.
15961606
/// - Returns: The return value, if any, of the `body` closure parameter.
15971607
@inlinable
1598-
public func withUnsafeBufferPointer<R>(
1599-
_ body: (UnsafeBufferPointer<Element>) throws -> R
1600-
) rethrows -> R {
1608+
@_alwaysEmitIntoClient
1609+
public func withUnsafeBufferPointer<R, E>(
1610+
_ body: (UnsafeBufferPointer<Element>) throws(E) -> R
1611+
) throws(E) -> R {
16011612
return try _buffer.withUnsafeBufferPointer(body)
16021613
}
16031614

stdlib/public/core/ArrayBuffer.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,21 +567,36 @@ extension _ArrayBuffer {
567567
}
568568
}
569569

570+
// Superseded by the typed-throws version of this function, but retained
571+
// for ABI reasons.
572+
@inlinable
573+
@_silgen_name("$ss12_ArrayBufferV010withUnsafeB7Pointeryqd__qd__SRyxGKXEKlF")
574+
internal func __abi_withUnsafeBufferPointer<R>(
575+
_ body: (UnsafeBufferPointer<Element>) throws -> R
576+
) rethrows -> R {
577+
if _fastPath(_isNative) {
578+
defer { _fixLifetime(self) }
579+
return try body(
580+
UnsafeBufferPointer(start: firstElementAddress, count: count))
581+
}
582+
return try ContiguousArray(self).withUnsafeBufferPointer(body)
583+
}
584+
570585
/// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the
571586
/// underlying contiguous storage. If no such storage exists, it is
572587
/// created on-demand.
573588
@inlinable
574-
internal func withUnsafeBufferPointer<R>(
575-
_ body: (UnsafeBufferPointer<Element>) throws -> R
576-
) rethrows -> R {
589+
internal func withUnsafeBufferPointer<R, E>(
590+
_ body: (UnsafeBufferPointer<Element>) throws(E) -> R
591+
) throws(E) -> R {
577592
if _fastPath(_isNative) {
578593
defer { _fixLifetime(self) }
579594
return try body(
580595
UnsafeBufferPointer(start: firstElementAddress, count: count))
581596
}
582597
return try ContiguousArray(self).withUnsafeBufferPointer(body)
583598
}
584-
599+
585600
/// Call `body(p)`, where `p` is an `UnsafeMutableBufferPointer`
586601
/// over the underlying contiguous storage.
587602
///

stdlib/public/core/ArrayBufferProtocol.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,20 @@ where Indices == Range<Int> {
7676
/// Returns a `_SliceBuffer` containing the elements in `bounds`.
7777
subscript(bounds: Range<Int>) -> _SliceBuffer<Element> { get }
7878

79-
/// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the
80-
/// underlying contiguous storage. If no such storage exists, it is
81-
/// created on-demand.
79+
// Superseded by the typed-throws version of this function, but retained
80+
// for ABI reasons.
8281
func withUnsafeBufferPointer<R>(
8382
_ body: (UnsafeBufferPointer<Element>) throws -> R
8483
) rethrows -> R
8584

85+
/// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the
86+
/// underlying contiguous storage. If no such storage exists, it is
87+
/// created on-demand.
88+
@available(SwiftStdlib 6.1, *)
89+
func withUnsafeBufferPointer<R, E>(
90+
_ body: (UnsafeBufferPointer<Element>) throws(E) -> R
91+
) throws(E) -> R
92+
8693
/// Call `body(p)`, where `p` is an `UnsafeMutableBufferPointer`
8794
/// over the underlying contiguous storage.
8895
///

stdlib/public/core/ArraySlice.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,16 @@ extension ArraySlice {
11641164
}
11651165

11661166
extension ArraySlice {
1167+
// Superseded by the typed-throws version of this function, but retained
1168+
// for ABI reasons.
1169+
@usableFromInline
1170+
@_disfavoredOverload
1171+
func withUnsafeBufferPointer<R>(
1172+
_ body: (UnsafeBufferPointer<Element>) throws -> R
1173+
) rethrows -> R {
1174+
return try _buffer.withUnsafeBufferPointer(body)
1175+
}
1176+
11671177
/// Calls a closure with a pointer to the array's contiguous storage.
11681178
///
11691179
/// Often, the optimizer can eliminate bounds checks within an array
@@ -1194,9 +1204,10 @@ extension ArraySlice {
11941204
/// valid only for the duration of the method's execution.
11951205
/// - Returns: The return value, if any, of the `body` closure parameter.
11961206
@inlinable
1197-
public func withUnsafeBufferPointer<R>(
1198-
_ body: (UnsafeBufferPointer<Element>) throws -> R
1199-
) rethrows -> R {
1207+
@_alwaysEmitIntoClient
1208+
public func withUnsafeBufferPointer<R, E>(
1209+
_ body: (UnsafeBufferPointer<Element>) throws(E) -> R
1210+
) throws(E) -> R {
12001211
return try _buffer.withUnsafeBufferPointer(body)
12011212
}
12021213

stdlib/public/core/ContiguousArray.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,16 @@ extension ContiguousArray {
11051105
initializingWith: initializer))
11061106
}
11071107

1108+
// Superseded by the typed-throws version of this function, but retained
1109+
// for ABI reasons.
1110+
@usableFromInline
1111+
@_disfavoredOverload
1112+
func withUnsafeBufferPointer<R>(
1113+
_ body: (UnsafeBufferPointer<Element>) throws -> R
1114+
) rethrows -> R {
1115+
return try _buffer.withUnsafeBufferPointer(body)
1116+
}
1117+
11081118
/// Calls a closure with a pointer to the array's contiguous storage.
11091119
///
11101120
/// Often, the optimizer can eliminate bounds checks within an array
@@ -1135,9 +1145,10 @@ extension ContiguousArray {
11351145
/// valid only for the duration of the method's execution.
11361146
/// - Returns: The return value, if any, of the `body` closure parameter.
11371147
@inlinable
1138-
public func withUnsafeBufferPointer<R>(
1139-
_ body: (UnsafeBufferPointer<Element>) throws -> R
1140-
) rethrows -> R {
1148+
@_alwaysEmitIntoClient
1149+
public func withUnsafeBufferPointer<R, E>(
1150+
_ body: (UnsafeBufferPointer<Element>) throws(E) -> R
1151+
) throws(E) -> R {
11411152
return try _buffer.withUnsafeBufferPointer(body)
11421153
}
11431154

stdlib/public/core/ContiguousArrayBuffer.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,17 +444,29 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
444444
return firstElementAddress
445445
}
446446

447-
/// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the
448-
/// underlying contiguous storage.
447+
// Superseded by the typed-throws version of this function, but retained
448+
// for ABI reasons.
449449
@inlinable
450-
internal func withUnsafeBufferPointer<R>(
450+
@_silgen_name("$ss22_ContiguousArrayBufferV010withUnsafeC7Pointeryqd__qd__SRyxGKXEKlF")
451+
internal func __abi_withUnsafeBufferPointer<R>(
451452
_ body: (UnsafeBufferPointer<Element>) throws -> R
452453
) rethrows -> R {
453454
defer { _fixLifetime(self) }
454455
return try body(UnsafeBufferPointer(start: firstElementAddress,
455456
count: count))
456457
}
457458

459+
/// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the
460+
/// underlying contiguous storage.
461+
@inlinable
462+
internal func withUnsafeBufferPointer<R, E>(
463+
_ body: (UnsafeBufferPointer<Element>) throws(E) -> R
464+
) throws(E) -> R {
465+
defer { _fixLifetime(self) }
466+
return try body(UnsafeBufferPointer(start: firstElementAddress,
467+
count: count))
468+
}
469+
458470
/// Call `body(p)`, where `p` is an `UnsafeMutableBufferPointer`
459471
/// over the underlying contiguous storage.
460472
@inlinable

stdlib/public/core/SliceBuffer.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,17 +430,29 @@ internal struct _SliceBuffer<Element>
430430
internal typealias Indices = Range<Int>
431431

432432
//===--- misc -----------------------------------------------------------===//
433-
/// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the
434-
/// underlying contiguous storage.
433+
// Superseded by the typed-throws version of this function, but retained
434+
// for ABI reasons.
435435
@inlinable
436-
internal func withUnsafeBufferPointer<R>(
436+
@_silgen_name("$ss12_SliceBufferV010withUnsafeB7Pointeryqd__qd__SRyxGKXEKlF")
437+
internal func __abi_withUnsafeBufferPointer<R>(
437438
_ body: (UnsafeBufferPointer<Element>) throws -> R
438439
) rethrows -> R {
439440
defer { _fixLifetime(self) }
440441
return try body(UnsafeBufferPointer(start: firstElementAddress,
441442
count: count))
442443
}
443444

445+
/// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the
446+
/// underlying contiguous storage.
447+
@inlinable
448+
internal func withUnsafeBufferPointer<R, E>(
449+
_ body: (UnsafeBufferPointer<Element>) throws(E) -> R
450+
) throws(E) -> R {
451+
defer { _fixLifetime(self) }
452+
return try body(UnsafeBufferPointer(start: firstElementAddress,
453+
count: count))
454+
}
455+
444456
/// Call `body(p)`, where `p` is an `UnsafeMutableBufferPointer`
445457
/// over the underlying contiguous storage.
446458
@inlinable

test/abi/macOS/arm64/stdlib.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,10 @@ Added: _$sSS20_immortalCocoaString5count8encodingSSyXl_Sixmtcs16_UnicodeEncoding
581581

582582
// Swift._stdlib_isVariantOSVersionAtLeast(Builtin.Word, Builtin.Word, Builtin.Word) -> Builtin.Int1
583583
Added: _$ss33_stdlib_isVariantOSVersionAtLeastyBi1_Bw_BwBwtF
584+
585+
// Typed throws for withUnsafePointer operations.
586+
Added: _$ss12_ArrayBufferV010withUnsafeB7Pointeryqd__qd__SRyxGqd_0_YKXEqd_0_YKs5ErrorRd_0_r0_lF
587+
Added: _$ss12_SliceBufferV010withUnsafeB7Pointeryqd__qd__SRyxGqd_0_YKXEqd_0_YKs5ErrorRd_0_r0_lF
588+
Added: _$ss20_ArrayBufferProtocolP010withUnsafeB7Pointeryqd__qd__SRy7ElementQzGqd_0_YKXEqd_0_YKs5ErrorRd_0_r0_lFTj
589+
Added: _$ss20_ArrayBufferProtocolP010withUnsafeB7Pointeryqd__qd__SRy7ElementQzGqd_0_YKXEqd_0_YKs5ErrorRd_0_r0_lFTq
590+
Added: _$ss22_ContiguousArrayBufferV010withUnsafeC7Pointeryqd__qd__SRyxGqd_0_YKXEqd_0_YKs5ErrorRd_0_r0_lF

test/abi/macOS/x86_64/stdlib.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,9 @@ Added: _$sSS20_immortalCocoaString5count8encodingSSyXl_Sixmtcs16_UnicodeEncoding
581581

582582
// Swift._stdlib_isVariantOSVersionAtLeast(Builtin.Word, Builtin.Word, Builtin.Word) -> Builtin.Int1
583583
Added: _$ss33_stdlib_isVariantOSVersionAtLeastyBi1_Bw_BwBwtF
584+
585+
// Typed throws for withUnsafePointer operations.
586+
Added: _$ss12_ArrayBufferV010withUnsafeB7Pointeryqd__qd__SRyxGqd_0_YKXEqd_0_YKs5ErrorRd_0_r0_lF
587+
Added: _$ss12_SliceBufferV010withUnsafeB7Pointeryqd__qd__SRyxGqd_0_YKXEqd_0_YKs5ErrorRd_0_r0_lF
588+
Added: _$ss20_ArrayBufferProtocolP010withUnsafeB7Pointeryqd__qd__SRy7ElementQzGqd_0_YKXEqd_0_YKs5ErrorRd_0_r0_lFTj
589+
Added: _$ss22_ContiguousArrayBufferV010withUnsafeC7Pointeryqd__qd__SRyxGqd_0_YKXEqd_0_YKs5ErrorRd_0_r0_lF

0 commit comments

Comments
 (0)