I thought that SE-0389 pretty clearly states that macros can introduce symbols which can then be used in other parts of the original source code (subject to certain restrictions). However, I am unable to extend a type that was introduced by a macro:
@Foo
struct Test { }
extension Bar { } // Error: Cannot find type 'Bar' in scope
// Error: Circular reference
public struct FooMacro: PeerMacro {
public static func expansion(
of node: AttributeSyntax,
providingPeersOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
[
"""
struct Bar { }
"""
]
}
}
The only workaround I know of is to move the extension into another file.
It's definitely an easy bug to encounter, and after introducing macros to some packages we distribute, many folks have encountered the issue and reported it to us, so hopefully it will be triaged and fixed in Swift soon.
After introducing macros to a few of our libraries this past week we've already had a dozen reports of this issue. Would be nice to get some eyes on it. We're having to document the Swift bug as a gotcha in all our libraries using macros.
~It seems that this issue has been fixed in Xcode 15.1 Beta 3 .~
I'm so sorry for my misleading.
The issue I had was not related to @Observable, but rather with the custom member macro I created.
The message I encountered was as follows, and it was resolved in Xcode 15.1 Beta 3.
Circular reference expanding member attribute macros on 'Something'
We're still regularly getting questions about this when folks adopt the macros we've introduced to our libraries. Any chance we can have a core team member acknowledge the issue, which has been open since the week of WWDC23 and still hasn't been triaged?