Skip to content

Commit a929016

Browse files
authored
Merge pull request #36201 from etcwilde/ewilde/fix-crash-in-async-main
[Concurrency] Fix async-main crash
2 parents ce0a3a2 + 6d18260 commit a929016

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4389,6 +4389,9 @@ ERROR(actorindependent_local_var,none,
43894389
ERROR(concurrency_lib_missing,none,
43904390
"missing '%0' declaration, probably because the '_Concurrency' "
43914391
"module was not imported", (StringRef))
4392+
ERROR(async_main_no_concurrency,none,
4393+
"'_Concurrency' module not imported, required for async main", ())
4394+
43924395
ERROR(enqueue_partial_task_not_in_context,none,
43934396
"'enqueue(partialTask:)' can only be implemented in the definition of "
43944397
"actor class %0", (Type))

lib/Sema/TypeCheckAttr.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1835,7 +1835,17 @@ synthesizeMainBody(AbstractFunctionDecl *fn, void *arg) {
18351835
// Resulting $main looks like:
18361836
// $main() { _runAsyncMain(main) }
18371837
auto *concurrencyModule = context.getLoadedModule(context.Id_Concurrency);
1838-
assert(concurrencyModule != nullptr && "Failed to find Concurrency module");
1838+
if (!concurrencyModule) {
1839+
context.Diags.diagnose(mainFunction->getAsyncLoc(),
1840+
diag::async_main_no_concurrency);
1841+
auto result = new (context) ErrorExpr(mainFunction->getSourceRange());
1842+
SmallVector<ASTNode, 1> stmts;
1843+
stmts.push_back(result);
1844+
auto body = BraceStmt::create(context, SourceLoc(), stmts,
1845+
SourceLoc(), /*Implicit*/true);
1846+
1847+
return std::make_pair(body, /*typechecked*/true);
1848+
}
18391849

18401850
SmallVector<ValueDecl *, 1> decls;
18411851
concurrencyModule->lookupQualified(
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %target-typecheck-verify-swift -parse-as-library %s
2+
// RUN: %target-typecheck-verify-swift -parse-as-library -enable-experimental-concurrency -parse-stdlib %s
3+
4+
@main struct Main {
5+
// expected-error@+1:22{{'_Concurrency' module not imported, required for async main}}
6+
static func main() async {
7+
}
8+
}

0 commit comments

Comments
 (0)