Repository: apple/swift
File: stdlib/public/Concurrency/CheckedContinuation.swift (Swift)

Code before:
  //
  // We need to keep around the isolated parameter ABI for compatibility though.
  return try await Builtin.withUnsafeThrowingContinuation {
    let unsafeContinuation = unsafe UnsafeContinuation<T, Error>($0)
    return body(unsafe CheckedContinuation(continuation: unsafeContinuation,
                                           function: function))
  }
}

// Note: hack to stage out @_unsafeInheritExecutor forms of various functions
// in favor of #isolation. The _unsafeInheritExecutor_ prefix is meaningful
// to the type checker.
//
// This function also doubles as an ABI-compatibility shim predating the
// introduction of #isolation.
//@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
//) async throws -> T {
//  return try await unsafe withUnsafeThrowingContinuation {
//    body(unsafe CheckedContinuation(continuation: $0, function: function))
//  }
//}

#if _runtime(_ObjC)

// Intrinsics used by SILGen to create, resume, or fail checked continuations.
@available(SwiftStdlib 5.1, *)
@_alwaysEmitIntoClient
internal func _createCheckedContinuation<T>(
  _ continuation: __owned UnsafeContinuation<T, Never>
) -> CheckedContinuation<T, Never> {
  return unsafe CheckedContinuation(continuation: continuation)
}

@available(SwiftStdlib 5.1, *)
@_alwaysEmitIntoClient
internal func _createCheckedThrowingContinuation<T>(
  _ continuation: __owned UnsafeContinuation<T, Error>
) -> CheckedContinuation<T, Error> {
  return unsafe CheckedContinuation(continuation: continuation)
}

@available(SwiftStdlib 5.1, *)
@_alwaysEmitIntoClient
internal func _resumeCheckedContinuation<T>(
  _ continuation: CheckedContinuation<T, Never>,
  _ value: sending T

Review comment (refactor):
FIXME: We need to keep this but we'd get duplicated symbols; should we relax the symbol duplication checking to be aware of always-emit-into-client funcs?

Code after:
/// indefinitely which will result in the task "hanging" as well as being leaked with
/// no possibility to destroy it.
///
/// The checked continuation offers detection of misuse, and dropping the last reference
/// to it, without having resumed it will trigger a warning. Resuming a continuation twice
/// is also diagnosed and will cause a crash.
///
/// - Parameters:
///   - function: A string identifying the declaration that is the notional
///     source for the continuation, used to identify the continuation in
///     runtime diagnostics related to misuse of this continuation.
///   - body: A closure that takes a `CheckedContinuation` parameter.
/// - Returns: The value continuation is resumed with.
///
/// - SeeAlso: `withCheckedContinuation(function:_:)`
/// - SeeAlso: `withUnsafeContinuation(function:_:)`
/// - SeeAlso: `withUnsafeThrowingContinuation(function:_:)`
@inlinable
@_alwaysEmitIntoClient
@available(SwiftStdlib 5.1, *)
// ABI Note: We need to use @abi here because the ABI of this function otherwise conflicts with the legacy
// @_unsafeInheritExecutor declaration, as none of them have (or mangle) the implicit actor parameter
@abi(
  nonisolated(nonsending) func withCheckedThrowingContinuationNonisolatedNonsending<T, E>(
    function: String,
    _ body: (CheckedContinuation<T, E>) -> Void
  ) async throws(E) -> sending T
)
public nonisolated(nonsending) func withCheckedThrowingContinuation<T, E>(
  function: String = #function,
  _ body: (CheckedContinuation<T, E>) -> Void
) async throws(E) -> sending T {
  do {
    return try await Builtin.withUnsafeThrowingContinuation {
      let unsafeContinuation = unsafe UnsafeContinuation<T, E>($0)
      return body(unsafe CheckedContinuation(continuation: unsafeContinuation,
                                             function: function))
    }
  } catch {
    throw error as! E
  }
}

@inlinable
@_alwaysEmitIntoClient
@available(SwiftStdlib 5.1, *)
// ABI Note: We need to use @abi here because the ABI of this function otherwise conflicts with the legacy
// @_unsafeInheritExecutor declaration, as none of them have (or mangle) the implicit actor parameter
@abi(
  nonisolated(nonsending) func withCheckedThrowingContinuationNonisolatedNonsending<T>(
    function: String,