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

Conversation

etcwilde
Copy link
Member

The compiler would assert/crash if someone tried to use async-main without enabling concurrency since it would fail to find the concurrency module. import _Concurrency in the source file would also work, but is kind of gross.

I've decided to go down the route of just protecting the feature behind a check for enabling concurrency. Once we have, the concurrency module is imported implicitly, and we avoid the crash. Alternatively, we could just recommend that folks import the concurrency module.

Fixes rdar://74821680

@etcwilde etcwilde requested a review from DougGregor February 27, 2021 09:49
@etcwilde
Copy link
Member Author

@swift-ci please smoke test

@etcwilde
Copy link
Member Author

@swift-ci please smoke test macOS

@etcwilde
Copy link
Member Author

@swift-ci please smoke test windows platform

@@ -1831,6 +1831,18 @@ synthesizeMainBody(AbstractFunctionDecl *fn, void *arg) {
Expr *returnedExpr;

if (mainFunction->hasAsync()) {
if (!context.LangOpts.EnableExperimentalConcurrency) {
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.

@etcwilde
Copy link
Member Author

@swift-ci please smoke test Windows

1 similar comment
@etcwilde
Copy link
Member Author

etcwilde commented Mar 1, 2021

@swift-ci please smoke test Windows

@etcwilde
Copy link
Member Author

etcwilde commented Mar 2, 2021

@swift-ci please smoke test windows

1 similar comment
@etcwilde
Copy link
Member Author

etcwilde commented Mar 2, 2021

@swift-ci please smoke test windows

@@ -1831,6 +1831,18 @@ synthesizeMainBody(AbstractFunctionDecl *fn, void *arg) {
Expr *returnedExpr;

if (mainFunction->hasAsync()) {
if (!context.LangOpts.EnableExperimentalConcurrency) {
context.Diags.diagnose(mainFunction->getAsyncLoc(),
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.

etcwilde added 2 commits March 2, 2021 14:55
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.
This patch adds a quick test to verify that we emit a reasonable error
message when trying to use async-main without enabling concurrency or
parsing as stdlib instead of crashing.

Under both circumstances, the _Concurrency library won't be implicitly
imported.
@etcwilde etcwilde force-pushed the ewilde/fix-crash-in-async-main branch from 42a5ef2 to 6d18260 Compare March 2, 2021 23:00
@etcwilde
Copy link
Member Author

etcwilde commented Mar 2, 2021

@swift-ci please smoke test

@etcwilde etcwilde merged commit a929016 into swiftlang:main Mar 3, 2021
@etcwilde etcwilde deleted the ewilde/fix-crash-in-async-main branch March 3, 2021 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants