Skip to content

Commit 136f6f2

Browse files
committed
[Macros] Look further up the syntax tree to find the type we need
1 parent fa817f3 commit 136f6f2

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,17 @@ private func findSyntaxNodeInSourceFile<Node: SyntaxProtocol>(
422422
return nil
423423
}
424424

425-
// Dig out its parent.
426-
guard let parentSyntax = token.parent else {
427-
print("not on a macro expansion node: \(token.recursiveDescription)")
428-
return nil
425+
var currentSyntax = Syntax(token)
426+
while let parentSyntax = currentSyntax.parent {
427+
if let typedParent = parentSyntax.as(type) {
428+
return typedParent
429+
}
430+
431+
currentSyntax = parentSyntax
429432
}
430433

431-
return parentSyntax.as(type)
434+
print("unable to find node: \(token.recursiveDescription)")
435+
return nil
432436
}
433437

434438
@_cdecl("swift_ASTGen_expandAttachedMacro")

0 commit comments

Comments
 (0)