Skip to content

Commit 79e6c61

Browse files
committed
[Concurrency] Treat actor-isolated references from closures as errors.
Following the approach that actors are meant to be isolated, promote the warning about accessing actor-isolated state from a potentially-concurrent context to an error.
1 parent 0a1be02 commit 79e6c61

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4141,10 +4141,14 @@ ERROR(actor_isolated_non_self_reference,none,
41414141
"actor-isolated %0 %1 can only be referenced "
41424142
"%select{inside the actor|on 'self'}2",
41434143
(DescriptiveDeclKind, DeclName, bool))
4144-
WARNING(concurrent_access,none,
4145-
"%select{local|actor-isolated}0 %1 %2 is unsafe to reference in code "
4144+
WARNING(concurrent_access_local,none,
4145+
"local %0 %1 is unsafe to reference in code that may execute "
4146+
"concurrently",
4147+
(DescriptiveDeclKind, DeclName))
4148+
ERROR(actor_isolated_concurrent_access,none,
4149+
"actor-isolated %0 %1 is unsafe to reference in code "
41464150
"that may execute concurrently",
4147-
(bool, DescriptiveDeclKind, DeclName))
4151+
(DescriptiveDeclKind, DeclName))
41484152
NOTE(actor_isolated_method,none,
41494153
"only asynchronous methods can be used outside the actor instance; "
41504154
"do you want to add 'async'?", ())

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ void swift::checkActorIsolation(const Expr *expr, const DeclContext *dc) {
551551
if (mayExecuteConcurrentlyWith(
552552
getDeclContext(), isolation.getLocalContext())) {
553553
ctx.Diags.diagnose(
554-
loc, diag::concurrent_access, false,
554+
loc, diag::concurrent_access_local,
555555
value->getDescriptiveKind(), value->getName());
556556
value->diagnose(
557557
diag::kind_declared_here, value->getDescriptiveKind());
@@ -593,7 +593,7 @@ void swift::checkActorIsolation(const Expr *expr, const DeclContext *dc) {
593593
if (mayExecuteConcurrentlyWith(
594594
getDeclContext(), selfVar->getDeclContext())) {
595595
ctx.Diags.diagnose(
596-
memberLoc, diag::concurrent_access, true,
596+
memberLoc, diag::actor_isolated_concurrent_access,
597597
member->getDescriptiveKind(), member->getName());
598598
noteIsolatedActorMember(member);
599599
return true;

test/Concurrency/actor_isolation.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ actor class MyActor: MySuperActor {
2828
class func synchronousClass() { }
2929
static func synchronousStatic() { }
3030

31-
func synchronous() -> String { text.first ?? "nothing" } // expected-note 3{{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
31+
func synchronous() -> String { text.first ?? "nothing" } // expected-note 4{{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
3232
func asynchronous() async -> String { synchronous() }
3333
}
3434

@@ -73,14 +73,15 @@ extension MyActor {
7373
let localConstant = 17
7474
var localVar = 17 // expected-note 2{{var declared here}}
7575
acceptClosure {
76-
_ = text[0] // expected-warning{{actor-isolated property 'text' is unsafe to reference in code that may execute concurrently}}
77-
_ = self.synchronous() // expected-warning{{actor-isolated instance method 'synchronous()' is unsafe to reference in code that may execute concurrently}}
76+
_ = text[0] // expected-error{{actor-isolated property 'text' is unsafe to reference in code that may execute concurrently}}
77+
_ = self.synchronous() // expected-error{{actor-isolated instance method 'synchronous()' is unsafe to reference in code that may execute concurrently}}
7878
_ = localVar // expected-warning{{local var 'localVar' is unsafe to reference in code that may execute concurrently}}
7979
_ = localConstant
8080
}
8181

8282
acceptEscapingClosure {
83-
_ = self.text[0] // expected-warning{{actor-isolated property 'text' is unsafe to reference in code that may execute concurrently}}
83+
_ = self.text[0] // expected-error{{actor-isolated property 'text' is unsafe to reference in code that may execute concurrently}}
84+
_ = self.synchronous() // expected-error{{actor-isolated instance method 'synchronous()' is unsafe to reference in code that may execute concurrently}}
8485
_ = localVar // expected-warning{{local var 'localVar' is unsafe to reference in code that may execute concurrently}}
8586
_ = localConstant
8687
}

0 commit comments

Comments
 (0)