Skip to content

Commit fbf3222

Browse files
authored
Merge pull request #2164 from DougGregor/member-macro-conformances-5.9
2 parents 4215ab9 + 32f5705 commit fbf3222

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -238,19 +238,12 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
238238
throw MacroExpansionError.declarationNotDeclGroup
239239
}
240240

241-
// Local function to expand a member macro once we've opened up
242-
// the existential.
243-
func expandMemberMacro(
244-
_ node: some DeclGroupSyntax
245-
) throws -> [DeclSyntax] {
246-
return try attachedMacro.expansion(
247-
of: attributeNode,
248-
providingMembersOf: node,
249-
in: context
250-
)
251-
}
252-
253-
let members = try _openExistential(declGroup, do: expandMemberMacro)
241+
let members = try attachedMacro.expansion(
242+
of: attributeNode,
243+
providingMembersOf: declGroup,
244+
conformingTo: conformanceList?.map(\.typeName) ?? [],
245+
in: context
246+
)
254247

255248
// Form a buffer of member declarations to return to the caller.
256249
return members.map { $0.formattedExpansion(definition.formatMode) }

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)