Skip to content

Commit ab06f36

Browse files
committed
[DistributedProtocolMacro] Properly handle same/inherit requirement constraints
1 parent 197466b commit ab06f36

4 files changed

+57
-49
lines changed

lib/Macros/Sources/SwiftMacros/DistributedProtocolMacro.swift

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import SwiftSyntaxBuilder
2121
public struct DistributedProtocolMacro: ExtensionMacro, PeerMacro {
2222
}
2323

24-
2524
// ===== -----------------------------------------------------------------------
2625
// MARK: Default Stub implementations Extension
2726

2827
extension DistributedProtocolMacro {
28+
2929
/// Introduce the `extension MyDistributedActor` which contains default
3030
/// implementations of the protocol's requirements.
3131
public static func expansion(
@@ -136,19 +136,29 @@ extension DistributedProtocolMacro {
136136

137137
let stubActorDecl: DeclSyntax =
138138
if (isGenericStub) {
139-
"""
140-
distributed actor $\(proto.name.trimmed)<ActorSystem>: \(proto.name.trimmed),
141-
Distributed._DistributedActorStub
142-
where ActorSystem: DistributedActorSystem<any \(raw: serializationRequirementType)>,
143-
ActorSystem.ActorID: \(raw: serializationRequirementType)
144-
{ }
145-
"""
139+
if let specificActorSystemRequirement {
140+
"""
141+
\(proto.modifiers) distributed actor $\(proto.name.trimmed)<ActorSystem>: \(proto.name.trimmed),
142+
Distributed._DistributedActorStub
143+
where ActorSystem: \(specificActorSystemRequirement)
144+
{ }
145+
"""
146+
} else {
147+
// FIXME: if just requirement was declared?
148+
"""
149+
\(proto.modifiers) distributed actor $\(proto.name.trimmed)<ActorSystem>: \(proto.name.trimmed),
150+
Distributed._DistributedActorStub
151+
where ActorSystem: DistributedActorSystem<any \(raw: serializationRequirementType)>,
152+
ActorSystem.ActorID: \(raw: serializationRequirementType)
153+
{ }
154+
"""
155+
}
146156
} else if let specificActorSystemRequirement {
147157
"""
148-
distributed actor $\\(proto.name.trimmed): \\(proto.name.trimmed),
158+
\(proto.modifiers) distributed actor $\(proto.name.trimmed): \(proto.name.trimmed),
149159
Distributed._DistributedActorStub
150160
{
151-
\(typealiasActorSystem(specificActorSystemRequirement))
161+
\(typealiasActorSystem(proto, specificActorSystemRequirement))
152162
}
153163
"""
154164
} else {
@@ -160,7 +170,7 @@ extension DistributedProtocolMacro {
160170
return [stubActorDecl]
161171
}
162172

163-
private static func typealiasActorSystem(_ type: TypeSyntax) -> DeclSyntax {
173+
private static func typealiasActorSystem(_ proto: ProtocolDeclSyntax, _ type: TypeSyntax) -> DeclSyntax {
164174
"typealias ActorSystem = \(type)"
165175
}
166176
}

test/Distributed/Macros/distributed_macro_expansion_DistributedProtocol_inheritance.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ protocol G3<ActorSystem>: DistributedActor, EmptyBase where ActorSystem: Distrib
3131
// @_DistributedProtocol ->
3232
// CHECK: distributed actor $G3<ActorSystem>: G3
3333
// CHECK: Distributed._DistributedActorStub
34-
// CHECK: where ActorSystem: DistributedActorSystem<any Codable>,
35-
// CHECK: ActorSystem.ActorID: Codable
34+
// CHECK: where ActorSystem: DistributedActorSystem<any Codable>
3635
// CHECK: {
3736
// CHECK: }
3837

test/Distributed/Macros/distributed_macro_expansion_DistributedProtocol_simple.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ protocol Greeter: DistributedActor where ActorSystem: DistributedActorSystem<any
2020

2121
// CHECK: distributed actor $Greeter<ActorSystem>: Greeter,
2222
// CHECK-NEXT: Distributed._DistributedActorStub
23-
// CHECK-NEXT: where ActorSystem: DistributedActorSystem<any Codable>,
24-
// CHECK-NEXT: ActorSystem.ActorID: Codable
23+
// CHECK-NEXT: where ActorSystem: DistributedActorSystem<any Codable>
2524
// CHECK-NEXT: {
2625
// CHECK-NEXT: }
2726

test/Distributed/Macros/distributed_macro_expansion_DistributedProtocol_various_requirements.swift

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@
1212

1313
import Distributed
1414

15-
//@_DistributedProtocol
16-
//protocol Greeter: DistributedActor where ActorSystem == FakeActorSystem {
17-
// distributed func greet(name: String) -> String
18-
//}
15+
@_DistributedProtocol
16+
protocol Greeter: DistributedActor where ActorSystem == FakeActorSystem {
17+
distributed func greet(name: String) -> String
18+
}
1919

2020
// @_DistributedProtocol ->
2121

22-
// CHECK: distributed actor $Greeter: Greeter,
23-
// CHECK-NEXT: Distributed._DistributedActorStub
24-
// CHECK-NEXT: {
25-
// CHECK-NEXT: typealias ActorSystem = FakeActorSystem
26-
// CHECK-NEXT: }
27-
28-
// CHECK: extension Greeter where Self: Distributed._DistributedActorStub {
29-
// CHECK-NEXT: distributed func greet(name: String) -> String {
30-
// CHECK-NEXT: if #available (SwiftStdlib 6.0, *) {
31-
// CHECK-NEXT: Distributed._distributedStubFatalError()
32-
// CHECK-NEXT: } else {
33-
// CHECK-NEXT: fatalError()
34-
// CHECK-NEXT: }
35-
// CHECK-NEXT: }
36-
// CHECK-NEXT: }
22+
// CHECK: distributed actor $Greeter: Greeter,
23+
// CHECK: Distributed._DistributedActorStub
24+
// CHECK: {
25+
// CHECK: typealias ActorSystem = FakeActorSystem
26+
// CHECK: }
27+
28+
// CHECK: extension Greeter where Self: Distributed._DistributedActorStub {
29+
// CHECK: distributed func greet(name: String) -> String {
30+
// CHECK: if #available (SwiftStdlib 6.0, *) {
31+
// CHECK: Distributed._distributedStubFatalError()
32+
// CHECK: } else {
33+
// CHECK: fatalError()
34+
// CHECK: }
35+
// CHECK: }
36+
// CHECK: }
3737

3838
@_DistributedProtocol
3939
protocol Greeter2: DistributedActor where ActorSystem: FakeRoundtripActorSystem {
@@ -42,18 +42,18 @@ protocol Greeter2: DistributedActor where ActorSystem: FakeRoundtripActorSystem
4242

4343
// @_DistributedProtocol ->
4444

45-
// CHECK: distributed actor $Greeter2: Greeter,
46-
//// CHECK-NEXT: Distributed._DistributedActorStub
47-
//// CHECK-NEXT: where ActorSystem: FakeActorSystem
48-
//// CHECK-NEXT: {
49-
//// CHECK-NEXT: }
50-
51-
// CHECK: extension Greeter2 where Self: Distributed._DistributedActorStub {
52-
// CHECK-NEXT: distributed func greet(name: String) -> String {
53-
// CHECK-NEXT: if #available (SwiftStdlib 6.0, *) {
54-
// CHECK-NEXT: Distributed._distributedStubFatalError()
55-
// CHECK-NEXT: } else {
56-
// CHECK-NEXT: fatalError()
57-
// CHECK-NEXT: }
58-
// CHECK-NEXT: }
59-
// CHECK-NEXT: }
45+
// CHECK: distributed actor $Greeter2<ActorSystem>: Greeter2,
46+
// CHECK: Distributed._DistributedActorStub
47+
// CHECK: where ActorSystem: FakeRoundtripActorSystem
48+
// CHECK: {
49+
// CHECK: }
50+
51+
// CHECK: extension Greeter2 where Self: Distributed._DistributedActorStub {
52+
// CHECK: distributed func greet(name: String) -> String {
53+
// CHECK: if #available (SwiftStdlib 6.0, *) {
54+
// CHECK: Distributed._distributedStubFatalError()
55+
// CHECK: } else {
56+
// CHECK: fatalError()
57+
// CHECK: }
58+
// CHECK: }
59+
// CHECK: }

0 commit comments

Comments
 (0)