Skip to content

Commit a11e264

Browse files
authored
Merge pull request #36972 from etcwilde/ewilde/no-global-actor-deinit
[Concurrency] Don't propagate global actor-ness to deinit
2 parents 444c35c + 284ac2a commit a11e264

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,18 +2255,18 @@ namespace {
22552255

22562256
LLVM_FALLTHROUGH;
22572257

2258-
case ActorIsolationRestriction::GlobalActor:
2259-
// If we are within an initializer and are referencing a stored
2260-
// property on "self", we are not crossing actors.
2261-
if (isa<ConstructorDecl>(getDeclContext()) &&
2262-
isa<VarDecl>(member) && cast<VarDecl>(member)->hasStorage() &&
2263-
getReferencedSelf(base))
2258+
case ActorIsolationRestriction::GlobalActor: {
2259+
const bool isInitDeInit = isa<ConstructorDecl>(getDeclContext()) ||
2260+
isa<DestructorDecl>(getDeclContext());
2261+
// If we are within an initializer or deinitilizer and are referencing a
2262+
// stored property on "self", we are not crossing actors.
2263+
if (isInitDeInit && isa<VarDecl>(member) &&
2264+
cast<VarDecl>(member)->hasStorage() && getReferencedSelf(base))
22642265
return false;
2265-
22662266
return checkGlobalActorReference(
22672267
memberRef, memberLoc, isolation.getGlobalActor(),
22682268
isolation.isCrossActor, context);
2269-
2269+
}
22702270
case ActorIsolationRestriction::Unsafe:
22712271
// This case is hit when passing actor state inout to functions in some
22722272
// cases. The error is emitted by diagnoseInOutArg.
@@ -2745,13 +2745,13 @@ static Optional<MemberIsolationPropagation> getMemberIsolationPropagation(
27452745
case DeclKind::OpaqueType:
27462746
case DeclKind::Param:
27472747
case DeclKind::Module:
2748+
case DeclKind::Destructor:
27482749
return None;
27492750

27502751
case DeclKind::PatternBinding:
27512752
case DeclKind::EnumCase:
27522753
case DeclKind::EnumElement:
27532754
case DeclKind::Constructor:
2754-
case DeclKind::Destructor:
27552755
return MemberIsolationPropagation::GlobalActor;
27562756

27572757
case DeclKind::Func:

test/Concurrency/actor_isolation.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ struct GenericGlobalActor<T> {
336336

337337
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
338338
@MainActor func beets() { onions() } // expected-error{{call to global actor 'SomeGlobalActor'-isolated global function 'onions()' in a synchronous main actor-isolated context}}
339+
// expected-note@-1{{calls to global function 'beets()' from outside of its actor context are implicitly asynchronous}}
339340

340341
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
341342
actor Crystal {
@@ -686,7 +687,7 @@ class SomeClassWithInits {
686687
var mutableState: Int = 17
687688
var otherMutableState: Int
688689

689-
static var shared = SomeClassWithInits() // expected-note{{static property declared here}}
690+
static var shared = SomeClassWithInits() // expected-note 2{{static property declared here}}
690691

691692
init() { // expected-note{{calls to initializer 'init()' from outside of its actor context are implicitly asynchronous}}
692693
self.mutableState = 42
@@ -696,7 +697,9 @@ class SomeClassWithInits {
696697
}
697698

698699
deinit {
699-
print(SomeClassWithInits.shared) // okay, we're actor-isolated
700+
print(mutableState) // Okay, we're actor-isolated
701+
print(SomeClassWithInits.shared) // expected-error{{static property 'shared' isolated to global actor 'MainActor' can not be referenced from this synchronous context}}
702+
beets() //expected-error{{call to main actor-isolated global function 'beets()' in a synchronous nonisolated context}}
700703
}
701704

702705
func isolated() { }

0 commit comments

Comments
 (0)