Skip to content

Commit 6419151

Browse files
authored
Rollup merge of #103594 - maniwani:fix-issue-91417, r=thomcc
Fix non-associativity of `Instant` math on `aarch64-apple-darwin` targets This is a duplicate of #94100 (since the original author is unresponsive), which resolves #91417. On `aarch64-apple-darwin` targets, the internal resolution of `Instant` is lower than that of `Duration`, so math between them becomes non-associative with small-enough durations. This PR makes this target use the standard Unix implementation (where `Instant` has 1ns resolution), but with `CLOCK_UPTIME_RAW` so it still returns the same values as `mach_absolute_time`[^1]. (Edit: I need someone to confirm that this still works, I do not have access to an M1 device.) [^1]: https://www.manpagez.com/man/3/clock_gettime/
2 parents 3efbf30 + f4f5159 commit 6419151

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

library/std/src/sys/unix/time.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,11 @@ impl From<libc::timespec> for Timespec {
149149
}
150150
}
151151

152-
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))]
152+
#[cfg(any(
153+
all(target_os = "macos", any(not(target_arch = "aarch64"), miri)),
154+
target_os = "ios",
155+
target_os = "watchos"
156+
))]
153157
mod inner {
154158
use crate::sync::atomic::{AtomicU64, Ordering};
155159
use crate::sys::cvt;
@@ -265,7 +269,11 @@ mod inner {
265269
}
266270
}
267271

268-
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "watchos")))]
272+
#[cfg(not(any(
273+
all(target_os = "macos", any(not(target_arch = "aarch64"), miri)),
274+
target_os = "ios",
275+
target_os = "watchos"
276+
)))]
269277
mod inner {
270278
use crate::fmt;
271279
use crate::mem::MaybeUninit;
@@ -281,7 +289,11 @@ mod inner {
281289

282290
impl Instant {
283291
pub fn now() -> Instant {
284-
Instant { t: Timespec::now(libc::CLOCK_MONOTONIC) }
292+
#[cfg(target_os = "macos")]
293+
const clock_id: libc::clockid_t = libc::CLOCK_UPTIME_RAW;
294+
#[cfg(not(target_os = "macos"))]
295+
const clock_id: libc::clockid_t = libc::CLOCK_MONOTONIC;
296+
Instant { t: Timespec::now(clock_id) }
285297
}
286298

287299
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
@@ -312,13 +324,8 @@ mod inner {
312324
}
313325
}
314326

315-
#[cfg(not(any(target_os = "dragonfly", target_os = "espidf", target_os = "horizon")))]
316-
pub type clock_t = libc::c_int;
317-
#[cfg(any(target_os = "dragonfly", target_os = "espidf", target_os = "horizon"))]
318-
pub type clock_t = libc::c_ulong;
319-
320327
impl Timespec {
321-
pub fn now(clock: clock_t) -> Timespec {
328+
pub fn now(clock: libc::clockid_t) -> Timespec {
322329
// Try to use 64-bit time in preparation for Y2038.
323330
#[cfg(all(target_os = "linux", target_env = "gnu", target_pointer_width = "32"))]
324331
{

library/std/src/time/tests.rs

+8
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ fn instant_math_is_associative() {
8888
// Changing the order of instant math shouldn't change the results,
8989
// especially when the expression reduces to X + identity.
9090
assert_eq!((now + offset) - now, (now - now) + offset);
91+
92+
// On any platform, `Instant` should have the same resolution as `Duration` (e.g. 1 nanosecond)
93+
// or better. Otherwise, math will be non-associative (see #91417).
94+
let now = Instant::now();
95+
let provided_offset = Duration::from_nanos(1);
96+
let later = now + provided_offset;
97+
let measured_offset = later - now;
98+
assert_eq!(measured_offset, provided_offset);
9199
}
92100

93101
#[test]

0 commit comments

Comments
 (0)