Invariant Macros Test Cases

I'm curious as to whether or not the development group has units tests that show code generated from a macro is equivalent to same said code being copied into the same project instead. By equivalent I mean the generated code is invariant at an AST level (not syntax), regardless of whether the generated code was copied in, or the associated macro was used.

I ask because I'm seeing the opposite of this effect. I have code that when using the macro manifests in a crash, but operates correctly when the macro is replaced by the generated code copied from the expanded version of the macro. I understand that in order to evaluate this question, I should provide code. However this is a large project, and ability to discover the underlying cause would be formidable, and I don't believe I have the requisite skill set. For example I would need to learn how ASTs can be generated from the compiler when invoked from within Xcode. Therefore I'm asking the invariant, test case question instead.

I do have this: Attached Macros and Comparable Conformance Issues, which is isolated, but that error shows up at compile time, not runtime.

For my runtime case, I can just use the hand copy paste mechanism; which I have decided to stick with. I'm taking compile time hits anyway. However I feel that such an invariant test case should exist. If such test cases do exist, then I'll start looking at my environment--is it some SwiftUI conflict or the version of Swift Syntax I'm using etc. Thanks in advance.

1 Like

Do you mean code using macro has different runtime behavior than code manually input? It's hard to believe it. Could your provide more details (the macro, the runtime crash, etc.)? I know it might be just a random SwiftUI crash stack trace and I'm certainly not capable to help on this, but just in case someone in the forum can spot something.

Yes. A copy of the expanded code—coming from the inline Xcode macro expansion UI, pasted in with the macro disabled, fixes the crash. This crash is coming from a SwiftUI binding initialization (not use) that does not involve the code generated from the macro or even any struct that has a macro applied to it.

As far as providing code, this would be difficult as I have not isolated the condition. I will say that my current area to test, when I get back to this (I’ve moved on just using the copied expanded code) is whether or not my use of nested packages is causing the issue. I have two proprietary local packages, one nested in the other. The inner package uses a third non local, proprietary package containing the macro. Even though the outer package does not export the macro, it does use the struct from the inner package which has the macro applied.

The macro involved is parametrized by various arguments which effectively direct the macro’s implementation on how to generate the code. Only the macro’s attributes are used to generate the code. The decl argument to the macro’s expansion(…) function is not used (probably should not be a member macro).

Anyway constructing such an analogous scenario where I would try to force a crash would be my first approach to try to derive an isolated example. I would also point out that the crash could be due to my macro exacerbating something I’m doing in the rest of the system that is not kosher. This is a fairly involved SwiftUI document based app that has one non-closure reference, and a UIView representable which uses bindings as events. I have not had an issue so far, but maybe my macro use is pointing the way. I don’t really believe this is the case, but maybe.

I really need to learn how to generate the AST from within Xcode. That way I would be able to possible spot the issue manually, or at least run a diff on the two versions; the version using the macro, vs the one utilizing the pasted code. Sorry for the lengthy verbiage. :weary: Since I have moved on, this issue has really become just one of an academic type interest.

1 Like