Skip to content

Commit 2441d19

Browse files
committed
Extend lifetime of children in SyntaxCollection initializer
We were already doing this for layout nodes but we forgot to do it for collection nodes.
1 parent 7116773 commit 2441d19

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

Sources/SwiftSyntax/SyntaxCollection.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ extension SyntaxCollection {
3535

3636
public init(_ children: [Element]) {
3737
let arena = SyntaxArena()
38-
let raw = RawSyntax.makeLayout(
39-
kind: Self.syntaxKind,
40-
from: children.map { $0.raw },
41-
arena: arena
42-
)
38+
// Extend the lifetime of children so their arenas don't get destroyed
39+
// before they can be added as children of the new arena.
40+
let raw = withExtendedLifetime(children) {
41+
RawSyntax.makeLayout(
42+
kind: Self.syntaxKind,
43+
from: children.map { $0.raw },
44+
arena: arena
45+
)
46+
}
4347
self.init(SyntaxData.forRoot(raw, rawNodeArena: arena))
4448
}
4549

Sources/SwiftSyntax/SyntaxData.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,13 @@ struct SyntaxData {
273273
self.init(absoluteRaw.raw, parent: parent.data, absoluteInfo: absoluteRaw.info)
274274
}
275275

276-
/// Creates a `SyntaxData` for a root raw node.
276+
/// Creates a ``SyntaxData`` for a root raw node.
277277
///
278-
/// `arena` must be the arena in which `raw` is allocated. It is passed to
279-
/// make sure the arena doesn’t get de-allocated before the ``SyntaxData`` has
280-
/// a chance to retain it.
278+
/// - Parameters:
279+
/// - raw: The raw node that will be the root of the the tree
280+
/// - rawNodeArena: The arena in which `raw` is allocated. It is passed to
281+
/// make sure the arena doesn’t get de-allocated before the ``SyntaxData``
282+
/// has a chance to retain it.
281283
static func forRoot(_ raw: RawSyntax, rawNodeArena: SyntaxArena) -> SyntaxData {
282284
precondition(rawNodeArena === raw.arena)
283285
return SyntaxData(raw, info: .root(.init(arena: rawNodeArena)))
@@ -307,7 +309,7 @@ struct SyntaxData {
307309
/// - Parameters:
308310
/// - newRaw: The node that should replace `self`
309311
/// - rawNodeArena: The arena in which `newRaw` resides
310-
/// - arena: The arena in which new nodes should be allocated
312+
/// - allocationArena: The arena in which new nodes should be allocated
311313
/// - Returns: A syntax tree with all parents where this node has been
312314
/// replaced by `newRaw`
313315
func replacingSelf(_ newRaw: RawSyntax, rawNodeArena: SyntaxArena, allocationArena: SyntaxArena) -> SyntaxData {

0 commit comments

Comments
 (0)