Skip to content

[Macros] Introduce a ConformanceMacro protocol. #1356

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
Feb 23, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Sources/SwiftSyntaxMacros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_swift_host_library(SwiftSyntaxMacros
MacroProtocols/AccessorMacro.swift
MacroProtocols/AttachedMacro.swift
MacroProtocols/CodeItemMacro.swift
MacroProtocols/ConformanceMacro.swift
MacroProtocols/DeclarationMacro.swift
MacroProtocols/ExpressionMacro.swift
MacroProtocols/FreestandingMacro.swift
Expand Down
36 changes: 36 additions & 0 deletions Sources/SwiftSyntaxMacros/MacroProtocols/ConformanceMacro.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax

/// Describes a macro that can add conformances to the declaration it's
/// attached to.
public protocol ConformanceMacro: AttachedMacro {
/// Expand an attached conformance macro to produce a set of conformances.
///
/// - Parameters:
/// - node: The custom attribute describing the attached macro.
/// - declaration: The declaration the macro attribute is attached to.
/// - context: The context in which to perform the macro expansion.
///
/// - Returns: the set of `(type, where-clause?)` pairs that each provide the
/// protocol type to which the declared type conforms, along with
/// an optional where clause.
static func expansion<
Declaration: DeclGroupSyntax,
Context: MacroExpansionContext
>(
of node: AttributeSyntax,
providingConformancesOf declaration: Declaration,
in context: Context
) throws -> [(TypeSyntax, WhereClauseSyntax?)]
}
2 changes: 1 addition & 1 deletion Sources/SwiftSyntaxMacros/MacroSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
return true
}

return !(macro is PeerMacro.Type || macro is MemberMacro.Type || macro is AccessorMacro.Type || macro is MemberAttributeMacro.Type)
return !(macro is PeerMacro.Type || macro is MemberMacro.Type || macro is AccessorMacro.Type || macro is MemberAttributeMacro.Type || macro is ConformanceMacro.Type)
}

if newAttributes.isEmpty {
Expand Down