Skip to content

Commit 23c1d11

Browse files
hborlaDougGregor
authored andcommitted
[Concurrency] Don't infer MainActor on top level globals in the task to thread
concurrency model, where global actors are not permitted. (cherry picked from commit 229d926)
1 parent d0244d7 commit 23c1d11

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3718,7 +3718,13 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
37183718

37193719
if (nominal->isMainActor() && Ctx.LangOpts.isConcurrencyModelTaskToThread() &&
37203720
!AvailableAttr::isUnavailable(D)) {
3721-
Ctx.Diags.diagnose(attr->getLocation(),
3721+
SourceLoc loc;
3722+
if (attr->isImplicit()) {
3723+
loc = D->getStartLoc();
3724+
} else {
3725+
loc = attr->getLocation();
3726+
}
3727+
Ctx.Diags.diagnose(loc,
37223728
diag::concurrency_task_to_thread_model_main_actor,
37233729
"task-to-thread concurrency model");
37243730
return;

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4631,8 +4631,10 @@ ActorIsolation ActorIsolationRequest::evaluate(
46314631
}
46324632

46334633
if (auto var = dyn_cast<VarDecl>(value)) {
4634-
if (var->isTopLevelGlobal() &&
4635-
(var->getASTContext().LangOpts.StrictConcurrencyLevel >=
4634+
auto &ctx = var->getASTContext();
4635+
if (!ctx.LangOpts.isConcurrencyModelTaskToThread() &&
4636+
var->isTopLevelGlobal() &&
4637+
(ctx.LangOpts.StrictConcurrencyLevel >=
46364638
StrictConcurrency::Complete ||
46374639
var->getDeclContext()->isAsyncContext())) {
46384640
if (Type mainActor = var->getASTContext().getMainActorType())
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-swift-frontend -concurrency-model=task-to-thread -typecheck -verify %s
2+
// RUN: %target-swift-frontend -concurrency-model=task-to-thread -typecheck -verify -verify-additional-prefix complete- -strict-concurrency=complete %s
3+
4+
// expected-complete-warning@+3 {{var 'global' is not concurrency-safe because it is non-isolated global shared mutable state; this is an error in Swift 6}}
5+
// expected-complete-note@+2 {{isolate 'global' to a global actor, or convert it to a 'let' constant and conform it to 'Sendable'}}
6+
// expected-complete-note@+1 {{var declared here}}
7+
var global = 10
8+
9+
// expected-complete-warning@+1 {{reference to var 'global' is not concurrency-safe because it involves shared mutable state; this is an error in Swift 6}}
10+
print(global)

0 commit comments

Comments
 (0)