Skip to content

Commit ab19ad2

Browse files
committed
[Macros] Look for specific syntax node kinds instead of Syntax when
expanding an attached macro. This fixes a crash in one of the attached macro tests.
1 parent 8ec70e2 commit ab19ad2

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

lib/ASTGen/Sources/MacroEvaluation/Macros.swift

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -583,15 +583,22 @@ func expandAttachedMacro(
583583
return 1
584584
}
585585

586-
// Dig out the node for the declaration to which the custom attribute is
587-
// attached.
588-
guard
589-
let declarationNode = findSyntaxNodeInSourceFile(
586+
func findNode<T: SyntaxProtocol>(type: T.Type) -> T? {
587+
findSyntaxNodeInSourceFile(
590588
sourceFilePtr: declarationSourceFilePtr,
591589
sourceLocationPtr: declarationSourceLocPointer,
592-
type: Syntax.self
590+
type: T.self
593591
)
594-
else {
592+
}
593+
594+
// Dig out the node for the closure or declaration to which the custom
595+
// attribute is attached.
596+
let node: Syntax
597+
if let closureNode = findNode(type: ClosureExprSyntax.self) {
598+
node = Syntax(closureNode)
599+
} else if let declNode = findNode(type: DeclSyntax.self) {
600+
node = Syntax(declNode)
601+
} else {
595602
return 1
596603
}
597604

@@ -622,7 +629,7 @@ func expandAttachedMacro(
622629
customAttrSourceFilePtr: customAttrSourceFilePtr,
623630
customAttrNode: customAttrNode,
624631
declarationSourceFilePtr: declarationSourceFilePtr,
625-
attachedTo: declarationNode,
632+
attachedTo: node,
626633
parentDeclSourceFilePtr: parentDeclSourceFilePtr,
627634
parentDeclNode: parentDeclNode
628635
)

0 commit comments

Comments
 (0)