Skip to content

Commit 883716b

Browse files
authored
Merge pull request #80858 from Azoy/remove-wubp-inlinearray
[stdlib] Remove _withUnsafeBufferPointer APIs on InlineArray
2 parents 2236206 + 596b015 commit 883716b

File tree

3 files changed

+84
-38
lines changed

3 files changed

+84
-38
lines changed

stdlib/public/core/InlineArray.swift

Lines changed: 74 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,34 @@ extension InlineArray where Element: ~Copyable {
8484
unsafe UnsafeBufferPointer<Element>(start: _address, count: count)
8585
}
8686

87+
/// Returns a pointer to the first element in the array while performing stack
88+
/// checking.
89+
///
90+
/// Use this when the value of the pointer could potentially be directly used
91+
/// by users (e.g. through the use of span or the unchecked subscript).
92+
@available(SwiftStdlib 6.2, *)
93+
@_alwaysEmitIntoClient
94+
@_transparent
95+
internal var _protectedAddress: UnsafePointer<Element> {
96+
#if $AddressOfProperty
97+
unsafe UnsafePointer<Element>(Builtin.addressOfBorrow(_storage))
98+
#else
99+
unsafe UnsafePointer<Element>(Builtin.addressOfBorrow(self))
100+
#endif
101+
}
102+
103+
/// Returns a buffer pointer over the entire array while performing stack
104+
/// checking.
105+
///
106+
/// Use this when the value of the pointer could potentially be directly used
107+
/// by users (e.g. through the use of span or the unchecked subscript).
108+
@available(SwiftStdlib 6.2, *)
109+
@_alwaysEmitIntoClient
110+
@_transparent
111+
internal var _protectedBuffer: UnsafeBufferPointer<Element> {
112+
unsafe UnsafeBufferPointer<Element>(start: _protectedAddress, count: count)
113+
}
114+
87115
/// Returns a mutable pointer to the first element in the array.
88116
@available(SwiftStdlib 6.2, *)
89117
@_alwaysEmitIntoClient
@@ -111,6 +139,41 @@ extension InlineArray where Element: ~Copyable {
111139
}
112140
}
113141

