Skip to content

Conversation

omanirudh
Copy link

@omanirudh omanirudh commented Apr 2, 2025

Feature gate: #![feature(duration_from_nanos_u128)]
ACP: rust-lang/libs-team#567
Tracking issue: #139201

@rustbot
Copy link
Collaborator

rustbot commented Apr 2, 2025

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 2, 2025
@rustbot rustbot assigned RalfJung and unassigned tgross35 Apr 2, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@RalfJung
Copy link
Member

RalfJung commented Apr 2, 2025

Thanks for taking this over!
However, this needs a libs reviewer. I just proposed the API. :)

r? libs

@rustbot rustbot assigned joboet and unassigned RalfJung Apr 2, 2025
@omanirudh
Copy link
Author

omanirudh commented Apr 2, 2025

Thanks for taking this over! However, this needs a libs reviewer. I just proposed the API. :)

r? libs

Happy to take this up. noted.

@tgross35
Copy link
Contributor

tgross35 commented Apr 2, 2025

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 2, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 2, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@omanirudh omanirudh marked this pull request as ready for review April 10, 2025 15:53
@rustbot rustbot added the has-merge-commits PR has merge commits, merge with caution. label Apr 10, 2025
@tgross35 tgross35 removed A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-attributes Area: Attributes (`#[…]`, `#![…]`) A-meta Area: Issues & PRs about the rust-lang/rust repository itself O-SGX Target: SGX A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic PG-exploit-mitigations Project group: Exploit mitigations O-hermit Operating System: Hermit O-solid Operating System: SOLID F-autodiff `#![feature(autodiff)]` labels Jul 3, 2025
@tgross35
Copy link
Contributor

tgross35 commented Jul 3, 2025

It looks like you did a merge rather than a rebase - that will need to get fixed since this branch still contains some weird history. Were you able to try what was suggested in rustc-dev-guide (linked above)?

@omanirudh
Copy link
Author

omanirudh commented Jul 4, 2025 via email

@alex-semenyuk
Copy link
Member

@omanirudh
Thanks for your contribution.
Form wg-triage. Any updates on this PR?

@omanirudh
Copy link
Author

omanirudh commented Aug 10, 2025 via email

Tracking issue: RUST-139201
@tgross35 tgross35 changed the title Add Duration from nanos u128 Add Duration::from_nanos_u128 Aug 10, 2025
@tgross35 tgross35 force-pushed the duration-from-nanos-u128 branch from 18ce05f to 27d11bc Compare August 10, 2025 07:37
@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-19-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
[RUSTC-TIMING] rand_core test:false 0.228
[RUSTC-TIMING] rand_xorshift test:false 0.112
[RUSTC-TIMING] rand test:false 0.956
[RUSTC-TIMING] vec_deque_alloc_error test:true 0.288
error[E0425]: cannot find value `NANOS_PER_SEC` in this scope
##[error]  --> coretests/tests/time.rs:51:32
   |
51 |     let overflow = (u64::MAX * NANOS_PER_SEC) + (NANOS_PER_SEC - 1) + 1;
   |                                ^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find value `NANOS_PER_SEC` in this scope
##[error]  --> coretests/tests/time.rs:51:50
   |
51 |     let overflow = (u64::MAX * NANOS_PER_SEC) + (NANOS_PER_SEC - 1) + 1;
   |                                                  ^^^^^^^^^^^^^ not found in this scope

[RUSTC-TIMING] allocbenches test:true 8.617
[RUSTC-TIMING] alloctests test:true 16.962
[RUSTC-TIMING] alloctests test:true 17.263
[RUSTC-TIMING] corebenches test:true 8.790
error[E0658]: use of unstable library feature `duration_from_nanos_u128`
##[error]  --> coretests/tests/time.rs:52:13
   |
52 |     let _ = Duration::from_nanos_u128(overflow);
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: see issue #139201 <https://github.com/rust-lang/rust/issues/139201> for more information
   = help: add `#![feature(duration_from_nanos_u128)]` to the crate attributes to enable
   = note: this compiler was built on 2025-08-10; consider upgrading it if it is out of date

error[E0308]: mismatched types
##[error]   --> coretests/tests/time.rs:52:39
    |
 52 |     let _ = Duration::from_nanos_u128(overflow);
    |             ------------------------- ^^^^^^^^ expected `u128`, found `u64`
    |             |
    |             arguments to this function are incorrect
    |
note: associated function defined here
   --> /checkout/library/core/src/time.rs:331:18
    |
