Skip to content

Commit 57b10ac

Browse files
authored
Merge pull request #34992 from DougGregor/concurrency-objc-interop-fixes
[Concurrency] Tweaks to heuristics for Objective-C imports
2 parents cc2e32f + 719e220 commit 57b10ac

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

lib/Basic/StringExtras.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,10 +1322,10 @@ bool swift::omitNeedlessWords(StringRef &baseName,
13221322
}
13231323

13241324
// If this is an asynchronous function where the completion handler is
1325-
// the second parameter, and the corresponding name has some additional
1326-
// information prior to WithCompletion(Handler), append that
1325+
// past the first parameter the corresponding name has some additional
1326+
// information prior to the completion-handled suffix, append that
13271327
// additional text to the base name.
1328-
if (isAsync && *completionHandlerIndex == 1 && completionHandlerName) {
1328+
if (isAsync && *completionHandlerIndex >= 1 && completionHandlerName) {
13291329
if (auto extraParamText = stripWithCompletionHandlerSuffix(
13301330
*completionHandlerName)) {
13311331
SmallString<32> newBaseName;
@@ -1355,20 +1355,6 @@ bool swift::omitNeedlessWords(StringRef &baseName,
13551355
name, paramTypes[i], role,
13561356
role == NameRole::BaseName ? allPropertyNames : nullptr);
13571357

1358-
// If this is an asynchronous function where the completion handler is
1359-
// past the second parameter and has additional information in the name,
1360-
// add that information to the prior argument name.
1361-
if (isAsync && completionHandlerName && *completionHandlerIndex > 1 &&
1362-
*completionHandlerIndex == i + 1) {
1363-
if (auto extraParamText = stripWithCompletionHandlerSuffix(
1364-
*completionHandlerName)) {
1365-
SmallString<32> extendedName;
1366-
extendedName += newName;
1367-
appendSentenceCase(extendedName, *extraParamText);
1368-
newName = scratch.copyString(extendedName);
1369-
}
1370-
}
1371-
13721358
if (name == newName) continue;
13731359

13741360
// Record this change.
@@ -1392,5 +1378,17 @@ Optional<StringRef> swift::stripWithCompletionHandlerSuffix(StringRef name) {
13921378
return name.drop_back(strlen("WithCompletion"));
13931379
}
13941380

1381+
if (name.endswith("WithCompletionBlock")) {
1382+
return name.drop_back(strlen("WithCompletionBlock"));
1383+
}
1384+
1385+
if (name.endswith("WithReplyTo")) {
1386+
return name.drop_back(strlen("WithReplyTo"));
1387+
}
1388+
1389+
if (name.endswith("WithReply")) {
1390+
return name.drop_back(strlen("WithReply"));
1391+
}
1392+
13951393
return None;
13961394
}

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/ClangImporter/objc_async.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func testSlowServer(slowServer: SlowServer) async throws {
2424
let _: Int = await try slowServer.magicNumber(withSeed: 42)
2525

2626
await slowServer.serverRestart("localhost")
27-
await slowServer.server("localhost", atPriorityRestart: 0.8)
27+
await slowServer.serverRestart("localhost", atPriority: 0.8)
2828

2929
_ = await slowServer.allOperations()
3030
}

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)