Skip to content

Make toplevel vars MainActor with -warn-concurrency #41763

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
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
4 changes: 3 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9056,7 +9056,9 @@ ActorIsolation swift::getActorIsolationOfContext(DeclContext *dc) {
}

if (auto *tld = dyn_cast<TopLevelCodeDecl>(dc)) {
if (dc->isAsyncContext()) {
if (dc->isAsyncContext() ||
(dc->getASTContext().LangOpts.WarnConcurrency &&
dc->getASTContext().LangOpts.EnableExperimentalAsyncTopLevel)) {
if (Type mainActor = dc->getASTContext().getMainActorType())
return ActorIsolation::forGlobalActor(mainActor, /*unsafe=*/false);
}
Expand Down
9 changes: 7 additions & 2 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ GlobalActorAttributeRequest::evaluate(
if (auto var = dyn_cast<VarDecl>(storage)) {

// ... but not if it's an async-context top-level global
if (var->isTopLevelGlobal() && var->getDeclContext()->isAsyncContext()) {
if (var->getASTContext().LangOpts.EnableExperimentalAsyncTopLevel &&
var->isTopLevelGlobal() && (var->getDeclContext()->isAsyncContext()
|| var->getASTContext().LangOpts.WarnConcurrency)) {
var->diagnose(diag::global_actor_top_level_var)
.highlight(globalActorAttr->getRangeWithAt());
return None;
Expand Down Expand Up @@ -3818,7 +3820,10 @@ ActorIsolation ActorIsolationRequest::evaluate(
}

if (auto var = dyn_cast<VarDecl>(value)) {
if (var->isTopLevelGlobal() && var->getDeclContext()->isAsyncContext()) {
if (var->getASTContext().LangOpts.EnableExperimentalAsyncTopLevel &&
var->isTopLevelGlobal() &&
(var->getASTContext().LangOpts.WarnConcurrency ||
var->getDeclContext()->isAsyncContext())) {
if (Type mainActor = var->getASTContext().getMainActorType())
return inferredIsolation(
ActorIsolation::forGlobalActor(mainActor,
Expand Down
31 changes: 31 additions & 0 deletions test/Concurrency/toplevel/synchronous_mainactor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %target-swift-frontend -disable-availability-checking -enable-experimental-async-top-level -warn-concurrency -typecheck -verify %s

var a = 10 // expected-note{{var declared here}}

@MainActor
var b = 15 // expected-error{{top-level code variables cannot have a global actor}}

func unsafeAccess() { // expected-note{{add '@MainActor' to make global function 'unsafeAccess()' part of global actor 'MainActor'}}
print(a) // expected-error@:11{{var 'a' isolated to global actor 'MainActor' can not be referenced from this synchronous context}}
}

func unsafeAsyncAccess() async {
print(a) // expected-error@:5{{expression is 'async' but is not marked with 'await'}}{{5-5=await }}
// expected-note@-1:11{{property access is 'async'}}
}

@MainActor
func safeAccess() {
print(a)
}

@MainActor
func safeSyncAccess() async {
print(a)
}

func safeAsyncAccess() async {
await print(a)
}

print(a)