Skip to content

AST: Ensure isPlatformActiveForTarget() returns the correct result for application extensions on every platform #60006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions lib/AST/PlatformKind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,37 @@ Optional<PlatformKind> swift::platformFromString(StringRef Name) {
.Default(Optional<PlatformKind>());
}

static bool isApplicationExtensionPlatform(PlatformKind Platform) {
switch (Platform) {
case PlatformKind::macOSApplicationExtension:
case PlatformKind::iOSApplicationExtension:
case PlatformKind::macCatalystApplicationExtension:
case PlatformKind::tvOSApplicationExtension:
case PlatformKind::watchOSApplicationExtension:
return true;
case PlatformKind::macOS:
case PlatformKind::iOS:
case PlatformKind::macCatalyst:
case PlatformKind::tvOS:
case PlatformKind::watchOS:
case PlatformKind::OpenBSD:
case PlatformKind::Windows:
case PlatformKind::none:
return false;
}
llvm_unreachable("bad PlatformKind");
}

static bool isPlatformActiveForTarget(PlatformKind Platform,
const llvm::Triple &Target,
bool EnableAppExtensionRestrictions) {
if (Platform == PlatformKind::none)
return true;

if (Platform == PlatformKind::macOSApplicationExtension ||
Platform == PlatformKind::iOSApplicationExtension ||
Platform == PlatformKind::macCatalystApplicationExtension)
if (!EnableAppExtensionRestrictions)
return false;


if (!EnableAppExtensionRestrictions &&
isApplicationExtensionPlatform(Platform))
return false;

// FIXME: This is an awful way to get the current OS.
switch (Platform) {
case PlatformKind::macOS:
Expand Down
19 changes: 19 additions & 0 deletions test/attr/attr_availability_appext_unavailable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Verify that declarations unavailable to application extensions are diagnosed
// as unavailable when compiling with `-application-extension`
// RUN: %target-typecheck-verify-swift -application-extension

// Remove `-application-extension` and verify no errors are emitted.
// RUN: %target-swift-frontend -typecheck %s

// REQUIRES: OS=macosx || OS=ios || OS=tvos || OS=watchos

@available(macOSApplicationExtension, unavailable)
@available(macCatalystApplicationExtension, unavailable)
@available(iOSApplicationExtension, unavailable)
@available(tvOSApplicationExtension, unavailable)
@available(watchOSApplicationExtension, unavailable)
func unavailableToExtensions() {} // expected-note {{'unavailableToExtensions()' has been explicitly marked unavailable here}}

func alwaysAvailable() {
unavailableToExtensions() // expected-error {{'unavailableToExtensions()' is unavailable in application extensions for}}
}