Macro implementation type XX doesn't conform to required protocol 'ConformanceMacro'

I'm using the latest Xcode 15 beta 3 (15A5195k) with CLTs (15A5195k). For the purpose of showcasing the behaviour I created a fresh macro Package. I added an attached(conformance) macro and implemented it. When using the macro, the compiler throws an error Macro implementation type 'xxx' doesn't conform to required protocol 'ConformanceMacro' even though conformance to ConformanceMacro should be done correctly.
My setup is the following:

Package.swift

let package = Package(
    name: "SampleMacro",
    platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)],
    products: [
        // Products define the executables and libraries a package produces, making them visible to other packages.
        .library(
            name: "SampleMacro",
            targets: ["SampleMacro"]
        ),
        .executable(
            name: "SampleMacroClient",
            targets: ["SampleMacroClient"]
        ),
    ],
    dependencies: [
        // Depend on the latest Swift 5.9 prerelease of SwiftSyntax
        .package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-5.9-DEVELOPMENT-SNAPSHOT-2023-04-25-b"),
    ],
    targets: [
        // Targets are the basic building blocks of a package, defining a module or a test suite.
        // Targets can depend on other targets in this package and products from dependencies.
        // Macro implementation that performs the source transformation of a macro.
        .macro(
            name: "SampleMacroMacros",
            dependencies: [
                .product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
                .product(name: "SwiftCompilerPlugin", package: "swift-syntax")
            ]
        ),

        // Library that exposes a macro as part of its API, which is used in client programs.
        .target(name: "SampleMacro", dependencies: ["SampleMacroMacros"]),

        // A client of the library, which is able to use the macro in its own code.
        .executableTarget(name: "SampleMacroClient", dependencies: ["SampleMacro"]),

        // A test target used to develop the macro implementation.
        .testTarget(
            name: "SampleMacroTests",
            dependencies: [
                "SampleMacroMacros",
                .product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
            ]
        ),
    ]
)

SampleMacroMacro.swift

@main
struct SampleMacroPlugin: CompilerPlugin {
    let providingMacros: [Macro.Type] = [
        ProtocolConformanceMacro.self,
    ]
}

ProtocolConformanceMacro.swift

public enum ProtocolConformanceMacro {
}

extension ProtocolConformanceMacro: ConformanceMacro {
    public static func expansion(
        of node: AttributeSyntax,
        providingConformancesOf declaration: some DeclGroupSyntax,
        in context: some MacroExpansionContext
    ) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
        [("IamNothing", nil)]
    }
}

The implementation is simply:

@ProtocolConformanceMacro
struct SampleStruct {
}

Not sure if this is a bug or I just did something incorrectly.

I'm running into the same issue and am also seeing it in GitHub - DougGregor/swift-macro-examples: Example macros for the Swift macros effort.

The problem appears to be that ConformanceMacro was reworked to use ExtensionMacro in the last week and the Swift compiler isn't aware of that.

I would venture that downgrading swift-syntax to an older commit like 7617b70fb8addcf29ab4d18f7e90d88bbe07851d will provide a temporary fix but won't represent production behavior.

Some details about the changes are at https://github.com/apple/swift-evolution/blob/main/proposals/0402-extension-macros.md.

I believe there's a way to use a nightly toolchain to get conformance macros working, but someone else can chime in on that since I've not successfully done it.

The problem appears to be that ConformanceMacro was reworked to use ExtensionMacro in the last week and the Swift compiler isn't aware of that.

Yeah, that's pretty much it - swift-syntax should have kept supporting the conformance role. Fix is up [5.9] Plugin should keep handling the conformance macro role by bnbarham · Pull Request #1891 · apple/swift-syntax · GitHub.

I would venture that downgrading swift-syntax to an older commit like 7617b70fb8addcf29ab4d18f7e90d88bbe07851d will provide a temporary fix but won't represent production behavior.

This would be fine until the fix is merged and in the nightly snapshot.

I believe there's a way to use a nightly toolchain to get conformance macros working, but someone else can chime in on that since I've not successfully done it.

This should also work - what error are you seeing there?

1 Like