Skip to content

Commit 7eae931

Browse files
authored
Merge pull request #73047 from ktoso/pick-wip-distributed-macro-diagnostics-improvements
[6.0][Distributed][Macro] Handle more edge cases with distributed protocol macro
2 parents 9083c69 + 79cae1c commit 7eae931

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

lib/Macros/Sources/SwiftMacros/DistributedProtocolMacro.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ extension DistributedProtocolMacro {
4949

5050
let requirementStubs =
5151
proto.memberBlock.members // requirements
52+
.filter { member in
53+
switch member.decl.kind {
54+
case .functionDecl: return true
55+
case .variableDecl: return true
56+
default:
57+
return false
58+
}
59+
}
5260
.map { member in
5361
stubMethodDecl(access: accessModifiers, member.trimmed)
5462
}
@@ -66,12 +74,22 @@ extension DistributedProtocolMacro {
6674
static func stubMethodDecl(access: String, _ requirement: MemberBlockItemListSyntax.Element) -> String {
6775
// do we need to stub a computed variable?
6876
if let variable = requirement.decl.as(VariableDeclSyntax.self) {
69-
// TODO(distributed): improve stubbing computed properties of all kinds
77+
var accessorStubs: [String] = []
78+
79+
for binding in variable.bindings {
80+
if let accessorBlock = binding.accessorBlock {
81+
for accessor in accessorBlock.accessors.children(viewMode: .all) {
82+
let accessorStub = "\(accessor) { \(stubFunctionBody()) }"
83+
accessorStubs.append(accessorStub)
84+
}
85+
}
86+
}
87+
7088
let name = variable.bindings.first!.pattern.trimmed
7189
let typeAnnotation = variable.bindings.first?.typeAnnotation.map { "\($0.trimmed)" } ?? "Any"
7290
return """
7391
\(access)\(variable.modifiers)\(variable.bindingSpecifier) \(name) \(typeAnnotation) {
74-
\(stubFunctionBody())
92+
\(accessorStubs.joined(separator: "\n "))
7593
}
7694
"""
7795
}

test/Distributed/Macros/distributed_macro_expansion_DistributedProtocol_errors.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,12 @@ protocol Fail: DistributedActor {
3131
distributed func method() -> String
3232
}
3333

34+
@_DistributedProtocol // expected-note{{in expansion of macro '_DistributedProtocol' on protocol 'SomeRoot' here}}
35+
public protocol SomeRoot: DistributedActor, Sendable
36+
where ActorSystem: DistributedActorSystem<any Codable> {
37+
38+
// TODO(distributed): we could diagnose this better?
39+
associatedtype AssociatedSomething: Sendable // expected-note{{protocol requires nested type 'AssociatedSomething'; add nested type 'AssociatedSomething' for conformance}}
40+
static var staticValue: String { get }
41+
var value: String { get }
42+
}

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)