@main not working yet in 5.3?

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" :upside_down_face:

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

I think this is probably the same bug as https://bugs.swift.org/browse/SR-12683 , 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.

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.

Terms of Service

Privacy Policy

Cookie Policy