Skip to content

Commit e2d4423

Browse files
committed
[SyntaxRewriter] Make 'visitChildren()' a non-generic mehod
This was a generic function only for casting the result at the end. Hoist the casting part to the caller and make 'visitChilden()' non-generic.
1 parent e664473 commit e2d4423

File tree

2 files changed

+295
-303
lines changed

2 files changed

+295
-303
lines changed

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxRewriterFile.swift

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
160160
/// - Returns: the rewritten node
161161
\(node.apiAttributes())\
162162
open func visit(_ node: \(node.kind.syntaxType)) -> \(node.kind.syntaxType) {
163-
return visitChildren(node)
163+
return visitChildren(node._syntaxNode)?.cast(\(node.kind.syntaxType).self) ?? node
164164
}
165165
"""
166166
)
@@ -172,7 +172,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
172172
/// - Returns: the rewritten node
173173
\(node.apiAttributes())\
174174
open func visit(_ node: \(node.kind.syntaxType)) -> \(node.baseType.syntaxBaseName) {
175-
return \(node.baseType.syntaxBaseName)(visitChildren(node))
175+
return \(node.baseType.syntaxBaseName)(visitChildren(node._syntaxNode)?.cast(\(node.kind.syntaxType).self) ?? node)
176176
}
177177
"""
178178
)
@@ -302,9 +302,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
302302

303303
DeclSyntax(
304304
"""
305-
private func visitChildren<SyntaxType: SyntaxProtocol>(
306-
_ node: SyntaxType
307-
) -> SyntaxType {
305+
private func visitChildren(_ node: Syntax) -> Syntax? {
308306
// Walk over all children of this node and rewrite them. Don't store any
309307
// rewritten nodes until the first non-`nil` value is encountered. When this
310308
// happens, retrieve all previous syntax nodes from the parent node to
@@ -320,11 +318,9 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
320318
// with 'Syntax'
321319
var rewrittens: ContiguousArray<RetainedSyntaxArena> = []
322320
323-
let syntaxNode = node._syntaxNode
324-
325321
// Incrementing i manually is faster than using .enumerated()
326322
var childIndex = 0
327-
for (raw, info) in RawSyntaxChildren(syntaxNode) {
323+
for (raw, info) in RawSyntaxChildren(node) {
328324
defer { childIndex += 1 }
329325
330326
guard let child = raw, viewMode.shouldTraverse(node: child) else {
@@ -333,7 +329,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
333329
}
334330
335331
// Build the Syntax node to rewrite
336-
var childNode = nodeFactory.create(parent: syntaxNode, raw: child, absoluteInfo: info)
332+
var childNode = nodeFactory.create(parent: node, raw: child, absoluteInfo: info)
337333
338334
dispatchVisit(&childNode)
339335
if childNode.raw.id != child.id {
@@ -365,11 +361,11 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
365361
newLayout.deallocate()
366362
// 'withExtendedLifetime' to keep 'SyntaxArena's of them alive until here.
367363
return withExtendedLifetime(rewrittens) {
368-
Syntax(raw: newRaw, rawNodeArena: arena).cast(SyntaxType.self)
364+
Syntax(raw: newRaw, rawNodeArena: arena)
369365
}
370366
} else {
371367
// No child node was rewritten. So no need to change this node as well.
372-
return node
368+
return nil
373369
}
374370
}
375371
"""

0 commit comments

Comments
 (0)