Skip to content

Commit 2c994e7

Browse files
committed
Merge remote-tracking branch 'origin/main' into rebranch
2 parents 076e338 + 83f8454 commit 2c994e7

File tree

8 files changed

+87
-46
lines changed

8 files changed

+87
-46
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ class ModuleDependencyInfoStorageBase {
116116

117117
ModuleDependencyInfoStorageBase(ModuleDependencyKind dependencyKind,
118118
const std::vector<std::string> &moduleImports,
119+
const std::vector<std::string> &optionalModuleImports,
119120
StringRef moduleCacheKey = "")
120121
: dependencyKind(dependencyKind), moduleImports(moduleImports),
122+
optionalModuleImports(optionalModuleImports),
121123
moduleCacheKey(moduleCacheKey.str()), resolved(false), finalized(false) {}
122124

123125
virtual ModuleDependencyInfoStorageBase *clone() const = 0;
@@ -296,11 +298,12 @@ class SwiftBinaryModuleDependencyStorage : public ModuleDependencyInfoStorageBas
296298
const std::string &moduleDocPath,
297299
const std::string &sourceInfoPath,
298300
const std::vector<std::string> &moduleImports,
301+
const std::vector<std::string> &optionalModuleImports,
299302
const std::vector<std::string> &headerImports,
300303
const bool isFramework,
301304
const std::string &moduleCacheKey)
302305
: ModuleDependencyInfoStorageBase(ModuleDependencyKind::SwiftBinary,
303-
moduleImports, moduleCacheKey),
306+
moduleImports, optionalModuleImports, moduleCacheKey),
304307
compiledModulePath(compiledModulePath), moduleDocPath(moduleDocPath),
305308
sourceInfoPath(sourceInfoPath), preCompiledBridgingHeaderPaths(headerImports),
306309
isFramework(isFramework) {}
@@ -471,12 +474,14 @@ class ModuleDependencyInfo {
471474
const std::string &moduleDocPath,
472475
const std::string &sourceInfoPath,
473476
const std::vector<std::string> &moduleImports,
477+
const std::vector<std::string> &optionalModuleImports,
474478
const std::vector<std::string> &headerImports,
475479
bool isFramework, const std::string &moduleCacheKey) {
476480
return ModuleDependencyInfo(
477481
std::make_unique<SwiftBinaryModuleDependencyStorage>(
478482
compiledModulePath, moduleDocPath, sourceInfoPath,
479-
moduleImports, headerImports, isFramework, moduleCacheKey));
483+
moduleImports, optionalModuleImports,
484+
headerImports, isFramework, moduleCacheKey));
480485
}
481486

482487
/// Describe the main Swift module.

include/swift/DependencyScan/SerializedModuleDependencyCacheFormat.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ using llvm::BCVBR;
3939

4040
/// Every .moddepcache file begins with these 4 bytes, for easy identification.
4141
const unsigned char MODULE_DEPENDENCY_CACHE_FORMAT_SIGNATURE[] = {'I', 'M', 'D','C'};
42-
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MAJOR = 4;
42+
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MAJOR = 5; // optionalModuleImports
4343
/// Increment this on every change.
4444
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MINOR = 1;
4545

@@ -124,6 +124,7 @@ using ModuleInfoLayout =
124124
IdentifierIDField, // moduleName
125125
ContextHashIDField, // contextHash
126126
ImportArrayIDField, // moduleImports
127+
ImportArrayIDField, // optionalModuleImports
127128
DependencyIDArrayIDField // resolvedDirectModuleDependencies
128129
>;
129130

