Skip to content

Adopt typed throws in one withUnsafePointer() API #70971

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 2 commits into from
Jan 19, 2024
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
23 changes: 12 additions & 11 deletions stdlib/public/core/LifetimeManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public func _withUnprotectedUnsafeMutablePointer<T, Result>(
#endif
}

#if !$Embedded
/// Invokes the given closure with a pointer to the given argument.
///
/// The `withUnsafePointer(to:_:)` function is useful for calling Objective-C
Expand All @@ -120,25 +119,27 @@ public func _withUnprotectedUnsafeMutablePointer<T, Result>(
/// type. If you need to mutate the argument through the pointer, use
/// `withUnsafeMutablePointer(to:_:)` instead.
/// - Returns: The return value, if any, of the `body` closure.
@_alwaysEmitIntoClient
@inlinable
public func withUnsafePointer<T, Result>(
public func withUnsafePointer<T, E, Result>(
to value: T,
_ body: (UnsafePointer<T>) throws -> Result
) rethrows -> Result
_ body: (UnsafePointer<T>) throws(E) -> Result
) throws(E) -> Result
{
return try body(UnsafePointer<T>(Builtin.addressOfBorrow(value)))
}
#else
// TODO: This should be unified with non-embedded Swift.
@inlinable
public func withUnsafePointer<T, E, Result>(

/// ABI: Historical withUnsafePointer(to:_:) rethrows, expressed as "throws",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be in a #if !$Embedded block, since there is no ABI to maintain?

Copy link
Contributor

@stephentyrone stephentyrone Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we shouldn't put all these legacy untyped-throws implementations in a file that we can simply not build for non-ABI or new platforms.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave this is it is in this PR, if you're okay with that, but yes, we want to decide how to organize the legacy implementations. I don't feel strongly about this, but let me just note that there is no impact for Embedded Swift even if we keep the legacy impl in -- unused functions do not contribute to binary outputs.

/// which is ABI-compatible with "rethrows".
@_silgen_name("$ss17withUnsafePointer2to_q_x_q_SPyxGKXEtKr0_lF")
@usableFromInline
func __abi_withUnsafePointer<T, Result>(
to value: T,
_ body: (UnsafePointer<T>) throws(E) -> Result
) throws(E) -> Result
_ body: (UnsafePointer<T>) throws -> Result
) throws -> Result
{
return try body(UnsafePointer<T>(Builtin.addressOfBorrow(value)))
}
#endif

/// Invokes the given closure with a pointer to the given argument.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Func Collection.map(_:) is now without @rethrows
Func Sequence.map(_:) has generic signature change from <Self, T where Self : Swift.Sequence> to <Self, T, E where Self : Swift.Sequence, E : Swift.Error>
Func Sequence.map(_:) is now without @rethrows
Constructor Result.init(catching:) has generic signature change from <Success, Failure where Failure == any Swift.Error> to <Success, Failure where Failure : Swift.Error>
Func withUnsafePointer(to:_:) has generic signature change from <T, Result> to <T, E, Result where E : Swift.Error>
Func withUnsafePointer(to:_:) is now without @rethrows
Func withoutActuallyEscaping(_:do:) has generic signature change from <ClosureType, ResultType> to <ClosureType, ResultType, Failure where Failure : Swift.Error>
Func withoutActuallyEscaping(_:do:) is now without @rethrows

Expand Down
3 changes: 3 additions & 0 deletions test/api-digester/stability-stdlib-abi-without-asserts.test
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ Constructor Result.init(__abi_catching:) is a new API without @available attribu
Constructor Result.init(catching:) has been removed
Func Result.get() has been renamed to Func __abi_get()
Func Result.get() has mangled name changing from 'Swift.Result.get() throws -> A' to 'Swift.Result.__abi_get() throws -> A'
Func withUnsafePointer(to:_:) has been renamed to Func __abi_withUnsafePointer(to:_:)
Func withUnsafePointer(to:_:) has mangled name changing from 'Swift.withUnsafePointer<A, B>(to: A, _: (Swift.UnsafePointer<A>) throws -> B) throws -> B' to 'Swift.__abi_withUnsafePointer<A, B>(to: A, _: (Swift.UnsafePointer<A>) throws -> B) throws -> B'
Func withUnsafePointer(to:_:) is now without @rethrows
Func withoutActuallyEscaping(_:do:) has been renamed to Func __abi_withoutActuallyEscaping(_:do:)
Func withoutActuallyEscaping(_:do:) has mangled name changing from 'Swift.withoutActuallyEscaping<A, B>(_: A, do: (A) throws -> B) throws -> B' to 'Swift.__abi_withoutActuallyEscaping<A, B>(_: A, do: (A) throws -> B) throws -> B'
Func withoutActuallyEscaping(_:do:) is now without @rethrows
Expand Down