Skip to content

Commit 857ed26

Browse files
authored
Merge pull request #82311 from gottesmm/pr-29a7ec5f7fd1b97205b69527c465cac0e3b4128a
Revert "[flow-isolation] Allow for initialization of fields of a Global Actor isolated class in its nonisolated inits"
2 parents 14c3d8d + bf78c62 commit 857ed26

File tree

5 files changed

+9
-82
lines changed

5 files changed

+9
-82
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7960,17 +7960,6 @@ ActorReferenceResult ActorReferenceResult::forReference(
79607960
(!actorInstance || actorInstance->isSelf())) {
79617961
auto type = fromDC->mapTypeIntoContext(decl->getInterfaceType());
79627962
if (!type->isSendableType()) {
7963-
// If we have an actor instance and our declIsolation is global actor
7964-
// isolated, but our context isolation is nonisolated... defer to flow
7965-
// isolation if this case passes the additional restrictions required by
7966-
// flow isolation (e.x.: the initializer's nominal type has to be
7967-
// isolated).
7968-
if (actorInstance &&
7969-
declIsolation.isGlobalActor() && contextIsolation.isNonisolated() &&
7970-
checkedByFlowIsolation(fromDC, *actorInstance, decl, declRefLoc, useKind)) {
7971-
return forSameConcurrencyDomain(declIsolation, options);
7972-
}
7973-
79747963
// Treat the decl isolation as 'preconcurrency' to downgrade violations
79757964
// to warnings, because violating Sendable here is accepted by the
79767965
// Swift 5.9 compiler.

test/Concurrency/flow_isolation.swift

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ struct Money {
3232
}
3333
}
3434

35-
actor OtherActorBackingActor { }
36-
37-
@globalActor
38-
struct OtherActor {
39-
static let shared = OtherActorBackingActor()
40-
}
41-
4235
@available(SwiftStdlib 5.1, *)
4336
func takeBob(_ b: Bob) {}
4437

@@ -839,78 +832,17 @@ func testActorWithInitAccessorInit() {
839832

840833
@available(SwiftStdlib 5.1, *)
841834
actor TestNonisolatedUnsafe {
842-
private nonisolated(unsafe) var child: MyOtherActor!
835+
private nonisolated(unsafe) var child: OtherActor!
843836
init() {
844-
child = MyOtherActor(parent: self)
837+
child = OtherActor(parent: self)
845838
}
846839
}
847840

848841
@available(SwiftStdlib 5.1, *)
849-
actor MyOtherActor {
842+
actor OtherActor {
850843
unowned nonisolated let parent: any Actor
851844

852845
init(parent: any Actor) {
853846
self.parent = parent
854847
}
855848
}
856-
857-
func globalActorNonIsolatedInitializerTests() {
858-
@MainActor
859-
class C {
860-
let ns: NonSendableType
861-
862-
nonisolated init() {
863-
self.ns = NonSendableType()
864-
}
865-
866-
nonisolated init(x: NonSendableType) {
867-
self.ns = x
868-
}
869-
870-
nonisolated func doSomething() {}
871-
872-
nonisolated init(x2 x: NonSendableType) {
873-
self.ns = x
874-
doSomething() // expected-note {{after calling instance method 'doSomething()', only nonisolated properties of 'self' can be accessed from this init}}
875-
print(self.ns) // expected-warning {{cannot access property 'ns' here in nonisolated initializer}}
876-
}
877-
}
878-
879-
// Make sure this does not apply in cases where self is not actor isolated.
880-
class D {
881-
@MainActor let ns: NonSendableType // expected-note {{mutation of this property is only permitted within the actor}}
882-
883-
nonisolated init() {
884-
self.ns = NonSendableType() // expected-warning {{main actor-isolated property 'ns' can not be mutated from a nonisolated context}}
885-
}
886-
}
887-
888-
actor A {
889-
@MainActor let ns: NonSendableType
890-
891-
init() {
892-
self.ns = NonSendableType()
893-
}
894-
}
895-
896-
@MainActor
897-
class C2 {
898-
@OtherActor let ns: NonSendableType
899-
900-
nonisolated init() {
901-
self.ns = NonSendableType()
902-
}
903-
904-
nonisolated init(_ x: NonSendableType) {
905-
self.ns = x
906-
}
907-
908-
nonisolated func doSomething() {}
909-
910-
nonisolated init(x2 x: NonSendableType) {
911-
self.ns = x
912-
doSomething() // expected-note {{after calling instance method 'doSomething()', only nonisolated properties of 'self' can be accessed from this init}}
913-
print(self.ns) // expected-warning {{cannot access property 'ns' here in nonisolated initializer}}
914-
}
915-
}
916-
}

test/Concurrency/global_actor_inference.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,13 @@ struct WrapperOnActor<Wrapped: Sendable> {
336336
public struct WrapperOnMainActor<Wrapped> {
337337
// Make sure inference of @MainActor on wrappedValue doesn't crash.
338338

339+
// expected-note@+1 {{mutation of this property is only permitted within the actor}}
339340
public var wrappedValue: Wrapped
340341

341342
public var accessCount: Int
342343

343344
nonisolated public init(wrappedValue: Wrapped) {
345+
// expected-warning@+1 {{main actor-isolated property 'wrappedValue' can not be mutated from a nonisolated context; this is an error in the Swift 6 language mode}}
344346
self.wrappedValue = wrappedValue
345347
}
346348
}

test/Concurrency/global_actor_inference_swift6.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ struct WrapperOnActor<Wrapped: Sendable> {
6767
public struct WrapperOnMainActor<Wrapped> {
6868
// Make sure inference of @MainActor on wrappedValue doesn't crash.
6969

70+
// expected-note@+1 {{mutation of this property is only permitted within the actor}}
7071
public var wrappedValue: Wrapped // expected-note {{property declared here}}
7172

7273
public var accessCount: Int
7374

7475
nonisolated public init(wrappedValue: Wrapped) {
76+
// expected-error@+1 {{main actor-isolated property 'wrappedValue' can not be mutated from a nonisolated context}}
7577
self.wrappedValue = wrappedValue
7678
}
7779
}

test/Concurrency/nonisolated_rules.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ nonisolated struct StructRemovesGlobalActor: GloballyIsolated {
7272
}
7373

7474
struct Nested: GloballyIsolated {
75+
// expected-note@+1 {{mutation of this property is only permitted within the actor}}
7576
var z: NonSendable
7677
nonisolated init(z: NonSendable) {
78+
// expected-error@+1 {{main actor-isolated property 'z' can not be mutated from a nonisolated context}}
7779
self.z = z
7880
}
7981
}

0 commit comments

Comments
 (0)