# Macro diagnostics when expanding for member and extension

**URL:** <https://forums.swift.org/t/macro-diagnostics-when-expanding-for-member-and-extension/81413>\
**Category:** Macros\
**Created:** [July 31, 2025, 12:16am UTC](https://forums.swift.org/t/macro-diagnostics-when-expanding-for-member-and-extension/81413 "2025-07-31T00:16:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanvoorden](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vanvoorden/32/33619_2.png) [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Post date:** [July 31, 2025, 12:16am UTC](https://forums.swift.org/t/macro-diagnostics-when-expanding-for-member-and-extension/81413/1 "2025-07-31T00:16:40Z")

</div>

I'm experimenting with a macro that is both a `MemberMacro` and a `ExtensionMacro`. The macro should _only_ be applied to a `struct`.

Both expansions look kind of similar with an early `guard`:

```swift
guard
  let declaration = declaration.as(StructDeclSyntax.self)
else {
  context.diagnose(
    Diagnostic(
      node: node,
      message: "not a struct"
    )
  )
  return []
}
...

```

What seems to be happening is that if I try and write a unit test to confirm my diagnostics are applied correctly… my compiler is generating _two_ diagnostics: one for `MemberMacro` and one for `ExtensionMacro`.

This sort of makes sense… but I could also be missing something here. Is there any kind of shortcut where these two macro expansions could produce "the same" diagnostic but the compiler "folds" them down to one on my behalf?

I'm not able to see this specific pattern used in production libraries from Swift… but I did find a sample macro that sort of tries to work around this. The `OptionSetMacro` example from `swift-syntax` is both a `MemberMacro` and a `ExtensionMacro` and must only be applied to a `struct`. The `MemberMacro` expansion tests for `struct` and applies diagnostics:

> <https://github.com/swiftlang/swift-syntax/blob/601.0.1/Examples/Sources/MacroExamples/Implementation/ComplexMacros/OptionSetMacro.swift#L184-L193>

But the `ExtensionMacro` expansions tests for `struct` _without_ diagnostics:

> <https://github.com/swiftlang/swift-syntax/blob/601.0.1/Examples/Sources/MacroExamples/Implementation/ComplexMacros/OptionSetMacro.swift#L159-L163>

Is this sort of the common practice at this point? Is it normal for `MemberMacro` to control diagnostics and `ExtensionMacro` generally "fails silently" when we assume the generated diagnostic would be identical?
