Skip to content

Commit 638a5b6

Browse files
committed
Fix crash with async main and no conc module
The concurrency module gets imported implicitly when `-enable-experimental-concurrency` is passed to the compiler. If the concurrency module isn't imported, we crash on th assert verifying that we were able to find it. We need the module to lookup the `_runAsyncMain` function. This patch updates the compiler to stop type-checking the async-main function and emit an error if the module isn't available instead of crashing.
1 parent 7feafbd commit 638a5b6

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4379,6 +4379,9 @@ ERROR(actorindependent_local_var,none,
43794379
ERROR(concurrency_lib_missing,none,
43804380
"missing '%0' declaration, probably because the '_Concurrency' "
43814381
"module was not imported", (StringRef))
4382+
ERROR(async_main_no_concurrency,none,
4383+
"'_Concurrency' module not imported, required for async main", ())
4384+
43824385
ERROR(enqueue_partial_task_not_in_context,none,
43834386
"'enqueue(partialTask:)' can only be implemented in the definition of "
43844387
"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(

0 commit comments

Comments
 (0)