[Macro] Circular reference error when adding extensions to a type decorated by a peer macro

Hi!
I've been working on macros recently. Here's a macro that replaces a generic parameter and puts a new specialized version alongside the original type.

The code without extension part compiles, but it produces circular reference error when adding the extension. And if I move the extension to another file, code compiles.

I did some search and only found this: https://github.com/apple/swift-evolution/blob/main/proposals/0389-attached-macros.md#restrictions-on-arbitrary-names , but not sure if they are actually the same situation.

Is it by design or some bug?

2 Likes

Source code of the macro: GitHub - li3zhen1/WithSpecializedGeneric: An experimental peer macro expanding generic struct or class to a specialized type, so as to avoid dynamic dispatch

This might be related to this bug, which has had quite a bit of discussion, though none from the team yet, and it hasn't been triaged.

1 Like

fyi, has been fixed: [Availability] Lazily expand type refinement contexts for extensions. by hborla · Pull Request #71365 · apple/swift · GitHub (not released yet)

This is a bug. Can you please fill an issue here and post it again to this thread.
Preferable with a simple reproducer if you can. Thank you!

This should be fixed by [Availability] Lazily expand type refinement contexts for extensions. by hborla · Pull Request #71365 · apple/swift · GitHub

2 Likes

Ah amazing, thank you! Recently run into this as well and could reproduce it on main but this PR was just merged last week.