@@ -1935,7 +1935,31 @@ synthesizeMainBody(AbstractFunctionDecl *fn, void *arg) {
19351935
19361936 Expr *returnedExpr;
19371937
1938- if (mainFunction->hasThrows ()) {
1938+ if (mainFunction->hasAsync ()) {
1939+ // Pass main into _runAsyncMain(_ asyncFunc: () async throws -> ())
1940+ // Resulting $main looks like:
1941+ // $main() { _runAsyncMain(main) }
1942+ auto *concurrencyModule = context.getLoadedModule (context.Id_Concurrency );
1943+ assert (concurrencyModule != nullptr && " Failed to find Concurrency module" );
1944+
1945+ SmallVector<ValueDecl *, 1 > decls;
1946+ concurrencyModule->lookupQualified (
1947+ concurrencyModule,
1948+ DeclNameRef (context.getIdentifier (" _runAsyncMain" )),
1949+ NL_QualifiedDefault | NL_IncludeUsableFromInline,
1950+ decls);
1951+ assert (!decls.empty () && " Failed to find _runAsyncMain" );
1952+ FuncDecl *runner = cast<FuncDecl>(decls[0 ]);
1953+
1954+ auto asyncRunnerDeclRef = ConcreteDeclRef (runner, substitutionMap);
1955+
1956+ DeclRefExpr *funcExpr = new (context) DeclRefExpr (asyncRunnerDeclRef,
1957+ DeclNameLoc (),
1958+ /* implicit=*/ true );
1959+ funcExpr->setType (runner->getInterfaceType ());
1960+ auto *callExpr = CallExpr::createImplicit (context, funcExpr, memberRefExpr, {});
1961+ returnedExpr = callExpr;
1962+ } else if (mainFunction->hasThrows ()) {
19391963 auto *tryExpr = new (context) TryExpr (
19401964 callExpr->getLoc (), callExpr, context.TheEmptyTupleType , /* implicit=*/ true );
19411965 returnedExpr = tryExpr;
0 commit comments