Skip to content

Conversation

@CrazyFanFan
Copy link
Contributor

Resolves #58518

It will create four different fix-its:

    static func main() {
        <#code#>
    }
 
    static func main() throws {
        <#code#>
    }
 
    static func main() async {
        <#code#>
    }
 
    static func main() async throws {
        <#code#>
    }

Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis left a comment

Choose a reason for hiding this comment

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

Nice!

Please add a test case with a nested @main struct to make sure we got the indents right.

@AnthonyLatsis AnthonyLatsis requested a review from ahoppen July 15, 2025 13:57
@CrazyFanFan
Copy link
Contributor Author

@AnthonyLatsis Thanks for the review! All issues have been addressed based on your feedback.

Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis left a comment

Choose a reason for hiding this comment

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

One more round. Are you using clang-format?

@CrazyFanFan CrazyFanFan force-pushed the feature/fix-it-add-static-main branch from 22a54ad to 285c484 Compare July 16, 2025 00:34
@CrazyFanFan
Copy link
Contributor Author

One more round. Are you using clang-format?

@AnthonyLatsis Yes, I ran clang-format before submitting:

git-clang-format HEAD~2
git add .
git commit --amend --no-edit

All other review feedback has been addressed as well.

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Very nice. Looking forward to using this 🤩

Copy link
Member

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 main function from the remaining body using a newline?

Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis Jul 16, 2025

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 formatCodeForInsertion function that parses a given string into a SwiftSyntax tree and formats it using SwiftBasicFormat (indentation, separation) for insertion at a given location.

Copy link
Member

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.

Copy link
Contributor Author

@CrazyFanFan CrazyFanFan Jul 18, 2025

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 \n at 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.

Copy link
Member

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. 👍🏽

@AnthonyLatsis
Copy link
Collaborator

@swift-ci please smoke test Linux

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Jul 17, 2025

01:57:17  Failed Tests (6):
01:57:17    Swift(linux-x86_64) :: attr/ApplicationMain/attr_main_arguments.swift
01:57:17    Swift(linux-x86_64) :: attr/ApplicationMain/attr_main_dynamicCallable.swift
01:57:17    Swift(linux-x86_64) :: attr/ApplicationMain/attr_main_dynamicMemberLookup.swift
01:57:17    Swift(linux-x86_64) :: attr/ApplicationMain/attr_main_extension_nofunc.swift
01:57:17    Swift(linux-x86_64) :: attr/ApplicationMain/attr_main_return.swift
01:57:17    Swift(linux-x86_64) :: attr/ApplicationMain/attr_main_struct_from_two_protocols_one_missing.swift

Let me know if you need help running the tests locally.

@CrazyFanFan
Copy link
Contributor Author

01:57:17  Failed Tests (6):
01:57:17    Swift(linux-x86_64) :: attr/ApplicationMain/attr_main_arguments.swift
01:57:17    Swift(linux-x86_64) :: attr/ApplicationMain/attr_main_dynamicCallable.swift
01:57:17    Swift(linux-x86_64) :: attr/ApplicationMain/attr_main_dynamicMemberLookup.swift
01:57:17    Swift(linux-x86_64) :: attr/ApplicationMain/attr_main_extension_nofunc.swift
01:57:17    Swift(linux-x86_64) :: attr/ApplicationMain/attr_main_return.swift
01:57:17    Swift(linux-x86_64) :: attr/ApplicationMain/attr_main_struct_from_two_protocols_one_missing.swift

Let me know if you need help running the tests locally.

@AnthonyLatsis Thank you for the detailed test failure information.​

All issues you mentioned have been fixed. Additionally, all tests in test/attr/ApplicationMain/ have been run in my local before this update, addressing the previously overlooked cases.​

Please let me know if you spot anything else or have further suggestions.​

@CrazyFanFan CrazyFanFan force-pushed the feature/fix-it-add-static-main branch from 285c484 to a35ff41 Compare July 18, 2025 02:47
Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis left a comment

Choose a reason for hiding this comment

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

Thanks! Please tidy up your history before we go for merge.

P.S. You are welcome to add a test for if (hasAsyncSupport) { in a follow-up.

@AnthonyLatsis
Copy link
Collaborator

@swift-ci please smoke test Linux

@CrazyFanFan
Copy link
Contributor Author

@AnthonyLatsis Hi,

Just to confirm: by tidying up history, do you mean amending/combining commits?
I’d like to add the test for if (hasAsyncSupport) but am unsure how to set up an Async-unsupported environment. Any tips?

Thanks!

@AnthonyLatsis
Copy link
Collaborator

Just to confirm: by tidying up history, do you mean amending/combining commits?

In this case I mean squashing the "review feedback" commit into the original.

I’d like to add the test for if (hasAsyncSupport) but am unsure how to set up an Async-unsupported environment. Any tips?

Try this (async is supported or can be deployed to platform versions aligned with 5.1 and onward):

// RUN: %target-typecheck-verify-swift -parse-as-library -target %target-swift-5.0-abi-triple

// REQUIRES: OS=macosx && CPU=x86_64

@main struct S {}

@CrazyFanFan CrazyFanFan force-pushed the feature/fix-it-add-static-main branch from a35ff41 to 0d0dfcf Compare July 22, 2025 13:30
@CrazyFanFan
Copy link
Contributor Author

CrazyFanFan commented Jul 22, 2025

I've squashed the "review feedback" commit into the original as suggested. I've also added the test for if (hasAsyncSupport). Let me know if anything needs adjusting.

Thanks!

@CrazyFanFan CrazyFanFan force-pushed the feature/fix-it-add-static-main branch from 0d0dfcf to 061ce72 Compare July 23, 2025 07:04
@AnthonyLatsis
Copy link
Collaborator

@swift-ci please smoke test macOS

@AnthonyLatsis
Copy link
Collaborator

FYI if you build the project without --swift-darwin-supported-archs=arm64 you will also get a x86_64 stdlib and a test-macosx-x86_64 directory that you can use to run these kinds of tests. You don’t need a x86_64 machine in this case.

@AnthonyLatsis
Copy link
Collaborator

@swift-ci please smoke test Linux

@AnthonyLatsis
Copy link
Collaborator

@swift-ci please smoke test Windows

@AnthonyLatsis AnthonyLatsis enabled auto-merge July 30, 2025 19:23
@AnthonyLatsis AnthonyLatsis merged commit 1035213 into swiftlang:main Jul 31, 2025
3 checks passed
@CrazyFanFan CrazyFanFan deleted the feature/fix-it-add-static-main branch July 31, 2025 04:35
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.

No Fix-It for @main struct without main static function

3 participants