Skip to content

Commit

Permalink
Merge pull request #2420 from ahoppen/ahoppen/509/conforming-to
Browse files Browse the repository at this point in the history
[509] Add `conformingTo` parameter to `MemberMacro.expansion` function
  • Loading branch information
ahoppen committed Jan 9, 2024
2 parents 6079de8 + d0a2288 commit 43c802f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
let members = try attachedMacro.expansion(
of: attributeNode,
providingMembersOf: declGroup,
conformingTo: conformanceList?.map(\.type) ?? [],
in: context
)

Expand Down
57 changes: 57 additions & 0 deletions Sources/SwiftSyntaxMacros/MacroProtocols/MemberMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,66 @@ public protocol MemberMacro: AttachedMacro {
///
/// - Returns: the set of member declarations introduced by this macro, which
/// are nested inside the `attachedTo` declaration.
///
/// - Warning: This is the legacy `expansion` function of `MemberMacro` that is provided for backwards-compatiblity.
/// Use ``expansion(of:providingMembersOf:conformingTo:in:)-1sxoe`` instead.
static func expansion(
of node: AttributeSyntax,
providingMembersOf declaration: some DeclGroupSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax]

/// Expand an attached declaration macro to produce a set of members.
///
/// - Parameters:
/// - node: The custom attribute describing the attached macro.
/// - declaration: The declaration the macro attribute is attached to.
/// - conformingTo: The set of protocols that were declared
/// in the set of conformances for the macro and to which the declaration
/// does not explicitly conform. The member macro itself cannot declare
/// conformances to these protocols (only an extension macro can do that),
/// but can provide supporting declarations, such as a required
/// initializer or stored property, that cannot be written in an
/// extension.
/// - context: The context in which to perform the macro expansion.
///
/// - Returns: the set of member declarations introduced by this macro, which
/// are nested inside the `attachedTo` declaration.
static func expansion(
of node: AttributeSyntax,
providingMembersOf declaration: some DeclGroupSyntax,
conformingTo protocols: [TypeSyntax],
in context: some MacroExpansionContext
) throws -> [DeclSyntax]
}

private struct UnimplementedExpansionMethodError: Error, CustomStringConvertible {
var description: String {
"""
Types conforming to `MemberMacro` must implement either \
expansion(of:providingMembersOf:in:) or \
expansion(of:providingMembersOf:conformingTo:in:)
"""
}
}

public extension MemberMacro {
/// Default implementation supplies no conformances.
static func expansion(
of node: AttributeSyntax,
providingMembersOf declaration: some DeclGroupSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
throw UnimplementedExpansionMethodError()
}

/// Default implementation that ignores the unhandled conformances.
static func expansion(
of node: AttributeSyntax,
providingMembersOf declaration: some DeclGroupSyntax,
conformingTo protocols: [TypeSyntax],
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
return try expansion(of: node, providingMembersOf: declaration, in: context)
}
}

0 comments on commit 43c802f

Please sign in to comment.