Skip to content

Commit 99fb440

Browse files
committed
Be more strict about invalid -module-name arguments
In the case this argument is passed explicitly and invalid, it was previous ignored and fell back to 'main' even in cases where you might not want that like compiling object files. It doesn't seem necessary to allow invalid cases like this through regardless of what object type is being built.
1 parent 738a6a3 commit 99fb440

File tree

2 files changed

+14
-29
lines changed

2 files changed

+14
-29
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,32 +2321,6 @@ extension Driver {
23212321
return ""
23222322
}
23232323

2324-
/// Whether we are going to be building an executable.
2325-
///
2326-
/// FIXME: Why "maybe"? Why isn't this all known in advance as captured in
2327-
/// linkerOutputType?
2328-
private static func maybeBuildingExecutable(
2329-
_ parsedOptions: inout ParsedOptions,
2330-
linkerOutputType: LinkOutputType?
2331-
) -> Bool {
2332-
switch linkerOutputType {
2333-
case .executable:
2334-
return true
2335-
2336-
case .dynamicLibrary, .staticLibrary:
2337-
return false
2338-
2339-
default:
2340-
break
2341-
}
2342-
2343-
if parsedOptions.hasArgument(.parseAsLibrary, .parseStdlib) {
2344-
return false
2345-
}
2346-
2347-
return parsedOptions.allInputs.count == 1
2348-
}
2349-
23502324
/// Determine how the module will be emitted and the name of the module.
23512325
private static func computeModuleInfo(
23522326
_ parsedOptions: inout ParsedOptions,
@@ -2419,10 +2393,9 @@ extension Driver {
24192393

24202394
func fallbackOrDiagnose(_ error: Diagnostic.Message) {
24212395
moduleNameIsFallback = true
2422-
if compilerOutputType == nil || maybeBuildingExecutable(&parsedOptions, linkerOutputType: linkerOutputType) {
2396+
if compilerOutputType == nil || !parsedOptions.hasArgument(.moduleName) {
24232397
moduleName = "main"
2424-
}
2425-
else {
2398+
} else {
24262399
diagnosticsEngine.emit(error)
24272400
moduleName = "__bad__"
24282401
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,18 @@ final class SwiftDriverTests: XCTestCase {
757757
XCTAssertEqual(try Driver(args: ["swiftc", "foo.swift", "-o", "+++.out"]).moduleOutputInfo.name, "main")
758758
XCTAssertEqual(try Driver(args: ["swift"]).moduleOutputInfo.name, "REPL")
759759
XCTAssertEqual(try Driver(args: ["swiftc", "foo.swift", "-emit-library", "-o", "libBaz.dylib"]).moduleOutputInfo.name, "Baz")
760+
761+
try assertDriverDiagnostics(
762+
args: ["swiftc", "foo.swift", "-module-name", "", "file.foo.swift"]
763+
) {
764+
$1.expect(.error("module name \"\" is not a valid identifier"))
765+
}
766+
767+
try assertDriverDiagnostics(
768+
args: ["swiftc", "foo.swift", "-module-name", "123", "file.foo.swift"]
769+
) {
770+
$1.expect(.error("module name \"123\" is not a valid identifier"))
771+
}
760772
}
761773

762774
func testEmitModuleSeparatelyDiagnosticPath() throws {

0 commit comments

Comments
 (0)