Skip to content

[Distributed] Allow @Resolvable to work with available on specific func #80296

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
Mar 26, 2025
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
36 changes: 26 additions & 10 deletions lib/Macros/Sources/SwiftMacros/DistributedResolvableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ extension DistributedResolvableMacro {

static func stubMethodDecl(access: DeclModifierListSyntax, _ requirement: MemberBlockItemListSyntax.Element) -> String {
// do we need to stub a computed variable?
if let variable = requirement.decl.as(VariableDeclSyntax.self) {
var accessorStubs: [String] = []
if var variable = requirement.decl.as(VariableDeclSyntax.self) {
variable.modifiers = variable.modifiers.filter { !$0.isAccessControl }
access.reversed().forEach { modifier in
variable.modifiers = variable.modifiers.prepending(modifier)
}

var accessorStubs: [String] = []
for binding in variable.bindings {
if let accessorBlock = binding.accessorBlock {
for accessor in accessorBlock.accessors.children(viewMode: .all) {
Expand All @@ -91,19 +95,31 @@ extension DistributedResolvableMacro {

let name = variable.bindings.first!.pattern.trimmed
let typeAnnotation = variable.bindings.first?.typeAnnotation.map { "\($0.trimmed)" } ?? "Any"

// computed property stub
return """
\(access)\(variable.modifiers)\(variable.bindingSpecifier) \(name) \(typeAnnotation) {
\(variable.attributes)
\(variable.modifiers)\(variable.bindingSpecifier) \(name) \(typeAnnotation) {
\(accessorStubs.joined(separator: "\n "))
}
"""
}
} else if var fun = requirement.decl.as(FunctionDeclSyntax.self) {
fun.modifiers = fun.modifiers.filter { !$0.isAccessControl }
access.reversed().forEach { modifier in
fun.modifiers = fun.modifiers.prepending(modifier)
}

// normal function stub
return """
\(access)\(requirement) {
\(stubFunctionBody())
}
"""
// normal function stub
return """
\(fun) {
\(stubFunctionBody())
}
"""
} else {
// some declaration type we could not handle, let's silently ignore; we should not really need to emit
// anything others than var and func here, and it's cleaner to just ignore rather than crash here.
return ""
}
}

static func stubFunctionBody() -> DeclSyntax {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// REQUIRES: swift_swift_parser, asserts
//
// UNSUPPORTED: back_deploy_concurrency
// REQUIRES: concurrency
// REQUIRES: distributed
//
// RUN: %empty-directory(%t)
// RUN: %empty-directory(%t-scratch)

// RUN: %target-swift-frontend -typecheck -target %target-swift-6.0-abi-triple -plugin-path %swift-plugin-dir -I %t -dump-macro-expansions %s -dump-macro-expansions 2>&1 | %FileCheck %s --color

import Distributed

@Resolvable
public protocol Greeter: DistributedActor where ActorSystem: DistributedActorSystem<any Codable> {
@available(iOS 6666, macOS 7777, tvOS 8888, visionOS 9999, *)
distributed func greet(name: String) -> String

@available(iOS 6666, macOS 7777, tvOS 8888, visionOS 9999, *)
distributed var name: String { get }
}

// @Resolvable ->

// CHECK: public distributed actor $Greeter<ActorSystem>: Greeter,
// CHECK: Distributed._DistributedActorStub
// CHECK: where ActorSystem: DistributedActorSystem<any Codable>
// CHECK: {
// CHECK: }

// CHECK: extension Greeter where Self: Distributed._DistributedActorStub {
// CHECK: @available(iOS 6666, macOS 7777, tvOS 8888, visionOS 9999, *)
// CHECK: public
// CHECK: distributed func greet(name: String) -> String {
// CHECK: if #available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) {
// CHECK: Distributed._distributedStubFatalError()
// CHECK: } else {
// CHECK: fatalError()
// CHECK: }
// CHECK: }

// CHECK: @available(iOS 6666, macOS 7777, tvOS 8888, visionOS 9999, *)
// CHECK: public
// CHECK: distributed var name : String {
// CHECK: get {
// CHECK: if #available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) {
// CHECK: Distributed._distributedStubFatalError()
// CHECK: } else {
// CHECK: fatalError()
// CHECK: }
// CHECK: }
// CHECK: }

// CHECK: }
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ protocol Greeter: DistributedActor where ActorSystem: DistributedActorSystem<any

// @Resolvable ->

// CHECK: distributed actor $Greeter<ActorSystem>: Greeter,
// CHECK-NEXT: Distributed._DistributedActorStub
// CHECK-NEXT: where ActorSystem: DistributedActorSystem<any Codable>
// CHECK: distributed actor $Greeter<ActorSystem>: Greeter,
// CHECK-NEXT: Distributed._DistributedActorStub
// CHECK-NEXT: where ActorSystem: DistributedActorSystem<any Codable>
// CHECK-NEXT: {
// CHECK: }
// CHECK: }

// CHECK: extension Greeter where Self: Distributed._DistributedActorStub {
// CHECK: extension Greeter where Self: Distributed._DistributedActorStub {
// CHECK-NEXT: distributed func greet(name: String) -> String {
// CHECK-NEXT: if #available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) {
// CHECK-NEXT: Distributed._distributedStubFatalError()
Expand Down