swift icon indicating copy to clipboard operation
swift copied to clipboard

ConformanceMacro does not execute the expansion method

Open hellojoelhuber opened this issue 2 years ago • 0 comments

Description Currently, it seems that the expansion method is not run for ConformanceMacros.

Steps to reproduce Write a macro conforming to ConformanceMacro

// declare macro as attached(conformance)
@attached(conformance)
public macro MyConformance() = #externalMacro(module: "MyMacros", type: "MyConformance")

// Add a breakpoint in the body of the  before the return statement, 
// or a print statement for declaration.description, or simply "Hello, debugger."
// or simply a fatalError, as I do in this example:
public struct MyConformanceMacro: ConformanceMacro {
    public static func expansion(
        of node: AttributeSyntax,
        providingConformancesOf declaration: some DeclGroupSyntax,
        in context: some MacroExpansionContext
    ) throws -> [(SwiftSyntax.TypeSyntax, SwiftSyntax.GenericWhereClauseSyntax?)] {
        fatalError("This is never called")
        return []
    }
}

// Add the macro to providingMacros and testMacros, 
// then run a unit test:
func test_MyConformanceMacro() {
        assertMacroExpansion(
            """
            @MyConformance
            struct SomeStruct {
                var someProp: String
            }
            """, expandedSource:
            """
            
            struct SomeStruct {
                var someProp: String
            }
            """, macros: testMacros)
    }

// This test case will pass - unexpectedly. The breakpoint is not triggered, or the fatal error is not thrown, or the print statements do not print.

Expected behavior The breakpoint will trigger, or the fatal error will be thrown, or the print statements will print.

Environment

  • Swift compiler version info
swift-driver version: 1.82.2 Apple Swift version 5.9 (swiftlang-5.9.0.114.6 clang-1500.0.27.1)
  • Xcode version info
Xcode 15.0
Build version 15A5160n
  • Deployment target: macOS 14.0

hellojoelhuber avatar Jun 08 '23 18:06 hellojoelhuber