Skip to content

Commit 7f8e46c

Browse files
committed
[swiftSyntax] Make AbsolutePosition a value type
AbsolutePosition had value semantics anyway, the only reason it was a reference type was so that we can use it in AtomicCache. But that can be worked around by boxing it into a reference type.
1 parent d962c83 commit 7f8e46c

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

tools/SwiftSyntax/AbsolutePosition.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
/// An absolute position in a source file as text - the absolute utf8Offset from
1414
/// the start, line, and column.
15-
public final class AbsolutePosition {
15+
public struct AbsolutePosition {
1616
public let utf8Offset: Int
1717
public let line: Int
1818
public let column: Int

tools/SwiftSyntax/SyntaxData.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ import Foundation
1818
/// exposed to clients.
1919
typealias NodeIdentifier = [Int]
2020

21+
/// Box a value type into a reference type
22+
fileprivate class Box<T> {
23+
let value: T
24+
25+
init(_ value: T) {
26+
self.value = value
27+
}
28+
}
29+
2130
/// SyntaxData is the underlying storage for each Syntax node.
2231
/// It's modelled as an array that stores and caches a SyntaxData for each raw
2332
/// syntax node in its layout. It is up to the specific Syntax nodes to maintain
@@ -38,7 +47,7 @@ final class SyntaxData: Equatable {
3847

3948
let childCaches: [AtomicCache<SyntaxData>]
4049

41-
let positionCache: AtomicCache<AbsolutePosition>
50+
private let positionCache: AtomicCache<Box<AbsolutePosition>>
4251

4352
fileprivate func calculatePosition() -> AbsolutePosition {
4453
guard let parent = parent else {
@@ -62,7 +71,7 @@ final class SyntaxData: Equatable {
6271

6372
/// The position of the start of this node's leading trivia
6473
var position: AbsolutePosition {
65-
return positionCache.value { return calculatePosition() }
74+
return positionCache.value({ return Box(calculatePosition()) }).value
6675
}
6776

6877
/// The position of the start of this node's content, skipping its trivia
@@ -93,7 +102,7 @@ final class SyntaxData: Equatable {
93102
self.indexInParent = indexInParent
94103
self.parent = parent
95104
self.childCaches = raw.layout.map { _ in AtomicCache<SyntaxData>() }
96-
self.positionCache = AtomicCache<AbsolutePosition>()
105+
self.positionCache = AtomicCache<Box<AbsolutePosition>>()
97106
}
98107

99108
/// The index path from this node to the root. This can be used to uniquely

0 commit comments

Comments
 (0)