Skip to content

Commit a0a6ce8

Browse files
committed
[MemberMacro] Add missingConformancesTo argument to expansion operation
Stage in an entrypoint for member macros that allows them to learn about which conformances that they've asked about are "missing", meaning that they are not present on the type (ignoring those that would be generated by an extension macro). This information is equivalent to the information provided to extension macros, although the member macro itself cannot create the conformance.
1 parent 03ee18b commit a0a6ce8

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-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+
missingConformancesTo: conformanceList?.map(\.type) ?? [],
239240
in: context
240241
)
241242

Sources/SwiftSyntaxMacros/MacroProtocols/MemberMacro.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,39 @@ public protocol MemberMacro: AttachedMacro {
2828
providingMembersOf declaration: some DeclGroupSyntax,
2929
in context: some MacroExpansionContext
3030
) throws -> [DeclSyntax]
31+
32+
/// Expand an attached declaration macro to produce a set of members.
33+
///
34+
/// - Parameters:
35+
/// - node: The custom attribute describing the attached macro.
36+
/// - declaration: The declaration the macro attribute is attached to.
37+
/// - missingConformancesTo: The set of protocols that were declared
38+
/// in the set of conformances for the macro and to which the declaration
39+
/// does not explicitly conform. The member macro itself cannot declare
40+
/// conformances to these protocols (only an extension macro can do that),
41+
/// but can provide supporting declarations, such as a required
42+
/// initializer or stored property, that cannot be written in an
43+
/// extension.
44+
/// - context: The context in which to perform the macro expansion.
45+
///
46+
/// - Returns: the set of member declarations introduced by this macro, which
47+
/// are nested inside the `attachedTo` declaration.
48+
static func expansion(
49+
of node: AttributeSyntax,
50+
providingMembersOf declaration: some DeclGroupSyntax,
51+
missingConformancesTo protocols: [TypeSyntax],
52+
in context: some MacroExpansionContext
53+
) throws -> [DeclSyntax]
54+
}
55+
56+
public extension MemberMacro {
57+
/// Default implementation that ignores the unhandled conformances.
58+
static func expansion(
59+
of node: AttributeSyntax,
60+
providingMembersOf declaration: some DeclGroupSyntax,
61+
missingConformancesTo protocols: [TypeSyntax],
62+
in context: some MacroExpansionContext
63+
) throws -> [DeclSyntax] {
64+
return try expansion(of: node, providingMembersOf: declaration, in: context)
65+
}
3166
}

0 commit comments

Comments
 (0)