Skip to content

Adopt @abi in the standard library #79937

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 8 additions & 6 deletions docs/ReferenceGuides/UnderscoredAttributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -1131,15 +1131,17 @@ ways to misuse it:
be reliably recovered through C interfaces like `dlsym`. If you want to
implement a plugin-style interface, use `Bundle`/`NSBundle` if available, or
export your plugin entry points as C entry points using `@_cdecl`.

- Don't use `@_silgen_name` when you need to make a change to an ABI-stable
declaration's signature that would normally alter its mangled name, but you
need to preserve the old mangled name for ABI compatibility. We used to use it
for this task, but `@abi` can do it with fewer limitations, more safety, and
better readability. If for some reason you do need `@_silgen_name`, you will
need to be careful that the change doesn't materially affect the actual
calling convention of the function in an incompatible way.

Legitimate uses may include:

- Use `@_silgen_name` if you're implementing the Swift runtime.
- Use `@_silgen_name` if you need to make a change to an ABI-stable
declaration's signature that would normally alter its mangled name, but you
need to preserve the old mangled name for ABI compatibility. You will need
to be careful that the change doesn't materially affect the actual calling
convention of the function in an incompatible way.
- Use `@_silgen_name` if certain declarations need to have predictable symbol
names, such as to be easily referenced by linker scripts or other highly
customized build environments (and it's OK for those predictable symbols to
Expand Down
2 changes: 2 additions & 0 deletions docs/StandardLibraryProgrammersManual.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ This attribute specifies the name that a declaration will have at link time. It
1. To specify the symbol name of a Swift function so that it can be called from Swift-aware C. Such functions have bodies.
2. To provide a Swift declaration which really represents a C declaration. Such functions do not have bodies.

In the past it was sometimes used for ABI backwards compatibility hacks, but `@abi` does this job better.

##### Using `@_silgen_name` to call Swift from Swift-aware C

Rather than hard-code Swift mangling into C code, `@_silgen_name` is used to provide a stable and known symbol name for linking. Note that C code still must understand and use the Swift calling convention (available in swift-clang) for such Swift functions (if they use Swift's CC). Example:
Expand Down
1 change: 1 addition & 0 deletions stdlib/cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ function(_compile_swift_files
list(APPEND swift_flags "-enable-experimental-feature" "LifetimeDependence")
list(APPEND swift_flags "-enable-experimental-feature" "InoutLifetimeDependence")
list(APPEND swift_flags "-enable-experimental-feature" "LifetimeDependenceMutableAccessors")
list(APPEND swift_flags "-enable-experimental-feature" "ABIAttribute")

list(APPEND swift_flags "-enable-upcoming-feature" "MemberImportVisibility")

Expand Down
7 changes: 6 additions & 1 deletion stdlib/public/Concurrency/AsyncStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,12 @@ public struct AsyncStream<Element> {
/// }
///
///
@_silgen_name("$sScS9unfolding8onCancelScSyxGxSgyYac_yyYbcSgtcfC")
@abi(
init(
unfolding produce: @escaping /* not @Sendable*/ () async -> Element?,
onCancel: (@Sendable () -> Void)?
)
)
@preconcurrency // Original API had `@Sendable` only on `onCancel`
public init(
unfolding produce: @escaping @Sendable () async -> Element?,
Expand Down
14 changes: 12 additions & 2 deletions stdlib/public/Concurrency/CheckedContinuation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,14 @@ public func withCheckedContinuation<T>(
//
// This function also doubles as an ABI-compatibility shim predating the
// introduction of #isolation.
@abi(
func withCheckedContinuation<T>(
function: String,
_ body: (CheckedContinuation<T, Never>) -> Void
) async -> T
)
@available(SwiftStdlib 5.1, *)
@_unsafeInheritExecutor // ABI compatibility with Swift 5.1
@_silgen_name("$ss23withCheckedContinuation8function_xSS_yScCyxs5NeverOGXEtYalF")
public func _unsafeInheritExecutor_withCheckedContinuation<T>(
function: String = #function,
_ body: (CheckedContinuation<T, Never>) -> Void
Expand Down Expand Up @@ -377,9 +382,14 @@ public func withCheckedThrowingContinuation<T>(
//
// This function also doubles as an ABI-compatibility shim predating the
// introduction of #isolation.
@abi(
func withCheckedThrowingContinuation<T>(
function: String,
_ body: (CheckedContinuation<T, Error>) -> Void
) async throws -> T
)
@available(SwiftStdlib 5.1, *)
@_unsafeInheritExecutor // ABI compatibility with Swift 5.1
@_silgen_name("$ss31withCheckedThrowingContinuation8function_xSS_yScCyxs5Error_pGXEtYaKlF")
public func _unsafeInheritExecutor_withCheckedThrowingContinuation<T>(
function: String = #function,
_ body: (CheckedContinuation<T, Error>) -> Void
Expand Down
6 changes: 5 additions & 1 deletion stdlib/public/Concurrency/Clock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ extension Clock {
//
// This function also doubles as an ABI-compatibility shim predating the
// introduction of #isolation.
@abi(
func measure(
_ work: () async throws -> Void
) async rethrows -> Instant.Duration
)
@available(SwiftStdlib 5.7, *)
@_silgen_name("$ss5ClockPsE7measurey8DurationQzyyYaKXEYaKF")
@_unsafeInheritExecutor // for ABI compatibility
public func _unsafeInheritExecutor_measure(
_ work: () async throws -> Void
Expand Down
14 changes: 12 additions & 2 deletions stdlib/public/Concurrency/DiscardingTaskGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,14 @@ public func withDiscardingTaskGroup<GroupResult>(
//
// This function also doubles as an ABI-compatibility shim predating the
// introduction of #isolation.
@abi(
func withDiscardingTaskGroup<GroupResult>(
returning returnType: GroupResult.Type,
body: (inout DiscardingTaskGroup) async -> GroupResult
) async -> GroupResult
)
@available(SwiftStdlib 5.9, *)
@_unsafeInheritExecutor // for ABI compatibility
@_silgen_name("$ss23withDiscardingTaskGroup9returning4bodyxxm_xs0bcD0VzYaXEtYalF")
public func _unsafeInheritExecutor_withDiscardingTaskGroup<GroupResult>(
returning returnType: GroupResult.Type = GroupResult.self,
body: (inout DiscardingTaskGroup) async -> GroupResult
Expand Down Expand Up @@ -367,9 +372,14 @@ public func withThrowingDiscardingTaskGroup<GroupResult>(
return result
}

@abi(
func withThrowingDiscardingTaskGroup<GroupResult>(
returning returnType: GroupResult.Type,
body: (inout ThrowingDiscardingTaskGroup<Error>) async throws -> GroupResult
) async throws -> GroupResult
)
@available(SwiftStdlib 5.9, *)
@_unsafeInheritExecutor // for ABI compatibility
@_silgen_name("$ss31withThrowingDiscardingTaskGroup9returning4bodyxxm_xs0bcdE0Vys5Error_pGzYaKXEtYaKlF")
public func _unsafeInheritExecutor_withThrowingDiscardingTaskGroup<GroupResult>(
returning returnType: GroupResult.Type = GroupResult.self,
body: (inout ThrowingDiscardingTaskGroup<Error>) async throws -> GroupResult
Expand Down
9 changes: 7 additions & 2 deletions stdlib/public/Concurrency/ExecutorAssertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,16 @@ extension Actor {
}
}

@abi(
nonisolated func assumeIsolated<T /* not ': Sendable' */>(
_ operation: (isolated Self) throws -> T,
file: StaticString, line: UInt
) rethrows -> T
)
@available(SwiftStdlib 5.9, *)
@usableFromInline
@_unavailableInEmbedded
@_silgen_name("$sScAsE14assumeIsolated_4file4lineqd__qd__xYiKXE_s12StaticStringVSutKlF")
internal nonisolated func __abi__assumeIsolated<T : Sendable>(
internal nonisolated func __nonsendable_assumeIsolated<T : Sendable>(
_ operation: (isolated Self) throws -> T,
_ file: StaticString, _ line: UInt
) rethrows -> T {
Expand Down
9 changes: 7 additions & 2 deletions stdlib/public/Concurrency/MainActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,15 @@ extension MainActor {
}
}

@abi(
static func assumeIsolated<T /* not ': Sendable' */>(
_ operation: @MainActor () throws -> T,
file: StaticString, line: UInt
) rethrows -> T
)
@available(SwiftStdlib 5.9, *)
@usableFromInline
@_silgen_name("$sScM14assumeIsolated_4file4linexxyKScMYcXE_s12StaticStringVSutKlFZ")
internal static func __abi__assumeIsolated<T : Sendable>(
internal static func __nonsendable_assumeIsolated<T : Sendable>(
_ operation: @MainActor () throws -> T,
_ file: StaticString, _ line: UInt
) rethrows -> T {
Expand Down
7 changes: 6 additions & 1 deletion stdlib/public/Concurrency/Task+TaskExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,15 @@ public func withTaskExecutorPreference<T, Failure>(
//
// This function also doubles as an ABI-compatibility shim predating the
// introduction of #isolation.
@abi(
func withTaskExecutorPreference<T: Sendable>(
_ taskExecutor: (any TaskExecutor)?,
operation: @Sendable () async throws -> T
) async rethrows -> T
)
@_unavailableInEmbedded
@available(SwiftStdlib 6.0, *)
@_unsafeInheritExecutor // for ABI compatibility
@_silgen_name("$ss26withTaskExecutorPreference_9operationxSch_pSg_xyYaYbKXEtYaKs8SendableRzlF")
public func _unsafeInheritExecutor_withTaskExecutorPreference<T: Sendable>(
_ taskExecutor: (any TaskExecutor)?,
operation: @Sendable () async throws -> T
Expand Down
7 changes: 6 additions & 1 deletion stdlib/public/Concurrency/TaskCancellation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,14 @@ public func withTaskCancellationHandler<T>(
//
// This function also doubles as an ABI-compatibility shim predating the
// introduction of #isolation.
@abi(
func withTaskCancellationHandler<T>(
operation: () async throws -> T,
onCancel handler: @Sendable () -> Void
) async rethrows -> T
)
@_unsafeInheritExecutor // ABI compatibility with Swift 5.1
@available(SwiftStdlib 5.1, *)
@_silgen_name("$ss27withTaskCancellationHandler9operation8onCancelxxyYaKXE_yyYbXEtYaKlF")
public func _unsafeInheritExecutor_withTaskCancellationHandler<T>(
operation: () async throws -> T,
onCancel handler: @Sendable () -> Void
Expand Down
20 changes: 17 additions & 3 deletions stdlib/public/Concurrency/TaskGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,15 @@ public func withTaskGroup<ChildTaskResult, GroupResult>(
//
// This function also doubles as an ABI-compatibility shim predating the
// introduction of #isolation.
@abi(
@preconcurrency // TaskGroup.ChildTaskResult: Sendable
func withTaskGroup<ChildTaskResult, GroupResult>(
of childTaskResultType: ChildTaskResult.Type,
returning returnType: GroupResult.Type,
body: (inout TaskGroup<ChildTaskResult>) async -> GroupResult
) async -> GroupResult
)
@available(SwiftStdlib 5.1, *)
@_silgen_name("$ss13withTaskGroup2of9returning4bodyq_xm_q_mq_ScGyxGzYaXEtYar0_lF")
@_unsafeInheritExecutor // for ABI compatibility
@inlinable
public func _unsafeInheritExecutor_withTaskGroup<ChildTaskResult, GroupResult>(
Expand Down Expand Up @@ -243,8 +250,15 @@ public func withThrowingTaskGroup<ChildTaskResult, GroupResult>(
//
// This function also doubles as an ABI-compatibility shim predating the
// introduction of #isolation.
@abi(
@preconcurrency // ThrowingTaskGroup.ChildTaskResult: Sendable
func withThrowingTaskGroup<ChildTaskResult, GroupResult>(
of childTaskResultType: ChildTaskResult.Type,
returning returnType: GroupResult.Type,
body: (inout ThrowingTaskGroup<ChildTaskResult, Error>) async throws -> GroupResult
) async rethrows -> GroupResult
)
@available(SwiftStdlib 5.1, *)
@_silgen_name("$ss21withThrowingTaskGroup2of9returning4bodyq_xm_q_mq_Scgyxs5Error_pGzYaKXEtYaKr0_lF")
@_unsafeInheritExecutor // for ABI compatibility
public func _unsafeInheritExecutor_withThrowingTaskGroup<ChildTaskResult, GroupResult>(
of childTaskResultType: ChildTaskResult.Type,
Expand Down Expand Up @@ -690,7 +704,7 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
return try await _taskGroupWaitNext(group: _group)
}

@_silgen_name("$sScg10nextResults0B0Oyxq_GSgyYaKF")
@abi(mutating func nextResult() async throws -> Result<ChildTaskResult, Failure>?)
@usableFromInline
mutating func nextResultForABI() async throws -> Result<ChildTaskResult, Failure>? {
do {
Expand Down
16 changes: 14 additions & 2 deletions stdlib/public/Concurrency/TaskLocal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,16 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
//
// This function also doubles as an ABI-compatibility shim predating the
// introduction of #isolation.
@abi(
func withValue<R>(
_ valueDuringOperation: Value,
operation: () async throws -> R,
file: String, line: UInt
) async rethrows -> R
)
@discardableResult
@_unsafeInheritExecutor // ABI compatibility with Swift 5.1
@available(SwiftStdlib 5.1, *)
@_silgen_name("$ss9TaskLocalC9withValue_9operation4file4lineqd__x_qd__yYaKXESSSutYaKlF")
public func _unsafeInheritExecutor_withValue<R>(
_ valueDuringOperation: Value,
operation: () async throws -> R,
Expand Down Expand Up @@ -265,7 +271,13 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
return try await operation()
}

@_silgen_name("$ss9TaskLocalC13withValueImpl_9operation4file4lineqd__xn_qd__yYaKXESSSutYaKlF")
@abi(
func withValueImpl<R>(
_ valueDuringOperation: __owned Value,
operation: () async throws -> R,
file: String, line: UInt
) async rethrows -> R
)
@inlinable
@discardableResult
@_unsafeInheritExecutor // internal for backwards compatibility; though may be able to be removed safely?
Expand Down
27 changes: 20 additions & 7 deletions stdlib/public/Distributed/DistributedActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,15 @@ extension DistributedActor {
/// state.
///
/// When the actor is remote, the closure won't be executed and this function will return nil.
@abi(
// We need to @abi here because the signature is the same as
// `__separately_compiled_typed_throws_whenLocal(_:)`, and even though this
// is @AEIC, the symbol name would conflict.
nonisolated func __typed_throws_whenLocal<T: Sendable, E>(
_ body: @Sendable (isolated Self) async throws(E) -> T
) async throws(E) -> T?
)
@_alwaysEmitIntoClient
// we need to silgen_name here because the signature is the same as __abi_whenLocal,
// and even though this is @AEIC, the symbol name would conflict.
@_silgen_name("$s11Distributed0A5ActorPAAE20whenLocalTypedThrowsyqd__Sgqd__xYiYaYbqd_0_YKXEYaqd_0_YKs8SendableRd__s5ErrorRd_0_r0_lF")
public nonisolated func whenLocal<T: Sendable, E>(
_ body: @Sendable (isolated Self) async throws(E) -> T
) async throws(E) -> T? {
Expand All @@ -380,18 +385,26 @@ extension DistributedActor {
// ABI: This is a workaround when in Swift 6 this method was introduced
// in order to support typed-throws, but missed to add @_aeic.
// In practice, this method should not ever be used by anyone, ever.
@abi(
nonisolated func whenLocal<T: Sendable, E>(
_ body: @Sendable (isolated Self) async throws(E) -> T
) async throws(E) -> T?
)
@usableFromInline
@_silgen_name("$s11Distributed0A5ActorPAAE9whenLocalyqd__Sgqd__xYiYaYbqd_0_YKXEYaqd_0_YKs8SendableRd__s5ErrorRd_0_r0_lF")
internal nonisolated func __abi_whenLocal<T: Sendable, E>(
nonisolated func __separately_compiled_typed_throws_whenLocal<T: Sendable, E>(
_ body: @Sendable (isolated Self) async throws(E) -> T
) async throws(E) -> T? {
try await whenLocal(body)
}

// ABI: Historical whenLocal, rethrows was changed to typed throws `throws(E)`
@_silgen_name("$s11Distributed0A5ActorPAAE9whenLocalyqd__Sgqd__xYiYaYbKXEYaKs8SendableRd__lF")
@abi(
nonisolated func whenLocal<T: Sendable>(
_ body: @Sendable (isolated Self) async throws -> T
) async rethrows -> T?
)
@usableFromInline
nonisolated func __abi_whenLocal<T: Sendable>(
nonisolated func __rethrows_whenLocal<T: Sendable>(
_ body: @Sendable (isolated Self) async throws -> T
) async rethrows -> T? {
try await whenLocal(body)
Expand Down
9 changes: 7 additions & 2 deletions stdlib/public/Distributed/DistributedAssertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,15 @@ extension DistributedActor {
}
}

@abi(
nonisolated func assumeIsolated<T /* not ': Sendable' */>(
_ operation: (isolated Self) throws -> T,
file: StaticString, line: UInt
) rethrows -> T
)
@available(SwiftStdlib 5.9, *)
@usableFromInline
@_silgen_name("$s11Distributed0A5ActorPAAE14assumeIsolated_4file4lineqd__qd__xYiKXE_s12StaticStringVSutKlF")
internal nonisolated func __abi__assumeIsolated<T : Sendable>(
internal nonisolated func __nonsendable_assumeIsolated<T : Sendable>(
_ operation: (isolated Self) throws -> T,
_ file: StaticString, _ line: UInt
) rethrows -> T {
Expand Down
8 changes: 6 additions & 2 deletions stdlib/public/core/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1634,12 +1634,16 @@ extension Array {

// Superseded by the typed-throws version of this function, but retained
// for ABI reasons.
@abi(
mutating func withUnsafeMutableBufferPointer<R>(
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
) rethrows -> R
)
@_semantics("array.withUnsafeMutableBufferPointer")
@_effects(notEscaping self.value**)
@usableFromInline
@inline(__always)
@_silgen_name("$sSa30withUnsafeMutableBufferPointeryqd__qd__SryxGzKXEKlF")
mutating func __abi_withUnsafeMutableBufferPointer<R>(
mutating func __rethrows_withUnsafeMutableBufferPointer<R>(
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
) rethrows -> R {
_makeMutableAndUnique()
Expand Down
16 changes: 12 additions & 4 deletions stdlib/public/core/ArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -649,9 +649,13 @@ extension _ArrayBuffer {
/// created on-demand.
// Superseded by the typed-throws version of this function, but retained
// for ABI reasons.
@abi(
func withUnsafeBufferPointer<R>(
_ body: (UnsafeBufferPointer<Element>) throws -> R
) rethrows -> R
)
@usableFromInline
@_silgen_name("$ss12_ArrayBufferV010withUnsafeB7Pointeryqd__qd__SRyxGKXEKlF")
internal func __abi_withUnsafeBufferPointer<R>(
internal func __rethrows_withUnsafeBufferPointer<R>(
_ body: (UnsafeBufferPointer<Element>) throws -> R
) rethrows -> R {
if _fastPath(_isNative) {
Expand Down Expand Up @@ -679,9 +683,13 @@ extension _ArrayBuffer {

// Superseded by the typed-throws version of this function, but retained
// for ABI reasons.
@abi(
mutating func withUnsafeMutableBufferPointer<R>(
_ body: (UnsafeMutableBufferPointer<Element>) throws -> R
) rethrows -> R
)
@usableFromInline
@_silgen_name("$ss12_ArrayBufferV017withUnsafeMutableB7Pointeryqd__qd__SryxGKXEKlF")
internal mutating func __abi_withUnsafeMutableBufferPointer<R>(
internal mutating func __rethrows_withUnsafeMutableBufferPointer<R>(
_ body: (UnsafeMutableBufferPointer<Element>) throws -> R
) rethrows -> R {
return try unsafe withUnsafeMutableBufferPointer(body)
Expand Down
Loading