Skip to content

Implement support for #isolation for distributed actors #71093

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 24, 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
8 changes: 8 additions & 0 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3460,6 +3460,13 @@ namespace {
actorExpr = new (ctx) DeclRefExpr(
const_cast<VarDecl *>(var), DeclNameLoc(loc),
/*Implicit=*/true);

// For a distributed actor, we need to retrieve the local
// actor.
if (isolation.isDistributedActor()) {
actorExpr = UnresolvedDotExpr::createImplicit(
ctx, actorExpr, ctx.getIdentifier("asLocalActor"));
}
break;
}
case ActorIsolation::GlobalActor: {
Expand All @@ -3480,6 +3487,7 @@ namespace {
break;
}


// Convert the actor argument to the appropriate type.
(void)TypeChecker::typeCheckExpression(
actorExpr, const_cast<DeclContext *>(getDeclContext()),
Expand Down
47 changes: 47 additions & 0 deletions test/Distributed/isolation_macro.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// RUN: %target-swift-frontend -typecheck %s -enable-experimental-feature OptionalIsolatedParameters -verify

// RUN: %target-swift-frontend -dump-ast %s -enable-experimental-feature OptionalIsolatedParameters | %FileCheck %s

// REQUIRES: concurrency
// REQUIRES: asserts
// REQUIRES: distributed

import Distributed

@available(SwiftStdlib 5.7, *)
extension DistributedActor {
// CHECK-LABEL: actorIsolationToSelf()
func actorIsolationToSelf() {
// CHECK: macro_expansion_expr
// CHECK: rewritten=current_context_isolation_expr
// CHECK-NEXT: inject_into_optional
// CHECK: member_ref_expr{{.*}}asLocalActor
// CHECK: declref_expr implicit type="Self"
_ = #isolation
}
}

// CHECK-LABEL: actorIsolationToParam(_:)
@available(SwiftStdlib 5.7, *)
func actorIsolationToParam(_ isolatedParam: isolated any DistributedActor) {
// CHECK: macro_expansion_expr
// CHECK: rewritten=current_context_isolation_expr
// CHECK: member_ref_expr{{.*}}asLocalActor
_ = #isolation
}

func acceptClosure(_ body: () -> Void) { }

// CHECK-LABEL: closureIsolatedToOuterParam(
@available(SwiftStdlib 5.7, *)
func closureIsolatedToOuterParam(_ isolatedParam: isolated any DistributedActor) {
// CHECK: closure_expr
// CHECK: macro_expansion_expr
// CHECK: rewritten=current_context_isolation_expr
// CHECK: inject_into_optional
// CHECK: member_ref_expr{{.*}}asLocalActor
acceptClosure {
_ = #isolation
print(isolatedParam)
}
}