Skip to content

[Concurrency] Check varIsSafeAcrossActors in getActorIsolationForReference. #73279

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

Merged
merged 1 commit into from
Apr 26, 2024
Merged
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: 9 additions & 2 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6465,12 +6465,19 @@ static ActorIsolation getActorIsolationForReference(ValueDecl *decl,
// Fall through to treat initializers like any other declaration.
}

// A 'nonisolated let' within an actor is treated as isolated from the
// perspective of the referencer.
// A 'nonisolated let' within an actor is treated as isolated if
// the access is outside the module or if the property type is not
// 'Sendable'.
//
// FIXME: getActorIsolation(decl) should treat these as isolated.
// FIXME: Expand this out to local variables?
if (auto var = dyn_cast<VarDecl>(decl)) {
auto *fromModule = fromDC->getParentModule();
ActorReferenceResult::Options options = std::nullopt;
if (varIsSafeAcrossActors(fromModule, var, declIsolation, options) &&
var->getTypeInContext()->isSendableType())
return ActorIsolation::forNonisolated(/*unsafe*/false);

if (var->isLet() && isStoredProperty(var) &&
declIsolation.isNonisolated()) {
if (auto nominal = var->getDeclContext()->getSelfNominalTypeDecl()) {
Expand Down
23 changes: 20 additions & 3 deletions test/Concurrency/actor_isolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ func checkIsolationValueType(_ formance: InferredFromConformance,
_ anno: NoGlobalActorValueType) async {
// these still do need an await in Swift 5
_ = await ext.point // expected-warning {{non-sendable type 'Point' in implicitly asynchronous access to main actor-isolated property 'point' cannot cross actor boundary}}
_ = formance.counter
_ = await anno.point // expected-warning {{non-sendable type 'Point' in implicitly asynchronous access to global actor 'SomeGlobalActor'-isolated property 'point' cannot cross actor boundary}}
// expected-warning@-1 {{non-sendable type 'NoGlobalActorValueType' passed in implicitly asynchronous call to global actor 'SomeGlobalActor'-isolated property 'point' cannot cross actor boundary}}
_ = anno.counter // expected-warning {{non-sendable type 'NoGlobalActorValueType' passed in call to main actor-isolated property 'counter' cannot cross actor boundary}}

_ = formance.counter
_ = anno.counter

// these will always need an await
_ = await (formance as MainCounter).counter // expected-warning {{non-sendable type 'any MainCounter' passed in implicitly asynchronous call to main actor-isolated property 'counter' cannot cross actor boundary}}
Expand All @@ -171,7 +172,7 @@ func checkIsolationValueType(_ formance: InferredFromConformance,
}

// expected-warning@+2 {{memberwise initializer for 'NoGlobalActorValueType' cannot be both nonisolated and global actor 'SomeGlobalActor'-isolated; this is an error in the Swift 6 language mode}}
// expected-note@+1 2 {{consider making struct 'NoGlobalActorValueType' conform to the 'Sendable' protocol}}
// expected-note@+1 {{consider making struct 'NoGlobalActorValueType' conform to the 'Sendable' protocol}}
struct NoGlobalActorValueType {
@SomeGlobalActor var point: Point
// expected-note@-1 {{initializer for property 'point' is global actor 'SomeGlobalActor'-isolated}}
Expand Down Expand Up @@ -1636,3 +1637,19 @@ nonisolated func accessAcrossActors() {
// expected-warning@+1 {{main actor-isolated static property 'shared' can not be referenced from a non-isolated context; this is an error in the Swift 6 language mode}}
let _ = MainActorIsolated.shared
}

@available(SwiftStdlib 5.1, *)
actor Iterator: AsyncIteratorProtocol {
init() {}
func next() throws -> Int? { nil }
}

@available(SwiftStdlib 5.1, *)
actor SafeMutatingCall {
private let iterator: Iterator

public init() async throws {
self.iterator = Iterator()
_ = try await self.iterator.next()
}
}