mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[Macros] Add an overload of findSyntaxNodeInSourceFile that accepts
a predicate. The new overload is used during macro expansion to find the nearest syntax node that is either a `DeclSyntax` or a `ClosureExprSyntax`. This avoids an issue where calling the overload that accepts a specific type may find an outer, unrelated declaration or closure.
This commit is contained in:
@@ -583,22 +583,15 @@ func expandAttachedMacro(
|
||||
return 1
|
||||
}
|
||||
|
||||
func findNode<T: SyntaxProtocol>(type: T.Type) -> T? {
|
||||
findSyntaxNodeInSourceFile(
|
||||
sourceFilePtr: declarationSourceFilePtr,
|
||||
sourceLocationPtr: declarationSourceLocPointer,
|
||||
type: T.self
|
||||
)
|
||||
}
|
||||
|
||||
// Dig out the node for the closure or declaration to which the custom
|
||||
// attribute is attached.
|
||||
let node: Syntax
|
||||
if let closureNode = findNode(type: ClosureExprSyntax.self) {
|
||||
node = Syntax(closureNode)
|
||||
} else if let declNode = findNode(type: DeclSyntax.self) {
|
||||
node = Syntax(declNode)
|
||||
} else {
|
||||
let node = findSyntaxNodeInSourceFile(
|
||||
sourceFilePtr: declarationSourceFilePtr,
|
||||
sourceLocationPtr: declarationSourceLocPointer,
|
||||
where: { $0.is(DeclSyntax.self) || $0.is(ClosureExprSyntax.self) }
|
||||
)
|
||||
|
||||
guard let node else {
|
||||
return 1
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user