Skip to content

Commit 91e9a2a

Browse files
committed
[Test] Ensure that freestanding macros in unprocessed #ifs aren't expanded.
Tracked by rdar://110083415.
1 parent e6c5871 commit 91e9a2a

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,3 +1505,29 @@ public struct SingleMemberMacro: MemberMacro {
15051505
]
15061506
}
15071507
}
1508+
1509+
public struct UseIdentifierMacro: DeclarationMacro {
1510+
public static func expansion(
1511+
of node: some FreestandingMacroExpansionSyntax,
1512+
in context: some MacroExpansionContext
1513+
) throws -> [DeclSyntax] {
1514+
guard let argument = node.argumentList.first?.expression.as(StringLiteralExprSyntax.self) else {
1515+
fatalError("boom")
1516+
}
1517+
return [
1518+
"""
1519+
func \(context.makeUniqueName("name"))() {
1520+
_ = \(raw: argument.segments.first!)
1521+
}
1522+
""",
1523+
"""
1524+
struct Foo {
1525+
var \(context.makeUniqueName("name")): Int {
1526+
_ = \(raw: argument.segments.first!)
1527+
return 0
1528+
}
1529+
}
1530+
"""
1531+
]
1532+
}
1533+
}

test/Macros/macro_expand_primary.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,13 @@ final class Dog: Observable {
8585
func test() {
8686
observeDog()
8787
}
88+
89+
90+
@freestanding(declaration, names: named(Foo)) macro useIdentifier(_ value: String) = #externalMacro(module: "MacroDefinition", type: "UseIdentifierMacro")
91+
92+
#if false
93+
#useIdentifier("totallyNotDefined")
94+
#else
95+
let hello = "Hello"
96+
#useIdentifier("hello")
97+
#endif

0 commit comments

Comments
 (0)