File tree Expand file tree Collapse file tree 3 files changed +41
-3
lines changed
test/Concurrency/toplevel Expand file tree Collapse file tree 3 files changed +41
-3
lines changed Original file line number Diff line number Diff line change @@ -9056,7 +9056,9 @@ ActorIsolation swift::getActorIsolationOfContext(DeclContext *dc) {
9056
9056
}
9057
9057
9058
9058
if (auto *tld = dyn_cast<TopLevelCodeDecl>(dc)) {
9059
- if (dc->isAsyncContext ()) {
9059
+ if (dc->isAsyncContext () ||
9060
+ (dc->getASTContext ().LangOpts .WarnConcurrency &&
9061
+ dc->getASTContext ().LangOpts .EnableExperimentalAsyncTopLevel )) {
9060
9062
if (Type mainActor = dc->getASTContext ().getMainActorType ())
9061
9063
return ActorIsolation::forGlobalActor (mainActor, /* unsafe=*/ false );
9062
9064
}
Original file line number Diff line number Diff line change @@ -344,7 +344,9 @@ GlobalActorAttributeRequest::evaluate(
344
344
if (auto var = dyn_cast<VarDecl>(storage)) {
345
345
346
346
// ... but not if it's an async-context top-level global
347
- if (var->isTopLevelGlobal () && var->getDeclContext ()->isAsyncContext ()) {
347
+ if (var->getASTContext ().LangOpts .EnableExperimentalAsyncTopLevel &&
348
+ var->isTopLevelGlobal () && (var->getDeclContext ()->isAsyncContext ()
349
+ || var->getASTContext ().LangOpts .WarnConcurrency )) {
348
350
var->diagnose (diag::global_actor_top_level_var)
349
351
.highlight (globalActorAttr->getRangeWithAt ());
350
352
return None;
@@ -3818,7 +3820,10 @@ ActorIsolation ActorIsolationRequest::evaluate(
3818
3820
}
3819
3821
3820
3822
if (auto var = dyn_cast<VarDecl>(value)) {
3821
- if (var->isTopLevelGlobal () && var->getDeclContext ()->isAsyncContext ()) {
3823
+ if (var->getASTContext ().LangOpts .EnableExperimentalAsyncTopLevel &&
3824
+ var->isTopLevelGlobal () &&
3825
+ (var->getASTContext ().LangOpts .WarnConcurrency ||
3826
+ var->getDeclContext ()->isAsyncContext ())) {
3822
3827
if (Type mainActor = var->getASTContext ().getMainActorType ())
3823
3828
return inferredIsolation (
3824
3829
ActorIsolation::forGlobalActor (mainActor,
Original file line number Diff line number Diff line change
1
+ // RUN: %target-swift-frontend -disable-availability-checking -enable-experimental-async-top-level -warn-concurrency -typecheck -verify %s
2
+
3
+ var a = 10 // expected-note{{var declared here}}
4
+
5
+ @MainActor
6
+ var b = 15 // expected-error{{top-level code variables cannot have a global actor}}
7
+
8
+ func unsafeAccess( ) { // expected-note{{add '@MainActor' to make global function 'unsafeAccess()' part of global actor 'MainActor'}}
9
+ print ( a) // expected-error@:11{{var 'a' isolated to global actor 'MainActor' can not be referenced from this synchronous context}}
10
+ }
11
+
12
+ func unsafeAsyncAccess( ) async {
13
+ print ( a) // expected-error@:5{{expression is 'async' but is not marked with 'await'}}{{5-5=await }}
14
+ // expected-note@-1:11{{property access is 'async'}}
15
+ }
16
+
17
+ @MainActor
18
+ func safeAccess( ) {
19
+ print ( a)
20
+ }
21
+
22
+ @MainActor
23
+ func safeSyncAccess( ) async {
24
+ print ( a)
25
+ }
26
+
27
+ func safeAsyncAccess( ) async {
28
+ await print ( a)
29
+ }
30
+
31
+ print ( a)
You can’t perform that action at this time.
0 commit comments