Skip to content

[6.0][region-isolation] When determining isolation from a class_method, check for global actor isolation first. #75045

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
Jul 8, 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
6 changes: 6 additions & 0 deletions include/swift/AST/ActorIsolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ class ActorIsolation {
return parameterIndex;
}

/// Returns true if this actor-instance isolation appllies to the self
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: appllies -> applies

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix that on main. This is for a cherry-pick to 6.0.

/// parameter of a method.
bool isActorInstanceForSelfParameter() const {
return getActorInstanceParameter() == 0;
}

bool isSILParsed() const { return silParsed; }

bool isActorIsolated() const {
Expand Down
59 changes: 37 additions & 22 deletions lib/SILOptimizer/Utils/SILIsolationInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,40 +603,55 @@ SILIsolationInfo SILIsolationInfo::get(SILInstruction *inst) {
bool isNonIsolatedUnsafe = exprAnalysis.hasNonisolatedUnsafe();
{
auto isolation = swift::getActorIsolation(dre->getDecl());
if (isolation.isActorIsolated() &&
(isolation.getKind() != ActorIsolation::ActorInstance ||
isolation.getActorInstanceParameter() == 0)) {
if (cmi->getOperand()->getType().isAnyActor()) {

if (isolation.isActorIsolated()) {
// Check if we have a global actor and handle it appropriately.
if (isolation.getKind() == ActorIsolation::GlobalActor) {
bool localNonIsolatedUnsafe =
isNonIsolatedUnsafe | isolation.isNonisolatedUnsafe();
return SILIsolationInfo::getGlobalActorIsolated(
cmi, isolation.getGlobalActor())
.withUnsafeNonIsolated(localNonIsolatedUnsafe);
}

// In this case, we have an actor instance that is self.
if (isolation.getKind() != ActorIsolation::ActorInstance &&
isolation.isActorInstanceForSelfParameter()) {
bool localNonIsolatedUnsafe =
isNonIsolatedUnsafe | isolation.isNonisolatedUnsafe();
return SILIsolationInfo::getActorInstanceIsolated(
cmi, cmi->getOperand(),
cmi->getOperand()
->getType()
.getNominalOrBoundGenericNominal());
cmi, cmi->getOperand(),
cmi->getOperand()
->getType()
.getNominalOrBoundGenericNominal())
.withUnsafeNonIsolated(localNonIsolatedUnsafe);
}
return SILIsolationInfo::getGlobalActorIsolated(
cmi, isolation.getGlobalActor());
}

isNonIsolatedUnsafe |= isolation.isNonisolatedUnsafe();
}

if (auto type = dre->getType()->getNominalOrBoundGenericNominal()) {
if (auto isolation = swift::getActorIsolation(type)) {
if (isolation.isActorIsolated() &&
(isolation.getKind() != ActorIsolation::ActorInstance ||
isolation.getActorInstanceParameter() == 0)) {
if (cmi->getOperand()->getType().isAnyActor()) {
if (isolation.isActorIsolated()) {
// Check if we have a global actor and handle it appropriately.
if (isolation.getKind() == ActorIsolation::GlobalActor) {
bool localNonIsolatedUnsafe =
isNonIsolatedUnsafe | isolation.isNonisolatedUnsafe();
return SILIsolationInfo::getGlobalActorIsolated(
cmi, isolation.getGlobalActor())
.withUnsafeNonIsolated(localNonIsolatedUnsafe);
}

// In this case, we have an actor instance that is self.
if (isolation.getKind() != ActorIsolation::ActorInstance &&
isolation.isActorInstanceForSelfParameter()) {
bool localNonIsolatedUnsafe =
isNonIsolatedUnsafe | isolation.isNonisolatedUnsafe();
return SILIsolationInfo::getActorInstanceIsolated(
cmi, cmi->getOperand(),
cmi->getOperand()
->getType()
.getNominalOrBoundGenericNominal())
.withUnsafeNonIsolated(isNonIsolatedUnsafe);
}

if (auto globalIso = SILIsolationInfo::getGlobalActorIsolated(
cmi, isolation.getGlobalActor())) {
return globalIso.withUnsafeNonIsolated(isNonIsolatedUnsafe);
.withUnsafeNonIsolated(localNonIsolatedUnsafe);
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/Concurrency/transfernonsendable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ actor MyActor {
func useSendableFunction(_: @Sendable () -> Void) {}
func useNonSendableFunction(_: () -> Void) {}
func doSomething() {}
@MainActor func useKlassMainActor(_ x: NonSendableKlass) {}
}

final actor FinalActor {
Expand Down Expand Up @@ -1799,3 +1800,16 @@ actor FunctionWithSendableResultAndIsolationActor {
return ""
}
}

@MainActor
func testThatGlobalActorTakesPrecedenceOverActorIsolationOnMethods() async {
let a = MyActor()
let ns = NonSendableKlass()

// 'ns' should be main actor isolated since useKlassMainActor is @MainActor
// isolated. Previously we would let MyActor take precedence here...
a.useKlassMainActor(ns)

// Meaning we would get an error here.
Task { @MainActor in print(ns) }
}