Skip to content

Commit 9154c42

Browse files
author
David Ungar
committed
Make nodes comparable
1 parent e51da14 commit 9154c42

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

Sources/SwiftDriver/Incremental Compilation/DependencyKey.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import Foundation
33

44
/// A filename from another module
5-
struct ExternalDependency: Hashable, CustomStringConvertible {
5+
struct ExternalDependency: Hashable, Comparable, CustomStringConvertible {
66
let fileName: String
77

88
var file: VirtualPath? {
@@ -14,6 +14,10 @@ struct ExternalDependency: Hashable, CustomStringConvertible {
1414
public var description: String {
1515
fileName.description
1616
}
17+
18+
public static func < (lhs: Self, rhs: Self) -> Bool {
19+
lhs.fileName < rhs.fileName
20+
}
1721
}
1822

1923

@@ -31,7 +35,7 @@ public struct DependencyKey: Hashable, CustomStringConvertible {
3135
/// implementations white. Each node holds an instance variable describing which
3236
/// aspect of the entity it represents.
3337

34-
enum DeclAspect {
38+
enum DeclAspect: Comparable {
3539
case interface, implementation
3640
}
3741

@@ -112,3 +116,15 @@ var correspondingImplementation: Self? {
112116
}
113117
}
114118

119+
// MARK: - Comparing
120+
/// Needed to sort nodes to make tracing deterministic to test against emitted diagnostics
121+
extension DependencyKey: Comparable {
122+
public static func < (lhs: Self, rhs: Self) -> Bool {
123+
lhs.aspect != rhs.aspect ? lhs.aspect < rhs.aspect :
124+
lhs.designator < rhs.designator
125+
}
126+
}
127+
128+
extension DependencyKey.Designator: Comparable {
129+
}
130+

Sources/SwiftDriver/Incremental Compilation/ModuleDependencyGraph Parts/Node.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ extension ModuleDependencyGraph.Node: Equatable, Hashable {
8080
}
8181
}
8282

83+
extension ModuleDependencyGraph.Node: Comparable {
84+
static func < (lhs: ModuleDependencyGraph.Node, rhs: ModuleDependencyGraph.Node) -> Bool {
85+
func lt<T: Comparable> (_ a: T?, _ b: T?) -> Bool {
86+
switch (a, b) {
87+
case let (x?, y?): return x < y
88+
case (nil, nil): return false
89+
case (nil, _?): return true
90+
case (_?, nil): return false
91+
}
92+
}
93+
return lhs.dependencyKey != rhs.dependencyKey ? lhs.dependencyKey < rhs.dependencyKey :
94+
lhs.swiftDeps != rhs.swiftDeps ? lt(lhs.swiftDeps, rhs.swiftDeps)
95+
: lt(lhs.fingerprint, rhs.fingerprint)
96+
}
97+
}
98+
8399

84100
extension ModuleDependencyGraph.Node: CustomStringConvertible {
85101
public var description: String {

Sources/SwiftDriver/Incremental Compilation/ModuleDependencyGraph Parts/SwiftDeps.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@ extension ModuleDependencyGraph.SwiftDeps {
4646
file.name
4747
}
4848
}
49+
// MARK: - comparing
50+
extension ModuleDependencyGraph.SwiftDeps: Comparable {
51+
static func < (lhs: Self, rhs: Self) -> Bool {
52+
lhs.file.name < rhs.file.name
53+
}
54+
}

0 commit comments

Comments
 (0)