Skip to content

Commit 55b9fa1

Browse files
committed
Load modules using real names if module aliases are used
rdar://83591943
1 parent 140c024 commit 55b9fa1

File tree

5 files changed

+65
-10
lines changed

5 files changed

+65
-10
lines changed

lib/AST/ASTContext.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,12 +1848,27 @@ ASTContext::getLoadedModules() const {
18481848
}
18491849

18501850
ModuleDecl *ASTContext::getLoadedModule(Identifier ModuleName) const {
1851-
return getImpl().LoadedModules.lookup(ModuleName);
1851+
// Look up a loaded module using an actual module name (physical name
1852+
// on disk). If the -module-alias option is used, the module name that
1853+
// appears in source code will be different from the real module name
1854+
// on disk, otherwise the same.
1855+
//
1856+
// For example, if '-module-alias Foo=Bar' is passed in to the frontend,
1857+
// and a source file has 'import Foo', a module called Bar (real name)
1858+
// will be loaded and returned.
1859+
auto realName = getRealModuleName(ModuleName);
1860+
return getImpl().LoadedModules.lookup(realName);
18521861
}
18531862

18541863
void ASTContext::addLoadedModule(ModuleDecl *M) {
18551864
assert(M);
1856-
getImpl().LoadedModules[M->getName()] = M;
1865+
// Add a loaded module using an actual module name (physical name
1866+
// on disk), in case -module-alias is used (otherwise same).
1867+
//
1868+
// For example, if '-module-alias Foo=Bar' is passed in to the frontend,
1869+
// and a source file has 'import Foo', a module called Bar (real name)
1870+
// will be loaded and added to the map.
1871+
getImpl().LoadedModules[M->getRealName()] = M;
18571872
}
18581873

