Skip to content

Commit b837675

Browse files
committed
[Concurrency] Introduce more completion-handler names to the import heuristics.
Extend the set of completion-handler names we look for to infer an `async` import of an Objective-C method, which includes: * (with)CompletionBlock * (with)reply * (with)replyTo both as parameter names and as base name suffixes.
1 parent 276e1a2 commit b837675

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

lib/Basic/StringExtras.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,5 +1392,17 @@ Optional<StringRef> swift::stripWithCompletionHandlerSuffix(StringRef name) {
13921392
return name.drop_back(strlen("WithCompletion"));
13931393
}
13941394

1395+
if (name.endswith("WithCompletionBlock")) {
1396+
return name.drop_back(strlen("WithCompletionBlock"));
1397+
}
1398+
1399+
if (name.endswith("WithReplyTo")) {
1400+
return name.drop_back(strlen("WithReplyTo"));
1401+
}
1402+
1403+
if (name.endswith("WithReply")) {
1404+
return name.drop_back(strlen("WithReply"));
1405+
}
1406+
13951407
return None;
13961408
}

lib/ClangImporter/ImportName.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,8 +1135,12 @@ Optional<ForeignErrorConvention::Info> NameImporter::considerErrorImport(
11351135

11361136
/// Whether the given parameter name identifies a completion handler.
11371137
static bool isCompletionHandlerParamName(StringRef paramName) {
1138-
return paramName == "completionHandler" || paramName == "completion" ||
1139-
paramName == "withCompletionHandler" || paramName == "withCompletion";
1138+
return paramName == "completionHandler" ||
1139+
paramName == "withCompletionHandler" ||
1140+
paramName == "completion" || paramName == "withCompletion" ||
1141+
paramName == "completionBlock" || paramName == "withCompletionBlock" ||
1142+
paramName == "reply" || paramName == "withReply";
1143+
paramName == "replyTo" || paramName == "withReplyTo";
11401144
}
11411145

11421146
// Determine whether the given type is a nullable NSError type.

test/IDE/print_clang_objc_async.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
// CHECK-DAG: func doSomethingDangerous(_ operation: String) async throws -> String
1414
// CHECK-DAG: func checkAvailability(completionHandler: @escaping (Bool) -> Void)
1515
// CHECK-DAG: func checkAvailability() async -> Bool
16+
// CHECK-DAG: func anotherExample() async -> String
17+
// CHECK-DAG: func finalExample() async -> String
18+
// CHECK-DAG: func replyingOperation(_ operation: String) async -> String
1619
// CHECK-DAG: func findAnswer(completionHandler handler: @escaping (String?, Error?) -> Void)
1720
// CHECK-DAG: func findAnswer() async throws -> String
1821
// CHECK-DAG: func findAnswerFailingly(completionHandler handler: @escaping (String?, Error?) -> Void) throws

test/Inputs/clang-importer-sdk/usr/include/ObjCConcurrency.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
-(void)doSomethingSlow:(NSString *)operation completionHandler:(void (^)(NSInteger))handler;
1313
-(void)doSomethingDangerous:(NSString *)operation completionHandler:(void (^ _Nullable)(NSString *_Nullable, NSError * _Nullable))handler;
1414
-(void)checkAvailabilityWithCompletionHandler:(void (^)(BOOL isAvailable))completionHandler;
15+
-(void)anotherExampleWithCompletionBlock:(void (^)(NSString *))block;
16+
-(void)finalExampleWithReplyTo:(void (^)(NSString *))block;
17+
-(void)replyingOperation:(NSString *)operation replyTo:(void (^)(NSString *))block;
1518
-(void)findAnswerAsynchronously:(void (^)(NSString *_Nullable, NSError * _Nullable))handler __attribute__((swift_name("findAnswer(completionHandler:)")));
1619
-(BOOL)findAnswerFailinglyWithError:(NSError * _Nullable * _Nullable)error completion:(void (^)(NSString *_Nullable, NSError * _Nullable))handler __attribute__((swift_name("findAnswerFailingly(completionHandler:)")));
1720
-(void)doSomethingFun:(NSString *)operation then:(void (^)(void))completionHandler;

0 commit comments

Comments
 (0)