Skip to content

Instant + Duration produce wrong result on aarch64-apple-darwin #91417

Closed
@wangrunji0408

Description

@wangrunji0408

I tried this code on M1 macOS:

use std::time::{Duration, Instant};
fn main() {
    let t0 = Instant::now();
    let t1 = t0 + Duration::from_nanos(50);
    let d = t1 - t0;
    dbg!(t0, t1, d);
    assert_eq!(t0 + d, t1);
}

It's expected to succeed.

Instead, this happened:

[src/main.rs:6] t0 = Instant {
    t: 147092181000,
}
[src/main.rs:6] t1 = Instant {
    t: 147092181001,
}
[src/main.rs:6] d = 41ns
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `Instant { t: 147092181000 }`,
 right: `Instant { t: 147092181001 }`', src/main.rs:7:5

It seems that there is something wrong with the Duration -> Instant casting in Rust std:

fn checked_dur2intervals(dur: &Duration) -> Option<u64> {
let nanos =
dur.as_secs().checked_mul(NSEC_PER_SEC)?.checked_add(dur.subsec_nanos() as u64)?;
let info = info();
Some(mul_div_u64(nanos, info.denom as u64, info.numer as u64))
}

This problem does not exist on x86_64-apple-darwin, because info.denom = info.numer = 1.

Meta

rustc --version --verbose:

rustc 1.56.1 (59eed8a2a 2021-11-01)
binary: rustc
commit-hash: 59eed8a2aac0230a8b53e89d4e99d55912ba6b35
commit-date: 2021-11-01
host: aarch64-apple-darwin
release: 1.56.1
LLVM version: 13.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-timeArea: TimeC-bugCategory: This is a bug.E-help-wantedCall for participation: Help is requested to fix this issue.O-AArch64Armv8-A or later processors in AArch64 modeO-macosOperating system: macOSP-mediumMedium priorityT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions