Skip to content

Fix canImport submodule checking in loaders not supporting submodules #40777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/Frontend/ModuleInterfaceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,8 @@ bool ExplicitSwiftModuleLoader::canImportModule(ImportPath::Module path,
llvm::VersionTuple version,
bool underlyingVersion) {
// FIXME: Swift submodules?
if (path.hasSubmodule())
return false;
ImportPath::Element mID = path.front();
// Look up the module with the real name (physical name on disk);
// in case `-module-alias` is used, the name appearing in source files
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/SourceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool SourceLoader::canImportModule(ImportPath::Module path,
llvm::VersionTuple version,
bool underlyingVersion) {
// FIXME: Swift submodules?
if (path.size() > 1)
if (path.hasSubmodule())
return false;

auto ID = path[0];
Expand Down
8 changes: 6 additions & 2 deletions lib/Serialization/SerializedModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,9 @@ swift::extractUserModuleVersionFromInterface(StringRef moduleInterfacePath) {
bool SerializedModuleLoaderBase::canImportModule(ImportPath::Module path,
llvm::VersionTuple version,
bool underlyingVersion) {
// 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;
Expand All @@ -1153,7 +1156,6 @@ bool SerializedModuleLoaderBase::canImportModule(ImportPath::Module path,
unusedModuleDocBuffer = &moduleDocBuffer;
}

// FIXME: Swift submodules?
auto mID = path[0];
auto found = findModule(mID, unusedModuleInterfacePath, unusedModuleBuffer,
unusedModuleDocBuffer, unusedModuleSourceInfoBuffer,
Expand Down Expand Up @@ -1192,10 +1194,12 @@ bool SerializedModuleLoaderBase::canImportModule(ImportPath::Module path,
bool MemoryBufferSerializedModuleLoader::canImportModule(
ImportPath::Module path, llvm::VersionTuple version,
bool underlyingVersion) {
// 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;
// FIXME: Swift submodules?
auto mID = path[0];
auto mIt = MemoryBuffers.find(mID.Item.str());
if (mIt == MemoryBuffers.end())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int fromSubmodule;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
framework module WithSubmodule {
explicit module Submodule {
header "Submodule.h"
export *
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %empty-directory(%t)
// RUN: cp -r %S/Inputs/WithSubmodule.framework %t
// 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

// RUN: %target-typecheck-verify-swift -F %t

// Testing 'canImport()' non-existing submodule in a top module loadable by other loaders.

#if !canImport(WithSubmodule.Submodule)
#error("Should can import WithSubmodule.Submodule")
#endif

// Should fail if checked for a non-existing submodule.
#if canImport(WithSubmodule.ButNotMe)
import WithSubmodule.Submodule
#endif

func testNotImported() {
fromSubmodule = 5
// expected-error@-1 {{cannot find 'fromSubmodule' in scope}}
}