diff --git a/Examples/Sources/ExamplePlugin/Macros.swift b/Examples/Sources/ExamplePlugin/Macros.swift index 6c61f04c176..6d34f51e3a6 100644 --- a/Examples/Sources/ExamplePlugin/Macros.swift +++ b/Examples/Sources/ExamplePlugin/Macros.swift @@ -76,13 +76,16 @@ struct MemberDeprecatedMacro: MemberAttributeMacro { } /// Add 'Equatable' conformance. -struct EquatableConformanceMacro: ConformanceMacro { +struct EquatableConformanceMacro: ExtensionMacro { static func expansion( of node: AttributeSyntax, - providingConformancesOf declaration: some DeclGroupSyntax, + attachedTo declaration: some DeclGroupSyntax, + providingExtensionsOf type: some TypeSyntaxProtocol, + conformingTo protocols: [TypeSyntax], in context: some MacroExpansionContext - ) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] { - return [("Equatable", nil)] + ) throws -> [ExtensionDeclSyntax] { + let ext: DeclSyntax = "extension \(type.trimmed): Equatable {}" + return [ext.cast(ExtensionDeclSyntax.self)] } } diff --git a/Sources/SwiftSyntaxMacros/CMakeLists.txt b/Sources/SwiftSyntaxMacros/CMakeLists.txt index 511cd7774ce..757b5eba93f 100644 --- a/Sources/SwiftSyntaxMacros/CMakeLists.txt +++ b/Sources/SwiftSyntaxMacros/CMakeLists.txt @@ -10,7 +10,6 @@ add_swift_host_library(SwiftSyntaxMacros MacroProtocols/AccessorMacro.swift MacroProtocols/AttachedMacro.swift MacroProtocols/CodeItemMacro.swift - MacroProtocols/ConformanceMacro.swift MacroProtocols/DeclarationMacro.swift MacroProtocols/ExpressionMacro.swift MacroProtocols/ExtensionMacro.swift diff --git a/Sources/SwiftSyntaxMacros/MacroProtocols/ConformanceMacro.swift b/Sources/SwiftSyntaxMacros/MacroProtocols/ConformanceMacro.swift deleted file mode 100644 index ac252f95729..00000000000 --- a/Sources/SwiftSyntaxMacros/MacroProtocols/ConformanceMacro.swift +++ /dev/null @@ -1,68 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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: ExtensionMacro { - /// 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( - of node: AttributeSyntax, - providingConformancesOf declaration: some DeclGroupSyntax, - in context: some MacroExpansionContext - ) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] -} - -extension ConformanceMacro { - public static func expansion( - of node: AttributeSyntax, - attachedTo declaration: some DeclGroupSyntax, - providingExtensionsOf type: some TypeSyntaxProtocol, - conformingTo protocols: [TypeSyntax], - in context: some MacroExpansionContext - ) throws -> [ExtensionDeclSyntax] { - - let newConformances = try expansion( - of: node, - providingConformancesOf: declaration, - in: context - ) - - var extensions: [ExtensionDeclSyntax] = [] - for (proto, whereClause) in newConformances { - let decl: DeclSyntax = - """ - extension \(type.trimmed): \(proto.trimmed) {} - """ - - var extensionDecl = decl.cast(ExtensionDeclSyntax.self) - - if let whereClause { - extensionDecl = extensionDecl.with(\.genericWhereClause, whereClause.trimmed) - } - - extensions.append(extensionDecl) - } - - return extensions - } -} diff --git a/Tests/SwiftSyntaxMacroExpansionTest/MacroSystemTests.swift b/Tests/SwiftSyntaxMacroExpansionTest/MacroSystemTests.swift index f0822d58ac8..00ed431c047 100644 --- a/Tests/SwiftSyntaxMacroExpansionTest/MacroSystemTests.swift +++ b/Tests/SwiftSyntaxMacroExpansionTest/MacroSystemTests.swift @@ -642,16 +642,6 @@ public struct DeclsFromStringsMacro: DeclarationMacro, MemberMacro { } } -public struct SendableConformanceMacro: ConformanceMacro { - public static func expansion( - of node: AttributeSyntax, - providingConformancesOf declaration: some DeclGroupSyntax, - in context: some MacroExpansionContext - ) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] { - return [("Sendable", nil)] - } -} - public struct SendableExtensionMacro: ExtensionMacro { public static func expansion( of node: AttributeSyntax, @@ -717,7 +707,6 @@ public let testMacros: [String: Macro.Type] = [ "wrapStoredProperties": WrapStoredProperties.self, "customTypeWrapper": CustomTypeWrapperMacro.self, "unwrap": UnwrapMacro.self, - "AddSendable": SendableConformanceMacro.self, "AddSendableExtension": SendableExtensionMacro.self, ] @@ -1537,44 +1526,6 @@ final class MacroSystemTests: XCTestCase { ) } - func testConformanceExpansion() { - assertMacroExpansion( - """ - @AddSendable - struct MyType { - } - """, - expandedSource: """ - - struct MyType { - } - - extension MyType: Sendable { - } - """, - macros: testMacros, - indentationWidth: indentationWidth - ) - - assertMacroExpansion( - """ - @AddSendable - extension A.B { - } - """, - expandedSource: """ - - extension A.B { - } - - extension A.B: Sendable { - } - """, - macros: testMacros, - indentationWidth: indentationWidth - ) - } - func testExtensionExpansion() { assertMacroExpansion( """