Skip to content

Commit 70762fa

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 53ad5de commit 70762fa

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

include/swift/Serialization/SerializedModuleLoader.h

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

179179
/// Load the module file into a buffer and also collect its module name.
180180
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

@@ -471,12 +471,12 @@ SerializedModuleLoaderBase::scanModuleFile(Twine modulePath, bool isFramework,
471471
// optional.
472472
auto binaryModuleImports =
473473
getImportsOfModule(*loadedModuleFile, ModuleLoadingBehavior::Required,
474-
Ctx.LangOpts.PackageName);
474+
Ctx.LangOpts.PackageName, isTestableImport);
475475

476476
// Lookup optional imports of this module also
477477
auto binaryModuleOptionalImports =
478478
getImportsOfModule(*loadedModuleFile, ModuleLoadingBehavior::Optional,
479-
Ctx.LangOpts.PackageName);
479+
Ctx.LangOpts.PackageName, isTestableImport);
480480

481481
auto importedModuleSet = binaryModuleImports.moduleImports;
482482
std::vector<std::string> importedModuleNames;

test/ScanDependencies/testable-dependencies.swift

Lines changed: 8 additions & 4 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, this is a dependency scanning error.
60-
// RUN: rm %t/testable/A.swiftinterface
59+
/// Regular import a testable module with no interface, don't load optional dependencies.
60+
// RUN: rm %t/testable.sdk/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: 'A'
63+
// RUN: -o %t/deps7.json -I %t/testable.sdk -swift-version 5 -Rmodule-loading
64+
// RUN: %{python} %S/../CAS/Inputs/SwiftDepsExtractor.py %t/deps7.json Test directDependencies | %FileCheck %s --check-prefix TEST7
65+
// TEST7: "swiftPrebuiltExternal": "A"
66+
// RUN: %{python} %S/../CAS/Inputs/SwiftDepsExtractor.py %t/deps7.json swiftPrebuiltExternal:A directDependencies | %FileCheck %s --check-prefix TEST7-A
67+
// TEST7-A: []
68+
6569

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

0 commit comments

Comments
 (0)