Skip to content

[Distributed] Allow isolated any DistributedActor #70906

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
Jan 15, 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
3 changes: 3 additions & 0 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,9 @@ class alignas(1 << TypeAlignInBits) TypeBase
/// Determines whether this type is an actor type.
bool isActorType();

/// Determines whether this type is an any actor type.
bool isAnyActorType();

/// Returns true if this type is a Sendable type.
bool isSendableType(DeclContext *declContext);

Expand Down
6 changes: 6 additions & 0 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,12 @@ bool TypeBase::isActorType() {
return false;
}

bool TypeBase::isAnyActorType() {
if (auto actor = getAnyActor())
return actor->isAnyActor();
return false;
}

bool TypeBase::isSendableType(DeclContext *ctx) {
return isSendableType(ctx->getParentModule());
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4484,12 +4484,12 @@ TypeResolver::resolveIsolatedTypeRepr(IsolatedTypeRepr *repr,
Type type = resolveType(repr->getBase(), options);

// isolated parameters must be of actor type
if (!type->hasTypeParameter() && !type->isActorType() && !type->hasError()) {
if (!type->hasTypeParameter() && !type->isAnyActorType() && !type->hasError()) {
// Optional actor types are fine - `nil` represents `nonisolated`.
auto wrapped = type->getOptionalObjectType();
auto allowOptional = getASTContext().LangOpts
.hasFeature(Feature::OptionalIsolatedParameters);
if (allowOptional && wrapped && wrapped->isActorType()) {
if (allowOptional && wrapped && wrapped->isAnyActorType()) {
return type;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out

// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: distributed

// rdar://76038845
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime
// UNSUPPORTED: freestanding

// FIXME(distributed): Distributed actors currently have some issues on windows rdar://82593574
// UNSUPPORTED: OS=windows-msvc

import StdlibUnittest
import Distributed
import FakeDistributedActorSystems

@available(SwiftStdlib 5.9, *)
typealias DefaultDistributedActorSystem = FakeRoundtripActorSystem

@available(SwiftStdlib 5.9, *)
func globalFunc(isolatedTo actor: isolated any DistributedActor) async {
MainActor.preconditionIsolated() // we forced the `actor` onto the main actor's executor
}

@available(SwiftStdlib 5.9, *) // because conforming to the protocol requirement introduced in 5.9
distributed actor NormalWorker {

nonisolated var unownedExecutor: UnownedSerialExecutor {
return MainActor.sharedUnownedExecutor
}

distributed func offerSelf() async {
print("executed: \(#function)")
MainActor.preconditionIsolated() // we forced the `actor` onto the main actor's executor

await globalFunc(isolatedTo: self)
MainActor.preconditionIsolated() // we forced the `actor` onto the main actor's executor
}
}

@available(SwiftStdlib 5.1, *)
@main struct Main {
@available(SwiftStdlib 5.1, *)
static func main() async {
Copy link
Member

Choose a reason for hiding this comment

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

Needs @available(SwiftStdlib 5.1, *) here, I think

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems so, fixing

if #available(SwiftStdlib 5.9, *) {
let tests = TestSuite("DistributedIsolatedTests")
let system = DefaultDistributedActorSystem()
let normalLocalWorker = NormalWorker(actorSystem: system)
precondition(__isLocalActor(normalLocalWorker), "must be local")

tests.test("remote actor reference should have crash-on-enqueue executor") {
try! await normalLocalWorker.offerSelf()
}

await runAllTestsAsync()
}
}
}
2 changes: 2 additions & 0 deletions test/Distributed/distributed_actor_isolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,5 @@ extension Greeting where SerializationRequirement == Codable {
}

func isolated_generic_ok<T: DistributedActor>(_ t: isolated T) {}

func isolated_existential_ok(_ t: isolated any DistributedActor) {}