Skip to content

[stdlib] Remove _withUnsafeBufferPointer APIs on InlineArray #80858

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 5 commits into from
Jun 26, 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
110 changes: 74 additions & 36 deletions stdlib/public/core/InlineArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,34 @@ extension InlineArray where Element: ~Copyable {
unsafe UnsafeBufferPointer<Element>(start: _address, count: count)
}

/// Returns a pointer to the first element in the array while performing stack
/// checking.
///
/// Use this when the value of the pointer could potentially be directly used
/// by users (e.g. through the use of span or the unchecked subscript).
@available(SwiftStdlib 6.2, *)
@_alwaysEmitIntoClient
@_transparent
internal var _protectedAddress: UnsafePointer<Element> {
#if $AddressOfProperty
unsafe UnsafePointer<Element>(Builtin.addressOfBorrow(_storage))
#else
unsafe UnsafePointer<Element>(Builtin.addressOfBorrow(self))
#endif
}

/// Returns a buffer pointer over the entire array while performing stack
/// checking.
///
/// Use this when the value of the pointer could potentially be directly used
/// by users (e.g. through the use of span or the unchecked subscript).
@available(SwiftStdlib 6.2, *)
@_alwaysEmitIntoClient
@_transparent
internal var _protectedBuffer: UnsafeBufferPointer<Element> {
unsafe UnsafeBufferPointer<Element>(start: _protectedAddress, count: count)
}

/// Returns a mutable pointer to the first element in the array.
@available(SwiftStdlib 6.2, *)
@_alwaysEmitIntoClient
Expand Down Expand Up @@ -111,6 +139,41 @@ extension InlineArray where Element: ~Copyable {
}
}

/// Returns a mutable pointer to the first element in the array while
/// performing stack checking.
///
/// Use this when the value of the pointer could potentially be directly used
/// by users (e.g. through the use of span or the unchecked subscript).
@available(SwiftStdlib 6.2, *)
@_alwaysEmitIntoClient
@_transparent
internal var _protectedMutableAddress: UnsafeMutablePointer<Element> {
mutating get {
#if $AddressOfProperty
unsafe UnsafeMutablePointer<Element>(Builtin.addressof(&_storage))
#else
unsafe UnsafeMutablePointer<Element>(Builtin.addressof(&self))
#endif
}
}

/// Returns a mutable buffer pointer over the entire array while performing
/// stack checking.
///
/// Use this when the value of the pointer could potentially be directly used
/// by users (e.g. through the use of span or the unchecked subscript).
@available(SwiftStdlib 6.2, *)
@_alwaysEmitIntoClient
@_transparent
internal var _protectedMutableBuffer: UnsafeMutableBufferPointer<Element> {
mutating get {
unsafe UnsafeMutableBufferPointer<Element>(
start: _protectedMutableAddress,
count: count
)
}
}

