Skip to content

Commit e185295

Browse files
committed
[Concurrency] Drop "get" prefix when importing Objective-C methods as async.
Implements rdar://70506634.
1 parent b6c0145 commit e185295

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

lib/ClangImporter/ImportName.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,7 @@ Optional<ForeignAsyncConvention::Info>
11891189
NameImporter::considerAsyncImport(
11901190
const clang::ObjCMethodDecl *clangDecl,
11911191
StringRef &baseName,
1192+
SmallVectorImpl<char> &baseNameScratch,
11921193
SmallVectorImpl<StringRef> &paramNames,
11931194
ArrayRef<const clang::ParmVarDecl *> params,
11941195
bool isInitializer, bool hasCustomName,
@@ -1224,6 +1225,17 @@ NameImporter::considerAsyncImport(
12241225
return None;
12251226
}
12261227

1228+
// If there's no custom name, and the base name starts with "get", drop the
1229+
// get.
1230+
if (!hasCustomName) {
1231+
StringRef currentBaseName = newBaseName ? *newBaseName : baseName;
1232+
if (currentBaseName.size() > 3 &&
1233+
camel_case::getFirstWord(currentBaseName) == "get") {
1234+
newBaseName = camel_case::toLowercaseInitialisms(
1235+
currentBaseName.substr(3), baseNameScratch);
1236+
}
1237+
}
1238+
12271239
// Used for returns once we've determined that the method cannot be
12281240
// imported as async, even though it has what looks like a completion handler
12291241
// parameter.
@@ -1461,6 +1473,7 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
14611473
}
14621474

14631475
// If we have a swift_name attribute, use that.
1476+
SmallString<32> asyncBaseNameScratch;
14641477
if (auto *nameAttr = findSwiftNameAttr(D, version)) {
14651478
bool skipCustomName = false;
14661479

@@ -1539,9 +1552,9 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
15391552

15401553
if (version.supportsConcurrency()) {
15411554
if (auto asyncInfo = considerAsyncImport(
1542-
method, parsedName.BaseName, parsedName.ArgumentLabels,
1543-
params, isInitializer, /*hasCustomName=*/true,
1544-
result.getErrorInfo())) {
1555+
method, parsedName.BaseName, asyncBaseNameScratch,
1556+
parsedName.ArgumentLabels, params, isInitializer,
1557+
/*hasCustomName=*/true, result.getErrorInfo())) {
15451558
result.info.hasAsyncInfo = true;
15461559
result.info.asyncInfo = *asyncInfo;
15471560

@@ -1816,8 +1829,8 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
18161829
if (version.supportsConcurrency() &&
18171830
result.info.accessorKind == ImportedAccessorKind::None) {
18181831
if (auto asyncInfo = considerAsyncImport(
1819-
objcMethod, baseName, argumentNames, params, isInitializer,
1820-
/*hasCustomName=*/false,
1832+
objcMethod, baseName, asyncBaseNameScratch,
1833+
argumentNames, params, isInitializer, /*hasCustomName=*/false,
18211834
result.getErrorInfo())) {
18221835
result.info.hasAsyncInfo = true;
18231836
result.info.asyncInfo = *asyncInfo;

lib/ClangImporter/ImportName.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ class NameImporter {
456456
Optional<ForeignAsyncConvention::Info>
457457
considerAsyncImport(const clang::ObjCMethodDecl *clangDecl,
458458
StringRef &baseName,
459+
SmallVectorImpl<char> &baseNameScratch,
459460
SmallVectorImpl<StringRef> &paramNames,
460461
ArrayRef<const clang::ParmVarDecl *> params,
461462
bool isInitializer, bool hasCustomName,

test/ClangImporter/objc_async.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ func testSlowServer(slowServer: SlowServer) async throws {
1818
// still async version...
1919
let _: Int = slowServer.doSomethingConflicted("thinking")
2020
// expected-error@-1{{call is 'async' but is not marked with 'await'}}
21+
22+
let _: String? = await try slowServer.fortune()
2123
}
2224

2325
func testSlowServerSynchronous(slowServer: SlowServer) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
-(void)findAnswerAsynchronously:(void (^)(NSString *_Nullable, NSError * _Nullable))handler __attribute__((swift_name("findAnswer(completionHandler:)")));
1111
-(BOOL)findAnswerFailinglyWithError:(NSError * _Nullable * _Nullable)error completion:(void (^)(NSString *_Nullable, NSError * _Nullable))handler __attribute__((swift_name("findAnswerFailingly(completionHandler:)")));
1212
-(void)doSomethingFun:(NSString *)operation then:(void (^)(void))completionHandler;
13+
-(void)getFortuneWithCompletionHandler:(void (^)(NSString *_Nullable, NSError * _Nullable))handler;
1314
@property(readwrite) void (^completionHandler)(NSInteger);
1415

1516
-(void)doSomethingConflicted:(NSString *)operation completionHandler:(void (^)(NSInteger))handler;

0 commit comments

Comments
 (0)