Skip to content

Commit fa9d4fd

Browse files
authored
Merge pull request swiftlang#34382 from DougGregor/concurrency-objc-get-async
[Concurrency] Asynchronous Objective-C method importer naming adjustments
2 parents bbe9cb7 + 8521453 commit fa9d4fd

File tree

7 files changed

+42
-13
lines changed

7 files changed

+42
-13
lines changed

include/swift/Basic/StringExtras.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ class InheritedNameSet {
442442
///
443443
/// \param allPropertyNames The set of property names in the enclosing context.
444444
///
445+
/// \param isAsync Whether this is a function imported as 'async'.
446+
///
445447
/// \param scratch Scratch space that will be used for modifications beyond
446448
/// just chopping names.
447449
///
@@ -455,6 +457,7 @@ bool omitNeedlessWords(StringRef &baseName,
455457
bool returnsSelf,
456458
bool isProperty,
457459
const InheritedNameSet *allPropertyNames,
460+
bool isAsync,
458461
StringScratchSpace &scratch);
459462

460463
} // end namespace swift

lib/Basic/StringExtras.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,7 @@ bool swift::omitNeedlessWords(StringRef &baseName,
12201220
bool returnsSelf,
12211221
bool isProperty,
12221222
const InheritedNameSet *allPropertyNames,
1223+
bool isAsync,
12231224
StringScratchSpace &scratch) {
12241225
bool anyChanges = false;
12251226
OmissionTypeName resultType = returnsSelf ? contextType : givenResultType;
@@ -1287,11 +1288,28 @@ bool swift::omitNeedlessWords(StringRef &baseName,
12871288
}
12881289
}
12891290

1291+
// If the base name of a method imported as "async" starts with the word
1292+
// "get", drop the "get".
1293+
if (isAsync && camel_case::getFirstWord(baseName) == "get" &&
1294+
baseName.size() > 3) {
1295+
baseName = baseName.substr(3);
1296+
anyChanges = true;
1297+
}
1298+
12901299
// If needed, split the base name.
12911300
if (!argNames.empty() &&
12921301
splitBaseName(baseName, argNames[0], paramTypes[0], firstParamName))
12931302
anyChanges = true;
12941303

1304+
// For a method imported as "async", drop the "Asynchronously" suffix from
1305+
// the base name. It is redundant with 'async'.
1306+
const StringRef asynchronously = "Asynchronously";
1307+
if (isAsync && camel_case::getLastWord(baseName) == asynchronously &&
1308+
baseName.size() > asynchronously.size()) {
1309+
baseName = baseName.drop_back(asynchronously.size());
1310+
anyChanges = true;
1311+
}
1312+
12951313
// Omit needless words based on parameter types.
12961314
for (unsigned i = 0, n = argNames.size(); i != n; ++i) {
12971315
// If there is no corresponding parameter, there is nothing to

lib/ClangImporter/IAMInference.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ class IAMInference {
448448
baseName = humbleBaseName.userFacingName();
449449
bool didOmit =
450450
omitNeedlessWords(baseName, argStrs, "", "", "", paramTypeNames, false,
451-
false, nullptr, scratch);
451+
false, nullptr, false, scratch);
452452
SmallVector<Identifier, 8> argLabels;
453453
for (auto str : argStrs)
454454
argLabels.push_back(getIdentifier(str));

lib/ClangImporter/ImportName.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ static bool omitNeedlessWordsInFunctionName(
804804
ArrayRef<const clang::ParmVarDecl *> params, clang::QualType resultType,
805805
const clang::DeclContext *dc, const SmallBitVector &nonNullArgs,
806806
Optional<unsigned> errorParamIndex, bool returnsSelf, bool isInstanceMethod,
807-
NameImporter &nameImporter) {
807+
Optional<unsigned> completionHandlerIndex, NameImporter &nameImporter) {
808808
clang::ASTContext &clangCtx = nameImporter.getClangContext();
809809

810810
// Collect the parameter type names.
@@ -817,10 +817,6 @@ static bool omitNeedlessWordsInFunctionName(
817817
if (i == 0)
818818
firstParamName = param->getName();
819819

820-
// Determine the number of parameters.
821-
unsigned numParams = params.size();
822-
if (errorParamIndex) --numParams;
823-
824820
bool isLastParameter
825821
= (i == params.size() - 1) ||
826822
(i == params.size() - 2 &&
@@ -858,7 +854,8 @@ static bool omitNeedlessWordsInFunctionName(
858854
getClangTypeNameForOmission(clangCtx, resultType),
859855
getClangTypeNameForOmission(clangCtx, contextType),
860856
paramTypes, returnsSelf, /*isProperty=*/false,
861-
allPropertyNames, nameImporter.getScratch());
857+
allPropertyNames, completionHandlerIndex.hasValue(),
858+
nameImporter.getScratch());
862859
}
863860

864861
/// Prepare global name for importing onto a swift_newtype.
@@ -1831,8 +1828,7 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
18311828
result.info.accessorKind == ImportedAccessorKind::None) {
18321829
if (auto asyncInfo = considerAsyncImport(
18331830
objcMethod, baseName, argumentNames, params, isInitializer,
1834-
/*hasCustomName=*/false,
1835-
result.getErrorInfo())) {
1831+
/*hasCustomName=*/false, result.getErrorInfo())) {
18361832
result.info.hasAsyncInfo = true;
18371833
result.info.asyncInfo = *asyncInfo;
18381834
}
@@ -1972,7 +1968,7 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
19721968
(void)omitNeedlessWords(baseName, {}, "", propertyTypeName,
19731969
contextTypeName, {}, /*returnsSelf=*/false,
19741970
/*isProperty=*/true, allPropertyNames,
1975-
scratch);
1971+
/*isAsync=*/false, scratch);
19761972
}
19771973
}
19781974

