@@ -23,9 +23,66 @@ public protocol MemberMacro: AttachedMacro {
23
23
///
24
24
/// - Returns: the set of member declarations introduced by this macro, which
25
25
/// are nested inside the `attachedTo` declaration.
26
+ ///
27
+ /// - Warning: This is the legacy `expansion` function of `MemberMacro` that is provided for backwards-compatiblity.
28
+ /// Use ``expansion(of:providingMembersOf:conformingTo:in:)-1sxoe`` instead.
26
29
static func expansion(
27
30
of node: AttributeSyntax ,
28
31
providingMembersOf declaration: some DeclGroupSyntax ,
29
32
in context: some MacroExpansionContext
30
33
) throws -> [ DeclSyntax ]
34
+
35
+ /// Expand an attached declaration macro to produce a set of members.
36
+ ///
37
+ /// - Parameters:
38
+ /// - node: The custom attribute describing the attached macro.
39
+ /// - declaration: The declaration the macro attribute is attached to.
40
+ /// - conformingTo: The set of protocols that were declared
41
+ /// in the set of conformances for the macro and to which the declaration
42
+ /// does not explicitly conform. The member macro itself cannot declare
43
+ /// conformances to these protocols (only an extension macro can do that),
44
+ /// but can provide supporting declarations, such as a required
45
+ /// initializer or stored property, that cannot be written in an
46
+ /// extension.
47
+ /// - context: The context in which to perform the macro expansion.
48
+ ///
49
+ /// - Returns: the set of member declarations introduced by this macro, which
50
+ /// are nested inside the `attachedTo` declaration.
51
+ static func expansion(
52
+ of node: AttributeSyntax ,
53
+ providingMembersOf declaration: some DeclGroupSyntax ,
54
+ conformingTo protocols: [ TypeSyntax ] ,
55
+ in context: some MacroExpansionContext
56
+ ) throws -> [ DeclSyntax ]
57
+ }
58
+
59
+ private struct UnimplementedExpansionMethodError : Error , CustomStringConvertible {
60
+ var description : String {
61
+ """
62
+ Types conforming to `MemberMacro` must implement either \
63
+ expansion(of:providingMembersOf:in:) or \
64
+ expansion(of:providingMembersOf:conformingTo:in:)
65
+ """
66
+ }
67
+ }
68
+
69
+ public extension MemberMacro {
70
+ /// Default implementation supplies no conformances.
71
+ static func expansion(
72
+ of node: AttributeSyntax ,
73
+ providingMembersOf declaration: some DeclGroupSyntax ,
74
+ in context: some MacroExpansionContext
75
+ ) throws -> [ DeclSyntax ] {
76
+ throw UnimplementedExpansionMethodError ( )
77
+ }
78
+
79
+ /// Default implementation that ignores the unhandled conformances.
80
+ static func expansion(
81
+ of node: AttributeSyntax ,
82
+ providingMembersOf declaration: some DeclGroupSyntax ,
83
+ conformingTo protocols: [ TypeSyntax ] ,
84
+ in context: some MacroExpansionContext
85
+ ) throws -> [ DeclSyntax ] {
86
+ return try expansion ( of: node, providingMembersOf: declaration, in: context)
87
+ }
31
88
}
0 commit comments