Skip to content

Constify SystemTime methods #144519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

clarfonthey
Copy link
Contributor

@clarfonthey clarfonthey commented Jul 26, 2025

This is separated out from #143949 due to the fact that it touches nontrivial system code. While the same arithmetic methods on Instant could be made const, since that type explicitly cannot be constructed at const-time, we don't bother. However, due to the fact that SystemTime::UNIX_EPOCH exists, it can be useful to create other anchors at other points in time, and thus these methods should be possible to use in const context.

Side comment: I would honestly like to just move every single implementation which is a thin wrapper over Duration or some integer clock into their own module which is tested on all targets, so that we don't have to worry about code for lower-tier targets not being tested. However, the comments in std::sys_common pointing out the desire to move things to std::sys::common confused me, particularly due to the fact that std::sys::common is taken from the hellish asterisk-import of std::sys::pal::common and others.

I think that, for trivial types like this, we should just have a platform-independent module that can be tested on all platforms, to both ensure that the code is properly tested regardless of whether higher-tier platforms needed, and to avoid the extreme mental gymnastics required to determine where in the code it lies.

However, since I'm not on any of the teams maintaining this code and am not involved, I'll settle for just copy-pasting like everyone else has been doing. I just want to express how I'm not happy about that.

Reviewer note: please only pay attention to the last commit of this PR, until its blocker is merged.

Since this depends on the previous PR:
@rustbot blocked

@rustbot
Copy link
Collaborator

rustbot commented Jul 26, 2025

r? @jhpratt

rustbot has assigned @jhpratt.
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 O-hermit Operating System: Hermit O-SGX Target: SGX O-solid Operating System: SOLID O-unix Operating system: Unix-like O-wasi Operating system: Wasi, Webassembly System Interface O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 26, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 26, 2025

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot rustbot added the S-blocked Status: Blocked on something else such as an RFC or other implementation work. label Jul 26, 2025
@clarfonthey
Copy link
Contributor Author

(This does not, in fact, include changes to clippy. That's from the other PR and can be safely ignored.)

@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-19-1 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] object test:false 8.838
error[E0658]: const trait impls are experimental
   --> library/std/src/time.rs:610:6
    |
