Skip to content

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

Merged
DougGregor merged 3 commits into
swiftlang:masterfrom
DougGregor:concurrency-objc-import-async
Aug 29, 2020
Merged

[Concurrency] Import Objective-C methods with completion handlers as async#33674
DougGregor merged 3 commits into
swiftlang:masterfrom
DougGregor:concurrency-objc-import-async

Conversation

@DougGregor

@DougGregor DougGregor commented Aug 28, 2020

Copy link
Copy Markdown
Member

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
Copy Markdown
Member Author

@swift-ci please smoke test

@DougGregor

Copy link
Copy Markdown
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
Copy Markdown
Member Author

@swift-ci please smoke test

@DougGregor

Copy link
Copy Markdown
Member Author

@swift-ci please smoke test Linux

@DougGregor

Copy link
Copy Markdown
Member Author

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

@DougGregor

Copy link
Copy Markdown
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

rolson commented Sep 29, 2020

Copy link
Copy Markdown

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

LuizZak commented Sep 29, 2020

Copy link
Copy Markdown
Contributor

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