[ScanDependencies] Make sure canImport resolution agrees with import

Fix the problem that when the only module can be found is an
invalid/out-of-date swift binary module, canImport and import statement
can have different view for if the module can be imported or not.

Now canImport will evaluate to false if the only module can be found for
name is an invalid swiftmodule, with a warning with the path to the
module so users will not be surprised by such behavior.

rdar://128876895
This commit is contained in:
Steven Wu
2024-06-06 19:43:02 -07:00
parent 8582828e7a
commit 7d85aa423d
16 changed files with 109 additions and 53 deletions

View File

@@ -33,6 +33,7 @@
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/VersionTuple.h"
#include "llvm/TargetParser/Host.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
@@ -1391,7 +1392,7 @@ swift::extractUserModuleVersionFromInterface(StringRef moduleInterfacePath) {
}
bool SerializedModuleLoaderBase::canImportModule(
ImportPath::Module path, ModuleVersionInfo *versionInfo,
ImportPath::Module path, SourceLoc loc, ModuleVersionInfo *versionInfo,
bool isTestableDependencyLookup) {
// FIXME: Swift submodules?
if (path.hasSubmodule())
@@ -1414,36 +1415,55 @@ bool SerializedModuleLoaderBase::canImportModule(
if (!found)
return false;
// If the caller doesn't want version info we're done.
if (!versionInfo)
return true;
assert(found);
llvm::VersionTuple swiftInterfaceVersion;
if (!moduleInterfaceSourcePath.empty()) {
swiftInterfaceVersion =
// If we found interface and version is not requested, we're done.
if (!versionInfo)
return true;
auto moduleVersion =
extractUserModuleVersionFromInterface(moduleInterfaceSourcePath);
// If version is requested and found in interface, return the version.
// Otherwise fallback to binary module handling.
if (!moduleVersion.empty()) {
versionInfo->setVersion(moduleVersion,
ModuleVersionSourceKind::SwiftInterface);
return true;
}
}
// If failing to extract the user version from the interface file, try the
// binary module format, if present.
if (swiftInterfaceVersion.empty() && moduleInputBuffer) {
if (moduleInputBuffer) {
auto metaData = serialization::validateSerializedAST(
moduleInputBuffer->getBuffer(),
Ctx.SILOpts.EnableOSSAModules,
moduleInputBuffer->getBuffer(), Ctx.SILOpts.EnableOSSAModules,
Ctx.LangOpts.SDKName);
versionInfo->setVersion(metaData.userModuleVersion,
// If we only found binary module, make sure that is valid.
if (metaData.status != serialization::Status::Valid &&
moduleInterfaceSourcePath.empty()) {
// Emit warning if the canImport check location is known.
if (loc.isValid())
Ctx.Diags.diagnose(loc, diag::can_import_invalid_swiftmodule,
moduleInputBuffer->getBufferIdentifier());
return false;
}
if (versionInfo)
versionInfo->setVersion(metaData.userModuleVersion,
ModuleVersionSourceKind::SwiftBinaryModule);
}
if (versionInfo && !versionInfo->isValid()) {
// If no version is found, set it to empty version.
versionInfo->setVersion(llvm::VersionTuple(),
ModuleVersionSourceKind::SwiftBinaryModule);
} else {
versionInfo->setVersion(swiftInterfaceVersion,
ModuleVersionSourceKind::SwiftInterface);
}
return true;
}
bool MemoryBufferSerializedModuleLoader::canImportModule(
ImportPath::Module path, ModuleVersionInfo *versionInfo,
ImportPath::Module path, SourceLoc loc, ModuleVersionInfo *versionInfo,
bool isTestableDependencyLookup) {
// FIXME: Swift submodules?
if (path.hasSubmodule())