File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -3126,6 +3126,25 @@ ActorIsolation ActorIsolationRequest::evaluate(
3126
3126
return inferred;
3127
3127
};
3128
3128
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
+
3129
3148
// If the declaration overrides another declaration, it must have the same
3130
3149
// actor isolation.
3131
3150
if (auto overriddenValue = value->getOverriddenDecl ()) {
Original file line number Diff line number Diff line change @@ -552,3 +552,17 @@ func acceptAsyncSendableClosureInheriting<T>(@_inheritActorContext _: @Sendable
552
552
await onlyOnMainActor ( ) // expected-warning{{no 'async' operations occur within 'await' expression}}
553
553
}
554
554
}
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
+ }
You can’t perform that action at this time.
0 commit comments