Skip to content

Commit b0105e1

Browse files
authored
Introduce -application-extension-library (#63769)
The current implementation of `-application-extension` has a problem that affects the generation of ObjC headers for regular Swift modules. The primary purpose of `-application-extension` is to prevent the use of unavailable APIs in app extensions. However, it has an impact on the generation of -Swift.h headers and exposes Swift's internal declarations to ObjC. This behavior is appropriate for mixed modules that are not consumed externally, such as app extensions, but it fails to address the situation when a module is not an extension itself but is consumed by the extension (c90cd11). To resolve this issue while maintaining the desired behavior, we can introduce a new flag for this particular use-case.
1 parent 3fb446d commit b0105e1

File tree

10 files changed

+21
-1
lines changed

10 files changed

+21
-1
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ namespace swift {
219219
/// Enable 'availability' restrictions for App Extensions.
220220
bool EnableAppExtensionRestrictions = false;
221221

222+
/// Enable 'availability' restrictions for App Extension Libraries.
223+
bool EnableAppExtensionLibraryRestrictions = false;
224+
222225
/// Diagnostic level to report when a public declarations doesn't declare
223226
/// an introduction OS version.
224227
llvm::Optional<DiagnosticBehavior> RequireExplicitAvailability = llvm::None;

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,10 @@ def enable_app_extension : Flag<["-"], "application-extension">,
865865
Flags<[FrontendOption, NoInteractiveOption]>,
866866
HelpText<"Restrict code to those available for App Extensions">;
867867

868+
def enable_app_extension_library : Flag<["-"], "application-extension-library">,
869+
Flags<[FrontendOption, NoInteractiveOption]>,
870+
HelpText<"Restrict code to those available for App Extension Libraries">;
871+
868872
def libc : Separate<["-"], "libc">,
869873
Flags<[SwiftAPIExtractOption, SwiftSymbolGraphExtractOption]>,
870874
HelpText<"libc runtime library to use">;

lib/AST/Module.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2715,7 +2715,9 @@ bool ModuleDecl::isExternallyConsumed() const {
27152715
// App extensions are special beasts because they build without entrypoints
27162716
// like library targets, but they behave like executable targets because
27172717
// their associated modules are not suitable for distribution.
2718-
if (getASTContext().LangOpts.EnableAppExtensionRestrictions) {
2718+
// However, app extension libraries might be consumed externally.
2719+
if (getASTContext().LangOpts.EnableAppExtensionRestrictions &&
2720+
!getASTContext().LangOpts.EnableAppExtensionLibraryRestrictions) {
27192721
return false;
27202722
}
27212723

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
251251
inputArgs.AddLastArg(arguments, options::OPT_warn_implicit_overrides);
252252
inputArgs.AddLastArg(arguments, options::OPT_typo_correction_limit);
253253
inputArgs.AddLastArg(arguments, options::OPT_enable_app_extension);
254+
inputArgs.AddLastArg(arguments, options::OPT_enable_app_extension_library);
254255
inputArgs.AddLastArg(arguments, options::OPT_enable_library_evolution);
255256
inputArgs.AddLastArg(arguments, options::OPT_require_explicit_availability);
256257
inputArgs.AddLastArg(arguments, options::OPT_require_explicit_availability_target);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
895895

896896
Opts.Features.insert(Feature::LayoutPrespecialization);
897897

898+
Opts.EnableAppExtensionLibraryRestrictions |= Args.hasArg(OPT_enable_app_extension_library);
898899
Opts.EnableAppExtensionRestrictions |= Args.hasArg(OPT_enable_app_extension);
900+
Opts.EnableAppExtensionRestrictions |= Opts.EnableAppExtensionLibraryRestrictions;
899901

900902
Opts.EnableSwift3ObjCInference =
901903
Args.hasFlag(OPT_enable_swift3_objc_inference,

test/ClangImporter/availability_app_extension.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify -application-extension %s
2+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify -application-extension-library %s
23

34
// Check the exact error message, which requires a regex match
45
// RUN: not %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -application-extension %s 2>&1 | %FileCheck %s

test/ClangImporter/availability_ios.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify -I %S/Inputs/custom-modules %s
22
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify -I %S/Inputs/custom-modules -application-extension %s
3+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify -I %S/Inputs/custom-modules -application-extension-library %s
34

45
// REQUIRES: OS=ios
56

test/ClangImporter/availability_macosx.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify -I %S/Inputs/custom-modules %s
22
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify -I %S/Inputs/custom-modules -application-extension %s
3+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify -I %S/Inputs/custom-modules -application-extension-library %s
34

45
// REQUIRES: OS=macosx
56

test/PrintAsObjC/accessibility.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
// RUN: %FileCheck -check-prefix=CHECK -check-prefix=CHECK-INTERNAL %s < %t/accessibility-appext.h
2020
// RUN: %check-in-clang %t/accessibility-appext.h
2121

22+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -parse-as-library %s -typecheck -application-extension-library -emit-objc-header-path %t/accessibility-appextlib.h -disable-objc-attr-requires-foundation-module
23+
// RUN: %FileCheck -check-prefix=CHECK -check-prefix=CHECK-PUBLIC %s < %t/accessibility-appextlib.h
24+
// RUN: %check-in-clang %t/accessibility-appextlib.h
25+
2226
// REQUIRES: objc_interop
2327

2428
// CHECK: #ifndef ACCESSIBILITY_SWIFT_H

test/attr/attr_availability_transitive_ios_appext.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %target-typecheck-verify-swift -application-extension
2+
// RUN: %target-typecheck-verify-swift -application-extension-library
23
// REQUIRES: OS=ios
34

45
// Allow referencing unavailable API in situations where the caller is marked unavailable in the same circumstances.

0 commit comments

Comments
 (0)