142+
/// Returns a mutable pointer to the first element in the array while
143+
/// performing stack checking.
144+
///
145+
/// Use this when the value of the pointer could potentially be directly used
146+
/// by users (e.g. through the use of span or the unchecked subscript).
147+
@available(SwiftStdlib 6.2, *)
148+
@_alwaysEmitIntoClient
149+
@_transparent
150+
internal var _protectedMutableAddress: UnsafeMutablePointer<Element> {
151+
mutating get {
152+
#if $AddressOfProperty
153+
unsafe UnsafeMutablePointer<Element>(Builtin.addressof(&_storage))
154+
#else
155+
unsafe UnsafeMutablePointer<Element>(Builtin.addressof(&self))
156+
#endif
157+
}
158+
}
159+
160+
/// Returns a mutable buffer pointer over the entire array while performing
161+
/// stack checking.
162+
///
163+
/// Use this when the value of the pointer could potentially be directly used
164+
/// by users (e.g. through the use of span or the unchecked subscript).
165+
@available(SwiftStdlib 6.2, *)
166+
@_alwaysEmitIntoClient
167+
@_transparent
168+
internal var _protectedMutableBuffer: UnsafeMutableBufferPointer<Element> {
169+
mutating get {
170+
unsafe UnsafeMutableBufferPointer<Element>(
171+
start: _protectedMutableAddress,
172+
count: count
173+
)
174+
}
175+
}
176+
114177
/// Converts the given raw pointer, which points at an uninitialized array
115178
/// instance, to a mutable buffer suitable for initialization.
116179
@available(SwiftStdlib 6.2, *)
@@ -415,12 +478,12 @@ extension InlineArray where Element: ~Copyable {
415478
public subscript(unchecked i: Index) -> Element {
416479
@_transparent
417480
unsafeAddress {
418-
unsafe _address + i
481+
unsafe _protectedAddress + i
419482
}
420483

421484
@_transparent
422485
unsafeMutableAddress {
423-
unsafe _mutableAddress + i
486+
unsafe _protectedMutableAddress + i
424487
}
425488
}
426489
}
@@ -467,53 +530,28 @@ extension InlineArray where Element: ~Copyable {
467530

468531
@available(SwiftStdlib 6.2, *)
469532
extension InlineArray where Element: ~Copyable {
470-
471533
@available(SwiftStdlib 6.2, *)
534+
@_alwaysEmitIntoClient
472535
public var span: Span<Element> {
473536
@lifetime(borrow self)
474-
@_alwaysEmitIntoClient
537+
@_transparent
475538
borrowing get {
476-
let pointer = unsafe _address
477-
let span = unsafe Span(_unsafeStart: pointer, count: count)
539+
let span = unsafe Span(_unsafeStart: _protectedAddress, count: count)
478540
return unsafe _overrideLifetime(span, borrowing: self)
479541
}
480542
}
481543

482544
@available(SwiftStdlib 6.2, *)
545+
@_alwaysEmitIntoClient
483546
public var mutableSpan: MutableSpan<Element> {
484547
@lifetime(&self)
485-
@_alwaysEmitIntoClient
548+
@_transparent
486549
mutating get {
487-
let pointer = unsafe _mutableAddress
488-
let span = unsafe MutableSpan(_unsafeStart: pointer, count: count)
550+
let span = unsafe MutableSpan(
551+
_unsafeStart: _protectedMutableAddress,
552+
count: count
553+
)
489554
return unsafe _overrideLifetime(span, mutating: &self)
490555
}
491556
}
492557
}
493-
494-
//===----------------------------------------------------------------------===//
495-
// MARK: - Unsafe APIs
496-
//===----------------------------------------------------------------------===//
497-
498-
@available(SwiftStdlib 6.2, *)
499-
extension InlineArray where Element: ~Copyable {
500-
// FIXME: @available(*, deprecated, renamed: "span.withUnsafeBufferPointer(_:)")
501-
@available(SwiftStdlib 6.2, *)
502-
@_alwaysEmitIntoClient
503-
@_transparent
504-
public borrowing func _withUnsafeBufferPointer<Result: ~Copyable, E: Error>(
505-
_ body: (UnsafeBufferPointer<Element>) throws(E) -> Result
506-
) throws(E) -> Result {
507-
try unsafe body(_buffer)
508-
}
509-
510-
// FIXME: @available(*, deprecated, renamed: "mutableSpan.withUnsafeMutableBufferPointer(_:)")
511-
@available(SwiftStdlib 6.2, *)
512-
@_alwaysEmitIntoClient
513-
@_transparent
514-
public mutating func _withUnsafeMutableBufferPointer<Result: ~Copyable, E: Error>(
515-
_ body: (UnsafeMutableBufferPointer<Element>) throws(E) -> Result
516-
) throws(E) -> Result {
517-
try unsafe body(_mutableBuffer)
518-
}
519-
}

test/abi/macOS/arm64/stdlib.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,6 @@ Added: _$sSs8UTF8ViewV4spans4SpanVys5UInt8VGvg
932932
Added: _$sSa11mutableSpans07MutableB0VyxGvr
933933
Added: _$ss10ArraySliceV11mutableSpans07MutableD0VyxGvr
934934
Added: _$ss15ContiguousArrayV11mutableSpans07MutableD0VyxGvr
935-
Added: _$ss11InlineArrayVsRi__rlE11mutableSpans07MutableD0Vyq_Gvr
936935
Added: _$ss15CollectionOfOneV11mutableSpans07MutableE0VyxGvr
937936
Added: _$sSrsRi_zrlE11mutableSpans07MutableB0VyxGvr
938937
Added: _$sSw12mutableBytess14MutableRawSpanVvr
@@ -1090,3 +1089,8 @@ Added: _$ss8UTF8SpanV9_asciiBits6UInt64VvpZMV
10901089

10911090
// printing foreign reference types requires a new displayStyle: .foreign
10921091
Added: _$ss6MirrorV12DisplayStyleO16foreignReferenceyA2DmFWC
1092+
1093+
// var InlineArray._protectedBuffer
1094+
// var InlineArray._protectedAddress
1095+
Added: _$ss11InlineArrayVsRi__rlE16_protectedBufferSRyq_GvpMV
1096+
Added: _$ss11InlineArrayVsRi__rlE17_protectedAddressSPyq_GvpMV

test/abi/macOS/x86_64/stdlib.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,6 @@ Added: _$sSs8UTF8ViewV4spans4SpanVys5UInt8VGvg
933933
Added: _$sSa11mutableSpans07MutableB0VyxGvr
934934
Added: _$ss10ArraySliceV11mutableSpans07MutableD0VyxGvr
935935
Added: _$ss15ContiguousArrayV11mutableSpans07MutableD0VyxGvr
936-
Added: _$ss11InlineArrayVsRi__rlE11mutableSpans07MutableD0Vyq_Gvr
937936
Added: _$ss15CollectionOfOneV11mutableSpans07MutableE0VyxGvr
938937
Added: _$sSrsRi_zrlE11mutableSpans07MutableB0VyxGvr
939938
Added: _$sSw12mutableBytess14MutableRawSpanVvr
@@ -1090,3 +1089,8 @@ Added: _$ss8UTF8SpanV9_asciiBits6UInt64VvpZMV
10901089

10911090
// printing foreign reference types requires a new displayStyle: .foreign
10921091
Added: _$ss6MirrorV12DisplayStyleO16foreignReferenceyA2DmFWC
1092+
1093+
// var InlineArray._protectedBuffer
1094+
// var InlineArray._protectedAddress
1095+
Added: _$ss11InlineArrayVsRi__rlE16_protectedBufferSRyq_GvpMV
1096+
Added: _$ss11InlineArrayVsRi__rlE17_protectedAddressSPyq_GvpMV

0 commit comments

Comments
 (0)