331 |     pub const fn from_nanos_u128(nanos: u128) -> Duration {
    |                  ^^^^^^^^^^^^^^^
help: you can convert a `u64` to a `u128`
    |
 52 |     let _ = Duration::from_nanos_u128(overflow.into());
    |                                               +++++++

error[E0658]: use of unstable library feature `duration_from_nanos_u128`
##[error]  --> coretests/tests/time.rs:91:16
   |
91 |     assert_eq!(Duration::from_nanos_u128(999_999_999).as_secs(), 0);
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: see issue #139201 <https://github.com/rust-lang/rust/issues/139201> for more information
   = help: add `#![feature(duration_from_nanos_u128)]` to the crate attributes to enable
   = note: this compiler was built on 2025-08-10; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature `duration_from_nanos_u128`
##[error]  --> coretests/tests/time.rs:92:16
   |
92 |     assert_eq!(Duration::from_nanos_u128(1_000_000_001).as_secs(), 1);
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: see issue #139201 <https://github.com/rust-lang/rust/issues/139201> for more information
   = help: add `#![feature(duration_from_nanos_u128)]` to the crate attributes to enable
   = note: this compiler was built on 2025-08-10; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature `duration_from_nanos_u128`
##[error]   --> coretests/tests/time.rs:107:16
    |
107 |     assert_eq!(Duration::from_nanos_u128(999_999_999).subsec_millis(), 999);
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: see issue #139201 <https://github.com/rust-lang/rust/issues/139201> for more information
    = help: add `#![feature(duration_from_nanos_u128)]` to the crate attributes to enable
    = note: this compiler was built on 2025-08-10; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature `duration_from_nanos_u128`
##[error]   --> coretests/tests/time.rs:108:16
    |
108 |     assert_eq!(Duration::from_nanos_u128(1_001_000_001).subsec_millis(), 1);
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: see issue #139201 <https://github.com/rust-lang/rust/issues/139201> for more information
    = help: add `#![feature(duration_from_nanos_u128)]` to the crate attributes to enable
    = note: this compiler was built on 2025-08-10; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature `duration_from_nanos_u128`
##[error]   --> coretests/tests/time.rs:123:16
    |
123 |     assert_eq!(Duration::from_nanos_u128(999_999_999).subsec_micros(), 999_999);
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: see issue #139201 <https://github.com/rust-lang/rust/issues/139201> for more information
    = help: add `#![feature(duration_from_nanos_u128)]` to the crate attributes to enable
    = note: this compiler was built on 2025-08-10; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature `duration_from_nanos_u128`
##[error]   --> coretests/tests/time.rs:124:16
    |
124 |     assert_eq!(Duration::from_nanos_u128(1_000_001_000).subsec_micros(), 1);
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: see issue #139201 <https://github.com/rust-lang/rust/issues/139201> for more information
    = help: add `#![feature(duration_from_nanos_u128)]` to the crate attributes to enable
    = note: this compiler was built on 2025-08-10; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature `duration_from_nanos_u128`
##[error]   --> coretests/tests/time.rs:139:16
    |
139 |     assert_eq!(Duration::from_nanos_u128(999_999_999).subsec_nanos(), 999_999_999);
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: see issue #139201 <https://github.com/rust-lang/rust/issues/139201> for more information
    = help: add `#![feature(duration_from_nanos_u128)]` to the crate attributes to enable
    = note: this compiler was built on 2025-08-10; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature `duration_from_nanos_u128`
##[error]   --> coretests/tests/time.rs:140:16
    |
140 |     assert_eq!(Duration::from_nanos_u128(1_000_000_001).subsec_nanos(), 1);
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: see issue #139201 <https://github.com/rust-lang/rust/issues/139201> for more information
    = help: add `#![feature(duration_from_nanos_u128)]` to the crate attributes to enable
    = note: this compiler was built on 2025-08-10; consider upgrading it if it is out of date

Some errors have detailed explanations: E0308, E0425, E0658.
For more information about an error, try `rustc --explain E0308`.
[RUSTC-TIMING] coretests test:true 51.353
error: could not compile `coretests` (test "coretests") due to 12 previous errors
env -u RUSTC_WRAPPER CARGO_ENCODED_RUSTDOCFLAGS="-Csymbol-mangling-version=v0\u{1f}-Zrandomize-layout\u{1f}-Zunstable-options\u{1f}--check-cfg=cfg(bootstrap)\u{1f}--check-cfg=cfg(llvm_enzyme)\u{1f}-Dwarnings\u{1f}-Wrustdoc::invalid_codeblock_attributes\u{1f}--crate-version\u{1f}1.91.0-nightly\t(f7211377d\t2025-08-10)" CARGO_ENCODED_RUSTFLAGS="-Csymbol-mangling-version=v0\u{1f}-Zrandomize-layout\u{1f}-Zunstable-options\u{1f}--check-cfg=cfg(bootstrap)\u{1f}--check-cfg=cfg(llvm_enzyme)\u{1f}-Zmacro-backtrace\u{1f}-Csplit-debuginfo=off\u{1f}-Clink-arg=-L/usr/lib/llvm-19/lib\u{1f}-Cllvm-args=-import-instr-limit=10\u{1f}-Clink-args=-Wl,-z,origin\u{1f}-Clink-args=-Wl,-rpath,$ORIGIN/../lib\u{1f}-Alinker-messages\u{1f}--cap-lints=allow\u{1f}--cfg\u{1f}randomized_layouts" RUSTC="/checkout/obj/build/aarch64-unknown-linux-gnu/stage1-tools/cg_clif/dist/rustc-clif" RUSTDOC="/checkout/obj/build/aarch64-unknown-linux-gnu/stage1-tools/cg_clif/dist/rustdoc-clif" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage0/bin/cargo" "test" "--manifest-path" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage1-tools/cg_clif/build/sysroot_tests/Cargo.toml" "--target-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage1-tools/cg_clif/build/sysroot_tests_target" "--locked" "--target" "aarch64-unknown-linux-gnu" "-p" "coretests" "-p" "alloctests" "--tests" "--" "-q" exited with status ExitStatus(unix_wait_status(25856))
Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:07:36
  local time: Sun Aug 10 07:51:55 UTC 2025
  network time: Sun, 10 Aug 2025 07:51:55 GMT
##[error]Process completed with exit code 1.
Post job cleanup.

@tgross35
Copy link
Contributor

tgross35 commented Aug 10, 2025

Okay yeah that history was a mess, there were two merge commits going opposite directions. For future reference, you can usually fix these kind of situations with

git reset "$(git merge-base HEAD master)"

Which wipes away history but leaves the files unstaged. So your repo will be reset to wherever you branched from master (the merge-base), and it will look like you just edited the files. Then you recreate a single commit with git commit -a and force push, and everything is back to normal :)

