Skip to content

Commit bd14089

Browse files
committed
Warn on multiple '-swift-module-file' options
1 parent e08b782 commit bd14089

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ ERROR(error_stdlib_module_name,none,
211211
"module name \"%0\" is reserved for the standard library"
212212
"%select{|; use -module-name flag to specify an alternate name}1",
213213
(StringRef, bool))
214+
WARNING(warn_multiple_module_inputs_same_name,none,
215+
"multiple Swift module file inputs with identifier \"%0\": replacing '%1' with '%2'",
216+
(StringRef, StringRef, StringRef))
214217

215218
ERROR(error_bad_export_as_name,none,
216219
"export-as name \"%0\" is not a valid identifier",

lib/Frontend/CompilerInvocation.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2310,7 +2310,15 @@ static bool validateSwiftModuleFileArgumentAndAdd(const std::string &swiftModule
23102310
Diags.diagnose(SourceLoc(), diag::error_bad_module_name, moduleName, false);
23112311
return true;
23122312
}
2313-
ExplicitSwiftModuleInputs.insert(std::make_pair(moduleName, modulePath));
2313+
2314+
auto priorEntryIt = ExplicitSwiftModuleInputs.find(moduleName);
2315+
if (priorEntryIt != ExplicitSwiftModuleInputs.end()) {
2316+
Diags.diagnose(SourceLoc(), diag::warn_multiple_module_inputs_same_name,
2317+
moduleName, priorEntryIt->getValue(), modulePath);
2318+
ExplicitSwiftModuleInputs[moduleName] = modulePath;
2319+
} else
2320+
ExplicitSwiftModuleInputs.insert(std::make_pair(moduleName, modulePath));
2321+
23142322
return false;
23152323
}
23162324

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/module-cache)
3+
// RUN: %empty-directory(%t/Inputs/Foo.swiftmodule)
4+
// RUN: split-file %s %t
5+
6+
// Step 1: build swift interface and swift module side by side
7+
// RUN: %target-swift-frontend -emit-module %t/Foo.swift -emit-module-path %t/Inputs/Foo.swiftmodule/%target-swiftmodule-name -module-name Foo -user-module-version 22
8+
9+
// Step 2: scan dependency should give us the binary module we specify with 'swift-module-file'
10+
// RUN: %target-swift-frontend -scan-dependencies %t/test.swift -o %t/deps.json -scanner-module-validation -swift-module-file=Foo=%t/Inputs/Foo.swiftmodule/%target-swiftmodule-name
11+
// RUN: %validate-json %t/deps.json | %FileCheck %s -check-prefix=CHECK-INPUT
12+
13+
// Step 3: ensure that versioned canImport still applies with direct -swift-module-file
14+
// RUN: %target-swift-frontend -scan-dependencies %t/test_too_new.swift -o %t/deps.json -scanner-module-validation -swift-module-file=Foo=%t/Inputs/Foo.swiftmodule/%target-swiftmodule-name
15+
// RUN: %validate-json %t/deps.json | %FileCheck %s -check-prefix=CHECK-MISSING
16+
17+
// CHECK-INPUT: "swiftPrebuiltExternal": "Foo"
18+
// CHECK-MISSING-NOT: "swiftPrebuiltExternal": "Foo"
19+
20+
//--- Foo.swift
21+
public func foo() {}
22+
23+
//--- test.swift
24+
#if canImport(Foo)
25+
import Foo
26+
#endif
27+
28+
//--- test_too_new.swift
29+
#if canImport(Foo, _version: 23)
30+
import Foo
31+
#endif

test/ScanDependencies/explicit-scanner-input.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99
// Step 2: scan dependency should give us the binary module we specify with 'swift-module-file'
1010
// RUN: %target-swift-frontend -scan-dependencies %t/test.swift -o %t/deps.json -scanner-module-validation -swift-module-file=Foo=%t/Inputs/Foo.swiftmodule/%target-swiftmodule-name
1111
// RUN: %validate-json %t/deps.json | %FileCheck %s -check-prefix=CHECK-INPUT
12-
// CHECK-INPUT: "swiftPrebuiltExternal": "Foo"
1312

14-
// Step 3: verify that the usual invalid module candidate diagnostics apply
13+
// Step 3: ensure that if multiple inputs for the same module are specified then a warning is emitted and the latter is preferred
14+
// RUN: echo "Gibberish" > %t/Inputs/Foo.swiftmodule/NotAModule.swiftmodule
15+
// RUN: %target-swift-frontend -scan-dependencies %t/test.swift -o %t/deps.json -scanner-module-validation -swift-module-file=Foo=%t/Inputs/Foo.swiftmodule/NotAModule.swiftmodule -swift-module-file=Foo=%t/Inputs/Foo.swiftmodule/%target-swiftmodule-name -diagnostic-style llvm 2>&1 | %FileCheck %s -check-prefix=CHECK-WARN-MULTIPLE
16+
// RUN: %validate-json %t/deps.json | %FileCheck %s -check-prefix=CHECK-INPUT
17+
18+
// Step 4: verify that the usual invalid module candidate diagnostics apply
1519
// RUN: echo "Not Really a module" > %t/Inputs/Foo.swiftmodule/%target-swiftmodule-name
1620
// RUN: %target-swift-frontend -scan-dependencies %t/test.swift -o %t/deps.json -scanner-module-validation -swift-module-file=Foo=%t/Inputs/Foo.swiftmodule/%target-swiftmodule-name -diagnostic-style llvm 2>&1 | %FileCheck %s -check-prefix=CHECK-INVALID-MODULE-DIAG
21+
22+
// CHECK-INPUT: "swiftPrebuiltExternal": "Foo"
23+
// CHECK-WARN-MULTIPLE: warning: multiple Swift module file inputs with identifier "Foo": replacing '{{.*}}NotAModule.swiftmodule'
1724
// CHECK-INVALID-MODULE-DIAG: error: unable to resolve Swift module dependency to a compatible module: 'Foo'
1825
// CHECK-INVALID-MODULE-DIAG: note: found incompatible module '{{.*}}': malformed
1926

0 commit comments

Comments
 (0)