Skip to content

Commit 276281d

Browse files
authored
Merge pull request #62476 from xymus/module-interface-export-as
[ModuleInterface] Ignore export_as in private swiftinterface
2 parents 2dc7585 + ab38752 commit 276281d

File tree

8 files changed

+141
-9
lines changed

8 files changed

+141
-9
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ struct PrintOptions {
649649
bool preferTypeRepr,
650650
bool printFullConvention,
651651
bool printSPIs,
652+
bool useExportedModuleNames,
652653
bool aliasModuleNames,
653654
llvm::SmallSet<StringRef, 4>
654655
*aliasModuleNamesTargets

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ EXPERIMENTAL_FEATURE(OneWayClosureParameters, false)
106106
EXPERIMENTAL_FEATURE(TypeWitnessSystemInference, false)
107107
EXPERIMENTAL_FEATURE(ResultBuilderASTTransform, true)
108108
EXPERIMENTAL_FEATURE(LayoutPrespecialization, false)
109+
EXPERIMENTAL_FEATURE(ModuleInterfaceExportAs, true)
109110

110111
/// Whether to enable experimental differentiable programming features:
111112
/// `@differentiable` declaration attribute, etc.

include/swift/Frontend/ModuleInterfaceSupport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ struct ModuleInterfaceOptions {
5050
/// ignored by the earlier version of the compiler.
5151
std::string IgnorableFlags;
5252

53-
/// Print SPI decls and attributes.
54-
bool PrintSPIs = false;
53+
/// Print for a private swiftinterface file, SPI decls and attributes.
54+
bool PrintPrivateInterfaceContent = false;
5555

5656
/// Print imports with both @_implementationOnly and @_spi, only applies
5757
/// when PrintSPIs is true.

lib/AST/ASTPrinter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
132132
bool preferTypeRepr,
133133
bool printFullConvention,
134134
bool printSPIs,
135+
bool useExportedModuleNames,
135136
bool aliasModuleNames,
136137
llvm::SmallSet<StringRef, 4>
137138
*aliasModuleNamesTargets
@@ -145,7 +146,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
145146
result.FullyQualifiedTypes = true;
146147
result.FullyQualifiedTypesIfAmbiguous = true;
147148
result.FullyQualifiedExtendedTypesIfAmbiguous = true;
148-
result.UseExportedModuleNames = true;
149+
result.UseExportedModuleNames = useExportedModuleNames;
149150
result.AllowNullTypes = false;
150151
result.SkipImports = true;
151152
result.OmitNameOfInaccessibleProperties = true;
@@ -3074,6 +3075,10 @@ static bool usesFeatureLayoutPrespecialization(Decl *decl) {
30743075
return false;
30753076
}
30763077

3078+
static bool usesFeatureModuleInterfaceExportAs(Decl *decl) {
3079+
return false;
3080+
}
3081+
30773082
static bool usesFeatureNamedOpaqueTypes(Decl *decl) {
30783083
return false;
30793084
}

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ static void ParseModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
384384
if (const Arg *A = Args.getLastArg(OPT_library_level)) {
385385
StringRef contents = A->getValue();
386386
if (contents == "spi") {
387-
Opts.PrintSPIs = true;
387+
Opts.PrintPrivateInterfaceContent = true;
388388
}
389389
}
390390
for (auto val: Args.getAllArgValues(OPT_skip_import_in_public_interface)) {

lib/Frontend/ModuleInterfaceSupport.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static void printImports(raw_ostream &out,
225225
// imports only if they are also SPI. First, list all implementation-only
226226
// imports and filter them later.
227227
llvm::SmallSet<ImportedModule, 4, ImportedModule::Order> ioiImportSet;
228-
if (Opts.PrintSPIs && Opts.ExperimentalSPIImports) {
228+
if (Opts.PrintPrivateInterfaceContent && Opts.ExperimentalSPIImports) {
229229

230230
SmallVector<ImportedModule, 4> ioiImports, allImports;
231231
M->getImportedModules(ioiImports,
@@ -246,7 +246,7 @@ static void printImports(raw_ostream &out,
246246

247247
/// Collect @_spiOnly imports that are not imported elsewhere publicly.
248248
llvm::SmallSet<ImportedModule, 4, ImportedModule::Order> spiOnlyImportSet;
249-
if (Opts.PrintSPIs) {
249+
if (Opts.PrintPrivateInterfaceContent) {
250250
SmallVector<ImportedModule, 4> spiOnlyImports, otherImports;
251251
M->getImportedModules(spiOnlyImports,
252252
ModuleDecl::ImportFilterKind::SPIOnly);
@@ -305,7 +305,7 @@ static void printImports(raw_ostream &out,
305305
if (publicImportSet.count(import))
306306
out << "@_exported ";
307307

308-
if (Opts.PrintSPIs) {
308+
if (Opts.PrintPrivateInterfaceContent) {
309309
// An import visible in the private swiftinterface only.
310310
//
311311
// In the long term, we want to print this attribute for consistency and
@@ -788,8 +788,18 @@ bool swift::emitSwiftInterface(raw_ostream &out,
788788

789789
printImports(out, Opts, M);
790790

791+
static bool forceUseExportedModuleNameInPublicOnly =
792+
getenv("SWIFT_DEBUG_USE_EXPORTED_MODULE_NAME_IN_PUBLIC_ONLY");
793+
bool useExportedModuleNameInPublicOnly =
794+
M->getASTContext().LangOpts.hasFeature(Feature::ModuleInterfaceExportAs) ||
795+
forceUseExportedModuleNameInPublicOnly;
796+
bool useExportedModuleNames = !(useExportedModuleNameInPublicOnly &&
797+
Opts.PrintPrivateInterfaceContent);
798+
791799
const PrintOptions printOptions = PrintOptions::printSwiftInterfaceFile(
792-
M, Opts.PreserveTypesAsWritten, Opts.PrintFullConvention, Opts.PrintSPIs,
800+
M, Opts.PreserveTypesAsWritten, Opts.PrintFullConvention,
801+
Opts.PrintPrivateInterfaceContent,
802+
useExportedModuleNames,
793803
Opts.AliasModuleNames, &aliasModuleNamesTargets);
794804
InheritedProtocolCollector::PerTypeMap inheritedProtocolMap;
795805

lib/FrontendTool/FrontendTool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs(
949949
if (opts.InputsAndOutputs.hasPrivateModuleInterfaceOutputPath()) {
950950
// Copy the settings from the module interface to add SPI printing.
951951
ModuleInterfaceOptions privOpts = Invocation.getModuleInterfaceOptions();
952-
privOpts.PrintSPIs = true;
952+
privOpts.PrintPrivateInterfaceContent = true;
953953
privOpts.ModulesToSkipInPublicInterface.clear();
954954

955955
hadAnyError |= printModuleInterfaceIfNeeded(
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/// Test the logic printing the export_as name in public swiftinterfaces
2+
/// and the real source module name in the private swiftinterfaces.
3+
4+
// RUN: %empty-directory(%t)
5+
// RUN: split-file %s %t
6+
7+
/// Build lib with an export_as.
8+
// RUN: %target-swift-frontend -emit-module %t/Exported.swift \
9+
// RUN: -module-name Exported -swift-version 5 -I %t \
10+
// RUN: -enable-library-evolution \
11+
// RUN: -emit-module-path %t/Exported.swiftmodule \
12+
// RUN: -emit-module-interface-path %t/Exported.swiftinterface \
13+
// RUN: -emit-private-module-interface-path %t/Exported.private.swiftinterface \
14+
// RUN: -enable-experimental-feature ModuleInterfaceExportAs
15+
// RUN: %target-swift-typecheck-module-from-interface(%t/Exported.private.swiftinterface) -module-name Exported -I %t
16+
// RUN: cat %t/Exported.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTER %s
17+
// RUN: cat %t/Exported.private.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTED %s
18+
19+
/// The public swiftinterface only builds under the name of Exporter.
20+
// RUN: sed -e "s/module-name Exported/module-name Exporter/" -ibk %t/Exported.swiftinterface
21+
// RUN: %target-swift-typecheck-module-from-interface(%t/Exported.swiftinterface) -I %t -module-name Exporter
22+
23+
/// Build lib with an @exported import of the exported one.
24+
/// Default/old behavior.
25+
// RUN: %target-swift-frontend -emit-module %t/Exporter.swift \
26+
// RUN: -module-name Exporter -swift-version 5 -I %t \
27+
// RUN: -enable-library-evolution \
28+
// RUN: -emit-module-path %t/Exporter.swiftmodule \
29+
// RUN: -emit-module-interface-path %t/Exporter.swiftinterface \
30+
// RUN: -emit-private-module-interface-path %t/Exporter.private.swiftinterface
31+
// RUN: %target-swift-typecheck-module-from-interface(%t/Exporter.swiftinterface) -I %t
32+
// RUN: %target-swift-typecheck-module-from-interface(%t/Exporter.private.swiftinterface) -module-name Exporter -I %t
33+
// RUN: cat %t/Exporter.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTER %s
34+
// RUN: cat %t/Exporter.private.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTER %s
35+
36+
/// Build lib with an @exported import of the exported one.
37+
/// New behavior via flag.
38+
// RUN: %target-swift-frontend -emit-module %t/Exporter.swift \
39+
// RUN: -module-name Exporter -swift-version 5 -I %t \
40+
// RUN: -enable-library-evolution \
41+
// RUN: -emit-module-path %t/Exporter.swiftmodule \
42+
// RUN: -emit-module-interface-path %t/Exporter.swiftinterface \
43+
// RUN: -emit-private-module-interface-path %t/Exporter.private.swiftinterface \
44+
// RUN: -enable-experimental-feature ModuleInterfaceExportAs
45+
// RUN: %target-swift-typecheck-module-from-interface(%t/Exporter.swiftinterface) -I %t
46+
// RUN: %target-swift-typecheck-module-from-interface(%t/Exporter.private.swiftinterface) -module-name Exporter -I %t
47+
// RUN: cat %t/Exporter.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTER %s
48+
// RUN: cat %t/Exporter.private.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTED %s
49+
50+
/// Build lib with an @exported import of the exported one.
51+
/// New behavior via env var.
52+
// RUN: env SWIFT_DEBUG_USE_EXPORTED_MODULE_NAME_IN_PUBLIC_ONLY=1 \
53+
// RUN: %target-swift-frontend -emit-module %t/Exporter.swift \
54+
// RUN: -module-name Exporter -swift-version 5 -I %t \
55+
// RUN: -enable-library-evolution \
56+
// RUN: -emit-module-path %t/Exporter.swiftmodule \
57+
// RUN: -emit-module-interface-path %t/Exporter.swiftinterface \
58+
// RUN: -emit-private-module-interface-path %t/Exporter.private.swiftinterface
59+
// RUN: %target-swift-typecheck-module-from-interface(%t/Exporter.swiftinterface) -I %t
60+
// RUN: %target-swift-typecheck-module-from-interface(%t/Exporter.private.swiftinterface) -module-name Exporter -I %t
61+
// RUN: cat %t/Exporter.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTER %s
62+
// RUN: cat %t/Exporter.private.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTED %s
63+
64+
/// Build a client of the exporter lib.
65+
// RUN: %target-swift-frontend -emit-module %t/Client.swift \
66+
// RUN: -module-name Client -swift-version 5 -I %t \
67+
// RUN: -enable-library-evolution \
68+
// RUN: -emit-module-path %t/Client.swiftmodule \
69+
// RUN: -emit-module-interface-path %t/Client.swiftinterface \
70+
// RUN: -emit-private-module-interface-path %t/Client.private.swiftinterface \
71+
// RUN: -enable-experimental-feature ModuleInterfaceExportAs
72+
// RUN: %target-swift-typecheck-module-from-interface(%t/Client.swiftinterface) -I %t
73+
// RUN: %target-swift-typecheck-module-from-interface(%t/Client.private.swiftinterface) -module-name Client -I %t
74+
// RUN: cat %t/Client.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTER %s
75+
// RUN: cat %t/Client.private.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTED %s
76+
77+
/// Build a client of the exporter lib.
78+
// RUN: rm %t/Exporter.private.swiftinterface %t/Exporter.swiftmodule
79+
// RUN: %target-swift-frontend -emit-module %t/Client.swift \
80+
// RUN: -module-name Client -swift-version 5 -I %t \
81+
// RUN: -enable-library-evolution \
82+
// RUN: -emit-module-path %t/Client.swiftmodule \
83+
// RUN: -emit-module-interface-path %t/Client.swiftinterface \
84+
// RUN: -emit-private-module-interface-path %t/Client.private.swiftinterface \
85+
// RUN: -enable-experimental-feature ModuleInterfaceExportAs
86+
// RUN: %target-swift-typecheck-module-from-interface(%t/Client.swiftinterface) -I %t
87+
// RUN: %target-swift-typecheck-module-from-interface(%t/Client.private.swiftinterface) -module-name Client -I %t
88+
// RUN: cat %t/Client.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTER %s
89+
// RUN: cat %t/Client.private.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTED %s
90+
91+
//--- module.modulemap
92+
module Exported {
93+
export_as Exporter
94+
header "Exported.h"
95+
}
96+
97+
//--- Exported.h
98+
struct exportedClangType {};
99+
100+
//--- Exported.swift
101+
@_exported import Exported
102+
103+
public func foo(a: exportedClangType) {}
104+
// CHECK-USE-EXPORTED: Exported.exportedClangType
105+
// CHECK-USE-EXPORTER: Exporter.exportedClangType
106+
107+
//--- Exporter.swift
108+
@_exported import Exported
109+
110+
public func foo(a: exportedClangType) {}
111+
112+
//--- Client.swift
113+
import Exporter
114+
115+
public func foo(a: exportedClangType) {}

0 commit comments

Comments
 (0)