Skip to content

Commit 8364d5b

Browse files
authored
Merge pull request #40777 from ApolloZhu/fix-canImport-submodule-checking
Fix canImport submodule checking in loaders not supporting submodules
2 parents 2bc2021 + cde0ce7 commit 8364d5b

File tree

8 files changed

+37
-3
lines changed

8 files changed

+37
-3
lines changed

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,8 @@ bool ExplicitSwiftModuleLoader::canImportModule(ImportPath::Module path,
18671867
llvm::VersionTuple version,
18681868
bool underlyingVersion) {
18691869
// FIXME: Swift submodules?
1870+
if (path.hasSubmodule())
1871+
return false;
18701872
ImportPath::Element mID = path.front();
18711873
// Look up the module with the real name (physical name on disk);
18721874
// in case `-module-alias` is used, the name appearing in source files

lib/Sema/SourceLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ bool SourceLoader::canImportModule(ImportPath::Module path,
7373
llvm::VersionTuple version,
7474
bool underlyingVersion) {
7575
// FIXME: Swift submodules?
76-
if (path.size() > 1)
76+
if (path.hasSubmodule())
7777
return false;
7878

7979
auto ID = path[0];

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,9 @@ swift::extractUserModuleVersionFromInterface(StringRef moduleInterfacePath) {
11331133
bool SerializedModuleLoaderBase::canImportModule(ImportPath::Module path,
11341134
llvm::VersionTuple version,
11351135
bool underlyingVersion) {
1136+
// FIXME: Swift submodules?
1137+
if (path.hasSubmodule())
1138+
return false;
11361139
// If underlying version is specified, this should be handled by Clang importer.
11371140
if (!version.empty() && underlyingVersion)
11381141
return false;
@@ -1153,7 +1156,6 @@ bool SerializedModuleLoaderBase::canImportModule(ImportPath::Module path,
11531156
unusedModuleDocBuffer = &moduleDocBuffer;
11541157
}
11551158

1156-
// FIXME: Swift submodules?
11571159
auto mID = path[0];
11581160
auto found = findModule(mID, unusedModuleInterfacePath, unusedModuleBuffer,
11591161
unusedModuleDocBuffer, unusedModuleSourceInfoBuffer,
@@ -1193,10 +1195,12 @@ bool SerializedModuleLoaderBase::canImportModule(ImportPath::Module path,
11931195
bool MemoryBufferSerializedModuleLoader::canImportModule(
11941196
ImportPath::Module path, llvm::VersionTuple version,
11951197
bool underlyingVersion) {
1198+
// FIXME: Swift submodules?
1199+
if (path.hasSubmodule())
1200+
return false;
11961201
// If underlying version is specified, this should be handled by Clang importer.
11971202
if (!version.empty() && underlyingVersion)
11981203
return false;
1199-
// FIXME: Swift submodules?
12001204
auto mID = path[0];
12011205
auto mIt = MemoryBuffers.find(mID.Item.str());
12021206
if (mIt == MemoryBuffers.end())

test/ClangImporter/MixedSource/Inputs/WithSubmodule.framework/Empty.swift

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int fromSubmodule;

test/ClangImporter/MixedSource/Inputs/WithSubmodule.framework/Modules/WithSubmodule.swiftmodule/.keep

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
framework module WithSubmodule {
2+
explicit module Submodule {
3+
header "Submodule.h"
4+
export *
5+
}
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: cp -r %S/Inputs/WithSubmodule.framework %t
3+
// RUN: %target-swift-frontend -emit-module -o %t/WithSubmodule.framework/Modules/WithSubmodule.swiftmodule/%target-swiftmodule-name %t/WithSubmodule.framework/Empty.swift -import-underlying-module -F %t -module-name WithSubmodule
4+
5+
// RUN: %target-typecheck-verify-swift -F %t
6+
7+
// Testing 'canImport()' non-existing submodule in a top module loadable by other loaders.
8+
9+
#if !canImport(WithSubmodule.Submodule)
10+
#error("Should can import WithSubmodule.Submodule")
11+
#endif
12+
13+
// Should fail if checked for a non-existing submodule.
14+
#if canImport(WithSubmodule.ButNotMe)
15+
import WithSubmodule.Submodule
16+
#endif
17+
18+
func testNotImported() {
19+
fromSubmodule = 5
20+
// expected-error@-1 {{cannot find 'fromSubmodule' in scope}}
21+
}

0 commit comments

Comments
 (0)