Skip to content

Commit aba6b92

Browse files
committed
Adding actionable note to @main error
The compiler enters "script" mode if there is a file called `main.swift`, or if there is only one file. Parsing files as a script means that anything in the file is interpreted as top-level code, which is incompatible with a valid @main-annotated struct, so an error is emitted. Unfortunately, it is intentional in many cases, and the diagnostic didn't provide anything actionable to indicate that the explicit main function is intentional and that the file being passed in is not actually a top-level context. The new note indicates that passing `-parse-as-library` to the compiler invocation will fix it if the explicit main function is intentional.
1 parent 50b01fa commit aba6b92

File tree

5 files changed

+9
-0
lines changed

5 files changed

+9
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3255,6 +3255,10 @@ ERROR(attr_ApplicationMain_with_script,none,
32553255
"%" SELECT_APPLICATION_MAIN "0 attribute cannot be used in a module that contains "
32563256
"top-level code",
32573257
(unsigned))
3258+
3259+
NOTE(attr_ApplicationMain_parse_as_library,none,
3260+
"pass '-parse-as-library' to compiler invocation if this is intentional",
3261+
())
32583262
NOTE(attr_ApplicationMain_script_here,none,
32593263
"top-level code defined in this source file",
32603264
())

lib/AST/Module.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,8 @@ bool ModuleDecl::registerEntryPointFile(FileUnit *file, SourceLoc diagLoc,
18411841
if (existingDiagLoc.isValid()) {
18421842
getASTContext().Diags.diagnose(existingDiagLoc,
18431843
diag::attr_ApplicationMain_script_here);
1844+
getASTContext().Diags.diagnose(existingDiagLoc,
1845+
diag::attr_ApplicationMain_parse_as_library);
18441846
}
18451847
}
18461848
}

test/attr/ApplicationMain/attr_NSApplicationMain_with_main/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// expected-note{{top-level code defined in this source file}}
2+
// expected-note@-1{{pass '-parse-as-library' to compiler invocation if this is intentional}}
23
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify %s %S/delegate.swift
34

45
// Serialized partial AST support:

test/attr/ApplicationMain/attr_UIApplicationMain_with_main/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// expected-note{{top-level code defined in this source file}}
2+
// expected-note@-1{{pass '-parse-as-library' to compiler invocation if this is intentional}}
23
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify %s %S/delegate.swift
34

45
// REQUIRES: objc_interop
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// expected-note{{top-level code defined in this source file}}
2+
// expected-note@-1{{pass '-parse-as-library' to compiler invocation if this is intentional}}
23
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify %s %S/file.swift
34

45
hi()

0 commit comments

Comments
 (0)