[swift-dev] Strange DispatchTime bug

Guillaume Lessard glessard at tffenterprises.com
Sat Aug 26 14:21:21 CDT 2017


On Linux (Ubuntu 16.04) and swift distributions from 3.0.2 to the latest 4.0 snapshot, I’m seeing incorrect behaviour involving DispatchTime.distantFuture:
(also: https://bugs.swift.org/browse/SR-5706)

***
import Dispatch

let now = DispatchTime.now()
let future = DispatchTime.distantFuture
assert(now != future, "this is not the distant future")

print(String(now.rawValue, radix: 16))                  // 1c20d6bbd3a (e.g.)
print(String(future.rawValue, radix: 16))               // ffffffffffffffff

print(now < future)                                     // false (incorrect)
print(future < now)                                     // false

print(now > future)                                     // false
print(future > now)                                     // false (incorrect)

extension DispatchTime {
  public static func compare(_ a: DispatchTime, happensBefore b: DispatchTime) -> Bool {
    return a.rawValue < b.rawValue
  }
}

print(DispatchTime.compare(now, happensBefore: future)) // true
print(DispatchTime.compare(future, happensBefore: now)) // false
***

Note that the static func I defined above is essentially the same as the less-than operator as defined in the libdispatch overlay: <https://github.com/apple/swift/blob/92f750aa3c3c4dce47eb55068850f3d1127b16bd/stdlib/public/SDK/Dispatch/Time.swift#L73>

Also, there is a test that should catch this problem:<https://github.com/apple/swift/blob/92f750aa3c3c4dce47eb55068850f3d1127b16bd/test/stdlib/Dispatch.swift#L104>


Could this operator func somehow be called correctly during testing, but incorrectly at runtime?
(In other words, I don’t see where the fix needs to be.)

Thanks,
Guillaume Lessard



More information about the swift-dev mailing list