Skip to content

Commit b478075

Browse files
authored
Merge pull request #60006 from tshortli/tvos-appext-unavailable
AST: Ensure `isPlatformActiveForTarget()` returns the correct result for application extensions on every platform
2 parents bf1164f + 2b75d9d commit b478075

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

lib/AST/PlatformKind.cpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,37 @@ Optional<PlatformKind> swift::platformFromString(StringRef Name) {
5858
.Default(Optional<PlatformKind>());
5959
}
6060

61+
static bool isApplicationExtensionPlatform(PlatformKind Platform) {
62+
switch (Platform) {
63+
case PlatformKind::macOSApplicationExtension:
64+
case PlatformKind::iOSApplicationExtension:
65+
case PlatformKind::macCatalystApplicationExtension:
66+
case PlatformKind::tvOSApplicationExtension:
67+
case PlatformKind::watchOSApplicationExtension:
68+
return true;
69+
case PlatformKind::macOS:
70+
case PlatformKind::iOS:
71+
case PlatformKind::macCatalyst:
72+
case PlatformKind::tvOS:
73+
case PlatformKind::watchOS:
74+
case PlatformKind::OpenBSD:
75+
case PlatformKind::Windows:
76+
case PlatformKind::none:
77+
return false;
78+
}
79+
llvm_unreachable("bad PlatformKind");
80+
}
81+
6182
static bool isPlatformActiveForTarget(PlatformKind Platform,
6283
const llvm::Triple &Target,
6384
bool EnableAppExtensionRestrictions) {
6485
if (Platform == PlatformKind::none)
6586
return true;
66-
67-
if (Platform == PlatformKind::macOSApplicationExtension ||
68-
Platform == PlatformKind::iOSApplicationExtension ||
69-
Platform == PlatformKind::macCatalystApplicationExtension)
70-
if (!EnableAppExtensionRestrictions)
71-
return false;
72-
87+
88+
if (!EnableAppExtensionRestrictions &&
89+
isApplicationExtensionPlatform(Platform))
90+
return false;
91+
7392
// FIXME: This is an awful way to get the current OS.
7493
switch (Platform) {
7594
case PlatformKind::macOS:
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Verify that declarations unavailable to application extensions are diagnosed
2+
// as unavailable when compiling with `-application-extension`
3+
// RUN: %target-typecheck-verify-swift -application-extension
4+
5+
// Remove `-application-extension` and verify no errors are emitted.
6+
// RUN: %target-swift-frontend -typecheck %s
7+
8+
// REQUIRES: OS=macosx || OS=ios || OS=tvos || OS=watchos
9+
10+
@available(macOSApplicationExtension, unavailable)
11+
@available(macCatalystApplicationExtension, unavailable)
12+
@available(iOSApplicationExtension, unavailable)
13+
@available(tvOSApplicationExtension, unavailable)
14+
@available(watchOSApplicationExtension, unavailable)
15+
func unavailableToExtensions() {} // expected-note {{'unavailableToExtensions()' has been explicitly marked unavailable here}}
16+
17+
func alwaysAvailable() {
18+
unavailableToExtensions() // expected-error {{'unavailableToExtensions()' is unavailable in application extensions for}}
19+
}

0 commit comments

Comments
 (0)