Skip to content

Commit 089b78f

Browse files
committed
Add @preconcurrency to toplevel vars
Top-level global variables should have the `@predatesConcurrency @MainActor` behaviour. This allows them to be somewhat concurrency safe while still working with old code.
1 parent 2c3bef8 commit 089b78f

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/AST/Decl.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,10 +733,16 @@ bool Decl::preconcurrency() const {
733733
if (isa<ClangModuleUnit>(getDeclContext()->getModuleScopeContext()))
734734
return true;
735735

736+
// Variables declared in top-level code are @_predatesConcurrency
737+
if (const VarDecl *var = dyn_cast<VarDecl>(this)) {
738+
const LangOptions &langOpts = getASTContext().LangOpts;
739+
return !langOpts.isSwiftVersionAtLeast(6) &&
740+
langOpts.EnableExperimentalAsyncTopLevel && var->isTopLevelGlobal();
741+
}
742+
736743
return false;
737744
}
738745

739-
740746
Expr *AbstractFunctionDecl::getSingleExpressionBody() const {
741747
assert(hasSingleExpressionBody() && "Not a single-expression body");
742748
auto braceStmt = getBody();

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3516,7 +3516,7 @@ ActorIsolation ActorIsolationRequest::evaluate(
35163516
if (Type mainActor = ctx.getMainActorType())
35173517
return inferredIsolation(
35183518
ActorIsolation::forGlobalActor(mainActor,
3519-
/*unsafe=*/false));
3519+
/*unsafe=*/var->preconcurrency()));
35203520
}
35213521
if (auto isolation = getActorIsolationFromWrappedProperty(var))
35223522
return inferredIsolation(isolation);

0 commit comments

Comments
 (0)