Skip to content

Keep 'SyntaxArena's of rewritten children alive. #803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Sources/SwiftSyntax/SyntaxRewriter.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ open class SyntaxRewriter {
// nodes are being collected.
var newLayout: ContiguousArray<RawSyntax?>?

// Rewritten children just to keep their 'SyntaxArena' alive until they are
// wrapped with 'Syntax'
var rewrittens: ContiguousArray<Syntax> = []

let syntaxNode = node._syntaxNode

// Incrementing i manually is faster than using .enumerated()
Expand Down Expand Up @@ -237,6 +241,7 @@ open class SyntaxRewriter {

// Now that we know we have a new layout in which we collect rewritten
// nodes, add it.
rewrittens.append(rewritten)
newLayout!.append(rewritten.raw)
} else {
// The node was not changed by the rewriter. Only store it if a previous
Expand All @@ -249,13 +254,15 @@ open class SyntaxRewriter {

if let newLayout = newLayout {
// A child node was rewritten. Build the updated node.

// Sanity check, ensure the new children are the same length.
assert(newLayout.count == node.raw.layoutView!.children.count)

let newRaw = node.raw.layoutView!.replacingLayout(with: Array(newLayout), arena: .default)
let newNode = SyntaxType(Syntax(SyntaxData.forRoot(newRaw)))!
return newNode
// 'withExtendedLifetime' to keep 'SyntaxArena's of them alive until here.
return withExtendedLifetime(rewrittens) {
SyntaxType(Syntax(SyntaxData.forRoot(newRaw)))!
}
} else {
// No child node was rewritten. So no need to change this node as well.
return node
Expand Down
15 changes: 11 additions & 4 deletions Sources/SwiftSyntax/gyb_generated/SyntaxRewriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5870,6 +5870,10 @@ open class SyntaxRewriter {
// nodes are being collected.
var newLayout: ContiguousArray<RawSyntax?>?

// Rewritten children just to keep their 'SyntaxArena' alive until they are
// wrapped with 'Syntax'
var rewrittens: ContiguousArray<Syntax> = []

let syntaxNode = node._syntaxNode

// Incrementing i manually is faster than using .enumerated()
Expand Down Expand Up @@ -5908,6 +5912,7 @@ open class SyntaxRewriter {

// Now that we know we have a new layout in which we collect rewritten
// nodes, add it.
rewrittens.append(rewritten)
newLayout!.append(rewritten.raw)
} else {
// The node was not changed by the rewriter. Only store it if a previous
Expand All @@ -5920,13 +5925,15 @@ open class SyntaxRewriter {

if let newLayout = newLayout {
// A child node was rewritten. Build the updated node.

// Sanity check, ensure the new children are the same length.
assert(newLayout.count == node.raw.layoutView!.children.count)

let newRaw = node.raw.layoutView!.replacingLayout(with: Array(newLayout), arena: .default)
let newNode = SyntaxType(Syntax(SyntaxData.forRoot(newRaw)))!
return newNode
// 'withExtendedLifetime' to keep 'SyntaxArena's of them alive until here.
return withExtendedLifetime(rewrittens) {
SyntaxType(Syntax(SyntaxData.forRoot(newRaw)))!
}
} else {
// No child node was rewritten. So no need to change this node as well.
return node
Expand Down
22 changes: 22 additions & 0 deletions Tests/SwiftParserTest/StringInterpolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,26 @@ final class StringInterpolationTests: XCTestCase {
"""
XCTAssertTrue(structNode.is(StructDeclSyntax.self))
}

func testRewriter() throws {
let sourceFile = try Parser.parse(source: """
class Foo {
func method() {}
}
""")
class Rewriter: SyntaxRewriter {
override func visit(_ node: FunctionDeclSyntax) -> DeclSyntax {
let newFunc = DeclSyntax("func newName() {}")
.withLeadingTrivia(node.leadingTrivia!)
.withTrailingTrivia(node.trailingTrivia!)
return DeclSyntax(newFunc)
}
}
let rewrittenSourceFile = Rewriter().visit(sourceFile)
XCTAssertEqual(rewrittenSourceFile.description, """
class Foo {
func newName() {}
}
""")
}
}