Skip to content

Commit 1561a9e

Browse files
authored
Merge pull request #1875 from rintaro/macros-openexistential
Stop using _openExistential
2 parents 8828c29 + 9a4a78a commit 1561a9e

File tree

3 files changed

+51
-162
lines changed

3 files changed

+51
-162
lines changed

Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift

Lines changed: 43 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -91,41 +91,38 @@ public func expandFreestandingMacro(
9191
in context: some MacroExpansionContext
9292
) -> String? {
9393
do {
94-
func _expand(node: some FreestandingMacroExpansionSyntax) throws -> String {
95-
let expandedSyntax: Syntax
96-
switch (macroRole, definition) {
97-
case (.expression, let exprMacroDef as ExpressionMacro.Type):
98-
expandedSyntax = try Syntax(exprMacroDef.expansion(of: node, in: context))
99-
100-
case (.declaration, let declMacroDef as DeclarationMacro.Type):
101-
var rewritten = try declMacroDef.expansion(of: node, in: context)
102-
// Copy attributes and modifiers to the generated decls.
103-
if let expansionDecl = node.as(MacroExpansionDeclSyntax.self) {
104-
let attributes = declMacroDef.propagateFreestandingMacroAttributes ? expansionDecl.attributes : nil
105-
let modifiers = declMacroDef.propagateFreestandingMacroModifiers ? expansionDecl.modifiers : nil
106-
rewritten = rewritten.map {
107-
$0.applying(attributes: attributes, modifiers: modifiers)
108-
}
94+
let expandedSyntax: Syntax
95+
switch (macroRole, definition) {
96+
case (.expression, let exprMacroDef as ExpressionMacro.Type):
97+
expandedSyntax = try Syntax(exprMacroDef.expansion(of: node, in: context))
98+
99+
case (.declaration, let declMacroDef as DeclarationMacro.Type):
100+
var rewritten = try declMacroDef.expansion(of: node, in: context)
101+
// Copy attributes and modifiers to the generated decls.
102+
if let expansionDecl = node.as(MacroExpansionDeclSyntax.self) {
103+
let attributes = declMacroDef.propagateFreestandingMacroAttributes ? expansionDecl.attributes : nil
104+
let modifiers = declMacroDef.propagateFreestandingMacroModifiers ? expansionDecl.modifiers : nil
105+
rewritten = rewritten.map {
106+
$0.applying(attributes: attributes, modifiers: modifiers)
109107
}
110-
expandedSyntax = Syntax(
111-
CodeBlockItemListSyntax(
112-
rewritten.map {
113-
CodeBlockItemSyntax(item: .decl($0))
114-
}
115-
)
108+
}
109+
expandedSyntax = Syntax(
110+
CodeBlockItemListSyntax(
111+
rewritten.map {
112+
CodeBlockItemSyntax(item: .decl($0))
113+
}
116114
)
115+
)
117116

118-
case (.codeItem, let codeItemMacroDef as CodeItemMacro.Type):
119-
let rewritten = try codeItemMacroDef.expansion(of: node, in: context)
120-
expandedSyntax = Syntax(CodeBlockItemListSyntax(rewritten))
117+
case (.codeItem, let codeItemMacroDef as CodeItemMacro.Type):
118+
let rewritten = try codeItemMacroDef.expansion(of: node, in: context)
119+
expandedSyntax = Syntax(CodeBlockItemListSyntax(rewritten))
121120

122-
case (.accessor, _), (.memberAttribute, _), (.member, _), (.peer, _), (.conformance, _), (.extension, _), (.expression, _), (.declaration, _),
123-
(.codeItem, _):
124-
throw MacroExpansionError.unmatchedMacroRole(definition, macroRole)
125-
}
126-
return expandedSyntax.formattedExpansion(definition.formatMode)
121+
case (.accessor, _), (.memberAttribute, _), (.member, _), (.peer, _), (.conformance, _), (.extension, _), (.expression, _), (.declaration, _),
122+
(.codeItem, _):
123+
throw MacroExpansionError.unmatchedMacroRole(definition, macroRole)
127124
}
128-
return try _openExistential(node, do: _expand)
125+
return expandedSyntax.formattedExpansion(definition.formatMode)
129126
} catch {
130127
context.addDiagnostics(from: error, node: node)
131128
return nil
@@ -208,22 +205,11 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
208205
throw MacroExpansionError.parentDeclGroupNil
209206
}
210207

211-
// Local function to expand a member attribute macro once we've opened up
212-
// the existential.
213-
func expandMemberAttributeMacro(
214-
_ node: some DeclGroupSyntax
215-
) throws -> [AttributeSyntax] {
216-
return try attachedMacro.expansion(
217-
of: attributeNode,
218-
attachedTo: node,
219-
providingAttributesFor: declarationNode,
220-
in: context
221-
)
222-
}
223-
224-
let attributes = try _openExistential(
225-
parentDeclGroup,
226-
do: expandMemberAttributeMacro
208+
let attributes = try attachedMacro.expansion(
209+
of: attributeNode,
210+
attachedTo: parentDeclGroup,
211+
providingAttributesFor: declarationNode,
212+
in: context
227213
)
228214

229215
// Form a buffer containing an attribute list to return to the caller.
@@ -238,19 +224,11 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
238224
throw MacroExpansionError.declarationNotDeclGroup
239225
}
240226

241-
// Local function to expand a member macro once we've opened up
242-
// the existential.
243-
func expandMemberMacro(
244-
_ node: some DeclGroupSyntax
245-
) throws -> [DeclSyntax] {
246-
return try attachedMacro.expansion(
247-
of: attributeNode,
248-
providingMembersOf: node,
249-
in: context
250-
)
251-
}
252-
253-
let members = try _openExistential(declGroup, do: expandMemberMacro)
227+
let members = try attachedMacro.expansion(
228+
of: attributeNode,
229+
providingMembersOf: declGroup,
230+
in: context
231+
)
254232

255233
// Form a buffer of member declarations to return to the caller.
256234
return members.map { $0.formattedExpansion(definition.formatMode) }
@@ -279,23 +257,12 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
279257

280258
let protocols = conformanceList?.map(\.typeName) ?? []
281259

282-
// Local function to expand an extension macro once we've opened up
283-
// the existential.
284-
func expandExtensionMacro(
285-
_ node: some DeclGroupSyntax
286-
) throws -> [ExtensionDeclSyntax] {
287-
return try attachedMacro.expansion(
288-
of: attributeNode,
289-
attachedTo: node,
290-
providingExtensionsOf: extendedType,
291-
conformingTo: protocols,
292-
in: context
293-
)
294-
}
295-
296-
let extensions = try _openExistential(
297-
declGroup,
298-
do: expandExtensionMacro
260+
let extensions = try attachedMacro.expansion(
261+
of: attributeNode,
262+
attachedTo: declGroup,
263+
providingExtensionsOf: extendedType,
264+
conformingTo: protocols,
265+
in: context
299266
)
300267

301268
// Form a buffer of peer declarations to return to the caller.

Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,6 @@ import SwiftDiagnostics
1414
import SwiftSyntax
1515
@_spi(MacroExpansion) import SwiftSyntaxMacros
1616

17-
private func expandMemberAttributeMacro(attribute: AttributeSyntax, attachedTo: DeclSyntax) -> AttributeListSyntax {
18-
fatalError("unimplemented")
19-
}
20-
21-
private func expandMemberMacro(attribute: AttributeSyntax, attachedTo: DeclGroupSyntax) -> MemberDeclListSyntax {
22-
fatalError("unimplemented")
23-
}
24-
25-
private func expandPeerMacro(attribute: AttributeSyntax, attachedTo: DeclSyntax) -> CodeBlockItemListSyntax {
26-
fatalError("unimplemented")
27-
}
28-
29-
private func expandConformanceMacro(attribute: AttributeSyntax, attachedTo: DeclSyntax) -> CodeBlockItemListSyntax {
30-
fatalError("unimplemented")
31-
}
32-
33-
private func expandAccessorMacro(attribute: AttributeSyntax, attachedTo: DeclSyntax) -> AccessorListSyntax {
34-
fatalError("unimplemented")
35-
}
36-
3717
/// Describes the kinds of errors that can occur within a macro system.
3818
enum MacroSystemError: Error {
3919
/// Indicates that a macro with the given name has already been defined.
@@ -155,7 +135,7 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
155135
if let expansion = item.item.asProtocol(FreestandingMacroExpansionSyntax.self),
156136
let macro = macroSystem.macros[expansion.macro.text]
157137
{
158-
func _expand(expansion: some FreestandingMacroExpansionSyntax) throws {
138+
do {
159139
if let macro = macro as? CodeItemMacro.Type {
160140
let expandedItemList = try macro.expansion(
161141
of: expansion,
@@ -186,9 +166,6 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
186166
)
187167
newItems.append(CodeBlockItemSyntax(item: .init(expandedExpr)))
188168
}
189-
}
190-
do {
191-
try _openExistential(expansion, do: _expand)
192169
} catch {
193170
context.addDiagnostics(from: error, node: node)
194171
}
@@ -493,27 +470,6 @@ extension MacroApplication {
493470
)
494471
}
495472

496-
private func expandMemberAttribute(
497-
attribute: AttributeSyntax,
498-
macro: MemberAttributeMacro.Type,
499-
decl: DeclGroupSyntax,
500-
member: DeclSyntax,
501-
in context: MacroExpansionContext
502-
) throws -> [AttributeSyntax] {
503-
#if false
504-
_openExistential(decl) { d in
505-
return try! macro.expansion(
506-
of: attribute,
507-
attachedTo: d,
508-
annotating: member,
509-
in: context
510-
)
511-
}
512-
#else
513-
return []
514-
#endif
515-
}
516-
517473
private func expandAttributes(
518474
for macroAttributes: [(AttributeSyntax, MemberAttributeMacro.Type)],
519475
attachedTo decl: DeclSyntax,
@@ -527,18 +483,13 @@ extension MacroApplication {
527483
for (attribute, attributeMacro) in macroAttributes {
528484
do {
529485
let typedDecl = decl.asProtocol(DeclGroupSyntax.self)!
530-
531-
func expand(_ decl: some DeclGroupSyntax) throws -> [AttributeSyntax] {
532-
return try attributeMacro.expansion(
486+
attributes.append(
487+
contentsOf: try attributeMacro.expansion(
533488
of: attribute,
534-
attachedTo: decl,
489+
attachedTo: typedDecl,
535490
providingAttributesFor: member.decl,
536491
in: context
537492
)
538-
}
539-
540-
attributes.append(
541-
contentsOf: try _openExistential(typedDecl, do: expand)
542493
)
543494
} catch {
544495
context.addDiagnostics(from: error, node: attribute)

Sources/SwiftSyntaxMacros/MacroSystem.swift

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
134134
if let expansion = item.item.asProtocol(FreestandingMacroExpansionSyntax.self),
135135
let macro = macroSystem.macros[expansion.macro.text]
136136
{
137-
func _expand(expansion: some FreestandingMacroExpansionSyntax) throws {
137+
do {
138138
if let macro = macro as? CodeItemMacro.Type {
139139
let expandedItemList = try macro.expansion(
140140
of: expansion,
@@ -165,9 +165,6 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
165165
)
166166
newItems.append(CodeBlockItemSyntax(item: .init(expandedExpr)))
167167
}
168-
}
169-
do {
170-
try _openExistential(expansion, do: _expand)
171168
} catch {
172169
context.addDiagnostics(from: error, node: node)
173170
}
@@ -472,27 +469,6 @@ extension MacroApplication {
472469
)
473470
}
474471

475-
private func expandMemberAttribute(
476-
attribute: AttributeSyntax,
477-
macro: MemberAttributeMacro.Type,
478-
decl: DeclGroupSyntax,
479-
member: DeclSyntax,
480-
in context: MacroExpansionContext
481-
) throws -> [AttributeSyntax] {
482-
#if false
483-
_openExistential(decl) { d in
484-
return try! macro.expansion(
485-
of: attribute,
486-
attachedTo: d,
487-
annotating: member,
488-
in: context
489-
)
490-
}
491-
#else
492-
return []
493-
#endif
494-
}
495-
496472
private func expandAttributes(
497473
for macroAttributes: [(AttributeSyntax, MemberAttributeMacro.Type)],
498474
attachedTo decl: DeclSyntax,
@@ -506,18 +482,13 @@ extension MacroApplication {
506482
for (attribute, attributeMacro) in macroAttributes {
507483
do {
508484
let typedDecl = decl.asProtocol(DeclGroupSyntax.self)!
509-
510-
func expand(_ decl: some DeclGroupSyntax) throws -> [AttributeSyntax] {
511-
return try attributeMacro.expansion(
485+
attributes.append(
486+
contentsOf: try attributeMacro.expansion(
512487
of: attribute,
513-
attachedTo: decl,
488+
attachedTo: typedDecl,
514489
providingAttributesFor: member.decl,
515490
in: context
516491
)
517-
}
518-
519-
attributes.append(
520-
contentsOf: try _openExistential(typedDecl, do: expand)
521492
)
522493
} catch {
523494
context.addDiagnostics(from: error, node: attribute)

0 commit comments

Comments
 (0)