Nested macro does not receive a proper MacroExpansionContext #78611
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
swift macro
Feature → declarations: Swift `macro` declarations
Description
Context
Consider the following use-case: I have a
@GenerateTest
macro is aMemberMacro
that generates a function which is decorated with a@Test
attribute.When the
@GenerateTest
macro is expanded, the member is properly generated with the@Test
attribute.When the
@Test
macro is expanded, the generated code is incorrect and does not compile.Details
Here is the initial code:
Then the
@GenerateTest
macro is expanded resulting in:Then the
@Test
macro is expanded resulting in:The generated thunk is not correct (1), and the container reference an instance function (2) which does not compile.
Analysis
I have step into the code to see if I could figure out what was going wrong.
Here are my observations:
@GenerateTest
macro is expanded, the passedMacroExpansionContext
contains atypeOfLexicalContext
value corresponding to theDummy
structure.@Test
macro is expanded, the passedMacroExpansionContext
DOES NOT contain atypeOfLexicalContext
. This affects the generated code by making it non compilable.I think the issue lies in the way the
MacroExpansionContext
is built before being passed to the macro:@GenerateTest
macro is expanded, theMacroExpansionContext
is built from the syntax node of the structure.@Test
macro is expanded, theMacroExpansionContext
is built from the newly generated member syntax node which is not attached to the syntax node of the structure.Steps to Reproduce
Here is a GIST containing a test case to reproduce (the code is a bit rough). The file can be dropped in the
TestingMacroTests
source folder as-is.The test case contains 3 tests:
@Test
annotation:TestDeclarationMacro
@GenerateTest
GenerateTestMacro
@GenerateTest
GenerateTestMacro
AND theTestDeclarationMacro
The text was updated successfully, but these errors were encountered: