Skip to content

Commit 099045a

Browse files
authored
Merge pull request #71039 from hborla/implicit-main-actor-freestanding
[Concurrency] Don't infer `@MainActor` on top level globals in the task-to-thread concurrency model.
2 parents 7b5fa6e + 229d926 commit 099045a

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
@@ -3981,7 +3981,13 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
39813981

39823982
if (nominal->isMainActor() && Ctx.LangOpts.isConcurrencyModelTaskToThread() &&
39833983
!AvailableAttr::isUnavailable(D)) {
3984-
Ctx.Diags.diagnose(attr->getLocation(),
3984+
SourceLoc loc;
3985+
if (attr->isImplicit()) {
3986+
loc = D->getStartLoc();
3987+
} else {
3988+
loc = attr->getLocation();
3989+
}
3990+
Ctx.Diags.diagnose(loc,
39853991
diag::concurrency_task_to_thread_model_main_actor,
39863992
"task-to-thread concurrency model");
39873993
return;

lib/Sema/TypeCheckConcurrency.cpp

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

49004900
if (auto var = dyn_cast<VarDecl>(value)) {
4901-
if (var->isTopLevelGlobal() &&
4902-
(var->getASTContext().LangOpts.StrictConcurrencyLevel >=
4901+
auto &ctx = var->getASTContext();
4902+
if (!ctx.LangOpts.isConcurrencyModelTaskToThread() &&
4903+
var->isTopLevelGlobal() &&
4904+
(ctx.LangOpts.StrictConcurrencyLevel >=
49034905
StrictConcurrency::Complete ||
49044906
var->getDeclContext()->isAsyncContext())) {
49054907
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)