Skip to content

Commit 59f04f4

Browse files
authored
Merge pull request #2004 from DougGregor/member-macro-conformances
Add conformsTo argument to member macro expansion operation
2 parents 56b057b + 5ff03e2 commit 59f04f4

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift

Lines changed: 1 addition & 0 deletions
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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,54 @@ 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+
@available(*, deprecated, message: "Use expansion(of:providingMembersOf:conformingTo:in:")
2627
static func expansion(
2728
of node: AttributeSyntax,
2829
providingMembersOf declaration: some DeclGroupSyntax,
2930
in context: some MacroExpansionContext
3031
) throws -> [DeclSyntax]
32+
33+
/// Expand an attached declaration macro to produce a set of members.
34+
///
35+
/// - Parameters:
36+
/// - node: The custom attribute describing the attached macro.
37+
/// - declaration: The declaration the macro attribute is attached to.
38+
/// - conformingTo: The set of protocols that were declared
39+
/// in the set of conformances for the macro and to which the declaration
40+
/// does not explicitly conform. The member macro itself cannot declare
41+
/// conformances to these protocols (only an extension macro can do that),
42+
/// but can provide supporting declarations, such as a required
43+
/// initializer or stored property, that cannot be written in an
44+
/// extension.
45+
/// - context: The context in which to perform the macro expansion.
46+
///
47+
/// - Returns: the set of member declarations introduced by this macro, which
48+
/// are nested inside the `attachedTo` declaration.
49+
static func expansion(
50+
of node: AttributeSyntax,
51+
providingMembersOf declaration: some DeclGroupSyntax,
52+
conformingTo protocols: [TypeSyntax],
53+
in context: some MacroExpansionContext
54+
) throws -> [DeclSyntax]
55+
}
56+
57+
public extension MemberMacro {
58+
/// Default implementation supplies no conformances.
59+
static func expansion(
60+
of node: AttributeSyntax,
61+
providingMembersOf declaration: some DeclGroupSyntax,
62+
in context: some MacroExpansionContext
63+
) throws -> [DeclSyntax] {
64+
return try expansion(of: node, providingMembersOf: declaration, conformingTo: [], in: context)
65+
}
66+
67+
/// Default implementation that ignores the unhandled conformances.
68+
static func expansion(
69+
of node: AttributeSyntax,
70+
providingMembersOf declaration: some DeclGroupSyntax,
71+
conformingTo protocols: [TypeSyntax],
72+
in context: some MacroExpansionContext
73+
) throws -> [DeclSyntax] {
74+
return try expansion(of: node, providingMembersOf: declaration, in: context)
75+
}
3176
}

0 commit comments

Comments
 (0)