Skip to content

Commit da0a331

Browse files
committed
Add -async-main flag to favor asynchronous main
This flag biases the overload checker in favor of selecting an asynchronous main function over a synchronous main. If no asynchronous main function exists, a synchronous one will still be selected. Likewise, if the flag is not passed and there are only asynchronous main functions available, the most specific asynchronous main function will still be selected.
1 parent 2021a73 commit da0a331

File tree

9 files changed

+33
-5
lines changed

9 files changed

+33
-5
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ namespace swift {
209209
/// Emit a remark after loading a module.
210210
bool EnableModuleLoadingRemarks = false;
211211

212+
/// Resolve main function as though it were called from an async context
213+
bool EnableAsyncMainResolution = false;
214+
212215
///
213216
/// Support for alternate usage modes
214217
///

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ def pch_output_dir: Separate<["-"], "pch-output-dir">,
270270
Flags<[FrontendOption, HelpHidden, ArgumentIsPath]>,
271271
HelpText<"Directory to persist automatically created precompiled bridging headers">;
272272

273+
def async_main: Flag<["-"], "async-main">,
274+
Flags<[FrontendOption]>,
275+
HelpText<"Resolve main function as if it were called from an asynchronous context">;
276+
273277
// FIXME: Unhide this once it doesn't depend on an output file map.
274278
def incremental : Flag<["-"], "incremental">,
275279
Flags<[NoInteractiveOption, HelpHidden, DoesNotAffectIncrementalBuild]>,

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,10 @@ enum class ConstraintSystemFlags {
14971497
/// calling conventions, say due to Clang attributes such as
14981498
/// `__attribute__((ns_consumed))`.
14991499
UseClangFunctionTypes = 0x80,
1500+
1501+
/// When set, nominal typedecl contexts are asynchronous contexts.
1502+
/// This is set while searching for the main function
1503+
ConsiderNominalTypeContextsAsync = 0x100,
15001504
};
15011505

15021506
/// Options that affect the constraint system as a whole.

include/swift/Sema/IDETypeChecking.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "swift/AST/Identifier.h"
2323
#include "swift/Basic/SourceLoc.h"
24+
#include "swift/Sema/ConstraintSystem.h"
2425
#include <memory>
2526
#include <tuple>
2627

@@ -93,8 +94,9 @@ namespace swift {
9394
/// Unlike other member lookup functions, \c swift::resolveValueMember()
9495
/// should be used when you want to look up declarations with the same name as
9596
/// one you already have.
96-
ResolvedMemberResult resolveValueMember(DeclContext &DC, Type BaseTy,
97-
DeclName Name);
97+
ResolvedMemberResult
98+
resolveValueMember(DeclContext &DC, Type BaseTy, DeclName Name,
99+
constraints::ConstraintSystemOptions Options = {});
98100

99101
/// Given a type and an extension to the original type decl of that type,
100102
/// decide if the extension has been applied, i.e. if the requirements of the

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
298298
options::OPT_verify_incremental_dependencies);
299299
inputArgs.AddLastArg(arguments, options::OPT_access_notes_path);
300300
inputArgs.AddLastArg(arguments, options::OPT_library_level);
301+
inputArgs.AddLastArg(arguments, options::OPT_async_main);
301302

302303
// Pass on any build config options
303304
inputArgs.AddAllArgs(arguments, options::OPT_D);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
489489
Diags.diagnose(SourceLoc(), diag::warn_flag_deprecated,
490490
"-enable-experimental-async-top-level");
491491

492+
Opts.EnableAsyncMainResolution = Args.hasArg(OPT_async_main);
493+
492494
Opts.DiagnoseInvalidEphemeralnessAsError |=
493495
Args.hasArg(OPT_enable_invalid_ephemeralness_as_error);
494496

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4421,9 +4421,10 @@ getMemberDecls(InterestedMemberKind Kind) {
44214421
}
44224422

44234423
ResolvedMemberResult
4424-
swift::resolveValueMember(DeclContext &DC, Type BaseTy, DeclName Name) {
4424+
swift::resolveValueMember(DeclContext &DC, Type BaseTy, DeclName Name,
4425+
ConstraintSystemOptions Options) {
44254426
ResolvedMemberResult Result;
4426-
ConstraintSystem CS(&DC, None);
4427+
ConstraintSystem CS(&DC, Options);
44274428

44284429
// Look up all members of BaseTy with the given Name.
44294430
MemberLookupResult LookupResult = CS.performMemberLookup(

lib/Sema/ConstraintSystem.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,6 +2875,11 @@ bool ConstraintSystem::isAsynchronousContext(DeclContext *dc) {
28752875
FunctionType::ExtInfo()).isAsync();
28762876
}
28772877

2878+
if (Options.contains(
2879+
ConstraintSystemFlags::ConsiderNominalTypeContextsAsync) &&
2880+
isa<NominalTypeDecl>(dc))
2881+
return true;
2882+
28782883
return false;
28792884
}
28802885

lib/Sema/TypeCheckAttr.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2171,8 +2171,14 @@ SynthesizeMainFunctionRequest::evaluate(Evaluator &evaluator,
21712171
// mainType.main() from the entry point, and that would require fully
21722172
// type-checking the call to mainType.main().
21732173

2174+
constraints::ConstraintSystemOptions lookupOptions;
2175+
if (context.LangOpts.EnableAsyncMainResolution)
2176+
lookupOptions |=
2177+
constraints::ConstraintSystemFlags::ConsiderNominalTypeContextsAsync;
2178+
21742179
auto resolution = resolveValueMember(
2175-
*declContext, nominal->getInterfaceType(), context.Id_main);
2180+
*declContext, nominal->getInterfaceType(), context.Id_main,
2181+
lookupOptions);
21762182
FuncDecl *mainFunction =
21772183
resolveMainFunctionDecl(declContext, resolution, context);
21782184
if (!mainFunction) {

0 commit comments

Comments
 (0)