@@ -19,12 +19,10 @@ public struct Syntax: SyntaxProtocol, SyntaxHashable {
1919 indirect case nonRoot( NonRoot )
2020
2121 // For root node.
22- struct Root : @unchecked Sendable {
23- // Unchecked conformance to sendable is fine because `arena` is not
24- // accessible. It is just used to keep the arena alive.
25- private var arena : SyntaxArena
22+ struct Root : Sendable {
23+ private var arena : RetainedSyntaxArena
2624
27- init ( arena: SyntaxArena ) {
25+ init ( arena: RetainedSyntaxArena ) {
2826 self . arena = arena
2927 }
3028 }
@@ -125,11 +123,16 @@ public struct Syntax: SyntaxProtocol, SyntaxHashable {
125123 /// - rawNodeArena: The arena in which `raw` is allocated. It is passed to
126124 /// make sure the arena doesn’t get de-allocated before the ``Syntax``
127125 /// has a chance to retain it.
128- static func forRoot( _ raw: RawSyntax , rawNodeArena: SyntaxArena ) -> Syntax {
126+ static func forRoot( _ raw: RawSyntax , rawNodeArena: RetainedSyntaxArena ) -> Syntax {
129127 precondition ( rawNodeArena == raw. arenaReference)
130128 return Syntax ( raw, info: . root( . init( arena: rawNodeArena) ) )
131129 }
132130
131+ static func forRoot( _ raw: RawSyntax , rawNodeArena: SyntaxArena ) -> Syntax {
132+ precondition ( rawNodeArena == raw. arenaReference)
133+ return Syntax ( raw, info: . root( . init( arena: RetainedSyntaxArena ( rawNodeArena) ) ) )
134+ }
135+
133136 /// Returns the child data at the provided index in this data's layout.
134137 /// - Note: This has O(n) performance, prefer using a proper Sequence type
135138 /// if applicable, instead of this.
@@ -157,7 +160,7 @@ public struct Syntax: SyntaxProtocol, SyntaxHashable {
157160 /// - allocationArena: The arena in which new nodes should be allocated
158161 /// - Returns: A syntax tree with all parents where this node has been
159162 /// replaced by `newRaw`
160- func replacingSelf( _ newRaw: RawSyntax , rawNodeArena: SyntaxArena , allocationArena: SyntaxArena ) -> Syntax {
163+ func replacingSelf( _ newRaw: RawSyntax , rawNodeArena: RetainedSyntaxArena , allocationArena: SyntaxArena ) -> Syntax {
161164 precondition ( newRaw. arenaReference == rawNodeArena)
162165 // If we have a parent already, then ask our current parent to copy itself
163166 // recursively up to the root.
@@ -182,45 +185,50 @@ public struct Syntax: SyntaxProtocol, SyntaxHashable {
182185 /// - Returns: The new root node created by this operation, and the new child
183186 /// syntax data.
184187 /// - SeeAlso: replacingSelf(_:)
185- func replacingChild( at index: Int , with newChild: RawSyntax ? , rawNodeArena: SyntaxArena ? , allocationArena: SyntaxArena ) -> Syntax {
188+ func replacingChild( at index: Int , with newChild: RawSyntax ? , rawNodeArena: RetainedSyntaxArena ? , allocationArena: SyntaxArena ) -> Syntax {
186189 precondition ( newChild == nil || ( rawNodeArena != nil && newChild!. arenaReference == rawNodeArena!) )
187190 // After newRaw has been allocated in `allocationArena`, `rawNodeArena` will
188191 // be a child arena of `allocationArena` and thus, `allocationArena` will
189192 // keep `newChild` alive.
190193 let newRaw = withExtendedLifetime ( rawNodeArena) {
191194 raw. layoutView!. replacingChild ( at: index, with: newChild, arena: allocationArena)
192195 }
193- return replacingSelf ( newRaw, rawNodeArena: allocationArena, allocationArena: allocationArena)
196+ return replacingSelf ( newRaw, rawNodeArena: RetainedSyntaxArena ( allocationArena) , allocationArena: allocationArena)
197+ }
198+
199+ /// Same as `replacingChild(at:with:rawNodeArena:allocationArena:)` but takes a `__SyntaxArena` instead of a `RetainedSyntaxArena`.
200+ func replacingChild( at index: Int , with newChild: RawSyntax ? , rawNodeArena: SyntaxArena ? , allocationArena: SyntaxArena ) -> Syntax {
201+ return self . replacingChild ( at: index, with: newChild, rawNodeArena: rawNodeArena. map ( RetainedSyntaxArena . init) , allocationArena: allocationArena)
194202 }
195203
196204 /// Identical to `replacingChild(at: Int, with: RawSyntax?, arena: SyntaxArena)`
197205 /// that ensures that the arena of`newChild` doesn’t get de-allocated before
198206 /// `newChild` has been addded to the result.
199207 func replacingChild( at index: Int , with newChild: Syntax ? , arena: SyntaxArena ) -> Syntax {
200208 return withExtendedLifetime ( newChild) {
201- return replacingChild ( at: index, with: newChild? . raw, rawNodeArena: newChild? . raw. arena , allocationArena: arena)
209+ return replacingChild ( at: index, with: newChild? . raw, rawNodeArena: newChild? . raw. arenaReference . retained , allocationArena: arena)
202210 }
203211 }
204212
205213 func withLeadingTrivia( _ leadingTrivia: Trivia , arena: SyntaxArena ) -> Syntax {
206214 if let raw = raw. withLeadingTrivia ( leadingTrivia, arena: arena) {
207- return replacingSelf ( raw, rawNodeArena: arena, allocationArena: arena)
215+ return replacingSelf ( raw, rawNodeArena: RetainedSyntaxArena ( arena) , allocationArena: arena)
208216 } else {
209217 return self
210218 }
211219 }
212220
213221 func withTrailingTrivia( _ trailingTrivia: Trivia , arena: SyntaxArena ) -> Syntax {
214222 if let raw = raw. withTrailingTrivia ( trailingTrivia, arena: arena) {
215- return replacingSelf ( raw, rawNodeArena: arena, allocationArena: arena)
223+ return replacingSelf ( raw, rawNodeArena: RetainedSyntaxArena ( arena) , allocationArena: arena)
216224 } else {
217225 return self
218226 }
219227 }
220228
221229 func withPresence( _ presence: SourcePresence , arena: SyntaxArena ) -> Syntax {
222230 if let raw = raw. tokenView? . withPresence ( presence, arena: arena) {
223- return replacingSelf ( raw, rawNodeArena: arena, allocationArena: arena)
231+ return replacingSelf ( raw, rawNodeArena: RetainedSyntaxArena ( arena) , allocationArena: arena)
224232 } else {
225233 return self
226234 }
@@ -234,10 +242,15 @@ public struct Syntax: SyntaxProtocol, SyntaxHashable {
234242 }
235243
236244 @_spi ( RawSyntax)
237- public init ( raw: RawSyntax , rawNodeArena: __shared SyntaxArena ) {
245+ public init ( raw: RawSyntax , rawNodeArena: __shared RetainedSyntaxArena ) {
238246 self = . forRoot( raw, rawNodeArena: rawNodeArena)
239247 }
240248
249+ @_spi ( RawSyntax)
250+ public init ( raw: RawSyntax , rawNodeArena: __shared SyntaxArena) {
251+ self = . forRoot( raw, rawNodeArena: RetainedSyntaxArena ( rawNodeArena) )
252+ }
253+
241254 /// Create a ``Syntax`` node from a specialized syntax node.
242255 public init ( _ syntax: some SyntaxProtocol ) {
243256 self = syntax. _syntaxNode
0 commit comments