/// Converts the given raw pointer, which points at an uninitialized array
/// instance, to a mutable buffer suitable for initialization.
@available(SwiftStdlib 6.2, *)
Expand Down Expand Up @@ -415,12 +478,12 @@ extension InlineArray where Element: ~Copyable {
public subscript(unchecked i: Index) -> Element {
@_transparent
unsafeAddress {
unsafe _address + i
unsafe _protectedAddress + i
}

@_transparent
unsafeMutableAddress {
unsafe _mutableAddress + i
unsafe _protectedMutableAddress + i
}
}
}
Expand Down Expand Up @@ -467,53 +530,28 @@ extension InlineArray where Element: ~Copyable {

@available(SwiftStdlib 6.2, *)
extension InlineArray where Element: ~Copyable {

@available(SwiftStdlib 6.2, *)
@_alwaysEmitIntoClient
public var span: Span<Element> {
@lifetime(borrow self)
@_alwaysEmitIntoClient
@_transparent
borrowing get {
let pointer = unsafe _address
let span = unsafe Span(_unsafeStart: pointer, count: count)
let span = unsafe Span(_unsafeStart: _protectedAddress, count: count)
return unsafe _overrideLifetime(span, borrowing: self)
}
}

@available(SwiftStdlib 6.2, *)
@_alwaysEmitIntoClient
public var mutableSpan: MutableSpan<Element> {
@lifetime(&self)
@_alwaysEmitIntoClient
@_transparent
mutating get {
let pointer = unsafe _mutableAddress
let span = unsafe MutableSpan(_unsafeStart: pointer, count: count)
let span = unsafe MutableSpan(
_unsafeStart: _protectedMutableAddress,
count: count
)
return unsafe _overrideLifetime(span, mutating: &self)
}
}
}

//===----------------------------------------------------------------------===//
// MARK: - Unsafe APIs
//===----------------------------------------------------------------------===//

@available(SwiftStdlib 6.2, *)
extension InlineArray where Element: ~Copyable {
// FIXME: @available(*, deprecated, renamed: "span.withUnsafeBufferPointer(_:)")
@available(SwiftStdlib 6.2, *)
@_alwaysEmitIntoClient
@_transparent
public borrowing func _withUnsafeBufferPointer<Result: ~Copyable, E: Error>(
_ body: (UnsafeBufferPointer<Element>) throws(E) -> Result
) throws(E) -> Result {
try unsafe body(_buffer)
}

// FIXME: @available(*, deprecated, renamed: "mutableSpan.withUnsafeMutableBufferPointer(_:)")
@available(SwiftStdlib 6.2, *)
@_alwaysEmitIntoClient
@_transparent
public mutating func _withUnsafeMutableBufferPointer<Result: ~Copyable, E: Error>(
_ body: (UnsafeMutableBufferPointer<Element>) throws(E) -> Result
) throws(E) -> Result {
try unsafe body(_mutableBuffer)
}
}
6 changes: 5 additions & 1 deletion test/abi/macOS/arm64/stdlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,6 @@ Added: _$sSs8UTF8ViewV4spans4SpanVys5UInt8VGvg
Added: _$sSa11mutableSpans07MutableB0VyxGvr
Added: _$ss10ArraySliceV11mutableSpans07MutableD0VyxGvr
Added: _$ss15ContiguousArrayV11mutableSpans07MutableD0VyxGvr
Added: _$ss11InlineArrayVsRi__rlE11mutableSpans07MutableD0Vyq_Gvr
Added: _$ss15CollectionOfOneV11mutableSpans07MutableE0VyxGvr
Added: _$sSrsRi_zrlE11mutableSpans07MutableB0VyxGvr
Added: _$sSw12mutableBytess14MutableRawSpanVvr
Expand Down Expand Up @@ -1090,3 +1089,8 @@ Added: _$ss8UTF8SpanV9_asciiBits6UInt64VvpZMV

// printing foreign reference types requires a new displayStyle: .foreign
Added: _$ss6MirrorV12DisplayStyleO16foreignReferenceyA2DmFWC

// var InlineArray._protectedBuffer
// var InlineArray._protectedAddress
Added: _$ss11InlineArrayVsRi__rlE16_protectedBufferSRyq_GvpMV
Added: _$ss11InlineArrayVsRi__rlE17_protectedAddressSPyq_GvpMV
6 changes: 5 additions & 1 deletion test/abi/macOS/x86_64/stdlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,6 @@ Added: _$sSs8UTF8ViewV4spans4SpanVys5UInt8VGvg
Added: _$sSa11mutableSpans07MutableB0VyxGvr
Added: _$ss10ArraySliceV11mutableSpans07MutableD0VyxGvr
Added: _$ss15ContiguousArrayV11mutableSpans07MutableD0VyxGvr
Added: _$ss11InlineArrayVsRi__rlE11mutableSpans07MutableD0Vyq_Gvr
Added: _$ss15CollectionOfOneV11mutableSpans07MutableE0VyxGvr
Added: _$sSrsRi_zrlE11mutableSpans07MutableB0VyxGvr
Added: _$sSw12mutableBytess14MutableRawSpanVvr
Expand Down Expand Up @@ -1090,3 +1089,8 @@ Added: _$ss8UTF8SpanV9_asciiBits6UInt64VvpZMV

// printing foreign reference types requires a new displayStyle: .foreign
Added: _$ss6MirrorV12DisplayStyleO16foreignReferenceyA2DmFWC

// var InlineArray._protectedBuffer
// var InlineArray._protectedAddress
Added: _$ss11InlineArrayVsRi__rlE16_protectedBufferSRyq_GvpMV
Added: _$ss11InlineArrayVsRi__rlE17_protectedAddressSPyq_GvpMV