Skip to content

Commit da9922d

Browse files
Avoid using generic static members on Duration across module boundaries (#73419)
* Avoid using generic static members on Duration across module boundaries Because the clocks are implemented in Concurrency, but Duration is in the Swift module, these don't get specialized. Add a fully-concrete internal init in Concurrency to avoid the problem. * Call self.init(_high:low:) explicitly. * Add availability annotation.
1 parent 102df47 commit da9922d

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

stdlib/public/Concurrency/ContinuousClock.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ public struct ContinuousClock: Sendable {
3434
public init() { }
3535
}
3636

37+
@available(SwiftStdlib 5.7, *)
38+
extension Duration {
39+
internal init(_seconds s: Int64, nanoseconds n: Int64) {
40+
let (secHi, secLo) = s.multipliedFullWidth(by: 1_000_000_000_000_000_000)
41+
// _nanoseconds is in 0 ..< 1_000_000_000, so the conversion to UInt64
42+
// and multiply cannot overflow. If you somehow trap here, it is because
43+
// the underlying clock hook that produced the time value is implemented
44+
// incorrectly on your platform, but because we trap we can't silently
45+
// get bogus data.
46+
let (low, carry) = secLo.addingReportingOverflow(UInt64(n) * 1_000_000_000)
47+
let high = secHi &+ (carry ? 1 : 0)
48+
self.init(_high: high, low: low)
49+
}
50+
}
51+
3752
@available(SwiftStdlib 5.7, *)
3853
extension Clock where Self == ContinuousClock {
3954
/// A clock that measures time that always increments but does not stop
@@ -60,7 +75,7 @@ extension ContinuousClock: Clock {
6075
seconds: &seconds,
6176
nanoseconds: &nanoseconds,
6277
clock: _ClockID.continuous.rawValue)
63-
return .seconds(seconds) + .nanoseconds(nanoseconds)
78+
return Duration(_seconds: seconds, nanoseconds: nanoseconds)
6479
}
6580

6681
/// The current continuous instant.
@@ -71,8 +86,9 @@ extension ContinuousClock: Clock {
7186
seconds: &seconds,
7287
nanoseconds: &nanoseconds,
7388
clock: _ClockID.continuous.rawValue)
74-
return ContinuousClock.Instant(_value:
75-
.seconds(seconds) + .nanoseconds(nanoseconds))
89+
return Instant(
90+
_value: Duration(_seconds: seconds, nanoseconds: nanoseconds)
91+
)
7692
}
7793

7894
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY

stdlib/public/Concurrency/SuspendingClock.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ extension SuspendingClock: Clock {
6161
seconds: &seconds,
6262
nanoseconds: &nanoseconds,
6363
clock: _ClockID.suspending.rawValue)
64-
return SuspendingClock.Instant(_value:
65-
.seconds(seconds) + .nanoseconds(nanoseconds))
64+
return Instant(
65+
_value: Duration(_seconds: seconds, nanoseconds: nanoseconds)
66+
)
6667
}
6768

6869
/// The minimum non-zero resolution between any two calls to `now`.
@@ -74,7 +75,7 @@ extension SuspendingClock: Clock {
7475
seconds: &seconds,
7576
nanoseconds: &nanoseconds,
7677
clock: _ClockID.suspending.rawValue)
77-
return .seconds(seconds) + .nanoseconds(nanoseconds)
78+
return Duration(_seconds: seconds, nanoseconds: nanoseconds)
7879
}
7980

8081
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY

0 commit comments

Comments
 (0)