Skip to content

Commit da9d8b2

Browse files
committed
[Distributed][Macro] handle effectful properties in protocol macro
1 parent 4866150 commit da9d8b2

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

lib/Macros/Sources/SwiftMacros/DistributedProtocolMacro.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,24 @@ extension DistributedProtocolMacro {
7474
static func stubMethodDecl(access: String, _ requirement: MemberBlockItemListSyntax.Element) -> String {
7575
// do we need to stub a computed variable?
7676
if let variable = requirement.decl.as(VariableDeclSyntax.self) {
77-
// TODO(distributed): improve stubbing computed properties of all kinds
77+
print("VAR = \(variable.debugDescription)")
78+
79+
var accessorStubs: [String] = []
80+
81+
for binding in variable.bindings {
82+
if let accessorBlock = binding.accessorBlock {
83+
for accessor in accessorBlock.accessors.children(viewMode: .all) {
84+
let accessorStub = "\(accessor) { \(stubFunctionBody()) }"
85+
accessorStubs.append(accessorStub)
86+
}
87+
}
88+
}
89+
7890
let name = variable.bindings.first!.pattern.trimmed
7991
let typeAnnotation = variable.bindings.first?.typeAnnotation.map { "\($0.trimmed)" } ?? "Any"
8092
return """
8193
\(access)\(variable.modifiers)\(variable.bindingSpecifier) \(name) \(typeAnnotation) {
82-
\(stubFunctionBody())
94+
\(accessorStubs.joined(separator: "\n "))
8395
}
8496
"""
8597
}

test/Distributed/Macros/distributed_macro_expansion_DistributedProtocol_errors.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,4 @@ public protocol SomeRoot: DistributedActor, Sendable
3939
associatedtype AssociatedSomething: Sendable // expected-note{{protocol requires nested type 'AssociatedSomething'; add nested type 'AssociatedSomething' for conformance}}
4040
static var staticValue: String { get }
4141
var value: String { get }
42-
var getSet: String { get set }
4342
}

test/Distributed/Macros/distributed_macro_expansion_DistributedProtocol_simple.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,15 @@ protocol Greeter: DistributedActor where ActorSystem: DistributedActorSystem<any
3333
// CHECK-NEXT: }
3434
// CHECK-NEXT: }
3535
// CHECK-NEXT: }
36+
37+
// Macro should be able to handle complex properties
38+
@_DistributedProtocol
39+
public protocol GetSet: DistributedActor, Sendable
40+
where ActorSystem: DistributedActorSystem<any Codable> {
41+
42+
distributed var dist: String { get }
43+
44+
var getSet: String { get set }
45+
46+
var asyncGetSet: String { get async throws }
47+
}

0 commit comments

Comments
 (0)