Skip to content
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

[SR-14488] Can't use @main in executable target with single file #56843

Closed
natecook1000 opened this issue Apr 14, 2021 · 5 comments
Closed

[SR-14488] Can't use @main in executable target with single file #56843

natecook1000 opened this issue Apr 14, 2021 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@natecook1000
Copy link
Member

Previous ID SR-14488
Radar rdar://problem/76728168
Original Reporter @natecook1000
Type Bug
Status Closed
Resolution Duplicate

Attachment: Download

Environment

This is using the swift-DEVELOPMENT-SNAPSHOT-2021-04-10-a toolchain from swift.org:

Apple Swift version 5.5-dev (LLVM bcf6482631963a8, Swift 37aa203b753b23b)
Target: x86_64-apple-macosx11.0
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 5673e04166479b5dfbd94eced5e04ace

duplicates:

  • SR-12683 @main is no longer usable due to misdetection of top level code

Issue Description:

When trying to build a package that has an executable target with only one file, I get the following error message, even when that file isn't named "main.swift":

.../spm-onefile/Sources/spm-onefile/OneFile.swift:1:1: error: 'main' attribute cannot be used in a module that contains top-level code
@main
^
.../spm-onefile/Sources/spm-onefile/OneFile.swift:1:1: note: top-level code defined in this source file
@main

The source of OneFile.swift:

@main
struct OneFile {
  static func main() {
    print("Hello, world!")
  }
}
@typesanitizer
Copy link

@swift-ci create

@abertelrud
Copy link
Contributor

This seems to be compiler behavior, with special-casing for a single source file vs multiple, and also special-casing of the file name "main.swift". There is a `-parse-as-library` flag that SwiftPM can pass but then "main.swift" will stop being recognized as an entry point:

❯ swiftc -parse-as-library -c main.swift

main.swift:1:1: error: expressions are not allowed at the top level

I'm not sure what SwiftPM can do here without some help from the frontend.

@abertelrud
Copy link
Contributor

Perhaps the compiler can have a flag that SwiftPM and other build systems can use to convey the intent that "this is intended to be an executable module, i.e. there should at the end be a single entry point from either a main.swift or a @main, please emit an error if that's not the case".

Perhaps something like `-parse-as-executable`? Leaving off the flag would have today's heuristics, but passing either `-parse-as-library` (with today's semantics) or `-parse-as-executable` (with new semantics roughly like above) would specifically convey intent, allowing the compiler to have better diagnostics than just leaving it to the linker to report the absence of a `_main` symbol?

In the meantime, I think SwiftPM will need to apply some heuristics to counteract the compiler's. We can't just pass `-parse-as-executable`, because then `main.swift` will stop working, but maybe we can look for the last path component of all of the source files, and unless one of them is `main.swift`, we pass `-parse-as-executable`.

@abertelrud
Copy link
Contributor

I put up at PR at apple/swift-package-manager#3410 for SwiftPM to mitigate this by passing `-parse-as-library` when there is just one source file in the module and it isn't named `main.swift`. For better ergonomics, I suggest what I mentioned above, so that SwiftPM and other build systems can convey their intent to the compiler, and it can disable its heuristics and use that intent for diagnostics (e.g. emit an error if there is no entry point at all when a single entry point was expected, etc).

@tbkka
Copy link
Contributor

tbkka commented May 18, 2021

Merging with SR-12683

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

4 participants