@@ -1984,7 +1980,12 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
19841980
result.getErrorInfo()
19851981
? Optional<unsigned>(result.getErrorInfo()->ErrorParameterIndex)
19861982
: None,
1987-
method->hasRelatedResultType(), method->isInstanceMethod(), *this);
1983+
method->hasRelatedResultType(), method->isInstanceMethod(),
1984+
result.getAsyncInfo().map(
1985+
[](const ForeignAsyncConvention::Info &info) {
1986+
return info.CompletionHandlerParamIndex;
1987+
}),
1988+
*this);
19881989
}
19891990

19901991
// If the result is a value, lowercase it.

lib/Sema/MiscDiagnostics.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4867,7 +4867,8 @@ Optional<DeclName> TypeChecker::omitNeedlessWords(AbstractFunctionDecl *afd) {
48674867
getTypeNameForOmission(resultType),
48684868
getTypeNameForOmission(contextType),
48694869
paramTypes, returnsSelf, false,
4870-
/*allPropertyNames=*/nullptr, scratch))
4870+
/*allPropertyNames=*/nullptr,
4871+
afd->hasAsync(), scratch))
48714872
return None;
48724873

48734874
/// Retrieve a replacement identifier.
@@ -4922,7 +4923,8 @@ Optional<Identifier> TypeChecker::omitNeedlessWords(VarDecl *var) {
49224923
OmissionTypeName contextTypeName = getTypeNameForOmission(contextType);
49234924
if (::omitNeedlessWords(name, { }, "", typeName, contextTypeName, { },
49244925
/*returnsSelf=*/false, true,
4925-
/*allPropertyNames=*/nullptr, scratch)) {
4926+
/*allPropertyNames=*/nullptr, /*isAsync=*/false,
4927+
scratch)) {
49264928
return Context.getIdentifier(name);
49274929
}
49284930

test/ClangImporter/objc_async.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ 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()
23+
let _: Int = await try slowServer.magicNumber(withSeed: 42)
2124
}
2225

2326
func testSlowServerSynchronous(slowServer: SlowServer) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
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)getFortuneAsynchronouslyWithCompletionHandler:(void (^)(NSString *_Nullable, NSError * _Nullable))handler;
14+
-(void)getMagicNumberAsynchronouslyWithSeed:(NSInteger)seed completionHandler:(void (^)(NSInteger, NSError * _Nullable))handler;
1315
@property(readwrite) void (^completionHandler)(NSInteger);
1416

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

0 commit comments

Comments
 (0)