Skip to content

Commit 59cf32f

Browse files
[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 (cherry picked from commit 7d85aa4)
1 parent 83fdb98 commit 59cf32f

File tree

16 files changed

+109
-53
lines changed

16 files changed

+109
-53
lines changed

include/swift/AST/ASTContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ class ASTContext final {
11091109
///
11101110
/// Note that even if this check succeeds, errors may still occur if the
11111111
/// module is loaded in full.
1112-
bool canImportModuleImpl(ImportPath::Module ModulePath,
1112+
bool canImportModuleImpl(ImportPath::Module ModulePath, SourceLoc loc,
11131113
llvm::VersionTuple version, bool underlyingVersion,
11141114
bool updateFailingList,
11151115
llvm::VersionTuple &foundVersion) const;
@@ -1146,7 +1146,7 @@ class ASTContext final {
11461146
///
11471147
/// Note that even if this check succeeds, errors may still occur if the
11481148
/// module is loaded in full.
1149-
bool canImportModule(ImportPath::Module ModulePath,
1149+
bool canImportModule(ImportPath::Module ModulePath, SourceLoc loc,
11501150
llvm::VersionTuple version = llvm::VersionTuple(),
11511151
bool underlyingVersion = false);
11521152

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,9 @@ ERROR(serialization_failed,none,
827827
"serialization of module %0 failed due to the errors above",
828828
(const ModuleDecl *))
829829

830+
WARNING(can_import_invalid_swiftmodule,none,
831+
"canImport() evaluated to false due to invalid swiftmodule: %0", (StringRef))
832+
830833
ERROR(serialization_load_failed,Fatal,
831834
"failed to load module '%0'", (StringRef))
832835
ERROR(module_interface_build_failed,Fatal,

include/swift/AST/ModuleLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ class ModuleLoader {
287287
///
288288
/// If a non-null \p versionInfo is provided, the module version will be
289289
/// parsed and populated.
290-
virtual bool canImportModule(ImportPath::Module named,
290+
virtual bool canImportModule(ImportPath::Module named, SourceLoc loc,
291291
ModuleVersionInfo *versionInfo,
292292
bool isTestableImport = false) = 0;
293293

include/swift/ClangImporter/ClangImporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class ClangImporter final : public ClangModuleLoader {
227227
///
228228
/// If a non-null \p versionInfo is provided, the module version will be
229229
/// parsed and populated.
230-
virtual bool canImportModule(ImportPath::Module named,
230+
virtual bool canImportModule(ImportPath::Module named, SourceLoc loc,
231231
ModuleVersionInfo *versionInfo,
232232
bool isTestableImport = false) override;
233233

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class ExplicitSwiftModuleLoader: public SerializedModuleLoaderBase {
163163
bool SkipBuildingInterface, bool IsFramework,
164164
bool IsTestableDependencyLookup = false) override;
165165

166-
bool canImportModule(ImportPath::Module named,
166+
bool canImportModule(ImportPath::Module named, SourceLoc loc,
167167
ModuleVersionInfo *versionInfo,
168168
bool isTestableDependencyLookup = false) override;
169169

@@ -212,7 +212,7 @@ class ExplicitCASModuleLoader : public SerializedModuleLoaderBase {
212212
bool SkipBuildingInterface, bool IsFramework,
213213
bool IsTestableDependencyLookup = false) override;
214214

215-
bool canImportModule(ImportPath::Module named,
215+
bool canImportModule(ImportPath::Module named, SourceLoc loc,
216216
ModuleVersionInfo *versionInfo,
217217
bool isTestableDependencyLookup = false) override;
218218

include/swift/Sema/SourceLoader.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ class SourceLoader : public ModuleLoader {
5959
///
6060
/// If a non-null \p versionInfo is provided, the module version will be
6161
/// parsed and populated.
62-
virtual bool canImportModule(ImportPath::Module named,
63-
ModuleVersionInfo *versionInfo,
64-
bool isTestableDependencyLookup = false) override;
62+
virtual bool
63+
canImportModule(ImportPath::Module named, SourceLoc loc,
64+
ModuleVersionInfo *versionInfo,
65+
bool isTestableDependencyLookup = false) override;
6566

6667
/// Import a module with the given module path.
6768
///

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,10 @@ class SerializedModuleLoaderBase : public ModuleLoader {
209209
///
210210
/// If a non-null \p versionInfo is provided, the module version will be
211211
/// parsed and populated.
212-
virtual bool canImportModule(ImportPath::Module named,
213-
ModuleVersionInfo *versionInfo,
214-
bool isTestableDependencyLookup = false) override;
212+
virtual bool
213+
canImportModule(ImportPath::Module named, SourceLoc loc,
214+
ModuleVersionInfo *versionInfo,
215+
bool isTestableDependencyLookup = false) override;
215216

216217
/// Import a module with the given module path.
217218
///
@@ -339,7 +340,7 @@ class MemoryBufferSerializedModuleLoader : public SerializedModuleLoaderBase {
339340
public:
340341
virtual ~MemoryBufferSerializedModuleLoader();
341342

342-
bool canImportModule(ImportPath::Module named,
343+
bool canImportModule(ImportPath::Module named, SourceLoc loc,
343344
ModuleVersionInfo *versionInfo,
344345
bool isTestableDependencyLookup = false) override;
345346

lib/AST/ASTContext.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,10 +2403,11 @@ void ASTContext::addSucceededCanImportModule(
24032403
}
24042404
}
24052405

2406-
bool ASTContext::canImportModuleImpl(
2407-
ImportPath::Module ModuleName, llvm::VersionTuple version,
2408-
bool underlyingVersion, bool updateFailingList,
2409-
llvm::VersionTuple &foundVersion) const {
2406+
bool ASTContext::canImportModuleImpl(ImportPath::Module ModuleName,
2407+
SourceLoc loc, llvm::VersionTuple version,
2408+
bool underlyingVersion,
2409+
bool updateFailingList,
2410+
llvm::VersionTuple &foundVersion) const {
24102411
SmallString<64> FullModuleName;
24112412
ModuleName.getString(FullModuleName);
24122413
auto ModuleNameStr = FullModuleName.str().str();
@@ -2447,7 +2448,7 @@ bool ASTContext::canImportModuleImpl(
24472448

24482449
// Otherwise, ask whether any module loader can load the module.
24492450
for (auto &importer : getImpl().ModuleLoaders) {
2450-
if (importer->canImportModule(ModuleName, nullptr))
2451+
if (importer->canImportModule(ModuleName, loc, nullptr))
24512452
return true;
24522453
}
24532454

@@ -2464,7 +2465,7 @@ bool ASTContext::canImportModuleImpl(
24642465
for (auto &importer : getImpl().ModuleLoaders) {
24652466
ModuleLoader::ModuleVersionInfo versionInfo;
24662467

2467-
if (!importer->canImportModule(ModuleName, &versionInfo))
2468+
if (!importer->canImportModule(ModuleName, loc, &versionInfo))
24682469
continue; // The loader can't find the module.
24692470

24702471
if (!versionInfo.isValid())
@@ -2504,11 +2505,11 @@ void ASTContext::forEachCanImportVersionCheck(
25042505
Callback(entry.first, entry.second.Version, entry.second.UnderlyingVersion);
25052506
}
25062507

2507-
bool ASTContext::canImportModule(ImportPath::Module moduleName,
2508+
bool ASTContext::canImportModule(ImportPath::Module moduleName, SourceLoc loc,
25082509
llvm::VersionTuple version,
25092510
bool underlyingVersion) {
25102511
llvm::VersionTuple versionInfo;
2511-
if (!canImportModuleImpl(moduleName, version, underlyingVersion, true,
2512+
if (!canImportModuleImpl(moduleName, loc, version, underlyingVersion, true,
25122513
versionInfo))
25132514
return false;
25142515

@@ -2522,8 +2523,8 @@ bool ASTContext::testImportModule(ImportPath::Module ModuleName,
25222523
llvm::VersionTuple version,
25232524
bool underlyingVersion) const {
25242525
llvm::VersionTuple versionInfo;
2525-
return canImportModuleImpl(ModuleName, version, underlyingVersion, false,
2526-
versionInfo);
2526+
return canImportModuleImpl(ModuleName, SourceLoc(), version,
2527+
underlyingVersion, false, versionInfo);
25272528
}
25282529

25292530
ModuleDecl *

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,7 @@ static llvm::VersionTuple getCurrentVersionFromTBD(llvm::vfs::FileSystem &FS,
21332133
}
21342134

21352135
bool ClangImporter::canImportModule(ImportPath::Module modulePath,
2136+
SourceLoc loc,
21362137
ModuleVersionInfo *versionInfo,
21372138
bool isTestableDependencyLookup) {
21382139
// Look up the top-level module to see if it exists.

lib/ClangImporter/ImportType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3510,7 +3510,7 @@ ModuleDecl *ClangImporter::Implementation::tryLoadFoundationModule() {
35103510
bool ClangImporter::Implementation::canImportFoundationModule() {
35113511
ImportPath::Module::Builder builder(SwiftContext.Id_Foundation);
35123512
auto modulePath = builder.get();
3513-
return SwiftContext.canImportModule(modulePath);
3513+
return SwiftContext.canImportModule(modulePath, SourceLoc());
35143514
}
35153515

35163516
Type ClangImporter::Implementation::getNamedSwiftType(ModuleDecl *module,

0 commit comments

Comments
 (0)