Skip to content

Commit 7e2469f

Browse files
committed
Remove unnecessary 'distributed' checks in actor isolation code.
Remove a few places where we are introducing extra distributed-related actor isolation checking that either isn't necesssary or is incorrect. Specifically: * Hopping to a global actor *from* a distributed-actor isolated context does not go through a distributed thunk (global actors can't have one). * "Unrestricted" declarations are always unrestricted, so we don't need an extra distributed check here (it won't ever occur). * Actor isolation computation doesn't need a special case for distributed; it marks too much, overriding (e.g.) global actor isolation. The primary semantic change visible here is that distributed actors can now have truly 'nonisolated' members. They aren't required to be 'distributed' because they can't touch state anyway.
1 parent ab96978 commit 7e2469f

File tree

4 files changed

+10
-40
lines changed

4 files changed

+10
-40
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,9 +1871,6 @@ namespace {
18711871

18721872
switch (contextIsolation) {
18731873
case ActorIsolation::DistributedActorInstance:
1874-
markNearestCallAsImplicitly(/*setAsync*/None, /*setThrows*/false,
1875-
/*setDistributedThunk*/true);
1876-
LLVM_FALLTHROUGH;
18771874
case ActorIsolation::ActorInstance: {
18781875
auto result = tryMarkImplicitlyAsync(
18791876
loc, valueRef, context,
@@ -2234,28 +2231,8 @@ namespace {
22342231
switch (auto isolation =
22352232
ActorIsolationRestriction::forDeclaration(
22362233
memberRef, getDeclContext())) {
2237-
case ActorIsolationRestriction::Unrestricted: {
2238-
// If a cross-actor reference is to an isolated actor, it's not
2239-
// crossing actors.
2240-
if (getIsolatedActor(base))
2241-
return false;
2242-
2243-
// Always fine to invoke constructors from outside of actors.
2244-
if (dyn_cast<ConstructorDecl>(member))
2245-
return false;
2246-
2247-
// While the member may be unrestricted, perhaps it is in a
2248-
// distributed actor, in which case we need to diagnose it.
2249-
if (auto classDecl = dyn_cast<ClassDecl>(member->getDeclContext())) {
2250-
if (classDecl->isDistributedActor()) {
2251-
ctx.Diags.diagnose(memberLoc, diag::distributed_actor_isolated_method);
2252-
noteIsolatedActorMember(member, context);
2253-
return true;
2254-
}
2255-
}
2256-
2234+
case ActorIsolationRestriction::Unrestricted:
22572235
return false;
2258-
}
22592236

22602237
case ActorIsolationRestriction::CrossDistributedActorSelf:
22612238
case ActorIsolationRestriction::CrossActorSelf: {
@@ -3078,13 +3055,6 @@ ActorIsolation ActorIsolationRequest::evaluate(
30783055
if (func->isSendable()) {
30793056
defaultIsolation = ActorIsolation::forIndependent();
30803057
}
3081-
3082-
if (auto nominal = value->getDeclContext()->getSelfNominalTypeDecl()) {
3083-
/// Unless the function is static, it is isolated to the dist actor
3084-
if (nominal->isDistributedActor() && !func->isStatic()) {
3085-
defaultIsolation = ActorIsolation::forDistributedActorInstance(nominal);
3086-
}
3087-
}
30883058
}
30893059

30903060
// An actor's convenience init is assumed to be actor-independent.

test/Distributed/distributed_actor_isolation.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ distributed actor DistributedActor_1 {
9191

9292
static func staticFunc() -> String { "" } // ok
9393

94-
// TODO: should be able to handle a static, global actor isolated function as well
95-
// @MainActor
96-
// static func staticMainActorFunc() -> String { "" } // ok
94+
@MainActor
95+
static func staticMainActorFunc() -> String { "" } // ok
9796

9897
static distributed func staticDistributedFunc() -> String {
9998
// expected-error@-1{{'distributed' functions cannot be 'static'}}{10-21=}
@@ -115,6 +114,9 @@ distributed actor DistributedActor_1 {
115114
await self.distHelloAsync()
116115
try self.distHelloThrows()
117116
try await self.distHelloAsyncThrows()
117+
118+
// Hops over to the global actor.
119+
_ = await DistributedActor_1.staticMainActorFunc()
118120
}
119121
}
120122

test/Distributed/distributed_actor_nonisolated.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ distributed actor DA {
1515
// expected-error@-1{{'nonisolated' can not be applied to distributed actor stored properties}}
1616

1717
nonisolated var computedNonisolated: Int {
18-
// expected-note@-1{{distributed actor state is only available within the actor instance}}
19-
2018
// nonisolated computed properties are outside of the actor and as such cannot access local
2119
_ = self.local // expected-error{{distributed actor-isolated property 'local' can only be referenced inside the distributed actor}}
2220

@@ -33,7 +31,7 @@ distributed actor DA {
3331
// self is a distributed actor self is NOT isolated
3432
_ = self.local // expected-error{{distributed actor-isolated property 'local' can only be referenced inside the distributed actor}}
3533
_ = try await self.dist() // ok, was made implicitly throwing and async
36-
_ = self.computedNonisolated // expected-error{{only 'distributed' functions can be called from outside the distributed actor}}
34+
_ = self.computedNonisolated // it's okay, only the body of computedNonisolated is wrong
3735
}
3836

3937
nonisolated distributed func nonisolatedDistributed() async {
@@ -46,4 +44,4 @@ distributed actor DA {
4644
fatalError()
4745
}
4846

49-
}
47+
}

test/Distributed/distributed_protocol_isolation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protocol DistProtocol: DistributedActor {
2727
@available(SwiftStdlib 5.5, *)
2828
distributed actor SpecificDist: DistProtocol {
2929

30-
nonisolated func local() -> String { "hi" } // expected-note{{only 'distributed' functions can be called from outside the distributed actor}}
30+
nonisolated func local() -> String { "hi" }
3131

3232
distributed func dist() -> String { "dist!" }
3333
distributed func dist(string: String) -> String { string }
@@ -49,7 +49,7 @@ distributed actor SpecificDist: DistProtocol {
4949

5050
@available(SwiftStdlib 5.5, *)
5151
func outside_good(dp: SpecificDist) async throws {
52-
_ = dp.local() // expected-error{{only 'distributed' functions can be called from outside the distributed actor}}
52+
_ = dp.local()
5353

5454
_ = try await dp.dist() // implicit async throws
5555
_ = try await dp.dist(string: "") // implicit async throws

0 commit comments

Comments
 (0)