Skip to content

Conversation

nate-chandler
Copy link
Contributor

@nate-chandler nate-chandler commented Mar 28, 2020

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.

@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - c429431b7a117ae748bea625532876a2296292ca

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - c429431b7a117ae748bea625532876a2296292ca

@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - c429431b7a117ae748bea625532876a2296292ca

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - c429431b7a117ae748bea625532876a2296292ca

@benrimmington
Copy link
Contributor

If you use @main without backticks (in comments or commit messages), Vlad Vannov may receive a notification!

@varungandhi-apple
Copy link
Contributor

Do you think it would be useful to add a test case for the interaction of @main with @available? I think it should work out since you are not skipping type-checking, but just in case it doesn't for some reason.

@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 7fd267eb97ccf8e854c5c92aeea6a1a0bc191d62

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 7fd267eb97ccf8e854c5c92aeea6a1a0bc191d62

@nate-chandler nate-chandler force-pushed the main-attribute branch 3 times, most recently from f1f16d8 to 684ea60 Compare April 17, 2020 16:53
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.
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 5f5815c08e8c4a2e18e7ac911807b9d68e6e0163

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 5f5815c08e8c4a2e18e7ac911807b9d68e6e0163

@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - b433fd2928e8dac4a2803919ebebb9c9eb51c3d8

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - b433fd2928e8dac4a2803919ebebb9c9eb51c3d8

@theblixguy theblixguy added the swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process label Apr 17, 2020
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.
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 333eaf8146a27eb4b3e87200f11c56dc318a2285

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 333eaf8146a27eb4b3e87200f11c56dc318a2285

@nate-chandler nate-chandler merged commit a41a2ff into swiftlang:master Apr 22, 2020
@nate-chandler nate-chandler deleted the main-attribute branch April 22, 2020 22:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants