Skip to content

Commit 857254b

Browse files
authored
[5.10] Introduce -application-extension-library flag (#69639)
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 c05dd0b commit 857254b

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
@@ -847,6 +847,10 @@ def enable_app_extension : Flag<["-"], "application-extension">,
847847
Flags<[FrontendOption, NoInteractiveOption]>,
848848
HelpText<"Restrict code to those available for App Extensions">;
849849

850+
def enable_app_extension_library : Flag<["-"], "application-extension-library">,
851+
Flags<[FrontendOption, NoInteractiveOption]>,
852+
HelpText<"Restrict code to those available for App Extension Libraries">;
853+
850854
def libc : Separate<["-"], "libc">,
851855
Flags<[SwiftAPIExtractOption, SwiftSymbolGraphExtractOption]>,
852856
HelpText<"libc runtime library to use">;

lib/AST/Module.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2697,7 +2697,9 @@ bool ModuleDecl::isExternallyConsumed() const {
26972697
// App extensions are special beasts because they build without entrypoints
26982698
// like library targets, but they behave like executable targets because
26992699
// their associated modules are not suitable for distribution.
2700-
if (getASTContext().LangOpts.EnableAppExtensionRestrictions) {
2700+
// However, app extension libraries might be consumed externally.
2701+
if (getASTContext().LangOpts.EnableAppExtensionRestrictions &&
2702+
!getASTContext().LangOpts.EnableAppExtensionLibraryRestrictions) {
27012703
return false;
27022704
}
27032705

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
252252
inputArgs.AddLastArg(arguments, options::OPT_warn_implicit_overrides);
253253
inputArgs.AddLastArg(arguments, options::OPT_typo_correction_limit);
254254
inputArgs.AddLastArg(arguments, options::OPT_enable_app_extension);
255+
inputArgs.AddLastArg(arguments, options::OPT_enable_app_extension_library);
255256
inputArgs.AddLastArg(arguments, options::OPT_enable_library_evolution);
256257
inputArgs.AddLastArg(arguments, options::OPT_require_explicit_availability);
257258
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
@@ -886,7 +886,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
886886

887887
Opts.Features.insert(Feature::LayoutPrespecialization);
888888

889+
Opts.EnableAppExtensionLibraryRestrictions |= Args.hasArg(OPT_enable_app_extension_library);
889890
Opts.EnableAppExtensionRestrictions |= Args.hasArg(OPT_enable_app_extension);
891+
Opts.EnableAppExtensionRestrictions |= Opts.EnableAppExtensionLibraryRestrictions;
890892

891893
Opts.EnableSwift3ObjCInference =
892894
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)