Skip to content

Commit 43c802f

Browse files
authored
Merge pull request #2420 from ahoppen/ahoppen/509/conforming-to
[509] Add `conformingTo` parameter to `MemberMacro.expansion` function
2 parents 6079de8 + d0a2288 commit 43c802f

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift

+1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
236236
let members = try attachedMacro.expansion(
237237
of: attributeNode,
238238
providingMembersOf: declGroup,
239+
conformingTo: conformanceList?.map(\.type) ?? [],
239240
in: context
240241
)
241242

Sources/SwiftSyntaxMacros/MacroProtocols/MemberMacro.swift

+57
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,66 @@ public protocol MemberMacro: AttachedMacro {
2323
///
2424
/// - Returns: the set of member declarations introduced by this macro, which
2525
/// 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.
2629
static func expansion(
2730
of node: AttributeSyntax,
2831
providingMembersOf declaration: some DeclGroupSyntax,
2932
in context: some MacroExpansionContext
3033
) 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+
}
3188
}

0 commit comments

Comments
 (0)