Skip to content

Throw an error if types conforming to MemberMacro implement no expansion method #2319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Sources/SwiftSyntaxMacros/MacroProtocols/MemberMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public protocol MemberMacro: AttachedMacro {
///
/// - Returns: the set of member declarations introduced by this macro, which
/// are nested inside the `attachedTo` declaration.
@available(*, deprecated, message: "Use expansion(of:providingMembersOf:conformingTo:in:")
///
/// - 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,
Expand Down Expand Up @@ -54,14 +56,24 @@ public protocol MemberMacro: AttachedMacro {
) 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] {
return try expansion(of: node, providingMembersOf: declaration, conformingTo: [], in: context)
throw UnimplementedExpansionMethodError()
}

/// Default implementation that ignores the unhandled conformances.
Expand Down