Skip to content

Commit 7d85aa4

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
1 parent 8582828 commit 7d85aa4

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
@@ -830,6 +830,9 @@ ERROR(serialization_failed,none,
830830
"serialization of module %0 failed due to the errors above",
831831
(const ModuleDecl *))
832832

833+
WARNING(can_import_invalid_swiftmodule,none,
834+
"canImport() evaluated to false due to invalid swiftmodule: %0", (StringRef))
835+
833836
ERROR(serialization_load_failed,Fatal,
834837
"failed to load module '%0'", (StringRef))
835838
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
@@ -2404,10 +2404,11 @@ void ASTContext::addSucceededCanImportModule(
24042404
}
24052405
}
24062406

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

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

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

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

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

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

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

25302531
ModuleDecl *

lib/ClangImporter/ClangImporter.cpp

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

21492149
bool ClangImporter::canImportModule(ImportPath::Module modulePath,
2150+
SourceLoc loc,
21502151
ModuleVersionInfo *versionInfo,
21512152
bool isTestableDependencyLookup) {
21522153
// 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
@@ -3513,7 +3513,7 @@ ModuleDecl *ClangImporter::Implementation::tryLoadFoundationModule() {
35133513
bool ClangImporter::Implementation::canImportFoundationModule() {
35143514
ImportPath::Module::Builder builder(SwiftContext.Id_Foundation);
35153515
auto modulePath = builder.get();
3516-
return SwiftContext.canImportModule(modulePath);
3516+
return SwiftContext.canImportModule(modulePath, SourceLoc());
35173517
}
35183518

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

0 commit comments

Comments
 (0)