# Attached Macros and Comparable Conformance Issues

**URL:** <https://forums.swift.org/t/attached-macros-and-comparable-conformance-issues/70048>\
**Category:** Using Swift\
**Tags:** attached-macros\
**Created:** [February 14, 2024, 11:44pm UTC](https://forums.swift.org/t/attached-macros-and-comparable-conformance-issues/70048 "2024-02-14T23:44:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gvenn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gvenn/32/34077_2.png) [@gvenn](https://forums.swift.org/u/gvenn)\
**Post date:** [February 14, 2024, 11:44pm UTC](https://forums.swift.org/t/attached-macros-and-comparable-conformance-issues/70048/1 "2024-02-14T23:44:58Z")

</div>

I discovered an issue I'm having with attached member macros, and Comparable conformance. I distilled it down to the following case, but I'm not identifying the issue, and therefore I'm thinking that there may be a bug. As usual this example is coming from a more intricate version. This is being done through Xcode. I have the following:

```swift
@testForceConstantVarDecl()
struct Junk: Comparable {
    var ident: Int

#if true
    static public
    func < (lhs: Junk, rhs: Junk) -> Bool {
        lhs.ident < rhs.ident
    }
#endif
}

```

When I expand the macro in Xcode, I see--which is what was desired:

```swift
struct Junk: Comparable {
    var ident: Int

#if true
    static public
    func < (lhs: Junk, rhs: Junk) -> Bool {
        lhs.ident < rhs.ident
    }
#endif
    var hello: String
}

```

However in this case, the compiler complains with:

```swift
...: error: type 'Junk' does not conform to protocol 'Comparable'
struct Junk: Comparable {
       ^
Swift.Comparable:2:17: note: multiple matching functions named '<' with type '(Junk, Junk) -> Bool'
    static func < (lhs: Self, rhs: Self) -> Bool
                ^
...: note: candidate exactly matches
    func < (lhs: Junk, rhs: Junk) -> Bool {
         ^
...: note: candidate exactly matches
    func < (lhs: Junk, rhs: Junk) -> Bool {

```

When I comment out the macro, as in:

```swift
//@testForceConstantVarDecl()
struct Junk: Comparable {
    var ident: Int

#if true
    static public
    func < (lhs: Junk, rhs: Junk) -> Bool {
        lhs.ident < rhs.ident
    }
#endif
}

```

The compiler does not complain.

In the macro package the following are created in the appropriate files:

**Macro Declaration**

```swift
@attached(member, names: arbitrary)
public macro testForceConstantVarDecl() =
    #externalMacro(
        module: "PurGMCMacrosPlugin",
	type: "PurGMCTestForceConstantVarDecl")

```

**Macro Reference**

```swift
#if canImport(SwiftCompilerPlugin)
import SwiftCompilerPlugin
import SwiftSyntaxMacros

@main
struct PurGMCMacrosPlugin: CompilerPlugin {
    let providingMacros: [Macro.Type] = [
        ...,
	    PurGMCTestForceConstantVarDecl.self
    ]
}
#endif

```

**Macro Implementation**

```swift
import Foundation
import SwiftSyntax
import SwiftSyntaxMacros

public struct PurGMCTestForceConstantVarDecl: MemberMacro {

    public static
    func expansion(of node: AttributeSyntax,
                   providingMembersOf declaration: some DeclGroupSyntax,
                   in context: some MacroExpansionContext) throws ->
                                                               [DeclSyntax] {
        guard let attrsArgs = node.arguments,
              case let .argumentList(attrsArgsList) = attrsArgs,
              attrsArgsList.count == 0 //,                                       
              else {
            return []
        }
        return [
            """                                                                  
            \n var hello: String                                              
            """
	]
    }
}

```

So I'm suspecting the compiler is automatically generating, or believing it should/has generated Comparable conformance when the macro invocation is not commented out. If it has generated it, I do not know how to see it.

When the macro is "turned on", and the `#if true` directive is changed to `#if false`, the compiler correctly (I believe) complains with:

`Automatic synthesis of 'Comparable' is not supported for struct declarations`

I'm therefore confused by the state where the macro and compiler directive are both "on". Is there something that I'm doing incorrectly? Although this is a trivial macro, which would have no real use, I'm a little hesitant calling this a bug, since I would think this would have been seen. This test case is derived from a parametrized macro which provides for proprietary boiler plate code generation. It all works except for this conformance issue exemplified by this test case. Maybe this is a known issue. Is it?

**Work Around**

I soon realized this was solvable via an extension. So the above test code becomes:

```swift
@testForceConstantVarDecl()
struct Junk {
    var ident: Int

#if true
    static public
    func < (lhs: Junk, rhs: Junk) -> Bool {
        lhs.ident < rhs.ident
    }
#endif
}

extension Junk: Comparable {
}

```

So yeah this works, and now I've convinced myself that yes this is a bug. Any incite? Even though the solution is fine, I thought I would post anyway, in case I learned something new from someone.

---

<div class="post-metadata">

**Author:** ![rayx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rayx/32/12204_2.png) [@rayx](https://forums.swift.org/u/rayx)\
**Post date:** [February 23, 2024, 7:29am UTC](https://forums.swift.org/t/attached-macros-and-comparable-conformance-issues/70048/2 "2024-02-23T07:29:17Z")

</div>

From my experience, the current implementation (I'm using Xcode 15.1) doesn't work well when using macro and protocols having synthesized conformance together. I for one ran into a few odd issues (see [#70086](https://github.com/apple/swift/issues/70086), [#70087](https://github.com/apple/swift/issues/70087), [#70181](https://github.com/apple/swift/issues/70181)). Most of my issues were with extension macro and I used the same workaround as the one you found for a while. I gave up in the end because there are more issues with extension macro (see [#69073](https://github.com/apple/swift/issues/69073) for example).

Now I mainly use member macros. In the places where I need extension macro, I use member macro this way:

```swift
@MyMemberMacro
extension Foo: FooProtocol {
}

```

The issue you reported is about member macro. I didn't run into it because (fortunately) I don't use compiler directive. It shows the issue with macro and synthesized conformance is general. I suspect the issue is well known by Apple engines and they have a long term plan on how to address it. But I haven't found any discussion about it.

---

<div class="post-metadata">

**Author:** ![gvenn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gvenn/32/34077_2.png) [@gvenn](https://forums.swift.org/u/gvenn)\
**Post date:** [February 23, 2024, 7:55pm UTC](https://forums.swift.org/t/attached-macros-and-comparable-conformance-issues/70048/3 "2024-02-23T19:55:32Z")

</div>

Thanks for the references, updated info.. 🙂
