Skip to content

Commit 1a29c96

Browse files
authored
Merge pull request #38491 from DougGregor/global-actor-defer-5.5
Global actor defer 5.5
2 parents 3f66f26 + fe787ca commit 1a29c96

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,6 +3126,25 @@ ActorIsolation ActorIsolationRequest::evaluate(
31263126
return inferred;
31273127
};
31283128

3129+
// If this is a "defer" function body, inherit the global actor isolation
3130+
// from its context.
3131+
if (auto func = dyn_cast<FuncDecl>(value)) {
3132+
if (func->isDeferBody()) {
3133+
switch (auto enclosingIsolation =
3134+
getActorIsolationOfContext(func->getDeclContext())) {
3135+
case ActorIsolation::ActorInstance:
3136+
case ActorIsolation::Independent:
3137+
case ActorIsolation::Unspecified:
3138+
// Do nothing.
3139+
break;
3140+
3141+
case ActorIsolation::GlobalActor:
3142+
case ActorIsolation::GlobalActorUnsafe:
3143+
return inferredIsolation(enclosingIsolation);
3144+
}
3145+
}
3146+
}
3147+
31293148
// If the declaration overrides another declaration, it must have the same
31303149
// actor isolation.
31313150
if (auto overriddenValue = value->getOverriddenDecl()) {

test/Concurrency/global_actor_inference.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,17 @@ func acceptAsyncSendableClosureInheriting<T>(@_inheritActorContext _: @Sendable
552552
await onlyOnMainActor() // expected-warning{{no 'async' operations occur within 'await' expression}}
553553
}
554554
}
555+
556+
557+
// defer bodies inherit global actor-ness
558+
@MainActor
559+
var statefulThingy: Bool = false
560+
561+
@MainActor
562+
func useFooInADefer() -> String {
563+
defer {
564+
statefulThingy = true
565+
}
566+
567+
return "hello"
568+
}

0 commit comments

Comments
 (0)