Skip to content

Commit 2efab71

Browse files
committed
Use real module names to load modules
Add ModuleInterface option meta tag for -module-alias Add tests loading modules with -module-alias for swiftmodule and swiftinterface with various loaders incl. serialized, explicit, and source loader.
1 parent 55b9fa1 commit 2efab71

File tree

5 files changed

+109
-8
lines changed

5 files changed

+109
-8
lines changed

include/swift/Option/Options.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,9 @@ def module_name_EQ : Joined<["-"], "module-name=">, Flags<[FrontendOption]>,
431431
Alias<module_name>;
432432

433433
def module_alias : Separate<["-"], "module-alias">,
434-
Flags<[FrontendOption]>,
434+
Flags<[FrontendOption, ModuleInterfaceOption]>,
435435
MetaVarName<"<alias_name=underlying_name>">,
436-
HelpText<"If a source file imports or references module <alias_name>, the underlying name is used for the contents of the file">;
436+
HelpText<"If a source file imports or references module <alias_name>, the <underlying_name> is used for the contents of the file">;
437437

438438
def module_link_name : Separate<["-"], "module-link-name">,
439439
Flags<[FrontendOption, ModuleInterfaceOption]>,

lib/Serialization/ModuleFile.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,15 @@ Status ModuleFile::associateWithFileContext(FileUnit *file, SourceLoc diagLoc,
131131
Status status = Status::Valid;
132132

133133
ModuleDecl *M = file->getParentModule();
134-
if (M->getName().str() != Core->Name)
134+
// The real (on-disk) name of the module should be checked here as that's the
135+
// actually loaded module. In case module aliasing is used when building the main
136+
// module, e.g. -module-name MyModule -module-alias Foo=Bar, the loaded module
137+
// that maps to 'Foo' is actually Bar.swiftmodule|.swiftinterface (applies to swift
138+
// modules only), which is retrieved via M->getRealName(). If no module aliasing is
139+
// used, M->getRealName() will return the same value as M->getName(), which is 'Foo'.
140+
if (M->getRealName().str() != Core->Name) {
135141
return error(Status::NameMismatch);
142+
}
136143

137144
ASTContext &ctx = getContext();
138145

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/// Test the -module-alias flag with an explicit module loader.
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 Bar.swiftmodule is created
10+
// RUN: test -f %t/Bar.swiftmodule
11+
12+
/// Next create an explicit module dependency map to build module Foo
13+
// RUN: mkdir -p %t/inputs
14+
// RUN: echo 'import Cat' > %t/FileFoo.swift
15+
16+
// RUN: echo "[{" > %/t/inputs/map.json
17+
// RUN: echo "\"moduleName\": \"Foo\"," >> %/t/inputs/map.json
18+
// RUN: echo "\"modulePath\": \"%/t/inputs/Foo.swiftmodule\"," >> %/t/inputs/map.json
19+
// RUN: echo "\"docPath\": \"%/t/inputs/Foo.swiftdoc\"," >> %/t/inputs/map.json
20+
// RUN: echo "\"sourceInfoPath\": \"%/t/inputs/Foo.swiftsourceinfo\"," >> %/t/inputs/map.json
21+
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
22+
// RUN: echo "}," >> %/t/inputs/map.json
23+
// RUN: echo "{" >> %/t/inputs/map.json
24+
// RUN: echo "\"moduleName\": \"SwiftOnoneSupport\"," >> %/t/inputs/map.json
25+
// RUN: echo "\"modulePath\": \"%/ononesupport_module\"," >> %/t/inputs/map.json
26+
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
27+
// RUN: echo "}," >> %/t/inputs/map.json
28+
// RUN: echo "{" >> %/t/inputs/map.json
29+
// RUN: echo "\"moduleName\": \"_Concurrency\"," >> %/t/inputs/map.json
30+
// RUN: echo "\"modulePath\": \"%/concurrency_module\"," >> %/t/inputs/map.json
31+
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
32+
// RUN: echo "}," >> %/t/inputs/map.json
33+
// RUN: echo "{" >> %/t/inputs/map.json
34+
// RUN: echo "\"moduleName\": \"Swift\"," >> %/t/inputs/map.json
35+
// RUN: echo "\"modulePath\": \"%/stdlib_module\"," >> %/t/inputs/map.json
36+
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
37+
// RUN: echo "}," >> %/t/inputs/map.json
38+
// RUN: echo "{" >> %/t/inputs/map.json
39+
// RUN: echo "\"moduleName\": \"_Distributed\"," >> %/t/inputs/map.json
40+
// RUN: echo "\"modulePath\": \"%/distributed_module\"," >> %/t/inputs/map.json
41+
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
42+
// RUN: echo "}]" >> %/t/inputs/map.json
43+
44+
/// Create a module Foo that imports Cat with -module-alias Cat=Bar with an explicit module loader
45+
// RUN: %target-swift-frontend -module-name Foo %t/FileFoo.swift -module-alias Cat=Bar -I %t -emit-module -emit-module-path %t/Foo.swiftmodule -disable-implicit-swift-modules -explicit-swift-module-map-file %t/inputs/map.json -Rmodule-loading 2> %t/load-result.output
46+
47+
// RUN: test -f %t/Foo.swiftmodule
48+
// RUN: test -f %t/Bar.swiftmodule
49+
// RUN: not test -f %t/Cat.swiftmodule
50+
51+
// RUN: %FileCheck %s -input-file %t/load-result.output -check-prefix CHECK
52+
// CHECK: remark: loaded module at {{.*}}Bar.swiftmodule
53+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// Test the -module-alias flag with a module interface loader.
2+
3+
// RUN: %empty-directory(%t)
4+
5+
/// Create a module Baz
6+
// RUN: echo 'public func baz() {}' > %t/FileBaz.swift
7+
// RUN: %target-swift-frontend -module-name Baz %t/FileBaz.swift -emit-module -emit-module-path %t/Baz.swiftmodule -enable-library-evolution
8+
9+
/// Check Baz.swiftmodule is created
10+
// RUN: test -f %t/Baz.swiftmodule
11+
12+
/// Create a module FooInterface that imports Cat with -module-alias Cat=Baz
13+
// RUN: echo 'import Cat' > %t/FileFoo.swift
14+
// RUN: %target-swift-frontend -module-name FooInterface -module-alias Cat=Baz %t/FileFoo.swift -emit-module -emit-module-interface-path %t/FooInterface.swiftinterface -swift-version 5 -enable-library-evolution -I %t -Rmodule-loading 2> %t/load-result.output
15+
16+
/// Check Foo.swiftinterface is created and Baz.swiftmodule is loaded
17+
// RUN: test -f %t/FooInterface.swiftinterface
18+
// RUN: test -f %t/Baz.swiftmodule
19+
// RUN: not test -f %t/Cat.swiftmodule
20+
21+
// RUN: %FileCheck %s -input-file %t/FooInterface.swiftinterface -check-prefix CHECK-IMPORT
22+
// CHECK-IMPORT: -module-alias Cat=Baz
23+
// CHECK-IMPORT: import Cat
24+
25+
// RUN: %FileCheck %s -input-file %t/load-result.output -check-prefix CHECK-LOAD
26+
// CHECK-LOAD: remark: loaded module at {{.*}}Baz.swiftmodule
27+

test/Frontend/load-module-with-alias.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,29 @@
66
// RUN: echo 'public func bar() {}' > %t/FileBar.swift
77
// RUN: %target-swift-frontend -module-name Bar %t/FileBar.swift -emit-module -emit-module-path %t/Bar.swiftmodule
88

9-
/// Check if Bar.swiftmodule is created
9+
/// Check Bar.swiftmodule is created
1010
// RUN: test -f %t/Bar.swiftmodule
11-
// RUN: not test -f %t/Cat.swiftmodule
1211

13-
/// Create a module Foo that imports Cat with -module-alias Cat=Bar
12+
/// Create a module Foo that imports Cat with -module-alias Cat=Bar with a serialized modue loader
1413
// 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
14+
// RUN: %target-swift-frontend -module-name Foo %t/FileFoo.swift -module-alias Cat=Bar -I %t -emit-module -emit-module-path %t/Foo.swiftmodule -Rmodule-loading 2> %t/load-result-foo.output
1615

17-
/// Check if Foo.swiftmodule is created without an error
16+
/// Check Foo.swiftmodule is created and Bar.swiftmodule is loaded
1817
// RUN: test -f %t/Foo.swiftmodule
1918
// RUN: test -f %t/Bar.swiftmodule
2019
// RUN: not test -f %t/Cat.swiftmodule
20+
21+
// RUN: %FileCheck %s -input-file %t/load-result-foo.output -check-prefix CHECK-FOO
22+
// CHECK-FOO: remark: loaded module at {{.*}}Bar.swiftmodule
23+
24+
/// Create a module Zoo that imports Cat with -module-alias Cat=Bar with a source loader
25+
// RUN: %target-swift-frontend -module-name Zoo %t/FileFoo.swift -module-alias Cat=Bar -I %t -emit-module -emit-module-path %t/Zoo.swiftmodule -enable-source-import -Rmodule-loading 2> %t/load-result-zoo.output
26+
27+
// RUN: test -f %t/Zoo.swiftmodule
28+
// RUN: test -f %t/Bar.swiftmodule
29+
// RUN: not test -f %t/Cat.swiftmodule
30+
31+
// RUN: %FileCheck %s -input-file %t/load-result-zoo.output -check-prefix CHECK-ZOO
32+
// CHECK-ZOO: remark: loaded module at {{.*}}Bar.swiftmodule
33+
34+

0 commit comments

Comments
 (0)