lib/DependencyScan/ModuleDependencyCacheSerialization.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ bool ModuleDependenciesCacheDeserializer::readGraph(SwiftDependencyScanningServi
155155
std::string currentModuleName;
156156
unsigned currentContextHashID;
157157
llvm::Optional<std::vector<std::string>> currentModuleImports;
158+
llvm::Optional<std::vector<std::string>> currentOptionalModuleImports;
158159
llvm::Optional<std::vector<ModuleDependencyID>> currentModuleDependencyIDs;
159160

160161
auto getContextHash = [&]() {
@@ -212,19 +213,24 @@ bool ModuleDependenciesCacheDeserializer::readGraph(SwiftDependencyScanningServi
212213
case MODULE_NODE: {
213214
hasCurrentModule = true;
214215
unsigned moduleNameID, contextHashID,
215-
moduleImportsArrayID, moduleDependencyIDArrayID;
216+
moduleImportsArrayID, optionalModuleImportsArrayID,
217+
moduleDependencyIDArrayID;
216218
ModuleInfoLayout::readRecord(Scratch, moduleNameID, contextHashID,
217219
moduleImportsArrayID,
220+
optionalModuleImportsArrayID,
218221
moduleDependencyIDArrayID);
219222
auto moduleName = getIdentifier(moduleNameID);
220223
if (!moduleName)
221224
llvm::report_fatal_error("Bad module name");
222225
currentModuleName = *moduleName;
223226
currentContextHashID = contextHashID;
224227
currentModuleImports = getStringArray(moduleImportsArrayID);
228+
currentOptionalModuleImports = getStringArray(optionalModuleImportsArrayID);
225229
currentModuleDependencyIDs = getModuleDependencyIDArray(moduleDependencyIDArrayID);
226230
if (!currentModuleImports)
227231
llvm::report_fatal_error("Bad direct dependencies: no imports");
232+
if (!currentOptionalModuleImports)
233+
llvm::report_fatal_error("Bad direct dependencies: no optional imports");
228234
if (!currentModuleDependencyIDs)
229235
llvm::report_fatal_error("Bad direct dependencies: no qualified dependencies");
230236
break;
@@ -296,6 +302,9 @@ bool ModuleDependenciesCacheDeserializer::readGraph(SwiftDependencyScanningServi
296302
// Add imports of this module
297303
for (const auto &moduleName : *currentModuleImports)
298304
moduleDep.addModuleImport(moduleName);
305+
// Add optional imports of this module
306+
for (const auto &moduleName : *currentOptionalModuleImports)
307+
moduleDep.addOptionalModuleImport(moduleName);
299308

300309
// Add qualified dependencies of this module
301310
moduleDep.resolveDirectDependencies(*currentModuleDependencyIDs);
@@ -404,6 +413,9 @@ bool ModuleDependenciesCacheDeserializer::readGraph(SwiftDependencyScanningServi
404413
// Add dependencies of this module
405414
for (const auto &moduleName : *currentModuleImports)
406415
moduleDep.addModuleImport(moduleName);
416+
// Add optional imports of this module
417+
for (const auto &moduleName : *currentOptionalModuleImports)
418+
moduleDep.addOptionalModuleImport(moduleName);
407419

408420
// Add bridging header file path
409421
if (bridgingHeaderFileID != 0) {
@@ -488,8 +500,8 @@ bool ModuleDependenciesCacheDeserializer::readGraph(SwiftDependencyScanningServi
488500
// Form the dependencies storage object
489501
auto moduleDep = ModuleDependencyInfo::forSwiftBinaryModule(
490502
*compiledModulePath, *moduleDocPath, *moduleSourceInfoPath,
491-
*currentModuleImports, *headerImports, isFramework,
492-
*moduleCacheKey);
503+
*currentModuleImports, *currentOptionalModuleImports,
504+
*headerImports, isFramework, *moduleCacheKey);
493505

494506
cache.recordDependency(currentModuleName, std::move(moduleDep),
495507
getContextHash());
@@ -523,6 +535,9 @@ bool ModuleDependenciesCacheDeserializer::readGraph(SwiftDependencyScanningServi
523535
// Add dependencies of this module
524536
for (const auto &moduleName : *currentModuleImports)
525537
moduleDep.addModuleImport(moduleName);
538+
// Add optional imports of this module
539+
for (const auto &moduleName : *currentOptionalModuleImports)
540+
moduleDep.addOptionalModuleImport(moduleName);
526541

527542
cache.recordDependency(currentModuleName, std::move(moduleDep),
528543
getContextHash());
@@ -581,6 +596,9 @@ bool ModuleDependenciesCacheDeserializer::readGraph(SwiftDependencyScanningServi
581596
// Add dependencies of this module
582597
for (const auto &moduleName : *currentModuleImports)
583598
moduleDep.addModuleImport(moduleName);
599+
// Add optional imports of this module
600+
for (const auto &moduleName : *currentOptionalModuleImports)
601+
moduleDep.addOptionalModuleImport(moduleName);
584602

585603
cache.recordDependency(currentModuleName, std::move(moduleDep),
586604
getContextHash());
@@ -707,6 +725,7 @@ bool swift::dependencies::module_dependency_cache_serialization::
707725
enum ModuleIdentifierArrayKind : uint8_t {
708726
Empty = 0,
709727
DependencyImports,
728+
OptionalDependencyImports,
710729
DependencyHeaders,
711730
QualifiedModuleDependencyIDs,
712731
CompiledModuleCandidates,
@@ -904,6 +923,7 @@ void ModuleDependenciesCacheSerializer::writeModuleInfo(
904923
Out, ScratchRecord, AbbrCodes[ModuleInfoLayout::Code],
905924
getIdentifier(moduleID.first), contextHashStrID,
906925
getArrayID(moduleID, ModuleIdentifierArrayKind::DependencyImports),
926+
getArrayID(moduleID, ModuleIdentifierArrayKind::OptionalDependencyImports),
907927
getArrayID(moduleID, ModuleIdentifierArrayKind::QualifiedModuleDependencyIDs));
908928

909929
switch (dependencyInfo.getKind()) {
@@ -1112,6 +1132,8 @@ void ModuleDependenciesCacheSerializer::collectStringsAndArrays(
11121132
// Add the module's dependencies
11131133
addStringArray(moduleID, ModuleIdentifierArrayKind::DependencyImports,
11141134
dependencyInfo->getModuleImports());
1135+
addStringArray(moduleID, ModuleIdentifierArrayKind::OptionalDependencyImports,
1136+
dependencyInfo->getOptionalModuleImports());
11151137
addDependencyIDArray(
11161138
moduleID, ModuleIdentifierArrayKind::QualifiedModuleDependencyIDs,
11171139
dependencyInfo->getDirectModuleDependencies());

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,15 @@ SerializedModuleLoaderBase::scanModuleFile(Twine modulePath, bool isFramework) {
457457
if (!binaryModuleImports)
458458
return binaryModuleImports.getError();
459459

460+
// Lookup optional imports of this module also
461+
auto binaryModuleOptionalImports = getImportsOfModule(
462+
modulePath, ModuleLoadingBehavior::Optional, isFramework,
463+
isRequiredOSSAModules(), Ctx.LangOpts.SDKName, Ctx.LangOpts.PackageName,
464+
Ctx.SourceMgr.getFileSystem().get(),
465+
Ctx.SearchPathOpts.DeserializedPathRecoverer);
466+
if (!binaryModuleOptionalImports)
467+
return binaryModuleImports.getError();
468+
460469
auto importedModuleSet = binaryModuleImports.get().moduleImports;
461470
std::vector<std::string> importedModuleNames;
462471
importedModuleNames.reserve(importedModuleSet.size());
@@ -475,11 +484,17 @@ SerializedModuleLoaderBase::scanModuleFile(Twine modulePath, bool isFramework) {
475484
return N.str();
476485
});
477486

487+
auto &importedOptionalModuleSet = binaryModuleOptionalImports.get().moduleImports;
488+
std::vector<std::string> importedOptionalModuleNames;
489+
for (const auto optionalImportedModule : importedOptionalModuleSet.keys())
490+
if (!importedModuleSet.contains(optionalImportedModule))
491+
importedOptionalModuleNames.push_back(optionalImportedModule.str());
492+
478493
// Map the set of dependencies over to the "module dependencies".
479494
auto dependencies = ModuleDependencyInfo::forSwiftBinaryModule(
480495
modulePath.str(), moduleDocPath, sourceInfoPath,
481-
importedModuleNames, importedHeaders, isFramework,
482-
/*module-cache-key*/ "");
496+
importedModuleNames, importedOptionalModuleNames,
497+
importedHeaders, isFramework, /*module-cache-key*/ "");
483498

484499
return std::move(dependencies);
485500
}

stdlib/public/core/CMakeLists.txt

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@
1010
#
1111
#===----------------------------------------------------------------------===#
1212

13-
# The list of sources without which it's impossible to build a core
14-
# standard library. Try to add new standard library sources to
15-
# SWIFTLIB_SOURCES, below, rather than SWIFTLIB_ESSENTIAL, if
16-
# possible, to improve layering. Check that you got it right by
17-
# configuring with -DSWIFT_CHECK_ESSENTIAL_STDLIB=YES
18-
set(SWIFTLIB_ESSENTIAL
19-
### PLEASE KEEP THIS LIST IN ALPHABETICAL ORDER ###
13+
# The complete list of sources in the core standard library.
14+
set(SWIFTLIB_SOURCES
15+
### "ESSENTIAL" SOURCES
16+
### -- PLEASE KEEP THIS LIST IN ALPHABETICAL ORDER ###
2017
# Some files can't be sorted alphabetically, see notes in the list below.
2118
Algorithm.swift
2219
ArrayBody.swift
@@ -199,22 +196,10 @@ set(SWIFTLIB_ESSENTIAL
199196
StringGraphemeBreaking.swift # ORDER DEPENDENCY: Must follow UTF16.swift
200197
ValidUTF8Buffer.swift
201198
WriteBackMutableSlice.swift
202-
MigrationSupport.swift)
203-
204-
set(SWIFTLIB_ESSENTIAL_GYB_SOURCES
205-
AtomicInt.swift.gyb
206-
FloatingPointParsing.swift.gyb
207-
FloatingPointTypes.swift.gyb
208-
IntegerTypes.swift.gyb
209-
UnsafeBufferPointer.swift.gyb
210-
UnsafeRawBufferPointer.swift.gyb
211-
)
199+
MigrationSupport.swift
212200

213-
# The complete list of sources in the core standard library. Includes
214-
# all the essential sources listed above.
215-
set(SWIFTLIB_SOURCES
216-
${SWIFTLIB_ESSENTIAL}
217-
### PLEASE KEEP THIS LIST IN ALPHABETICAL ORDER ###
201+
### "NON-ESSENTIAL" SOURCES, LAYERED ON TOP OF THE "ESSENTIAL" ONES
202+
### -- PLEASE KEEP THIS LIST IN ALPHABETICAL ORDER ###
218203
Availability.swift
219204
CollectionDifference.swift
220205
CollectionOfOne.swift
@@ -236,7 +221,12 @@ set(SWIFTLIB_SOURCES
236221
)
237222

238223
set(SWIFTLIB_GYB_SOURCES
239-
${SWIFTLIB_ESSENTIAL_GYB_SOURCES}
224+
AtomicInt.swift.gyb
225+
FloatingPointParsing.swift.gyb
226+
FloatingPointTypes.swift.gyb
227+
IntegerTypes.swift.gyb
228+
UnsafeBufferPointer.swift.gyb
229+
UnsafeRawBufferPointer.swift.gyb
240230
Int128.swift.gyb
241231
Tuple.swift.gyb
242232
)
@@ -267,10 +257,6 @@ elseif(SWIFT_PRIMARY_VARIANT_SDK STREQUAL "WINDOWS")
267257
list(APPEND swift_core_private_link_libraries shell32;DbgHelp;Synchronization)
268258
endif()
269259

270-
option(SWIFT_CHECK_ESSENTIAL_STDLIB
271-
"Check core standard library layering by linking its essential subset"
272-
FALSE)
273-
274260
if(NOT "${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
275261
list(APPEND swift_stdlib_compile_flags "-Xllvm" "-sil-inline-generics")
276262
list(APPEND swift_stdlib_compile_flags "-Xllvm" "-sil-partial-specialization")
@@ -292,15 +278,6 @@ list(APPEND swift_stdlib_compile_flags "-Xfrontend" "-enable-experimental-concis
292278
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "Macros")
293279
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "FreestandingMacros")
294280

295-
if(SWIFT_CHECK_ESSENTIAL_STDLIB)
296-
add_swift_target_library(swift_stdlib_essential ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB IS_STDLIB_CORE
297-
INSTALL_IN_COMPONENT never_install
298-
${SWIFTLIB_ESSENTIAL}
299-
GYB_SOURCES
300-
${SWIFTLIB_ESSENTIAL_GYB_SOURCES})
301-
target_link_libraries(swift_stdlib_essential ${RUNTIME_DEPENDENCY})
302-
endif()
303-
304281
set(swift_core_incorporate_object_libraries)
305282
list(APPEND swift_core_incorporate_object_libraries swiftRuntime)
306283
list(APPEND swift_core_incorporate_object_libraries swiftLLVMSupport)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/clang-module-cache)
3+
// RUN: %empty-directory(%t/Foo.swiftmodule)
4+
// RUN: echo "@_implementationOnly import A; public func foo() {}" > %t/Foo.swift
5+
// REQUIRES: executable_test
6+
// REQUIRES: objc_interop
7+
8+
@testable import Foo
9+
10+
// Step 1: build a binary swift module for `Foo`, make it testable
11+
// RUN: %target-swift-frontend -emit-module %t/Foo.swift -emit-module-path %t/Foo.swiftmodule/%target-swiftmodule-name -module-name Foo -I %S/Inputs/CHeaders -I %S/Inputs/Swift -enable-testing
12+
13+
// Step 2: scan dependencies
14+
// RUN: %target-swift-frontend -scan-dependencies %s -o %t/deps.json -I %t -sdk %t -prebuilt-module-cache-path %t/clang-module-cache -I %S/Inputs/CHeaders -I %S/Inputs/Swift
15+
// RUN: %validate-json %t/deps.json | %FileCheck %s
16+
17+
// The dependency of `Foo` on `A` will not be visible if the scanner simply scans the textual interface
18+
// of `Foo`. So we verify that for a `@testable` import, the scanner also opens up the adjacent binary module and
19+
// attemtps to resolve optional dependencies contained within.
20+
//
21+
// CHECK: "swift": "A"

test/ScanDependencies/optional_deps_of_testable_imports.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// Step 1: build swift interface and swift module side by side, make them testable
1111
// RUN: %target-swift-frontend -emit-module %t/Foo.swift -emit-module-path %t/Foo.swiftmodule/%target-swiftmodule-name -module-name Foo -emit-module-interface-path %t/Foo.swiftmodule/%target-swiftinterface-name -I %S/Inputs/CHeaders -I %S/Inputs/Swift -enable-testing
1212

13-
// Step 3: scan dependencies
13+
// Step 2: scan dependencies
1414
// RUN: %target-swift-frontend -scan-dependencies %s -o %t/deps.json -I %t -sdk %t -prebuilt-module-cache-path %t/clang-module-cache -I %S/Inputs/CHeaders -I %S/Inputs/Swift
1515
// RUN: %validate-json %t/deps.json | %FileCheck %s
1616

test/ScanDependencies/required_deps_of_testable_imports.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// Step 1: build swift interface and swift module side by side, make them testable
1111
// RUN: %target-swift-frontend -emit-module %t/Foo.swift -emit-module-path %t/Foo.swiftmodule/%target-swiftmodule-name -module-name Foo -emit-module-interface-path %t/Foo.swiftmodule/%target-swiftinterface-name -I %S/Inputs/CHeaders -I %S/Inputs/Swift -enable-testing -enable-experimental-feature AccessLevelOnImport
1212

13-
// Step 3: scan dependencies
13+
// Step 2: scan dependencies
1414
// RUN: %target-swift-frontend -scan-dependencies %s -o %t/deps.json -I %t -sdk %t -prebuilt-module-cache-path %t/clang-module-cache -I %S/Inputs/CHeaders -I %S/Inputs/Swift
1515
// RUN: %validate-json %t/deps.json | %FileCheck %s
1616

0 commit comments

Comments
 (0)