Skip to content

Commit 3f9fc44

Browse files
committed
[Macros] Add a test that invokes macro expansion through the conformance
checker while type checking a freestanding macro argument in the same scope. (cherry picked from commit f9f63e3)
1 parent 8d2da14 commit 3f9fc44

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,3 +1603,26 @@ extension SelfAlwaysEqualOperator: MemberMacro {
16031603
]
16041604
}
16051605
}
1606+
1607+
public struct InitializableMacro: ConformanceMacro, MemberMacro {
1608+
public static func expansion(
1609+
of node: AttributeSyntax,
1610+
providingConformancesOf decl: some DeclGroupSyntax,
1611+
in context: some MacroExpansionContext
1612+
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1613+
return [("Initializable", nil)]
1614+
}
1615+
1616+
public static func expansion(
1617+
of node: AttributeSyntax,
1618+
providingMembersOf decl: some DeclGroupSyntax,
1619+
in context: some MacroExpansionContext
1620+
) throws -> [DeclSyntax] {
1621+
let requirement: DeclSyntax =
1622+
"""
1623+
init(value: Int) {}
1624+
"""
1625+
1626+
return [requirement]
1627+
}
1628+
}

test/Macros/top_level_freestanding.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,24 @@ func testGlobalVariable() {
104104
}
105105

106106
#endif
107+
108+
109+
@freestanding(declaration)
110+
macro Empty<T>(_ closure: () -> T) = #externalMacro(module: "MacroDefinition", type: "EmptyDeclarationMacro")
111+
112+
#Empty {
113+
S(a: 10, b: 10)
114+
}
115+
116+
@attached(conformance)
117+
@attached(member, names: named(init))
118+
macro Initializable() = #externalMacro(module: "MacroDefinition", type: "InitializableMacro")
119+
120+
protocol Initializable {
121+
init(value: Int)
122+
}
123+
124+
@Initializable
125+
struct S {
126+
init(a: Int, b: Int) {}
127+
}

0 commit comments

Comments
 (0)