Skip to content

[Concurrency] Fix async-main crash #36201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4379,6 +4379,9 @@ ERROR(actorindependent_local_var,none,
ERROR(concurrency_lib_missing,none,
"missing '%0' declaration, probably because the '_Concurrency' "
"module was not imported", (StringRef))
ERROR(async_main_no_concurrency,none,
"'_Concurrency' module not imported, required for async main", ())

ERROR(enqueue_partial_task_not_in_context,none,
"'enqueue(partialTask:)' can only be implemented in the definition of "
"actor class %0", (Type))
Expand Down
12 changes: 11 additions & 1 deletion lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1835,7 +1835,17 @@ synthesizeMainBody(AbstractFunctionDecl *fn, void *arg) {
// Resulting $main looks like:
// $main() { _runAsyncMain(main) }
auto *concurrencyModule = context.getLoadedModule(context.Id_Concurrency);
assert(concurrencyModule != nullptr && "Failed to find Concurrency module");
if (!concurrencyModule) {
context.Diags.diagnose(mainFunction->getAsyncLoc(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want a comment relating how this fixes the crash because _Concurrency implicitly gets imported when -enable-experimental-concurrency is passed in, but doesn't when it's not. If that changes, the crash comes back.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll work the way it is now, but how about checking for !concurrencyModule below instead of -enable-experimental-concurrency? That would avoid the crash if the module doesn't get loaded for any reason.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, now checks against the concurrency module. Also added a test passing both -enable-experimental-concurrency and -parse-stdlib since that won't implicitly import _Concurrency either.

diag::async_main_no_concurrency);
auto result = new (context) ErrorExpr(mainFunction->getSourceRange());
SmallVector<ASTNode, 1> stmts;
stmts.push_back(result);
auto body = BraceStmt::create(context, SourceLoc(), stmts,
SourceLoc(), /*Implicit*/true);

return std::make_pair(body, /*typechecked*/true);
}

SmallVector<ValueDecl *, 1> decls;
concurrencyModule->lookupQualified(
Expand Down
8 changes: 8 additions & 0 deletions test/Concurrency/async_main_no_concurrency.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %target-typecheck-verify-swift -parse-as-library %s
// RUN: %target-typecheck-verify-swift -parse-as-library -enable-experimental-concurrency -parse-stdlib %s

@main struct Main {
// expected-error@+1:22{{'_Concurrency' module not imported, required for async main}}
static func main() async {
}
}