Skip to content

Commit 9902575

Browse files
committed
Create a dedicated type for the rootId of a tree
Passing a `UInt` around has always felt a little unsafe and didn’t give any semantic meaning that describes what the `UInt` represents.
1 parent e99a573 commit 9902575

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Sources/SwiftSyntax/SyntaxChildren.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public struct SyntaxChildrenIndex: Hashable, Comparable, ExpressibleByNilLiteral
102102

103103
fileprivate extension AbsoluteSyntaxInfo {
104104
/// Construct `AbsoluteSyntaxInfo` from the given index data and a `rootId`.
105-
init(index: SyntaxChildrenIndexData, rootId: UInt) {
105+
init(index: SyntaxChildrenIndexData, rootId: RootID) {
106106
let position = AbsoluteSyntaxPosition(
107107
offset: index.offset,
108108
indexInParent: index.indexInParent
@@ -152,7 +152,7 @@ struct RawSyntaxChildren: BidirectionalCollection {
152152
}
153153

154154
/// The rootId of the tree the child nodes belong to
155-
private let rootId: UInt
155+
private let rootId: RootID
156156
/// The number of children in `parent`. Cached to avoid reaching into `parent` for every index
157157
/// advancement
158158
// FIXME: Do we need this cached?

Sources/SwiftSyntax/SyntaxData.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ public struct SyntaxIndexInTree: Comparable, Hashable {
9393
}
9494
}
9595

96+
/// Uniquely identifies a syntax tree.
97+
struct RootID: Hashable {
98+
/// The pointer value of the root.
99+
///
100+
/// Since the root ID might outlive the tree, it is not unsafe to access this.
101+
private var id: UInt
102+
103+
fileprivate init(_ pointer: UnsafeRawPointer) {
104+
self.id = UInt(bitPattern: pointer)
105+
}
106+
}
107+
96108
/// Provides a stable and unique identity for ``Syntax`` nodes.
97109
///
98110
/// Note that two nodes might have the same contents even if their IDs are
@@ -111,7 +123,7 @@ public struct SyntaxIdentifier: Hashable {
111123
/// same instance. This guarantees that the trees with the same 'rootId' have
112124
/// exact the same structure. But, two trees with exactly the same structure
113125
/// might still have different 'rootId's.
114-
let rootId: UInt
126+
let rootId: RootID
115127
/// Unique value for a node within its own tree.
116128
@_spi(RawSyntax)
117129
public let indexInTree: SyntaxIndexInTree
@@ -128,7 +140,7 @@ public struct SyntaxIdentifier: Hashable {
128140

129141
static func forRoot(_ raw: RawSyntax) -> SyntaxIdentifier {
130142
return .init(
131-
rootId: UInt(bitPattern: raw.pointer),
143+
rootId: RootID(raw.pointer),
132144
indexInTree: .zero
133145
)
134146
}
@@ -163,7 +175,7 @@ struct AbsoluteRawSyntax {
163175
return nil
164176
}
165177

166-
func replacingSelf(_ newRaw: RawSyntax, newRootId: UInt) -> AbsoluteRawSyntax {
178+
func replacingSelf(_ newRaw: RawSyntax, newRootId: RootID) -> AbsoluteRawSyntax {
167179
let nodeId = SyntaxIdentifier(rootId: newRootId, indexInTree: info.nodeId.indexInTree)
168180
let newInfo = AbsoluteSyntaxInfo(position: info.position, nodeId: nodeId)
169181
return .init(raw: newRaw, info: newInfo)

0 commit comments

Comments
 (0)