Reviews are an important part of the Swift evolution process. All review feedback should be either on this forum thread or, if you would like to keep your feedback private, directly to me as the review manager by email or DM. When contacting the review manager directly, please put "SE-0563" in the subject line.
Trying it out
Toolchains with the latest implementation are currently building on the implementation PR. I will post toolchain links in the review thread once they are ready.
What goes into a review?
The goal of the review process is to improve the proposal under review through constructive criticism and, eventually, determine the direction of Swift. When writing your review, here are some questions you might want to answer in your review:
What is your evaluation of the proposal?
Is the problem being addressed significant enough to warrant a change to Swift?
Does this proposal fit well with the feel and direction of Swift?
If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
How much effort did you put into your review? A glance, a quick reading, or an in-depth study?
More information about the Swift evolution process is available at:
This has been a thorn in my side with Bazel swift_binary targets, where we want to be able to support top-level code for legacy reasons, but since most binaries nowadays use @main all of our targets are littered with those compiler flags. Excited to one day remove them.
Just to verify: the behavior will track the swift-tools-version, not the actual tools version?
Details...
i.e., for a script wrapper (e.g., [1]) running builds with swiftPM that has worked around this main.swift/@main distinction by avoiding the name main.swift as needed, the solution moving forward is to continue the workaround for prior tools versions as declared in the package manifest; so even when the wrapper runs SPM 6.5, if the wrapper is building a Package.swift with swift-tools-version: 6.4 it would not have these fixes and should continue with the workaround.
Also, if the user updates the tools version in the manifest, it does not require that any main.swift be renamed, and it will continue to compile in top-level code mode.
(And we can assume for an SPM library module which contains only one file without any @main attribute, there is an SPM heuristic that the module is built with -parse-as-library or -emit-library, so those are unaffected. This only impacts executables (and tests which have synthesized entrypoints, but those generally use @main).
The proposed compiler behavior change will be active for all compiles, and is designed to be fully source compatible. It should only accept code which was previously invalid, and shouldn't cause previously valid code to fail to compile or be interpreted differently.
The proposed SwiftPM behavior change:
SwiftPM currently applies a flawed heuristic to pass -parse-as-library to the compile of executable target sources which contain the literal text "@main". In the upcoming tools version, this heuristic will be removed and executable targets will never be built with -parse-as-library.
will only drop the -parse-as-library heuristic in newer tools versions. In practice, the build system level heuristic is often unnecessary when using a compiler with the proposed change to build a package with an older tools version. However, there are a small number of packages which rely on the old heuristic triggering on code which, for example, includes the literal text "@main" in a comment. Restricting only the SwiftPM change to a newer tools-version maximizes compatibility.
If you are mechanically generating packages designed to work with older tools versions, I'd recommend keeping any workarounds you have in place until you're ready to drop support for older compilers.
I think this is a great improvement.. The interaction between @main and top level code has been confusing and this seems like a much more intuitive approach. I also appreciate the source compatibility consideration.
One thing I'm curious about, when a package updates its swift-tools-version, does the SwiftPM behavior change apply even if it's built using an older installed toolchain, or does it require a toolchain that supports the new behavior as well ?