-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[Diagnostics] Add fix-it to @main struct without main static function.
#82989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AnthonyLatsis
merged 1 commit into
swiftlang:main
from
CrazyFanFan:feature/fix-it-add-static-main
Jul 31, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| // RUN: %target-swift-frontend -typecheck -parse-as-library -verify %s | ||
|
|
||
| @main // expected-error{{'MyBase' is annotated with '@main' and must provide a main static function}} | ||
| // expected-note@-1{{add 'static func main()'}} {{8:16-16=\n static func main() {\n <#code#>\n }\n}} | ||
| // expected-note@-2{{add 'static func main() throws'}} {{8:16-16=\n static func main() throws {\n <#code#>\n }\n}} | ||
| // expected-note@-3{{add 'static func main() async'}} {{8:16-16=\n static func main() async {\n <#code#>\n }\n}} | ||
| // expected-note@-4{{add 'static func main() async throws'}} {{8:16-16=\n static func main() async throws {\n <#code#>\n }\n}} | ||
| struct MyBase { | ||
| static func main(_ argc: Int, _ argv: [String]) { | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| // RUN: %target-swift-frontend -typecheck -parse-as-library -verify %s | ||
|
|
||
| @main @dynamicMemberLookup // expected-error{{'Main' is annotated with '@main' and must provide a main static function}} | ||
| // expected-note@-1{{add 'static func main()'}} {{8:14-14=\n static func main() {\n <#code#>\n }\n}} | ||
| // expected-note@-2{{add 'static func main() throws'}} {{8:14-14=\n static func main() throws {\n <#code#>\n }\n}} | ||
| // expected-note@-3{{add 'static func main() async'}} {{8:14-14=\n static func main() async {\n <#code#>\n }\n}} | ||
| // expected-note@-4{{add 'static func main() async throws'}} {{8:14-14=\n static func main() async throws {\n <#code#>\n }\n}} | ||
| struct Main { | ||
| subscript(dynamicMember member: String) -> () -> Void { | ||
| return { | ||
| } | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,23 @@ | ||
| // RUN: %target-swift-frontend -typecheck -parse-as-library -verify %s | ||
|
|
||
| @main // expected-error{{'MyBase' is annotated with '@main' and must provide a main static function}} | ||
| // expected-note@-1{{add 'static func main()'}} {{8:15-15=\n static func main() {\n <#code#>\n }\n}} | ||
| // expected-note@-2{{add 'static func main() throws'}} {{8:15-15=\n static func main() throws {\n <#code#>\n }\n}} | ||
| // expected-note@-3{{add 'static func main() async'}} {{8:15-15=\n static func main() async {\n <#code#>\n }\n}} | ||
| // expected-note@-4{{add 'static func main() async throws'}} {{8:15-15=\n static func main() async throws {\n <#code#>\n }\n}} | ||
| class MyBase { | ||
| func main() { | ||
| } | ||
| } | ||
|
|
||
| enum Nested { | ||
| @main // expected-error{{'MyBase' is annotated with '@main' and must provide a main static function}} | ||
| // expected-note@-1{{add 'static func main()'}} {{19:17-17=\n static func main() {\n <#code#>\n }\n}} | ||
| // expected-note@-2{{add 'static func main() throws'}} {{19:17-17=\n static func main() throws {\n <#code#>\n }\n}} | ||
| // expected-note@-3{{add 'static func main() async'}} {{19:17-17=\n static func main() async {\n <#code#>\n }\n}} | ||
| // expected-note@-4{{add 'static func main() async throws'}} {{19:17-17=\n static func main() async throws {\n <#code#>\n }\n}} | ||
| class MyBase { | ||
| func main() { | ||
| } | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
test/attr/ApplicationMain/attr_main_instance_without_async_support.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // RUN: %target-swift-frontend -typecheck -parse-as-library -target %target-swift-5.0-abi-triple -verify %s | ||
| // REQUIRES: OS=macosx && CPU=x86_64 | ||
|
|
||
| @main // expected-error{{'MyBaseWithoutAsyncSupport' is annotated with '@main' and must provide a main static function}} | ||
| // expected-note@-1{{add 'static func main()'}} {{7:34-34=\n static func main() {\n <#code#>\n }\n}} | ||
| // expected-note@-2{{add 'static func main() throws'}} {{7:34-34=\n static func main() throws {\n <#code#>\n }\n}} | ||
| class MyBaseWithoutAsyncSupport { | ||
| func main() { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| // RUN: %target-swift-frontend -typecheck -parse-as-library -verify %s | ||
|
|
||
| @main // expected-error{{'MyBase' is annotated with '@main' and must provide a main static function}} | ||
| // expected-note@-1{{add 'static func main()'}} {{8:16-16=\n static func main() {\n <#code#>\n }\n}} | ||
| // expected-note@-2{{add 'static func main() throws'}} {{8:16-16=\n static func main() throws {\n <#code#>\n }\n}} | ||
| // expected-note@-3{{add 'static func main() async'}} {{8:16-16=\n static func main() async {\n <#code#>\n }\n}} | ||
| // expected-note@-4{{add 'static func main() async throws'}} {{8:16-16=\n static func main() async throws {\n <#code#>\n }\n}} | ||
| struct MyBase { | ||
| static func main() -> Int { | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: Should we add two newlines here to separate the generated
mainfunction from the remaining body using a newline?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t have a clear preference. Ideally (not in this PR!), I imagine a
formatCodeForInsertionfunction that parses a given string into a SwiftSyntax tree and formats it using SwiftBasicFormat (indentation, separation) for insertion at a given location.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way that Fix-Its and refactorings are designed to work is that the editor applies its own indentation after applying the Fix-It refactoring because it can really know about the user’s preferences (the compiler can only guess based on existing lines). But whether to add a newline is currently an opinionated decision by the compiler.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the discussion.
I've looked at other fix-it instances in TypeCheckProtocol.cpp and TypeCheckDistributed.cpp, and found that they only add a single
\nat the end of inserted code, without additional blank lines.Considering this, and editors applying indentation based on user preferences, I'm inclined to stay consistent with existing practices and not insert extra blank lines.
Of course, if you have any other thoughts, I'm open to further discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for finding those examples. In that case let’s stick with the existing practice of only adding one newline. 👍🏽