I did this for you and force pushed your branch so you don't need to worry about it. This just needs a few small changes if you are interested in finishing it up, now that git shouldn't be a problem.

Comment on lines +315 to +316
/// Panics if the given number of nanoseconds is greater than what Duration can handle,
/// which is `(u64::MAX * NANOS_PER_SEC) + NANOS_PER_SEC - 1`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Panics if the given number of nanoseconds is greater than what Duration can handle,
/// which is `(u64::MAX * NANOS_PER_SEC) + NANOS_PER_SEC - 1`
/// Panics if the given number of nanoseconds is greater than [`Duration::MAX`].

Technically we say this can vary by platform, so we don't need to give specifics.

Comment on lines +317 to +318
/// Use this function if you need to specify time greater than what can fit in u64
/// (around 584 years).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Use this function if you need to specify time greater than what can fit in u64
/// (around 584 years).

I think this is reasonably straightforward

const NANOS_PER_SEC: u128 = self::NANOS_PER_SEC as u128;
let secs: u128 = nanos / NANOS_PER_SEC;
if secs > u64::MAX as u128 {
panic!("overflow in duration in Duration::from_nanos_u128");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
panic!("overflow in duration in Duration::from_nanos_u128");
panic!("overflow in `Duration::from_nanos_u128`");

#[unstable(feature = "duration_from_nanos_u128", issue = "139201")]
#[must_use]
#[inline]
pub const fn from_nanos_u128(nanos: u128) -> Duration {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub const fn from_nanos_u128(nanos: u128) -> Duration {
#[track_caller]
pub const fn from_nanos_u128(nanos: u128) -> Duration {

Makes the panic message show where this was called, rather than reporting std's time.rs as the panic location

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to add #![feature(duration_from_nanos_u128)] to coretests/tests/lib.rs, even for testing the unstable feature needs to be enabled (this is your CI failure)

@@ -123,6 +136,8 @@ fn nanos() {
assert_eq!(Duration::from_micros(1_000_001).subsec_nanos(), 1000);
assert_eq!(Duration::from_nanos(999_999_999).subsec_nanos(), 999_999_999);
assert_eq!(Duration::from_nanos(1_000_000_001).subsec_nanos(), 1);
assert_eq!(Duration::from_nanos_u128(999_999_999).subsec_nanos(), 999_999_999);
assert_eq!(Duration::from_nanos_u128(1_000_000_001).subsec_nanos(), 1);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a test to duration_const below (look for const NANOS)

actuallylost added a commit to actuallylost/rust that referenced this pull request Aug 28, 2025
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 30, 2025
…28, r=tgross35

Add Duration::from_nanos_u128

Feature Gate: `#![feature(duration_from_nanos_u128)]`
ACP: rust-lang/libs-team#567
Tracking issue: rust-lang#139201
Recreated from rust-lang#139243
rust-timer added a commit that referenced this pull request Aug 30, 2025
Rollup merge of #145969 - actuallylost:duration-from-nanos-128, r=tgross35

Add Duration::from_nanos_u128

Feature Gate: `#![feature(duration_from_nanos_u128)]`
ACP: rust-lang/libs-team#567
Tracking issue: #139201
Recreated from #139243
@bors
Copy link
Collaborator

bors commented Aug 30, 2025

☔ The latest upstream changes (presumably #146026) made this pull request unmergeable. Please resolve the merge conflicts.

@GuillaumeGomez
Copy link
Member

It was done in #145969 so this PR can be closed.

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 30, 2025
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Sep 9, 2025
…28, r=tgross35

Add Duration::from_nanos_u128

Feature Gate: `#![feature(duration_from_nanos_u128)]`
ACP: rust-lang/libs-team#567
Tracking issue: rust-lang#139201
Recreated from rust-lang#139243
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-unix Operating system: Unix-like T-libs Relevant to the library team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.