Hey all,
Sorry if this isn't the right channel; I'm playing around with the new features in 5.3 and am having a problem trying to use the @main attribute:
@main
struct NewApp {
static func main() {
print("Started the app!")
}
}
gives me:
~/temp $ swift ./testmain.swift 1 ↵
./testmain.swift:1:1: error: 'main' attribute cannot be used in a module that contains top-level code
@main
^
./testmain.swift:1:1: note: top-level code defined in this source file
@main
^
~/temp $
This happens 100% of the time, regardless if @main is on the first line or not (I first noticed it when it complained about having import Foundation as "top level code" 
This is as of the 2020-06-05-SNAPSHOT build. I did some searching to see if there were any bug reports or yet-unresolved issues, but my searches didn't lead me to anything useful.
Thanks for any info!
Ron
owenv
(Owen Voorhees)
2
I think this is probably the same bug as [SR-12683] `@main` is no longer usable due to misdetection of top level code · Issue #55127 · apple/swift · GitHub , or at least related to that issue. The comments have a temporary workaround involving -parse-as-library you might be able to use to verify.
2 Likes
Hey Owen-
Yes, the workaround in the bug report does work. The interesting thing is that it works if compiling (i.e. swiftc) but not just running; I'd think that, with this attribute, I should be able to throw a bunch of .swift files at swift and let it figure out what to run based on @main without needing an actual main.swift.
iMostfa
(Mostfa Essam)
4
renaming from main.swift to something else, like Program.swift, solved the problem
9 Likes