610 | impl const Add<Duration> for SystemTime {
    |      ^^^^^
    |
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error[E0658]: const trait impls are experimental
   --> library/std/src/time.rs:624:6
    |
624 | impl const AddAssign<Duration> for SystemTime {
    |      ^^^^^
    |
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error[E0658]: const trait impls are experimental
   --> library/std/src/time.rs:632:6
    |
632 | impl const Sub<Duration> for SystemTime {
    |      ^^^^^
    |
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error[E0658]: const trait impls are experimental
   --> library/std/src/time.rs:642:6
    |
642 | impl const SubAssign<Duration> for SystemTime {
    |      ^^^^^
    |
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error[E0658]: use of unstable const library feature `const_ops`
   --> library/std/src/time.rs:610:12
    |
610 | impl const Add<Duration> for SystemTime {
    |            ^^^^^^^^^^^^^ trait is not stable as const yet
    |
    = note: see issue #143802 <https://github.com/rust-lang/rust/issues/143802> for more information
    = help: add `#![feature(const_ops)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error[E0658]: use of unstable const library feature `const_ops`
   --> library/std/src/time.rs:624:12
    |
624 | impl const AddAssign<Duration> for SystemTime {
    |            ^^^^^^^^^^^^^^^^^^^ trait is not stable as const yet
    |
    = note: see issue #143802 <https://github.com/rust-lang/rust/issues/143802> for more information
    = help: add `#![feature(const_ops)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error[E0658]: use of unstable const library feature `const_ops`
   --> library/std/src/time.rs:632:12
    |
632 | impl const Sub<Duration> for SystemTime {
    |            ^^^^^^^^^^^^^ trait is not stable as const yet
    |
    = note: see issue #143802 <https://github.com/rust-lang/rust/issues/143802> for more information
    = help: add `#![feature(const_ops)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error[E0658]: use of unstable const library feature `const_ops`
   --> library/std/src/time.rs:642:12
    |
642 | impl const SubAssign<Duration> for SystemTime {
    |            ^^^^^^^^^^^^^^^^^^^ trait is not stable as const yet
    |
    = note: see issue #143802 <https://github.com/rust-lang/rust/issues/143802> for more information
    = help: add `#![feature(const_ops)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error[E0015]: cannot call non-const method `core::result::Result::<Duration, Duration>::map_err::<SystemTimeError, fn(Duration) -> SystemTimeError {SystemTimeError}>` in constant functions
   --> library/std/src/time.rs:556:37
    |
556 |         self.0.sub_time(&earlier.0).map_err(SystemTimeError)
    |                                     ^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error[E0658]: cannot call conditionally-const method `core::option::Option::<pal::unix::time::SystemTime>::map::<time::SystemTime, fn(pal::unix::time::SystemTime) -> time::SystemTime {time::SystemTime}>` in constant functions
   --> library/std/src/time.rs:595:48
    |
595 |         self.0.checked_add_duration(&duration).map(SystemTime)
    |                                                ^^^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `core::option::Option::<T>::map` is not yet stable as a const fn
   --> library/std/src/time.rs:595:9
    |
595 |         self.0.checked_add_duration(&duration).map(SystemTime)
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: add `#![feature(const_option_ops)]` to the crate attributes to enable
   --> library/std/src/lib.rs:425:1
    |
425 + #![feature(const_option_ops)]
    |

error[E0658]: cannot call conditionally-const method `core::option::Option::<pal::unix::time::SystemTime>::map::<time::SystemTime, fn(pal::unix::time::SystemTime) -> time::SystemTime {time::SystemTime}>` in constant functions
   --> library/std/src/time.rs:604:48
    |
604 |         self.0.checked_sub_duration(&duration).map(SystemTime)
    |                                                ^^^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `core::option::Option::<T>::map` is not yet stable as a const fn
   --> library/std/src/time.rs:604:9
    |
604 |         self.0.checked_sub_duration(&duration).map(SystemTime)
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: add `#![feature(const_option_ops)]` to the crate attributes to enable
   --> library/std/src/lib.rs:425:1
    |
425 + #![feature(const_option_ops)]
    |

error[E0658]: cannot call conditionally-const operator in constant functions
   --> library/std/src/time.rs:626:17
    |
626 |         *self = *self + other;
    |                 ^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `Add` is not yet stable as a const trait
   --> library/std/src/time.rs:626:17
    |
626 |         *self = *self + other;
    |                 ^^^^^^^^^^^^^
    |
help: add `#![feature(const_ops)]` to the crate attributes to enable
   --> library/std/src/lib.rs:425:1
    |
425 + #![feature(const_ops)]
    |

error[E0658]: cannot call conditionally-const operator in constant functions
   --> library/std/src/time.rs:644:17
    |
644 |         *self = *self - other;
    |                 ^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `Sub` is not yet stable as a const trait
   --> library/std/src/time.rs:644:17
    |
644 |         *self = *self - other;
    |                 ^^^^^^^^^^^^^
    |
help: add `#![feature(const_ops)]` to the crate attributes to enable
   --> library/std/src/lib.rs:425:1
    |
425 + #![feature(const_ops)]
    |

error[E0658]: `?` is not allowed on `core::option::Option<Timespec>` in constant functions
  --> library/std/src/sys/pal/unix/time.rs:48:30
   |
48 |         Some(SystemTime { t: self.t.checked_add_duration(other)? })
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
   = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
   = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
   = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `Try` is not yet stable as a const trait
  --> library/std/src/sys/pal/unix/time.rs:48:30
   |
48 |         Some(SystemTime { t: self.t.checked_add_duration(other)? })
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: add `#![feature(const_try)]` to the crate attributes to enable
  --> library/std/src/lib.rs:425:1
   |
425+ #![feature(const_try)]
   |

error[E0658]: `?` is not allowed on `core::option::Option<pal::unix::time::SystemTime>` in constant functions
  --> library/std/src/sys/pal/unix/time.rs:48:30
   |
48 |         Some(SystemTime { t: self.t.checked_add_duration(other)? })
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
   = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
   = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
   = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `FromResidual` is not yet stable as a const trait
  --> library/std/src/sys/pal/unix/time.rs:48:30
   |
48 |         Some(SystemTime { t: self.t.checked_add_duration(other)? })
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: add `#![feature(const_try)]` to the crate attributes to enable
  --> library/std/src/lib.rs:425:1
   |
425+ #![feature(const_try)]
   |

error[E0658]: `?` is not allowed on `core::option::Option<Timespec>` in constant functions
  --> library/std/src/sys/pal/unix/time.rs:53:30
   |
53 |         Some(SystemTime { t: self.t.checked_sub_duration(other)? })
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
   = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
   = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
   = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `Try` is not yet stable as a const trait
  --> library/std/src/sys/pal/unix/time.rs:53:30
   |
53 |         Some(SystemTime { t: self.t.checked_sub_duration(other)? })
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: add `#![feature(const_try)]` to the crate attributes to enable
  --> library/std/src/lib.rs:425:1
   |
425+ #![feature(const_try)]
   |

error[E0658]: `?` is not allowed on `core::option::Option<pal::unix::time::SystemTime>` in constant functions
  --> library/std/src/sys/pal/unix/time.rs:53:30
   |
53 |         Some(SystemTime { t: self.t.checked_sub_duration(other)? })
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
   = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
   = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
   = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `FromResidual` is not yet stable as a const trait
  --> library/std/src/sys/pal/unix/time.rs:53:30
   |
53 |         Some(SystemTime { t: self.t.checked_sub_duration(other)? })
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: add `#![feature(const_try)]` to the crate attributes to enable
  --> library/std/src/lib.rs:425:1
   |
---
    |            ^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error[E0658]: `?` is not allowed on `core::option::Option<i64>` in constant functions
   --> library/std/src/sys/pal/unix/time.rs:178:24
    |
178 |         let mut secs = self.tv_sec.checked_add_unsigned(other.as_secs())?;
    |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `Try` is not yet stable as a const trait
   --> library/std/src/sys/pal/unix/time.rs:178:24
    |
178 |         let mut secs = self.tv_sec.checked_add_unsigned(other.as_secs())?;
    |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: add `#![feature(const_try)]` to the crate attributes to enable
   --> library/std/src/lib.rs:425:1
    |
425 + #![feature(const_try)]
    |

error[E0658]: `?` is not allowed on `core::option::Option<Timespec>` in constant functions
   --> library/std/src/sys/pal/unix/time.rs:178:24
    |
178 |         let mut secs = self.tv_sec.checked_add_unsigned(other.as_secs())?;
    |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `FromResidual` is not yet stable as a const trait
   --> library/std/src/sys/pal/unix/time.rs:178:24
    |
178 |         let mut secs = self.tv_sec.checked_add_unsigned(other.as_secs())?;
    |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: add `#![feature(const_try)]` to the crate attributes to enable
   --> library/std/src/lib.rs:425:1
    |
425 + #![feature(const_try)]
    |

error[E0658]: `?` is not allowed on `core::option::Option<i64>` in constant functions
   --> library/std/src/sys/pal/unix/time.rs:185:20
    |
185 |             secs = secs.checked_add(1)?;
    |                    ^^^^^^^^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `Try` is not yet stable as a const trait
   --> library/std/src/sys/pal/unix/time.rs:185:20
    |
185 |             secs = secs.checked_add(1)?;
    |                    ^^^^^^^^^^^^^^^^^^^^
    |
help: add `#![feature(const_try)]` to the crate attributes to enable
   --> library/std/src/lib.rs:425:1
    |
425 + #![feature(const_try)]
    |

error[E0658]: `?` is not allowed on `core::option::Option<Timespec>` in constant functions
   --> library/std/src/sys/pal/unix/time.rs:185:20
    |
185 |             secs = secs.checked_add(1)?;
    |                    ^^^^^^^^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `FromResidual` is not yet stable as a const trait
   --> library/std/src/sys/pal/unix/time.rs:185:20
    |
185 |             secs = secs.checked_add(1)?;
    |                    ^^^^^^^^^^^^^^^^^^^^
    |
help: add `#![feature(const_try)]` to the crate attributes to enable
   --> library/std/src/lib.rs:425:1
    |
425 + #![feature(const_try)]
    |

error[E0658]: cannot call conditionally-const method `<u32 as core::convert::Into<i64>>::into` in constant functions
   --> library/std/src/sys/pal/unix/time.rs:187:58
    |
187 |         Some(unsafe { Timespec::new_unchecked(secs, nsec.into()) })
    |                                                          ^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `core::convert::Into` is not yet stable as a const trait
   --> library/std/src/sys/pal/unix/time.rs:187:53
    |
187 |         Some(unsafe { Timespec::new_unchecked(secs, nsec.into()) })
    |                                                     ^^^^^^^^^^^
    |
help: add `#![feature(const_from)]` to the crate attributes to enable
   --> library/std/src/lib.rs:425:1
    |
425 + #![feature(const_from)]
    |

error[E0658]: `?` is not allowed on `core::option::Option<i64>` in constant functions
   --> library/std/src/sys/pal/unix/time.rs:192:24
    |
192 |         let mut secs = self.tv_sec.checked_sub_unsigned(other.as_secs())?;
    |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `Try` is not yet stable as a const trait
   --> library/std/src/sys/pal/unix/time.rs:192:24
    |
192 |         let mut secs = self.tv_sec.checked_sub_unsigned(other.as_secs())?;
    |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: add `#![feature(const_try)]` to the crate attributes to enable
   --> library/std/src/lib.rs:425:1
    |
425 + #![feature(const_try)]
    |

error[E0658]: `?` is not allowed on `core::option::Option<Timespec>` in constant functions
   --> library/std/src/sys/pal/unix/time.rs:192:24
    |
192 |         let mut secs = self.tv_sec.checked_sub_unsigned(other.as_secs())?;
    |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `FromResidual` is not yet stable as a const trait
   --> library/std/src/sys/pal/unix/time.rs:192:24
    |
192 |         let mut secs = self.tv_sec.checked_sub_unsigned(other.as_secs())?;
    |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: add `#![feature(const_try)]` to the crate attributes to enable
   --> library/std/src/lib.rs:425:1
    |
425 + #![feature(const_try)]
    |

error[E0658]: `?` is not allowed on `core::option::Option<i64>` in constant functions
   --> library/std/src/sys/pal/unix/time.rs:198:20
    |
198 |             secs = secs.checked_sub(1)?;
    |                    ^^^^^^^^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `Try` is not yet stable as a const trait
   --> library/std/src/sys/pal/unix/time.rs:198:20
    |
198 |             secs = secs.checked_sub(1)?;
    |                    ^^^^^^^^^^^^^^^^^^^^
    |
help: add `#![feature(const_try)]` to the crate attributes to enable
   --> library/std/src/lib.rs:425:1
    |
425 + #![feature(const_try)]
    |

error[E0658]: `?` is not allowed on `core::option::Option<Timespec>` in constant functions
   --> library/std/src/sys/pal/unix/time.rs:198:20
    |
198 |             secs = secs.checked_sub(1)?;
    |                    ^^^^^^^^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `FromResidual` is not yet stable as a const trait
   --> library/std/src/sys/pal/unix/time.rs:198:20
    |
198 |             secs = secs.checked_sub(1)?;
    |                    ^^^^^^^^^^^^^^^^^^^^
    |
help: add `#![feature(const_try)]` to the crate attributes to enable
   --> library/std/src/lib.rs:425:1
    |
425 + #![feature(const_try)]
    |

error[E0658]: cannot call conditionally-const method `<i32 as core::convert::Into<i64>>::into` in constant functions
   --> library/std/src/sys/pal/unix/time.rs:200:58
    |
200 |         Some(unsafe { Timespec::new_unchecked(secs, nsec.into()) })
    |                                                          ^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
    = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
    = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
    = note: this compiler was built on 2025-07-26; consider upgrading it if it is out of date

error: `core::convert::Into` is not yet stable as a const trait
   --> library/std/src/sys/pal/unix/time.rs:200:53
    |
200 |         Some(unsafe { Timespec::new_unchecked(secs, nsec.into()) })
    |                                                     ^^^^^^^^^^^
    |
help: add `#![feature(const_from)]` to the crate attributes to enable
   --> library/std/src/lib.rs:425:1
    |

@jhpratt
Copy link
Member

jhpratt commented Jul 26, 2025

I'm mildly surprised that this is even possible at compile time, but I guess it makes sense given the internals. My concern is that this would (if/when stabilized) provide a guarantee that all future targets having SystemTime would necessarily have to have it supported in const contexts, which may not be technically possible (conceivably).

But if the team is fine with this, I'm not going to get in the way. The final commit LGTM once CI passes and it's unblocked.

@jhpratt jhpratt removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 26, 2025
@clarfonthey
Copy link
Contributor Author

clarfonthey commented Jul 27, 2025

My main thought process is that we've effectively guaranteed that all targets have the ability to represent this due to the presence of UNIX_EPOCH, since not all targets will necessarily use the unix epoch as their "zero" point. (Even though a lot of them will.)

We even have a direct example of this for Windows, documented here: the epoch for Windows' FILETIME is January 1, 1601.

I also just find it very doubtful that any target would have a concept of "system time" where the units are not defined by the spec. The only theoretical target we could support now which would not fall under this system is one where:

  1. The unix epoch is the zero time.
  2. The units of time depend on the current system configuration.

Imagine such a target where, for example, the "current time" system call had configurable units: depending on the system configuration, such a system call would return either milliseconds or seconds since the epoch. That just sounds like an absolute hell to deal with when it comes to figuring out relative times it makes a lot more sense for there to just be different system calls for different units. Plus, what happens if the configuration changes while the system is running?

Like, again, it's not strictly impossible to happen, but it feels like the inability to support this kind of functionality is just the target's problem, not ours. To me, it's equivalent to a target just not having any clock functionality, which is much more likely than this, despite that also being unlikely.

(Unrelated to that, I will also definitely fix the compilation errors before this is unblocked. I definitely missed a couple feature flags & const attributes.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-hermit Operating System: Hermit O-SGX Target: SGX O-solid Operating System: SOLID O-unix Operating system: Unix-like O-wasi Operating system: Wasi, Webassembly System Interface O-windows Operating system: Windows S-blocked Status: Blocked on something else such as an RFC or other implementation work. T-clippy Relevant to the Clippy team. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants