Skip to content

[6.0][Distributed][Macro] Handle more edge cases with distributed protocol macro #73047

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
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
22 changes: 20 additions & 2 deletions lib/Macros/Sources/SwiftMacros/DistributedProtocolMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ extension DistributedProtocolMacro {

let requirementStubs =
proto.memberBlock.members // requirements
.filter { member in
switch member.decl.kind {
case .functionDecl: return true
case .variableDecl: return true
default:
return false
}
}
.map { member in
stubMethodDecl(access: accessModifiers, member.trimmed)
}
Expand All @@ -66,12 +74,22 @@ extension DistributedProtocolMacro {
static func stubMethodDecl(access: String, _ requirement: MemberBlockItemListSyntax.Element) -> String {
// do we need to stub a computed variable?
if let variable = requirement.decl.as(VariableDeclSyntax.self) {
// TODO(distributed): improve stubbing computed properties of all kinds
var accessorStubs: [String] = []

for binding in variable.bindings {
if let accessorBlock = binding.accessorBlock {
for accessor in accessorBlock.accessors.children(viewMode: .all) {
let accessorStub = "\(accessor) { \(stubFunctionBody()) }"
accessorStubs.append(accessorStub)
}
}
}

let name = variable.bindings.first!.pattern.trimmed
let typeAnnotation = variable.bindings.first?.typeAnnotation.map { "\($0.trimmed)" } ?? "Any"
return """
\(access)\(variable.modifiers)\(variable.bindingSpecifier) \(name) \(typeAnnotation) {
\(stubFunctionBody())
\(accessorStubs.joined(separator: "\n "))
}
"""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ protocol Fail: DistributedActor {
distributed func method() -> String
}

@_DistributedProtocol // expected-note{{in expansion of macro '_DistributedProtocol' on protocol 'SomeRoot' here}}
public protocol SomeRoot: DistributedActor, Sendable
where ActorSystem: DistributedActorSystem<any Codable> {

// TODO(distributed): we could diagnose this better?
associatedtype AssociatedSomething: Sendable // expected-note{{protocol requires nested type 'AssociatedSomething'; add nested type 'AssociatedSomething' for conformance}}
static var staticValue: String { get }
var value: String { get }
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,15 @@ protocol Greeter: DistributedActor where ActorSystem: DistributedActorSystem<any
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: }

// Macro should be able to handle complex properties
@_DistributedProtocol
public protocol GetSet: DistributedActor, Sendable
where ActorSystem: DistributedActorSystem<any Codable> {

distributed var dist: String { get }

var getSet: String { get set }

var asyncGetSet: String { get async throws }
}