-
Notifications
You must be signed in to change notification settings - Fork 10.6k
@main: Attribute to add an entry point to a type. #30693
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
Conversation
@swift-ci please test |
Build failed |
Build failed |
c429431
to
7fd267e
Compare
@swift-ci please test |
Build failed |
Build failed |
If you use |
Do you think it would be useful to add a test case for the interaction of |
7fd267e
to
5f5815c
Compare
@swift-ci please test |
Build failed |
Build failed |
f1f16d8
to
684ea60
Compare
When a type (class, enum, or struct) is annotated @main, it is required to provide a function with the following signature: static func main() -> () That function will be called when the executable the type is defined within is launched.
Previously, the function was being added to the list of top level decls in the source file as that list was being iterated, leading to iterator issues. Here, the function is instead added to the nominal type decl which is decorated with @main. Doing so requires calling the $main function with the metatype for the nominal type. The workaround for the iterator invalidation issue which had been applied previously, namely changing the type of the Decls member of SourceFile from std::vector<Decl *> to SmallVector<Decl *, 16> is reverted.
Previously the @main attribute could only be applied to NominalTypeDecls. Here, that limitation is lifted so that the attribute can be applied to ExtensionDecls.
684ea60
to
b433fd2
Compare
@swift-ci please test |
Build failed |
Build failed |
b433fd2
to
333eaf8
Compare
@swift-ci please test |
Build failed |
Build failed |
SE-0281 was accepted with the modification that the main function should be allowed to be throwing. Here support for enabling that is added. Support is implemented in two steps: (1) The $main wrapper function is modified to be throwing static func $main() throws { return try main() } whenever the main function is throwing (it remains non-throwing when $main is not throwing). (2) The @main entry point is modified to be sil [ossa] @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 { entry(%argc : $Int32, %argv : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>): %the_main_type = metatype $@thin TheMainType.Type %the_main_func = function_ref @`TheMainType.main()` : $@convention(method) (@thin TheMainType.Type) -> @error Error try_apply %the_main_func(%the_main_type) : $@convention(method) (@thin TheMainType.Type) -> @error Error, normal success, error failure success(%_ : $()): %success_code_builtin_int32 = integer_literal $Builtin.Int32, 0 br bb1(%success_code_builtin_int32 : $Builtin.Int32) failure(%error : @owned $Error): %_ = builtin "errorInMain"(%error : $Error) : $() end_lifetime %error : $Error %error_code_builtin_int32 = integer_literal $Builtin.Int32, 1 br bb1(%error_code_builtin_int32 : $Builtin.Int32) exit(%code_builtin_int32 : $Builtin.Int32): %code = struct $Int32 (%code_builtin_int32 : $Builtin.Int32) return %code : $Int32 } whenever the main function is throwing (and consequently $main also is). In the non-throwing case, (a) the try_apply instruction is replaced with an apply instruction, (b) the body of the success block is appended to the entry block, and (c) the success and failure blocks are removed.
333eaf8
to
81dc64f
Compare
@swift-ci please test |
Build failed |
Build failed |
Here a new attribute @main is added. The type or extension that it is added to must have a static function
() -> Void
or() throws -> Void
visible from the attribute. If it does, that function will be called at process launch. The same restrictions that apply to the existing@UIApplicationMain
and@NSApplicationMain
apply to the new attribute as well.