Skip to content

[Concurrency] Import Objective-C methods with completion handlers as async #33674

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 3 commits into from
Aug 29, 2020

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Aug 28, 2020

When a given Objective-C method has a completion handler parameter
with an appropriate signature, import that Objective-C method as
async. For example, consider the following CloudKit API:

- (void)fetchShareParticipantWithUserRecordID:(CKRecordID *)userRecordID
        completionHandler:(void (^)(CKShareParticipant * _Nullable shareParticipant, NSError * _Nullable error))completionHandler;

With the experimental concurrency model, this would import as:

func fetchShareParticipant(withUserRecordID userRecordID: CKRecord.ID) async throws -> CKShare.Participant?

The compiler will be responsible for turning the caller's continuation
into a block to pass along to the completion handler. When the error
parameter of the completion handler is non-null, the async call
will result in that error being thrown. Otherwise, the other arguments
passed to that completion handler will be returned as the result of
the async call.

async versions of methods are imported alongside their
completion-handler versions, to maintain source compatibility with
existing code that provides a completion handler.

Note that this only covers the Clang importer portion of this task.

This operation is not actually dependent on the version.
@DougGregor DougGregor changed the title @DougGregor [Concurrency] Import Objective-C methods with completion handlers as async [Concurrency] Import Objective-C methods with completion handlers as async Aug 28, 2020
@DougGregor DougGregor force-pushed the concurrency-objc-import-async branch from 5b2f81f to 3a936bc Compare August 28, 2020 04:09
…async

When a given Objective-C method has a completion handler parameter
with an appropriate signature, import that Objective-C method as
async. For example, consider the following CloudKit API:

    - (void)fetchShareParticipantWithUserRecordID:(CKRecordID
*)userRecordID
            completionHandler:(void (^)(CKShareParticipant * _Nullable shareParticipant, NSError * _Nullable error))completionHandler;

With the experimental concurrency model, this would import as:

    func fetchShareParticipant(withUserRecordID userRecordID: CKRecord.ID) async throws -> CKShare.Participant?

The compiler will be responsible for turning the caller's continuation
into a block to pass along to the completion handler. When the error
parameter of the completion handler is non-null, the async call
will result in that error being thrown. Otherwise, the other arguments
passed to that completion handler will be returned as the result of
the async call.

async versions of methods are imported alongside their
completion-handler versions, to maintain source compatibility with
existing code that provides a completion handler.

Note that this only covers the Clang importer portion of this task.
@DougGregor DougGregor force-pushed the concurrency-objc-import-async branch from 3a936bc to 1e5d30f Compare August 28, 2020 14:54
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

…ync import

Extend the check for completion handler parameters to also consider the
name of the parameter (not its argument label). If it's `completion` or
`completionHandler`, we have a completion handler. This extends our
API coverage for importing Objective-C methods with completion
handlers as 'async'.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please smoke test Linux

@DougGregor
Copy link
Member Author

sigh Linux failure is what @CodaFi is working on fixing in Clang

@DougGregor
Copy link
Member Author

@swift-ci please smoke test Linux

@DougGregor DougGregor merged commit 10fa19b into swiftlang:master Aug 29, 2020
@DougGregor DougGregor deleted the concurrency-objc-import-async branch August 29, 2020 02:48
@rolson
Copy link

rolson commented Sep 29, 2020

If the async method doesn't end up throwing, then should the return value be non-optional? ie

func fetchShareParticipant(withUserRecordID userRecordID: CKRecord.ID) async throws -> CKShare.Participant

@LuizZak
Copy link
Contributor

LuizZak commented Sep 29, 2020

I assume it has to be optional to cover cases where both the result and error are nil.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants