Skip to content

Commit 157d25f

Browse files
committed
Use RawSyntax address as the 'rootId' of SyntaxIdentifier
We don't need to guarantee the uniqueness throughout the process lifetime. but only need to guarantee the uniqueness while the object is alive.
1 parent 2efeb95 commit 157d25f

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Diff for: Sources/SwiftSyntax/SyntaxChildren.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public struct SyntaxChildrenIndex: Comparable, ExpressibleByNilLiteral {
9191

9292
fileprivate extension AbsoluteSyntaxInfo {
9393
/// Construct `AbsoluteSyntaxInfo` from the given index data and a `rootId`.
94-
init(index: SyntaxChildrenIndexData, rootId: UInt32) {
94+
init(index: SyntaxChildrenIndexData, rootId: UInt) {
9595
let position = AbsoluteSyntaxPosition(offset: index.offset,
9696
indexInParent: index.indexInParent)
9797
let identifier = SyntaxIdentifier(rootId: rootId,
@@ -137,7 +137,7 @@ struct RawSyntaxChildren: BidirectionalCollection {
137137
}
138138

139139
/// The rootId of the tree the child nodes belong to
140-
private let rootId: UInt32
140+
private let rootId: UInt
141141
/// The number of childer in `parent`. Cached to avoid reaching into `parent` for every index
142142
/// advancement
143143
// FIXME: Do we need this cached?

Diff for: Sources/SwiftSyntax/SyntaxData.swift

+13-8
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ struct AbsoluteSyntaxInfo {
6464
return .init(position: newPosition, nodeId: newNodeId)
6565
}
6666

67-
static var forRoot: AbsoluteSyntaxInfo {
68-
return .init(position: .forRoot, nodeId: .newRoot())
67+
static func forRoot(_ raw: RawSyntax) -> AbsoluteSyntaxInfo {
68+
return .init(position: .forRoot, nodeId: .forRoot(raw))
6969
}
7070
}
7171

@@ -103,8 +103,13 @@ struct SyntaxIndexInTree: Hashable {
103103

104104
/// Provides a stable and unique identity for `Syntax` nodes.
105105
public struct SyntaxIdentifier: Hashable {
106-
/// Unique value for each root node created.
107-
let rootId: UInt32
106+
/// Unique value for the root node.
107+
///
108+
/// Multiple trees may have the same 'rootId' if their root RawSyntax is the
109+
/// same instance. This guarantees that the trees with the same 'rootId' have
110+
/// exact the same structure. But, two trees with exactly the same structure
111+
/// might still have different 'rootId's.
112+
let rootId: UInt
108113
/// Unique value for a node within its own tree.
109114
let indexInTree: SyntaxIndexInTree
110115

@@ -123,8 +128,8 @@ public struct SyntaxIdentifier: Hashable {
123128
return .init(rootId: self.rootId, indexInTree: newIndexInTree)
124129
}
125130

126-
static func newRoot() -> SyntaxIdentifier {
127-
return .init(rootId: UInt32(truncatingIfNeeded: AtomicCounter.next()),
131+
static func forRoot(_ raw: RawSyntax) -> SyntaxIdentifier {
132+
return .init(rootId: UInt(bitPattern: raw.pointer),
128133
indexInTree: .zero)
129134
}
130135
}
@@ -168,14 +173,14 @@ struct AbsoluteRawSyntax {
168173
return nil
169174
}
170175

171-
func replacingSelf(_ newRaw: RawSyntax, newRootId: UInt32) -> AbsoluteRawSyntax {
176+
func replacingSelf(_ newRaw: RawSyntax, newRootId: UInt) -> AbsoluteRawSyntax {
172177
let nodeId = SyntaxIdentifier(rootId: newRootId, indexInTree: info.nodeId.indexInTree)
173178
let newInfo = AbsoluteSyntaxInfo(position: info.position, nodeId: nodeId)
174179
return .init(raw: newRaw, info: newInfo)
175180
}
176181

177182
static func forRoot(_ raw: RawSyntax) -> AbsoluteRawSyntax {
178-
return .init(raw: raw, info: .forRoot)
183+
return .init(raw: raw, info: .forRoot(raw))
179184
}
180185
}
181186

0 commit comments

Comments
 (0)