Skip to content

Commit 0c6063f

Browse files
authored
Merge pull request #38481 from DougGregor/global-actor-defer
2 parents 77b2b4f + 5dc4fcf commit 0c6063f

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3225,6 +3225,26 @@ ActorIsolation ActorIsolationRequest::evaluate(
32253225
return inferred;
32263226
};
32273227

3228+
// If this is a "defer" function body, inherit the global actor isolation
3229+
// from its context.
3230+
if (auto func = dyn_cast<FuncDecl>(value)) {
3231+
if (func->isDeferBody()) {
3232+
switch (auto enclosingIsolation =
3233+
getActorIsolationOfContext(func->getDeclContext())) {
3234+
case ActorIsolation::ActorInstance:
3235+
case ActorIsolation::DistributedActorInstance:
3236+
case ActorIsolation::Independent:
3237+
case ActorIsolation::Unspecified:
3238+
// Do nothing.
3239+
break;
3240+
3241+
case ActorIsolation::GlobalActor:
3242+
case ActorIsolation::GlobalActorUnsafe:
3243+
return inferredIsolation(enclosingIsolation);
3244+
}
3245+
}
3246+
}
3247+
32283248
// If the declaration overrides another declaration, it must have the same
32293249
// actor isolation.
32303250
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
@@ -538,3 +538,17 @@ func acceptAsyncSendableClosureInheriting<T>(@_inheritActorContext _: @Sendable
538538
await onlyOnMainActor() // expected-warning{{no 'async' operations occur within 'await' expression}}
539539
}
540540
}
541+
542+
543+
// defer bodies inherit global actor-ness
544+
@MainActor
545+
var statefulThingy: Bool = false
546+
547+
@MainActor
548+
func useFooInADefer() -> String {
549+
defer {
550+
statefulThingy = true
551+
}
552+
553+
return "hello"
554+
}

0 commit comments

Comments
 (0)