Skip to content

Commit 301510e

Browse files
lilyballmoiseev
authored andcommitted
Fix dispatch time comparisons (#5078)
* [Dispatch] Don't crash when comparing DispatchWallTimes with < * [Dispatch] Make comparisons with distantFuture work as expected
1 parent d5fdd60 commit 301510e

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

stdlib/public/SDK/Dispatch/Time.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public struct DispatchTime : Comparable {
4545
}
4646

4747
public func <(a: DispatchTime, b: DispatchTime) -> Bool {
48-
if a.rawValue == ~0 || b.rawValue == ~0 { return false }
4948
return a.rawValue < b.rawValue
5049
}
5150

@@ -73,8 +72,12 @@ public struct DispatchWallTime : Comparable {
7372
}
7473

7574
public func <(a: DispatchWallTime, b: DispatchWallTime) -> Bool {
76-
if a.rawValue == ~0 || b.rawValue == ~0 { return false }
77-
return -Int64(a.rawValue) < -Int64(b.rawValue)
75+
if b.rawValue == ~0 {
76+
return a.rawValue != ~0
77+
} else if a.rawValue == ~0 {
78+
return false
79+
}
80+
return -Int64(bitPattern: a.rawValue) < -Int64(bitPattern: b.rawValue)
7881
}
7982

8083
public func ==(a: DispatchWallTime, b: DispatchWallTime) -> Bool {

test/stdlib/Dispatch.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,19 @@ DispatchAPI.test("dispatch_data_t deallocator") {
8585
expectEqual(1, t)
8686
}
8787
}
88+
89+
DispatchAPI.test("DispatchTime comparisons") {
90+
do {
91+
let now = DispatchTime.now()
92+
checkComparable([now, now + .milliseconds(1), .distantFuture], oracle: {
93+
return $0 < $1 ? .lt : $0 == $1 ? .eq : .gt
94+
})
95+
}
96+
97+
do {
98+
let now = DispatchWallTime.now()
99+
checkComparable([now, now + .milliseconds(1), .distantFuture], oracle: {
100+
return $0 < $1 ? .lt : $0 == $1 ? .eq : .gt
101+
})
102+
}
103+
}

0 commit comments

Comments
 (0)