mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
AST: Make the versioned variants of #if canImport() more reliable and consistent.
Previously, when evaluating a `#if canImport(Module, _version: 42)` directive the compiler could diagnose and ignore the directive under the following conditions: - The associated binary module is corrupt/bogus. - The .tbd for an underlying Clang module is missing a current-version field. This behavior is surprising when there is a valid `.swiftinterface` available and it only becomes apparent when building against an SDK with an old enough version of the module that the version in the `.swiftinterface` is too low, making this failure easy to miss. Some modules have different versioning systems for their Swift and Clang modules and it can also be intentional for a distributed binary `.swiftmodule` to contain bogus data (to force the compiler to recompile the `.swiftinterface`) so we need to handle both of these cases gracefully and predictably. Now the compiler will enumerate all module loaders, ask each of them to attempt to parse the module version and then consistently use the parsed version from a single source. The `.swiftinterface` is preferred if present, then the binary module if present, and then finally the `.tbd`. The `.tbd` is still always used exclusively for the `_underlyingVersion` variant of `canImport()`. Resolves rdar://88723492
This commit is contained in:
@@ -1125,15 +1125,12 @@ swift::extractUserModuleVersionFromInterface(StringRef moduleInterfacePath) {
|
||||
return result;
|
||||
}
|
||||
|
||||
bool SerializedModuleLoaderBase::canImportModule(ImportPath::Module path,
|
||||
llvm::VersionTuple version,
|
||||
bool underlyingVersion) {
|
||||
bool SerializedModuleLoaderBase::canImportModule(
|
||||
ImportPath::Module path, ModuleVersionInfo *versionInfo) {
|
||||
// FIXME: Swift submodules?
|
||||
if (path.hasSubmodule())
|
||||
return false;
|
||||
// If underlying version is specified, this should be handled by Clang importer.
|
||||
if (!version.empty() && underlyingVersion)
|
||||
return false;
|
||||
|
||||
// Look on disk.
|
||||
SmallVectorImpl<char> *unusedModuleInterfacePath = nullptr;
|
||||
std::unique_ptr<llvm::MemoryBuffer> *unusedModuleBuffer = nullptr;
|
||||
@@ -1145,7 +1142,7 @@ bool SerializedModuleLoaderBase::canImportModule(ImportPath::Module path,
|
||||
llvm::SmallString<256> moduleInterfacePath;
|
||||
std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer;
|
||||
std::unique_ptr<llvm::MemoryBuffer> moduleDocBuffer;
|
||||
if (!version.empty()) {
|
||||
if (versionInfo) {
|
||||
unusedModuleInterfacePath = &moduleInterfacePath;
|
||||
unusedModuleBuffer = &moduleInputBuffer;
|
||||
unusedModuleDocBuffer = &moduleDocBuffer;
|
||||
@@ -1159,58 +1156,53 @@ bool SerializedModuleLoaderBase::canImportModule(ImportPath::Module path,
|
||||
// If we cannot find the module, don't continue.
|
||||
if (!found)
|
||||
return false;
|
||||
// If no version number is specified, don't continue.
|
||||
if (version.empty())
|
||||
|
||||
// If the caller doesn't want version info we're done.
|
||||
if (!versionInfo)
|
||||
return true;
|
||||
|
||||
assert(found);
|
||||
assert(!version.empty());
|
||||
assert(!underlyingVersion);
|
||||
llvm::VersionTuple currentVersion;
|
||||
llvm::VersionTuple swiftInterfaceVersion;
|
||||
if (!moduleInterfacePath.empty()) {
|
||||
currentVersion = extractUserModuleVersionFromInterface(moduleInterfacePath);
|
||||
swiftInterfaceVersion =
|
||||
extractUserModuleVersionFromInterface(moduleInterfacePath);
|
||||
}
|
||||
// If failing to extract the user version from the interface file, try the binary
|
||||
// format, if present.
|
||||
if (currentVersion.empty() && *unusedModuleBuffer) {
|
||||
|
||||
// If failing to extract the user version from the interface file, try the
|
||||
// binary module format, if present.
|
||||
if (swiftInterfaceVersion.empty() && *unusedModuleBuffer) {
|
||||
auto metaData = serialization::validateSerializedAST(
|
||||
(*unusedModuleBuffer)->getBuffer(), Ctx.SILOpts.EnableOSSAModules,
|
||||
Ctx.LangOpts.SDKName);
|
||||
currentVersion = metaData.userModuleVersion;
|
||||
versionInfo->setVersion(metaData.userModuleVersion,
|
||||
ModuleVersionSourceKind::SwiftBinaryModule);
|
||||
} else {
|
||||
versionInfo->setVersion(swiftInterfaceVersion,
|
||||
ModuleVersionSourceKind::SwiftInterface);
|
||||
}
|
||||
|
||||
if (currentVersion.empty()) {
|
||||
Ctx.Diags.diagnose(mID.Loc, diag::cannot_find_project_version, "Swift",
|
||||
mID.Item.str());
|
||||
return true;
|
||||
}
|
||||
|
||||
return currentVersion >= version;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MemoryBufferSerializedModuleLoader::canImportModule(
|
||||
ImportPath::Module path, llvm::VersionTuple version,
|
||||
bool underlyingVersion) {
|
||||
ImportPath::Module path, ModuleVersionInfo *versionInfo) {
|
||||
// FIXME: Swift submodules?
|
||||
if (path.hasSubmodule())
|
||||
return false;
|
||||
// If underlying version is specified, this should be handled by Clang importer.
|
||||
if (!version.empty() && underlyingVersion)
|
||||
return false;
|
||||
|
||||
auto mID = path[0];
|
||||
auto mIt = MemoryBuffers.find(mID.Item.str());
|
||||
if (mIt == MemoryBuffers.end())
|
||||
return false;
|
||||
if (version.empty())
|
||||
|
||||
if (!versionInfo)
|
||||
return true;
|
||||
if (mIt->second.userVersion.empty()) {
|
||||
Ctx.Diags.diagnose(mID.Loc, diag::cannot_find_project_version, "Swift",
|
||||
mID.Item.str());
|
||||
return true;
|
||||
}
|
||||
assert(!version.empty());
|
||||
assert(!(mIt->second.userVersion.empty()));
|
||||
return mIt->second.userVersion >= version;
|
||||
|
||||
versionInfo->setVersion(mIt->second.userVersion,
|
||||
ModuleVersionSourceKind::SwiftBinaryModule);
|
||||
return true;
|
||||
}
|
||||
|
||||
ModuleDecl *
|
||||
SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
|
||||
ImportPath::Module path) {
|
||||
|
||||
Reference in New Issue
Block a user