Skip to content

Commit 077bd71

Browse files
committed
Attempt to get this compiling with Swift 5.5
1 parent 83012a5 commit 077bd71

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,25 @@ func evaluateMacro(
167167
}
168168

169169
macroName = parentExpansion.macro.text
170-
evaluatedSyntax = Syntax(
171-
try exprMacro.expansion(
170+
171+
func expandExpressionMacro<Node: FreestandingMacroExpansionSyntax>(
172+
_ node: Node
173+
) throws -> ExprSyntax {
174+
return try exprMacro.expansion(
172175
of: sourceManager.detach(
173-
parentExpansion, in: context,
176+
node, in: context,
174177
foldingWith: OperatorTable.standardOperators
175178
),
176179
in: context
177180
)
181+
}
182+
183+
evaluatedSyntax = Syntax(
184+
try _openExistential(parentExpansion, do: expandExpressionMacro)
178185
)
179186

180-
// Handle expression macro. The resulting decls are wrapped in a `CodeBlockItemListSyntax`.
187+
// Handle declaration macro. The resulting decls are wrapped in a
188+
// `CodeBlockItemListSyntax`.
181189
case let declMacro as DeclarationMacro.Type:
182190
guard let parentExpansion = parentSyntax.as(MacroExpansionDeclSyntax.self) else {
183191
print("not on a macro expansion node: \(token.recursiveDescription)")
@@ -354,11 +362,23 @@ func expandAttachedMacro(
354362
return 1
355363
}
356364

357-
let attributes = try attachedMacro.expansion(
358-
of: context.detach(customAttrNode),
359-
attachedTo: context.detach(parentDeclGroup),
360-
providingAttributesFor: context.detach(declarationNode),
361-
in: context
365+
// Local function to expand a member atribute macro once we've opened up
366+
// the existential.
367+
func expandMemberAttributeMacro<Node: DeclGroupSyntax>(
368+
_ node: Node
369+
) throws -> [AttributeSyntax] {
370+
return try attachedMacro.expansion(
371+
of: context.detach(customAttrNode),
372+
attachedTo: sourceManager.detach(node, in: context),
373+
providingAttributesFor: sourceManager.detach(
374+
declarationNode, in: context
375+
),
376+
in: context
377+
)
378+
}
379+
380+
let attributes = try _openExistential(
381+
parentDeclGroup, do: expandMemberAttributeMacro
362382
)
363383

364384
// Form a buffer containing an attribute list to return to the caller.
@@ -372,11 +392,19 @@ func expandAttachedMacro(
372392
return 1
373393
}
374394

375-
let members = try attachedMacro.expansion(
376-
of: context.detach(customAttrNode),
377-
providingMembersOf: context.detach(declGroup),
378-
in: context
379-
)
395+
// Local function to expand a member macro once we've opened up
396+
// the existential.
397+
func expandMemberMacro<Node: DeclGroupSyntax>(
398+
_ node: Node
399+
) throws -> [DeclSyntax] {
400+
return try attachedMacro.expansion(
401+
of: sourceManager.detach(customAttrNode, in: context),
402+
providingMembersOf: sourceManager.detach(node, in: context),
403+
in: context
404+
)
405+
}
406+
407+
let members = try _openExistential(declGroup, do: expandMemberMacro)
380408

381409
// Form a buffer of member declarations to return to the caller.
382410
evaluatedSyntaxStr = members.map {

lib/ASTGen/Sources/ASTGen/SourceManager+Diagnostics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import SwiftDiagnostics
33
import SwiftSyntax
44

55
extension SourceManager {
6-
private func diagnoseSingle(
6+
private func diagnoseSingle<Node: SyntaxProtocol>(
77
message: String,
88
severity: DiagnosticSeverity,
9-
node: some SyntaxProtocol,
9+
node: Node,
1010
position: AbsolutePosition,
1111
highlights: [Syntax] = [],
1212
fixItChanges: [FixIt.Change] = []

lib/ASTGen/Sources/ASTGen/SourceManager.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ extension SourceManager {
6060

6161
/// Find the root source file and offset from within that file for the given
6262
/// syntax node.
63-
func rootSourceFile(
64-
of node: some SyntaxProtocol
63+
func rootSourceFile<Node: SyntaxProtocol>(
64+
of node: Node
6565
) -> (SourceFileSyntax, AbsolutePosition)? {
6666
let root = node.root
6767

@@ -87,8 +87,8 @@ extension SourceManager {
8787

8888
/// Produce the C++ source location for a given position based on a
8989
/// syntax node.
90-
func cxxSourceLocation(
91-
for node: some SyntaxProtocol,
90+
func cxxSourceLocation<Node: SyntaxProtocol>(
91+
for node: Node,
9292
at position: AbsolutePosition? = nil
9393
) -> CxxSourceLoc? {
9494
// Find the source file and this node's position within it.

0 commit comments

Comments
 (0)