Expand macros on members guarded by an #if - #3396
Open
maxches99 wants to merge 1 commit into
Open
Conversation
MacroApplication expands the members of a declaration in visit(_: MemberBlockSyntax), but members guarded by an #if are stored directly in the IfConfigClauseSyntax as a MemberBlockItemListSyntax, for which there is no override. Those members were never routed through the member expansion, so member attribute macros, peer macros and freestanding declaration macros attached to them were silently dropped. Extract the member handling into expandMemberBlockItems and apply it to the members of every clause of an #if as well. Extension macros and the member macros of the enclosing declaration are not expanded inside an #if, since their expansion is not inserted in place of the member: an extension would be hoisted to the top level, where the #if no longer applies. Fixes swiftlang#3106
maxches99
requested review from
hamishknight,
hborla and
rintaro
as code owners
August 2, 2026 13:58
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MacroApplicationexpands the members of a declaration invisit(_: MemberBlockSyntax), but the members guarded by an#ifare not stored in aMemberBlockSyntax— they live directly in theIfConfigClauseSyntaxas aMemberBlockItemListSyntax, for which there is no override. Those members are therefore never routed through the member expansion, and macros attached to them are silently dropped.This affects more than member attribute macros:
Freestanding declaration macros inside such an
#ifare likewise left unexpanded. (Top-level#ifalready works, because there the clause holds aCodeBlockItemListSyntax, which does have an override.)The member handling is extracted from
visit(_: MemberBlockSyntax)intoexpandMemberBlockItems(_:parentDeclGroup:isInsideIfConfig:), which is then also applied to the members of every clause of an#if, recursively.Two roles are deliberately not expanded inside an
#if, because their output does not stay where the member is:#ifno longer applies, so expanding them would emit an extension of a type that may not be compiled at all. They keep being skipped, and there is now a test that pins this down.#if, so they are still added once, to the outer member block.Macros are expanded in every clause rather than only in the active one.
MacroApplicationdoes not resolve#ifitself and keeps all branches in its output, so restricting expansion to one clause would leave the others looking unexpanded; expanding per clause is also correct whichever branch ends up being compiled, since these macros are applied per member. If you would rather gate this on theBuildConfigurationthatassertMacroExpansionalready accepts (as suggested in the issue), I am happy to follow up — that would also let the inactive clauses be left entirely untouched.Fixes #3106