File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff 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 ()) {
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments