Skip to content

[lldb][test] Compile with -parse-as-library for test main.swift sources containing @main#12838

Open
kastiglione wants to merge 2 commits intostable/21.xfrom
dl/lldb-test-Compile-with-parse-as-library-for-test-main.swift-sources-containing-main
Open

[lldb][test] Compile with -parse-as-library for test main.swift sources containing @main#12838
kastiglione wants to merge 2 commits intostable/21.xfrom
dl/lldb-test-Compile-with-parse-as-library-for-test-main.swift-sources-containing-main

Conversation

@kastiglione
Copy link
Copy Markdown

@kastiglione kastiglione commented Apr 24, 2026

Update Swift.rules to automatically use the -parse-as-library compiler flag when the following conditions are true:

  1. A source file is named main.swift
  2. main.swift contains the @main marker

@kastiglione
Copy link
Copy Markdown
Author

@swift-ci test

@kastiglione
Copy link
Copy Markdown
Author

@swift-ci test

@felipepiovezan
Copy link
Copy Markdown

Out of curiosity, what is the benefit of this?

@kastiglione
Copy link
Copy Markdown
Author

kastiglione commented Apr 24, 2026

If I'm understanding the question: When using @main, you must also use -parse-as-library.

With this change, tests can use @main without having to explicitly add SWIFTFLAGS_EXTRAS := -parse-as-library to its Makefile.

Demonstration:

% swiftc main.swift
main.swift:1:1: error: 'main' attribute cannot be used in a module that contains top-level code
1 | @main struct Entry {
  | |- error: 'main' attribute cannot be used in a module that contains top-level code
  | |- note: top-level code defined in this source file
  | `- note: pass '-parse-as-library' to compiler invocation if this is intentional
2 |   static func main() {
3 |   }

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.

2 participants