Skip to content

Adding actionable note to @main error #59919

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
merged 1 commit into from
Jul 6, 2022

Conversation

etcwilde
Copy link
Member

@etcwilde etcwilde commented Jul 6, 2022

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.

rdar://89888060

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.
@etcwilde etcwilde requested a review from nate-chandler July 6, 2022 16:33
@etcwilde
Copy link
Member Author

etcwilde commented Jul 6, 2022

@swift-ci please test

Copy link
Contributor

@nate-chandler nate-chandler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Nice improvement for the behavior of @UIApplicationMain/@NSApplicationMain/@main.

@etcwilde etcwilde merged commit 8f6d22c into swiftlang:main Jul 6, 2022
@etcwilde etcwilde deleted the ewilde/improve-main-error branch July 6, 2022 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants