Skip to content

Commit abddb06

Browse files
[ScanDependencies] Do not count optional dependencies when not needed
If a testable module is loaded from a non-testable import, ignore its optional dependencies because the consumer should not use them. This matches the behavior of the implicit build or the behavior how forwarding module is created.
1 parent bc4eeb8 commit abddb06

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class SerializedModuleLoaderBase : public ModuleLoader {
176176
static BinaryModuleImports
177177
getImportsOfModule(const ModuleFileSharedCore &loadedModule,
178178
ModuleLoadingBehavior transitiveBehavior,
179-
StringRef packageName);
179+
StringRef packageName, bool isTestableImport);
180180

181181
/// Load the module file into a buffer and also collect its module name.
182182
static std::unique_ptr<llvm::MemoryBuffer>

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,8 @@ std::error_code SerializedModuleLoaderBase::openModuleFile(
395395
SerializedModuleLoaderBase::BinaryModuleImports
396396
SerializedModuleLoaderBase::getImportsOfModule(
397397
const ModuleFileSharedCore &loadedModuleFile,
398-
ModuleLoadingBehavior transitiveBehavior, StringRef packageName) {
398+
ModuleLoadingBehavior transitiveBehavior, StringRef packageName,
399+
bool isTestableImport) {
399400
llvm::StringSet<> importedModuleNames;
400401
std::string importedHeader = "";
401402
for (const auto &dependency : loadedModuleFile.getDependencies()) {
@@ -410,8 +411,7 @@ SerializedModuleLoaderBase::getImportsOfModule(
410411
loadedModuleFile.getTransitiveLoadingBehavior(
411412
dependency,
412413
/*debuggerMode*/ false,
413-
/*isPartialModule*/ false, packageName,
414-
loadedModuleFile.isTestable());
414+
/*isPartialModule*/ false, packageName, isTestableImport);
415415
if (dependencyTransitiveBehavior > transitiveBehavior)
416416
continue;
417417

@@ -475,12 +475,12 @@ SerializedModuleLoaderBase::scanModuleFile(Twine modulePath, bool isFramework,
475475
// optional.
476476
auto binaryModuleImports =
477477
getImportsOfModule(*loadedModuleFile, ModuleLoadingBehavior::Required,
478-
Ctx.LangOpts.PackageName);
478+
Ctx.LangOpts.PackageName, isTestableImport);
479479

480480
// Lookup optional imports of this module also
481481
auto binaryModuleOptionalImports =
482482
getImportsOfModule(*loadedModuleFile, ModuleLoadingBehavior::Optional,
483-
Ctx.LangOpts.PackageName);
483+
Ctx.LangOpts.PackageName, isTestableImport);
484484

485485
auto importedModuleSet = binaryModuleImports.moduleImports;
486486
std::vector<std::string> importedModuleNames;

test/ScanDependencies/testable-dependencies.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,16 @@
5656
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib -enable-testing \
5757
// RUN: -o %t/deps5.json -I %t/regular -swift-version 5 -Rmodule-loading
5858

59-
/// Regular import a testable module with no interface, will try to import binary module but fail to look up the dependency.
59+
/// Regular import a testable module with no interface, don't load optional dependencies.
6060
// RUN: rm %t/testable/A.swiftinterface
6161
// RUN: %target-swift-frontend -scan-dependencies -module-load-mode prefer-interface -module-name Test %t/main.swift \
6262
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib -enable-testing \
63-
// RUN: -o %t/deps6.json -I %t/testable -swift-version 5 -Rmodule-loading 2>&1 | %FileCheck %s --check-prefix ERROR
64-
// ERROR: error: Unable to find module dependency: 'B'
63+
// RUN: -o %t/deps6.json -I %t/testable -swift-version 5 -Rmodule-loading
64+
// RUN: %{python} %S/../CAS/Inputs/SwiftDepsExtractor.py %t/deps6.json Test directDependencies | %FileCheck %s --check-prefix TEST6
65+
// TEST6: "swiftPrebuiltExternal": "A"
66+
// RUN: %{python} %S/../CAS/Inputs/SwiftDepsExtractor.py %t/deps6.json swiftPrebuiltExternal:A directDependencies | %FileCheck %s --check-prefix TEST6-A
67+
// TEST6-A: []
68+
6569

6670
//--- main.swift
6771
import A

0 commit comments

Comments
 (0)