Skip to content

Commit d295e6a

Browse files
DougGregorahoppen
authored andcommitted
[SE-0407] Add missingConformancesTo argument to MemberMacro 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 6079de8 commit d295e6a

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)