Skip to content

Commit fed08ca

Browse files
committed
Error on duplicate module names in Swift module maps
Duplicate module names on search paths produces an error, but providing duplicate module names in a Swift explicit module map file does not, instead the first entry will be chosen. Modify the module map parser to error on duplicated module names as well.
1 parent 6a6cc48 commit fed08ca

File tree

2 files changed

+172
-4
lines changed

2 files changed

+172
-4
lines changed

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ class ExplicitModuleMapParser {
414414
if (moduleName.empty())
415415
return true;
416416

417+
bool didInsert;
417418
if (swiftModulePath.has_value()) {
418419
assert((clangModuleMapPath.empty() &&
419420
clangModulePath.empty()) &&
@@ -425,7 +426,7 @@ class ExplicitModuleMapParser {
425426
isFramework,
426427
isSystem,
427428
swiftModuleCacheKey);
428-
swiftModuleMap.try_emplace(moduleName, std::move(entry));
429+
didInsert = swiftModuleMap.try_emplace(moduleName, std::move(entry)).second;
429430
} else {
430431
assert((!clangModuleMapPath.empty() ||
431432
!clangModulePath.empty()) &&
@@ -436,10 +437,10 @@ class ExplicitModuleMapParser {
436437
isSystem,
437438
isBridgingHeaderDependency,
438439
clangModuleCacheKey);
439-
clangModuleMap.try_emplace(moduleName, std::move(entry));
440+
didInsert = clangModuleMap.try_emplace(moduleName, std::move(entry)).second;
440441
}
441-
442-
return false;
442+
// Prevent duplicate module names.
443+
return !didInsert;
443444
}
444445

445446
llvm::StringSaver Saver;
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// UNSUPPORTED: OS=windows-msvc
2+
// RUN: %empty-directory(%t)
3+
// RUN: mkdir -p %t/clang-module-cache
4+
// RUN: mkdir -p %t/inputs
5+
// RUN: split-file %s %t
6+
// RUN: sed -e "s|INPUTDIR|%t|g" -e "s|SWIFTLIBDIR|%swift-lib-dir|g" %t/inputs/valid_map.json.template > %t/inputs/valid_map.json
7+
// RUN: sed -e "s|INPUTDIR|%t|g" -e "s|SWIFTLIBDIR|%swift-lib-dir|g" %t/inputs/map_with_duped_swift_module.json.template > %t/inputs/map_with_duped_swift_module.json
8+
// RUN: sed -e "s|INPUTDIR|%t|g" -e "s|SWIFTLIBDIR|%swift-lib-dir|g" %t/inputs/map_with_duped_clang_module.json.template > %t/inputs/map_with_duped_clang_module.json
9+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/inputs/Foo.swiftmodule -emit-module-doc-path %t/inputs/Foo.swiftdoc -emit-module-source-info -emit-module-source-info-path %t/inputs/Foo.swiftsourceinfo -module-cache-path %t.module-cache %t/foo.swift -module-name Foo
10+
// RUN: %target-swift-emit-pcm -module-name SwiftShims %swift-lib-dir/swift/shims/module.modulemap -o %t/inputs/SwiftShims.pcm
11+
// RUN: %target-swift-emit-pcm -module-name _SwiftConcurrencyShims %swift-lib-dir/swift/shims/module.modulemap -o %t/inputs/_SwiftConcurrencyShims.pcm
12+
13+
// RUN: %target-swift-frontend -typecheck %t/foo.swift -explicit-swift-module-map-file %t/inputs/valid_map.json -disable-implicit-swift-modules -disable-implicit-concurrency-module-import
14+
// RUN: not %target-swift-frontend -typecheck %t/foo.swift -explicit-swift-module-map-file %t/inputs/map_with_duped_swift_module.json -disable-implicit-swift-modules -disable-implicit-concurrency-module-import
15+
// RUN: not %target-swift-frontend -typecheck %t/foo.swift -explicit-swift-module-map-file %t/inputs/map_with_duped_clang_module.json -disable-implicit-swift-modules -disable-implicit-concurrency-module-import
16+
17+
//--- foo.swift
18+
public func foo() {}
19+
20+
//--- inputs/valid_map.json.template
21+
[{
22+
"moduleName": "Foo",
23+
"modulePath": "INPUTDIR/inputs/Foo.swiftmodule",
24+
"docPath": "INPUTDIR/inputs/Foo.swiftdoc",
25+
"sourceInfoPath": "INPUTDIR/inputs/Foo.swiftsourceinfo",
26+
"isFramework": false
27+
},
28+
{
29+
"moduleName": "Swift",
30+
"modulePath": "SWIFTLIBDIR/swift/macosx/Swift.swiftmodule/arm64-apple-macos.swiftmodule",
31+
"isFramework": false
32+
},
33+
{
34+
"moduleName": "SwiftOnoneSupport",
35+
"modulePath": "SWIFTLIBDIR/swift/macosx/SwiftOnoneSupport.swiftmodule/arm64-apple-macos.swiftmodule",
36+
"isFramework": false
37+
},
38+
{
39+
"moduleName": "_Concurrency",
40+
"modulePath": "SWIFTLIBDIR/swift/macosx/_Concurrency.swiftmodule/arm64-apple-macos.swiftmodule",
41+
"isFramework": false
42+
},
43+
{
44+
"moduleName": "_StringProcessing",
45+
"modulePath": "SWIFTLIBDIR/swift/macosx/_StringProcessing.swiftmodule/arm64-apple-macos.swiftmodule",
46+
"isFramework": false
47+
},
48+
{
49+
"moduleName": "SwiftShims",
50+
"isFramework": false,
51+
"clangModuleMapPath": "SWIFTLIBDIR/swift/shims/module.modulemap",
52+
"clangModulePath": "INPUTDIR/inputs/SwiftShims.pcm"
53+
},
54+
{
55+
"moduleName": "_SwiftConcurrencyShims",
56+
"isFramework": false,
57+
"clangModuleMapPath": "SWIFTLIBDIR/swift/shims/module.modulemap",
58+
"clangModulePath": "INPUTDIR/inputs/_SwiftConcurrencyShims.pcm"
59+
},
60+
{
61+
"moduleName": "Distributed",
62+
"modulePath": "SWIFTLIBDIR/swift/macosx/Distributed.swiftmodule/arm64-apple-macos.swiftmodule",
63+
"isFramework": false
64+
}]
65+
66+
//--- inputs/map_with_duped_swift_module.json.template
67+
[{
68+
"moduleName": "Foo",
69+
"modulePath": "INPUTDIR/inputs/Foo.swiftmodule",
70+
"docPath": "INPUTDIR/inputs/Foo.swiftdoc",
71+
"sourceInfoPath": "INPUTDIR/inputs/Foo.swiftsourceinfo",
72+
"isFramework": false
73+
},
74+
{
75+
"moduleName": "Swift",
76+
"modulePath": "SWIFTLIBDIR/swift/macosx/Swift.swiftmodule/arm64-apple-macos.swiftmodule",
77+
"isFramework": false
78+
},
79+
{
80+
"moduleName": "Swift",
81+
"modulePath": "SWIFTLIBDIR/swift/macosx/Swift.swiftmodule/arm64-apple-macos.swiftmodule",
82+
"isFramework": false
83+
},
84+
{
85+
"moduleName": "SwiftOnoneSupport",
86+
"modulePath": "SWIFTLIBDIR/swift/macosx/SwiftOnoneSupport.swiftmodule/arm64-apple-macos.swiftmodule",
87+
"isFramework": false
88+
},
89+
{
90+
"moduleName": "_Concurrency",
91+
"modulePath": "SWIFTLIBDIR/swift/macosx/_Concurrency.swiftmodule/arm64-apple-macos.swiftmodule",
92+
"isFramework": false
93+
},
94+
{
95+
"moduleName": "_StringProcessing",
96+
"modulePath": "SWIFTLIBDIR/swift/macosx/_StringProcessing.swiftmodule/arm64-apple-macos.swiftmodule",
97+
"isFramework": false
98+
},
99+
{
100+
"moduleName": "SwiftShims",
101+
"isFramework": false,
102+
"clangModuleMapPath": "SWIFTLIBDIR/swift/shims/module.modulemap",
103+
"clangModulePath": "INPUTDIR/inputs/SwiftShims.pcm"
104+
},
105+
{
106+
"moduleName": "_SwiftConcurrencyShims",
107+
"isFramework": false,
108+
"clangModuleMapPath": "SWIFTLIBDIR/swift/shims/module.modulemap",
109+
"clangModulePath": "INPUTDIR/inputs/_SwiftConcurrencyShims.pcm"
110+
},
111+
{
112+
"moduleName": "Distributed",
113+
"modulePath": "SWIFTLIBDIR/swift/macosx/Distributed.swiftmodule/arm64-apple-macos.swiftmodule",
114+
"isFramework": false
115+
}]
116+
117+
//--- inputs/map_with_duped_clang_module.json.template
118+
[{
119+
"moduleName": "Foo",
120+
"modulePath": "INPUTDIR/inputs/Foo.swiftmodule",
121+
"docPath": "INPUTDIR/inputs/Foo.swiftdoc",
122+
"sourceInfoPath": "INPUTDIR/inputs/Foo.swiftsourceinfo",
123+
"isFramework": false
124+
},
125+
{
126+
"moduleName": "Swift",
127+
"modulePath": "SWIFTLIBDIR/swift/macosx/Swift.swiftmodule/arm64-apple-macos.swiftmodule",
128+
"isFramework": false
129+
},
130+
{
131+
"moduleName": "SwiftOnoneSupport",
132+
"modulePath": "SWIFTLIBDIR/swift/macosx/SwiftOnoneSupport.swiftmodule/arm64-apple-macos.swiftmodule",
133+
"isFramework": false
134+
},
135+
{
136+
"moduleName": "_Concurrency",
137+
"modulePath": "SWIFTLIBDIR/swift/macosx/_Concurrency.swiftmodule/arm64-apple-macos.swiftmodule",
138+
"isFramework": false
139+
},
140+
{
141+
"moduleName": "_StringProcessing",
142+
"modulePath": "SWIFTLIBDIR/swift/macosx/_StringProcessing.swiftmodule/arm64-apple-macos.swiftmodule",
143+
"isFramework": false
144+
},
145+
{
146+
"moduleName": "SwiftShims",
147+
"isFramework": false,
148+
"clangModuleMapPath": "SWIFTLIBDIR/swift/shims/module.modulemap",
149+
"clangModulePath": "INPUTDIR/inputs/SwiftShims.pcm"
150+
},
151+
{
152+
"moduleName": "SwiftShims",
153+
"isFramework": false,
154+
"clangModuleMapPath": "SWIFTLIBDIR/swift/shims/module.modulemap",
155+
"clangModulePath": "INPUTDIR/inputs/SwiftShims.pcm"
156+
},
157+
{
158+
"moduleName": "_SwiftConcurrencyShims",
159+
"isFramework": false,
160+
"clangModuleMapPath": "SWIFTLIBDIR/swift/shims/module.modulemap",
161+
"clangModulePath": "INPUTDIR/inputs/_SwiftConcurrencyShims.pcm"
162+
},
163+
{
164+
"moduleName": "Distributed",
165+
"modulePath": "SWIFTLIBDIR/swift/macosx/Distributed.swiftmodule/arm64-apple-macos.swiftmodule",
166+
"isFramework": false
167+
}]

0 commit comments

Comments
 (0)