Skip to content

[ScanDependency] Allow importing binary testable module when no interface #72908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/swift/Serialization/SerializedModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,10 @@ class SerializedModuleLoaderBase : public ModuleLoader {
}

/// Scan the given serialized module file to determine dependencies.
llvm::ErrorOr<ModuleDependencyInfo>
scanModuleFile(Twine modulePath, bool isFramework, bool isTestableImport);
llvm::ErrorOr<ModuleDependencyInfo> scanModuleFile(Twine modulePath,
bool isFramework,
bool isTestableImport,
bool hasInterface);

struct BinaryModuleImports {
llvm::StringSet<> moduleImports;
Expand Down
8 changes: 5 additions & 3 deletions lib/Serialization/ScanningLoaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ std::error_code SwiftModuleScanner::findModuleFilesInDirectory(
if (fs.exists(ModPath)) {
// The module file will be loaded directly.
auto dependencies =
scanModuleFile(ModPath, IsFramework, isTestableDependencyLookup);
scanModuleFile(ModPath, IsFramework, isTestableDependencyLookup,
/*hasInterface=*/false);
if (dependencies) {
this->dependencies = std::move(dependencies.get());
return std::error_code();
Expand Down Expand Up @@ -159,8 +160,9 @@ SwiftModuleScanner::scanInterfaceFile(Twine moduleInterfacePath,
!Ctx.SearchPathOpts.NoScannerModuleValidation) {
assert(compiledCandidates.size() == 1 &&
"Should only have 1 candidate module");
auto BinaryDep = scanModuleFile(compiledCandidates[0], isFramework,
isTestableImport);
auto BinaryDep =
scanModuleFile(compiledCandidates[0], isFramework,
isTestableImport, /*hasInterface=*/true);
if (BinaryDep) {
Result = *BinaryDep;
return std::error_code();
Expand Down
8 changes: 6 additions & 2 deletions lib/Serialization/SerializedModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,8 @@ SerializedModuleLoaderBase::getImportsOfModule(

llvm::ErrorOr<ModuleDependencyInfo>
SerializedModuleLoaderBase::scanModuleFile(Twine modulePath, bool isFramework,
bool isTestableImport) {
bool isTestableImport,
bool hasInterface) {
const std::string moduleDocPath;
const std::string sourceInfoPath;

Expand All @@ -455,7 +456,10 @@ SerializedModuleLoaderBase::scanModuleFile(Twine modulePath, bool isFramework,
return std::make_error_code(std::errc::no_such_file_or_directory);
}

if (loadedModuleFile->isTestable() && !isTestableImport) {
// If the module file has interface file and not testable imported, don't
// import the testable module because it contains more interfaces than
// needed and can pull in more dependencies.
if (loadedModuleFile->isTestable() && !isTestableImport && hasInterface) {
if (Ctx.LangOpts.EnableModuleLoadingRemarks)
Ctx.Diags.diagnose(SourceLoc(), diag::skip_module_testable,
modulePath.str());
Expand Down
4 changes: 2 additions & 2 deletions test/ScanDependencies/testable-dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib -enable-testing \
// RUN: -o %t/deps5.json -I %t/regular -swift-version 5 -Rmodule-loading

/// Regular import a testable module with no interface, this is a dependency scanning error.
/// Regular import a testable module with no interface, will try to import binary module but fail to look up the dependency.
// RUN: rm %t/testable/A.swiftinterface
// RUN: %target-swift-frontend -scan-dependencies -module-load-mode prefer-interface -module-name Test %t/main.swift \
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib -enable-testing \
// RUN: -o %t/deps6.json -I %t/testable -swift-version 5 -Rmodule-loading 2>&1 | %FileCheck %s --check-prefix ERROR
// ERROR: error: Unable to find module dependency: 'A'
// ERROR: error: Unable to find module dependency: 'B'

//--- main.swift
import A
Expand Down