18591874
void ASTContext::registerGenericSignatureBuilder(

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,13 @@ bool ExplicitSwiftModuleLoader::findModule(ImportPath::Element ModuleID,
17391739
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
17401740
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
17411741
bool skipBuildingInterface, bool &IsFramework, bool &IsSystemModule) {
1742-
StringRef moduleName = ModuleID.Item.str();
1742+
// Find a module with an actual, physical name on disk, in case
1743+
// -module-alias is used (otherwise same).
1744+
//
1745+
// For example, if '-module-alias Foo=Bar' is passed in to the frontend, and an
1746+
// input file has 'import Foo', a module called Bar (real name) should be searched.
1747+
StringRef moduleName = Ctx.getRealModuleName(ModuleID.Item).str();
1748+
17431749
auto it = Impl.ExplicitModuleMap.find(moduleName);
17441750
// If no explicit module path is given matches the name, return with an
17451751
// error code.

lib/Sema/SourceLoader.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,20 @@ using namespace swift;
3434
// FIXME: Basically the same as SerializedModuleLoader.
3535
using FileOrError = llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>;
3636

37-
static FileOrError findModule(ASTContext &ctx, StringRef moduleID,
37+
static FileOrError findModule(ASTContext &ctx, Identifier moduleID,
3838
SourceLoc importLoc) {
3939
llvm::SmallString<128> inputFilename;
40+
// Find a module with an actual, physical name on disk, in case
41+
// -module-alias is used (otherwise same).
42+
//
43+
// For example, if '-module-alias Foo=Bar' is passed in to the frontend,
44+
// and a source file has 'import Foo', a module called Bar (real name)
45+
// should be searched.
46+
StringRef moduleNameRef = ctx.getRealModuleName(moduleID).str();
4047

4148
for (auto Path : ctx.SearchPathOpts.ImportSearchPaths) {
4249
inputFilename = Path;
43-
llvm::sys::path::append(inputFilename, moduleID);
50+
llvm::sys::path::append(inputFilename, moduleNameRef);
4451
inputFilename.append(".swift");
4552
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
4653
ctx.SourceMgr.getFileSystem()->getBufferForFile(inputFilename.str());
@@ -66,7 +73,7 @@ bool SourceLoader::canImportModule(ImportPath::Element ID,
6673
llvm::VersionTuple version,
6774
bool underlyingVersion) {
6875
// Search the memory buffers to see if we can find this file on disk.
69-
FileOrError inputFileOrError = findModule(Ctx, ID.Item.str(),
76+
FileOrError inputFileOrError = findModule(Ctx, ID.Item,
7077
ID.Loc);
7178
if (!inputFileOrError) {
7279
auto err = inputFileOrError.getError();
@@ -88,7 +95,7 @@ ModuleDecl *SourceLoader::loadModule(SourceLoc importLoc,
8895

8996
auto moduleID = path[0];
9097

91-
FileOrError inputFileOrError = findModule(Ctx, moduleID.Item.str(),
98+
FileOrError inputFileOrError = findModule(Ctx, moduleID.Item,
9299
moduleID.Loc);
93100
if (!inputFileOrError) {
94101
auto err = inputFileOrError.getError();

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,14 @@ SerializedModuleLoaderBase::findModule(ImportPath::Element moduleID,
533533
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
534534
std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer,
535535
bool skipBuildingInterface, bool &isFramework, bool &isSystemModule) {
536-
SmallString<32> moduleName(moduleID.Item.str());
536+
// Find a module with an actual, physical name on disk, in case
537+
// -module-alias is used (otherwise same).
538+
//
539+
// For example, if '-module-alias Foo=Bar' is passed in to the frontend,
540+
// and a source file has 'import Foo', a module called Bar (real name)
541+
// should be searched.
542+
StringRef moduleNameRef = Ctx.getRealModuleName(moduleID.Item).str();
543+
SmallString<32> moduleName(moduleNameRef);
537544
SerializedModuleBaseName genericBaseName(moduleName);
538545

539546
auto genericModuleFileName =
@@ -1156,7 +1163,6 @@ bool MemoryBufferSerializedModuleLoader::canImportModule(
11561163
assert(!(mIt->second.userVersion.empty()));
11571164
return mIt->second.userVersion >= version;
11581165
}
1159-
11601166
ModuleDecl *
11611167
SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
11621168
ImportPath::Module path) {
@@ -1220,13 +1226,14 @@ MemoryBufferSerializedModuleLoader::loadModule(SourceLoc importLoc,
12201226
return nullptr;
12211227

12221228
auto moduleID = path[0];
1229+
auto moduleName = Ctx.getRealModuleName(moduleID.Item).str();
12231230

12241231
// See if we find it in the registered memory buffers.
12251232

12261233
// FIXME: Right now this works only with access paths of length 1.
12271234
// Once submodules are designed, this needs to support suffix
12281235
// matching and a search path.
1229-
auto bufIter = MemoryBuffers.find(moduleID.Item.str());
1236+
auto bufIter = MemoryBuffers.find(moduleName);
12301237
if (bufIter == MemoryBuffers.end())
12311238
return nullptr;
12321239

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// Test the -module-alias flag.
2+
3+
// RUN: %empty-directory(%t)
4+
5+
/// Create a module Bar
6+
// RUN: echo 'public func bar() {}' > %t/FileBar.swift
7+
// RUN: %target-swift-frontend -module-name Bar %t/FileBar.swift -emit-module -emit-module-path %t/Bar.swiftmodule
8+
9+
/// Check if Bar.swiftmodule is created
10+
// RUN: test -f %t/Bar.swiftmodule
11+
// RUN: not test -f %t/Cat.swiftmodule
12+
13+
/// Create a module Foo that imports Cat with -module-alias Cat=Bar
14+
// RUN: echo 'import Cat' > %t/FileFoo.swift
15+
// RUN: %target-swift-frontend -module-name Foo -module-alias Cat=Bar %t/FileFoo.swift -emit-module -emit-module-path %t/Foo.swiftmodule -I %t
16+
17+
/// Check if Foo.swiftmodule is created without an error
18+
// RUN: test -f %t/Foo.swiftmodule
19+
// RUN: test -f %t/Bar.swiftmodule
20+
// RUN: not test -f %t/Cat.swiftmodule

0 commit comments

Comments
 (0)