Skip to content

Commit 7509270

Browse files
authored
Merge pull request swiftlang#35862 from DougGregor/concurrency-import-cleanups
2 parents 842cc9c + ae3049f commit 7509270

File tree

7 files changed

+63
-7
lines changed

7 files changed

+63
-7
lines changed

lib/Basic/StringExtras.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,10 @@ Optional<StringRef> swift::stripWithCompletionHandlerSuffix(StringRef name) {
13741374
return name.drop_back(strlen("WithCompletionBlock"));
13751375
}
13761376

1377+
if (name.endswith("WithBlock")) {
1378+
return name.drop_back(strlen("WithBlock"));
1379+
}
1380+
13771381
if (name.endswith("WithReplyTo")) {
13781382
return name.drop_back(strlen("WithReplyTo"));
13791383
}

lib/ClangImporter/ClangAdapter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,8 @@ bool importer::isUnavailableInSwift(
731731
llvm::VersionTuple version = attr->getDeprecated();
732732
if (version.empty())
733733
continue;
734-
if (platformAvailability.treatDeprecatedAsUnavailable(decl, version)) {
734+
if (platformAvailability.treatDeprecatedAsUnavailable(
735+
decl, version, /*isAsync=*/false)) {
735736
return true;
736737
}
737738
}

lib/ClangImporter/ClangImporter.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1942,17 +1942,25 @@ PlatformAvailability::PlatformAvailability(const LangOptions &langOpts)
19421942
case PlatformKind::tvOSApplicationExtension:
19431943
deprecatedAsUnavailableMessage =
19441944
"APIs deprecated as of iOS 7 and earlier are unavailable in Swift";
1945+
asyncDeprecatedAsUnavailableMessage =
1946+
"APIs deprecated as of iOS 12 and earlier are not imported as 'async'";
19451947
break;
19461948

19471949
case PlatformKind::watchOS:
19481950
case PlatformKind::watchOSApplicationExtension:
19491951
deprecatedAsUnavailableMessage = "";
1952+
asyncDeprecatedAsUnavailableMessage =
1953+
"APIs deprecated as of watchOS 5 and earlier are not imported as "
1954+
"'async'";
19501955
break;
19511956

19521957
case PlatformKind::macOS:
19531958
case PlatformKind::macOSApplicationExtension:
19541959
deprecatedAsUnavailableMessage =
19551960
"APIs deprecated as of macOS 10.9 and earlier are unavailable in Swift";
1961+
asyncDeprecatedAsUnavailableMessage =
1962+
"APIs deprecated as of macOS 10.14 and earlier are not imported as "
1963+
"'async'";
19561964
break;
19571965

19581966
case PlatformKind::OpenBSD:
@@ -2009,7 +2017,8 @@ bool PlatformAvailability::isPlatformRelevant(StringRef name) const {
20092017
}
20102018

20112019
bool PlatformAvailability::treatDeprecatedAsUnavailable(
2012-
const clang::Decl *clangDecl, const llvm::VersionTuple &version) const {
2020+
const clang::Decl *clangDecl, const llvm::VersionTuple &version,
2021+
bool isAsync) const {
20132022
assert(!version.empty() && "Must provide version when deprecated");
20142023
unsigned major = version.getMajor();
20152024
Optional<unsigned> minor = version.getMinor();
@@ -2020,6 +2029,13 @@ bool PlatformAvailability::treatDeprecatedAsUnavailable(
20202029

20212030
case PlatformKind::macOS:
20222031
case PlatformKind::macOSApplicationExtension:
2032+
// Anything deprecated by macOS 10.14 is unavailable for async import
2033+
// in Swift.
2034+
if (isAsync && !clangDecl->hasAttr<clang::SwiftAsyncAttr>()) {
2035+
return major < 10 ||
2036+
(major == 10 && (!minor.hasValue() || minor.getValue() <= 14));
2037+
}
2038+
20232039
// Anything deprecated in OSX 10.9.x and earlier is unavailable in Swift.
20242040
return major < 10 ||
20252041
(major == 10 && (!minor.hasValue() || minor.getValue() <= 9));
@@ -2028,6 +2044,12 @@ bool PlatformAvailability::treatDeprecatedAsUnavailable(
20282044
case PlatformKind::iOSApplicationExtension:
20292045
case PlatformKind::tvOS:
20302046
case PlatformKind::tvOSApplicationExtension:
2047+
// Anything deprecated by iOS 12 is unavailable for async import
2048+
// in Swift.
2049+
if (isAsync && !clangDecl->hasAttr<clang::SwiftAsyncAttr>()) {
2050+
return major <= 12;
2051+
}
2052+
20312053
// Anything deprecated in iOS 7.x and earlier is unavailable in Swift.
20322054
return major <= 7;
20332055

@@ -2038,6 +2060,12 @@ bool PlatformAvailability::treatDeprecatedAsUnavailable(
20382060

20392061
case PlatformKind::watchOS:
20402062
case PlatformKind::watchOSApplicationExtension:
2063+
// Anything deprecated by watchOS 5.0 is unavailable for async import
2064+
// in Swift.
2065+
if (isAsync && !clangDecl->hasAttr<clang::SwiftAsyncAttr>()) {
2066+
return major <= 5;
2067+
}
2068+
20412069
// No deprecation filter on watchOS
20422070
return false;
20432071

lib/ClangImporter/ImportDecl.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7919,6 +7919,11 @@ void ClangImporter::Implementation::importAttributes(
79197919
if (maybeDefinition.getValue())
79207920
ClangDecl = cast<clang::NamedDecl>(maybeDefinition.getValue());
79217921

7922+
// Determine whether this is an async import.
7923+
bool isAsync = false;
7924+
if (auto func = dyn_cast<AbstractFunctionDecl>(MappedDecl))
7925+
isAsync = func->hasAsync();
7926+
79227927
// Scan through Clang attributes and map them onto Swift
79237928
// equivalents.
79247929
bool AnyUnavailable = MappedDecl->getAttrs().isUnavailable(C);
@@ -8023,12 +8028,18 @@ void ClangImporter::Implementation::importAttributes(
80238028
llvm::VersionTuple deprecated = avail->getDeprecated();
80248029

80258030
if (!deprecated.empty()) {
8026-
if (platformAvailability.treatDeprecatedAsUnavailable(ClangDecl,
8027-
deprecated)) {
8031+
if (platformAvailability.treatDeprecatedAsUnavailable(
8032+
ClangDecl, deprecated, isAsync)) {
80288033
AnyUnavailable = true;
80298034
PlatformAgnostic = PlatformAgnosticAvailabilityKind::Unavailable;
8030-
if (message.empty())
8031-
message = platformAvailability.deprecatedAsUnavailableMessage;
8035+
if (message.empty()) {
8036+
if (isAsync) {
8037+
message =
8038+
platformAvailability.asyncDeprecatedAsUnavailableMessage;
8039+
} else {
8040+
message = platformAvailability.deprecatedAsUnavailableMessage;
8041+
}
8042+
}
80328043
}
80338044
}
80348045

lib/ClangImporter/ImporterImpl.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,17 @@ struct PlatformAvailability {
257257
/// should be inlucded in the cutoff of imported deprecated APIs marked
258258
/// unavailable.
259259
bool treatDeprecatedAsUnavailable(const clang::Decl *clangDecl,
260-
const llvm::VersionTuple &version) const;
260+
const llvm::VersionTuple &version,
261+
bool isAsync) const;
261262

262263
/// The message to embed for implicitly unavailability if a deprecated
263264
/// API is now unavailable.
264265
std::string deprecatedAsUnavailableMessage;
265266

267+
/// The message to embed for implicit async unavailability based on
268+
/// deprecation.
269+
std::string asyncDeprecatedAsUnavailableMessage;
270+
266271
PlatformAvailability(const LangOptions &opts);
267272

268273
private:

test/ClangImporter/objc_async.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ func testSlowServer(slowServer: SlowServer) async throws {
4444
let _: String = await slowServer.__leap(17)
4545

4646
slowServer.repeatTrick("jump") // expected-error{{missing argument for parameter 'completionHandler' in call}}
47+
48+
_ = try await slowServer.oldAPI(); // expected-error{{'oldAPI()' is unavailable in macOS: APIs deprecated as of macOS 10.14 and earlier are not imported as 'async'}}
49+
_ = try await slowServer.someAsyncMethod()
4750
}
4851

4952
func testSlowServerSynchronous(slowServer: SlowServer) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ typedef void (^CompletionHandler)(NSString * _Nullable, NSString * _Nullable_res
5353

5454
// rdar://73798726
5555
- (void)getSomeObjectWithCompletionHandler:(nullable void (^)(NSObject *_Nullable x, NSError *_Nullable error))handler;
56+
57+
-(void)oldAPIWithCompletionHandler:(void (^ _Nonnull)(NSString *_Nullable, NSError *_Nullable))handler __attribute__((availability(macosx, deprecated=10.14)));
58+
59+
-(void)someAsyncMethodWithBlock:(void (^ _Nonnull)(NSString *_Nullable, NSError *_Nullable))completionHandler;
5660
@end
5761

5862
@protocol RefrigeratorDelegate<NSObject>

0 commit comments

Comments
 (0)