Skip to content

Commit f42ba0e

Browse files
authored
Revert "[5.7🍒][Concurrency] Handle actor isolation for defers in closures"
1 parent b545ccb commit f42ba0e

File tree

3 files changed

+21
-104
lines changed

3 files changed

+21
-104
lines changed

‎lib/AST/Decl.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9140,20 +9140,14 @@ ActorIsolation swift::getActorIsolation(ValueDecl *value) {
91409140
}
91419141

91429142
ActorIsolation swift::getActorIsolationOfContext(DeclContext *dc) {
9143-
auto dcToUse = dc;
9144-
// Defer bodies share actor isolation of their enclosing context.
9145-
if (auto FD = dyn_cast<FuncDecl>(dcToUse)) {
9146-
if (FD->isDeferBody()) {
9147-
dcToUse = FD->getDeclContext();
9148-
}
9149-
}
9150-
if (auto *vd = dyn_cast_or_null<ValueDecl>(dcToUse->getAsDecl()))
9143+
if (auto *vd = dyn_cast_or_null<ValueDecl>(dc->getAsDecl()))
91519144
return getActorIsolation(vd);
91529145

9153-
if (auto *var = dcToUse->getNonLocalVarDecl())
9154-
return getActorIsolation(var);
9146+
if (auto *var = dc->getNonLocalVarDecl())
9147+
return getActorIsolation(var);
9148+
91559149

9156-
if (auto *closure = dyn_cast<AbstractClosureExpr>(dcToUse)) {
9150+
if (auto *closure = dyn_cast<AbstractClosureExpr>(dc)) {
91579151
switch (auto isolation = closure->getActorIsolation()) {
91589152
case ClosureActorIsolation::Independent:
91599153
return ActorIsolation::forIndependent()
@@ -9176,14 +9170,14 @@ ActorIsolation swift::getActorIsolationOfContext(DeclContext *dc) {
91769170
}
91779171
}
91789172

9179-
if (auto *tld = dyn_cast<TopLevelCodeDecl>(dcToUse)) {
9180-
if (dcToUse->isAsyncContext() ||
9181-
dcToUse->getASTContext().LangOpts.StrictConcurrencyLevel >=
9182-
StrictConcurrency::Complete) {
9183-
if (Type mainActor = dcToUse->getASTContext().getMainActorType())
9173+
if (auto *tld = dyn_cast<TopLevelCodeDecl>(dc)) {
9174+
if (dc->isAsyncContext() ||
9175+
dc->getASTContext().LangOpts.StrictConcurrencyLevel
9176+
>= StrictConcurrency::Complete) {
9177+
if (Type mainActor = dc->getASTContext().getMainActorType())
91849178
return ActorIsolation::forGlobalActor(
91859179
mainActor,
9186-
/*unsafe=*/!dcToUse->getASTContext().isSwiftVersionAtLeast(6));
9180+
/*unsafe=*/!dc->getASTContext().isSwiftVersionAtLeast(6));
91879181
}
91889182
}
91899183

‎lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,6 @@ static FuncDecl *findAnnotatableFunction(DeclContext *dc) {
14891489
}
14901490

14911491
/// Note when the enclosing context could be put on a global actor.
1492-
// FIXME: This should handle closures too.
14931492
static void noteGlobalActorOnContext(DeclContext *dc, Type globalActor) {
14941493
// If we are in a synchronous function on the global actor,
14951494
// suggest annotating with the global actor itself.
@@ -3756,6 +3755,14 @@ ActorIsolation ActorIsolationRequest::evaluate(
37563755
// If this is a local function, inherit the actor isolation from its
37573756
// context if it global or was captured.
37583757
if (auto func = dyn_cast<FuncDecl>(value)) {
3758+
// If this is a defer body, inherit unconditionally; we don't
3759+
// care if the enclosing function captures the isolated parameter.
3760+
if (func->isDeferBody()) {
3761+
auto enclosingIsolation =
3762+
getActorIsolationOfContext(func->getDeclContext());
3763+
return inferredIsolation(enclosingIsolation);
3764+
}
3765+
37593766
if (func->isLocalCapture() && !func->isSendable()) {
37603767
switch (auto enclosingIsolation =
37613768
getActorIsolationOfContext(func->getDeclContext())) {

‎test/Concurrency/actor_defer.swift

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
func doSomething() {}
77

8-
// expected-note @+1 6 {{calls to global function 'requiresMainActor()' from outside of its actor context are implicitly asynchronous}}
8+
// expected-note @+1 4 {{calls to global function 'requiresMainActor()' from outside of its actor context are implicitly asynchronous}}
99
@MainActor func requiresMainActor() {}
1010

1111
@MainActor func testNonDefer_positive() {
@@ -66,7 +66,7 @@ func testGlobalActorAsync_negative() async {
6666

6767
@available(SwiftStdlib 5.1, *)
6868
actor Actor {
69-
// expected-note @+1 6 {{mutation of this property is only permitted within the actor}}
69+
// expected-note @+1 3 {{mutation of this property is only permitted within the actor}}
7070
var actorProperty = 0
7171

7272
func testActor_positive() {
@@ -76,13 +76,6 @@ actor Actor {
7676
doSomething()
7777
}
7878

79-
func testActor_task_positive() {
80-
Task {
81-
defer { actorProperty += 1 }
82-
doSomething()
83-
}
84-
}
85-
8679
#if NEGATIVES
8780
nonisolated func testActor_negative() {
8881
defer {
@@ -91,30 +84,13 @@ actor Actor {
9184
}
9285
doSomething()
9386
}
94-
95-
nonisolated func testActor_task_negative() {
96-
Task {
97-
// expected-error @+1 {{actor-isolated property 'actorProperty' can not be mutated from a non-isolated context}}
98-
defer { actorProperty += 1 }
99-
doSomething()
100-
}
101-
}
102-
10387
@MainActor func testActor_negative_globalActor() {
10488
defer {
10589
// expected-error @+1 {{actor-isolated property 'actorProperty' can not be mutated from the main actor}}
10690
actorProperty += 1
10791
}
10892
doSomething()
10993
}
110-
111-
func testActor_task_negative_globalActor() {
112-
Task { @MainActor in
113-
// expected-error @+1 {{actor-isolated property 'actorProperty' can not be mutated from the main actor}}
114-
defer { actorProperty += 1 }
115-
doSomething()
116-
}
117-
}
11894
#endif
11995

12096
@MainActor func testGlobalActor_positive() {
@@ -123,13 +99,6 @@ actor Actor {
12399
}
124100
doSomething()
125101
}
126-
127-
func testGlobalActor_task_positive() {
128-
Task { @MainActor in
129-
defer { requiresMainActor() }
130-
doSomething()
131-
}
132-
}
133102

134103
#if NEGATIVES
135104
func testGlobalActor_negative() {
@@ -139,14 +108,6 @@ actor Actor {
139108
}
140109
doSomething()
141110
}
142-
143-
func testGlobalActor_task_negative() {
144-
Task {
145-
// expected-error @+1 {{call to main actor-isolated global function 'requiresMainActor()' in a synchronous nonisolated context}}
146-
defer { requiresMainActor() }
147-
doSomething()
148-
}
149-
}
150111
#endif
151112
}
152113

@@ -169,48 +130,3 @@ func testIsolatedActor_negative(actor: Actor) {
169130
doSomething()
170131
}
171132
#endif
172-
173-
@available(SwiftStdlib 5.1, *)
174-
func testGlobalActor_inTask_positive() {
175-
Task { @MainActor in
176-
defer { requiresMainActor() }
177-
doSomething()
178-
}
179-
}
180-
181-
#if NEGATIVES
182-
@available(SwiftStdlib 5.1, *)
183-
func testGlobalActor_inTask_negative() {
184-
Task {
185-
// expected-error @+1 {{call to main actor-isolated global function 'requiresMainActor()' in a synchronous nonisolated context}}
186-
defer { requiresMainActor() }
187-
doSomething()
188-
}
189-
}
190-
#endif
191-
192-
@available(SwiftStdlib 5.1, *)
193-
func takeClosureWithIsolatedParam(body: (isolated Actor) -> Void) {}
194-
195-
@available(SwiftStdlib 5.1, *)
196-
func takeClosureWithNotIsolatedParam(body: (Actor) -> Void) {}
197-
198-
@available(SwiftStdlib 5.1, *)
199-
func testIsolatedActor_closure_positive() {
200-
takeClosureWithIsolatedParam { actor in
201-
actor.actorProperty += 1
202-
defer { actor.actorProperty += 1 }
203-
doSomething()
204-
}
205-
}
206-
207-
#if NEGATIVES
208-
@available(SwiftStdlib 5.1, *)
209-
func testIsolatedActor_closure_negative() {
210-
takeClosureWithNotIsolatedParam { actor in
211-
// expected-error @+1 {{actor-isolated property 'actorProperty' can not be mutated from a non-isolated context}}
212-
defer { actor.actorProperty += 1 }
213-
doSomething()
214-
}
215-
}
216-
#endif

0 commit comments

Comments
 (0)