Skip to content

Revert "[flow-isolation] Allow for initialization of fields of a Global Actor isolated class in its nonisolated inits" #82311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7919,17 +7919,6 @@ ActorReferenceResult ActorReferenceResult::forReference(
(!actorInstance || actorInstance->isSelf())) {
auto type = fromDC->mapTypeIntoContext(decl->getInterfaceType());
if (!type->isSendableType()) {
// If we have an actor instance and our declIsolation is global actor
// isolated, but our context isolation is nonisolated... defer to flow
// isolation if this case passes the additional restrictions required by
// flow isolation (e.x.: the initializer's nominal type has to be
// isolated).
if (actorInstance &&
declIsolation.isGlobalActor() && contextIsolation.isNonisolated() &&
checkedByFlowIsolation(fromDC, *actorInstance, decl, declRefLoc, useKind)) {
return forSameConcurrencyDomain(declIsolation, options);
}

// Treat the decl isolation as 'preconcurrency' to downgrade violations
// to warnings, because violating Sendable here is accepted by the
// Swift 5.9 compiler.
Expand Down
74 changes: 3 additions & 71 deletions test/Concurrency/flow_isolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ struct Money {
}
}

actor OtherActorBackingActor { }

@globalActor
struct OtherActor {
static let shared = OtherActorBackingActor()
}

@available(SwiftStdlib 5.1, *)
func takeBob(_ b: Bob) {}

Expand Down Expand Up @@ -839,78 +832,17 @@ func testActorWithInitAccessorInit() {

@available(SwiftStdlib 5.1, *)
actor TestNonisolatedUnsafe {
private nonisolated(unsafe) var child: MyOtherActor!
private nonisolated(unsafe) var child: OtherActor!
init() {
child = MyOtherActor(parent: self)
child = OtherActor(parent: self)
}
}

@available(SwiftStdlib 5.1, *)
actor MyOtherActor {
actor OtherActor {
unowned nonisolated let parent: any Actor

init(parent: any Actor) {
self.parent = parent
}
}

func globalActorNonIsolatedInitializerTests() {
@MainActor
class C {
let ns: NonSendableType

nonisolated init() {
self.ns = NonSendableType()
}

nonisolated init(x: NonSendableType) {
self.ns = x
}

nonisolated func doSomething() {}

nonisolated init(x2 x: NonSendableType) {
self.ns = x
doSomething() // expected-note {{after calling instance method 'doSomething()', only nonisolated properties of 'self' can be accessed from this init}}
print(self.ns) // expected-warning {{cannot access property 'ns' here in nonisolated initializer}}
}
}

// Make sure this does not apply in cases where self is not actor isolated.
class D {
@MainActor let ns: NonSendableType // expected-note {{mutation of this property is only permitted within the actor}}

nonisolated init() {
self.ns = NonSendableType() // expected-warning {{main actor-isolated property 'ns' can not be mutated from a nonisolated context}}
}
}

actor A {
@MainActor let ns: NonSendableType

init() {
self.ns = NonSendableType()
}
}

@MainActor
class C2 {
@OtherActor let ns: NonSendableType

nonisolated init() {
self.ns = NonSendableType()
}

nonisolated init(_ x: NonSendableType) {
self.ns = x
}

nonisolated func doSomething() {}

nonisolated init(x2 x: NonSendableType) {
self.ns = x
doSomething() // expected-note {{after calling instance method 'doSomething()', only nonisolated properties of 'self' can be accessed from this init}}
print(self.ns) // expected-warning {{cannot access property 'ns' here in nonisolated initializer}}
}
}
}
2 changes: 2 additions & 0 deletions test/Concurrency/global_actor_inference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,13 @@ struct WrapperOnActor<Wrapped: Sendable> {
public struct WrapperOnMainActor<Wrapped> {
// Make sure inference of @MainActor on wrappedValue doesn't crash.

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

public var accessCount: Int

nonisolated public init(wrappedValue: Wrapped) {
// 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}}
self.wrappedValue = wrappedValue
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/Concurrency/global_actor_inference_swift6.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ struct WrapperOnActor<Wrapped: Sendable> {
public struct WrapperOnMainActor<Wrapped> {
// Make sure inference of @MainActor on wrappedValue doesn't crash.

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

public var accessCount: Int

nonisolated public init(wrappedValue: Wrapped) {
// expected-error@+1 {{main actor-isolated property 'wrappedValue' can not be mutated from a nonisolated context}}
self.wrappedValue = wrappedValue
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/Concurrency/nonisolated_rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ nonisolated struct NonisolatedStruct: GloballyIsolated {
}

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